Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions finam/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

__all__ = ['Market',
'Timeframe',
'Fileformat'
]


Expand Down Expand Up @@ -40,3 +41,10 @@ class Timeframe(IntEnum):
DAILY = 8
WEEKLY = 9
MONTHLY = 10

class Fileformat(IntEnum):

CSV = 1
CSVGZ = 2
PKL = 3
PKLXZ = 4
29 changes: 24 additions & 5 deletions scripts/finam-download.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
Timeframe,
Market,
FinamExportError,
FinamObjectNotFoundError)
FinamObjectNotFoundError,
Fileformat)
from finam.utils import click_validate_enum


Expand Down Expand Up @@ -79,8 +80,13 @@ def _arg_split(ctx, param, value):
@click.option('--ext',
help='Resulting file extension',
default='csv')
@click.option('--format',
help='Format of output file with data',
default='CSV',
callback=partial(click_validate_enum, Fileformat),
required=False)
def main(contracts, market, timeframe, destdir, lineterm,
delay, startdate, enddate, skiperr, ext):
delay, startdate, enddate, skiperr, ext, format):
exporter = Exporter()

if not any((contracts, market)):
Expand Down Expand Up @@ -115,10 +121,23 @@ def main(contracts, market, timeframe, destdir, lineterm,
continue
else:
raise
destpath = os.path.join(destdir, '{}-{}.{}'
.format(contract.code, timeframe, ext))
destpath = os.path.join(destdir, '{}-{}'
.format(contract.code, timeframe))

# extention is taken from param if output file is in csv format
if format == 'CSV':
destpath += f'.{ext}'
data.to_csv(destpath, index=False, line_terminator=lineterm)
elif format == 'CSVGZ':
destpath += f'.csv.gz'
data.to_csv(destpath, index=False, line_terminator=lineterm, compression='gzip')
if format == 'PKL':
destpath += f'.pkl'
data.to_pickle(destpath)
if format == 'PKLXZ':
destpath += f'.pkl.xz'
data.to_pickle(destpath, compression='xz')

data.to_csv(destpath, index=False, line_terminator=lineterm)
if delay > 0:
logger.info('Sleeping for {} second(s)'.format(delay))
time.sleep(delay)
Expand Down