Beispiel #1
0
def main():
    exporter = Exporter()
    print('*** Current Russian ruble exchange rates ***')
    rub = exporter.lookup(name='USDRUB_TOD', market=Market.CURRENCIES)
    assert len(rub) == 1
    data = exporter.download(rub.index[0], market=Market.CURRENCIES)
    print(data.tail(1))

    print('*** Current Brent Oil price ***')
    oil = exporter.lookup(name='Brent',
                          market=Market.COMMODITIES,
                          name_comparator=LookupComparator.EQUALS)
    assert len(oil) == 1
    data = exporter.download(oil.index[0], market=Market.COMMODITIES)
    print(data.tail(1))
Beispiel #2
0
def main():
    exporter = Exporter()
    print('*** Looking up all RTS futures codes ***')
    res = exporter.lookup(
        market=[Market.FUTURES_ARCHIVE, Market.FUTURES],
        name='RTS-',
        name_comparator=LookupComparator.STARTSWITH)
    print(','.join(res['code']))

    print('*** Looking up Russian Ministry of Finance\'s bonds ***')
    print(exporter.lookup(market=Market.BONDS, name=u'ОФЗ',
                          name_comparator=LookupComparator.STARTSWITH))

    print('*** Looking up Microsoft ***')
    print(exporter.lookup(market=Market.USA, name='Microsoft',
                          name_comparator=LookupComparator.CONTAINS))
Beispiel #3
0
def get_shares(code=None, start_date=None, end_date=None):
    if code:
        code = code.upper()

    exporter = Exporter()

    # все акции в пандас таблице из финама
    items = exporter.lookup(market=Market.SHARES)
    share_id = items[items.code == code]

    if share_id.empty:
        return None
    else:
        share_id = share_id.index[0]
        temp = exporter.download(
            share_id,
            Market.SHARES,  # акции
            start_date=start_date,
            end_date=end_date,
            timeframe=Timeframe.DAILY)
        temp.reset_index(inplace=True)
        temp.columns = [x[1:-1].lower() for x in temp.columns]
        temp.rename(columns={'nde': 'timestamp'}, inplace=True)

        return temp
Beispiel #4
0
def main():
    logging.basicConfig(level=logging.DEBUG)
    exporter = Exporter()
    for market in Market:
        print('{0.name:*^25}'.format(market))
        items = exporter.lookup(market=market)
        print('Total items: {}'.format(len(items)))
        print('Sample: {}'.format(', '.join(items['code'][:SAMPLE_SIZE])))
Beispiel #5
0
def main(contracts, market, timeframe, destdir, lineterm,
         delay, startdate, enddate, skiperr, ext):
    exporter = Exporter()

    if not any((contracts, market)):
        raise click.BadParameter('Neither contracts nor market is specified')

    market_filter = dict()
    if market:
        market_filter.update(market=Market[market])
        if not contracts:
            contracts = exporter.lookup(**market_filter)['code'].tolist()

    for contract_code in contracts:
        logging.info('Handling {}'.format(contract_code))
        try:
            contracts = exporter.lookup(code=contract_code, **market_filter)
        except FinamObjectNotFoundError:
            logger.error('unknown contract "{}"'.format(contract_code))
            sys.exit(1)
        else:
            contract = contracts.reset_index().iloc[0]

        logger.info(u'Downloading contract {}'.format(contract))
        try:
            data = exporter.download(contract.id,
                                     start_date=startdate,
                                     end_date=enddate,
                                     timeframe=Timeframe[timeframe],
                                     market=Market(contract.market))
        except FinamExportError as e:
            if skiperr:
                logger.error(repr(e))
                continue
            else:
                raise
        destpath = os.path.join(destdir, '{}-{}.{}'
                                .format(contract.code, timeframe, ext))

        data.to_csv(destpath, index=False, line_terminator=lineterm)
        if delay > 0:
            logger.info('Sleeping for {} second(s)'.format(delay))
            time.sleep(delay)
Beispiel #6
0
def main(contract, market):
    exporter = Exporter()

    if all((contract, market)):
        raise click.BadParameter('Either contract or market must be specified')
    elif not any((contract, market)):
        raise click.BadParameter('Neither contract nor market is specified')

    pd.options.display.max_rows = 1000

    if contract:
        try:
            meta = exporter.lookup(code=contract)
        except FinamObjectNotFoundError:
            logger.info('No such contract')
        else:
            print(meta)
    else:
        contracts = exporter.lookup(market=Market[market])
        print(contracts)
Beispiel #7
0
def main(contracts, market, timeframe, destdir, lineterm, delay, startdate,
         enddate, skiperr):
    exporter = Exporter()

    if all((contracts, market)):
        raise click.BadParameter('Either contracts or '
                                 'market must be specified')
    elif not any((contracts, market)):
        raise click.BadParameter('Neither contracts nor market is specified')
    elif market:
        contracts = exporter.lookup(market=Market[market])['code'].tolist()

    for contract_code in contracts:
        logging.info('Handling {}'.format(contract_code))
        try:
            contracts = exporter.lookup(code=contract_code)
        except FinamObjectNotFoundError:
            raise RuntimeError('unknown contract "{}"'.format(contract_code))
        else:
            contract = contracts.reset_index().iloc[0]

        logger.info(u'Downloading contract {}'.format(contract))
        try:
            data = exporter.download(contract.id,
                                     start_date=startdate,
                                     end_date=enddate,
                                     timeframe=Timeframe[timeframe],
                                     market=Market(contract.market))
        except FinamExportError as e:
            if skiperr:
                logger.error(e.message)
                continue
            else:
                raise
        destpath = os.path.join(destdir,
                                '{}-{}.csv'.format(contract.code, timeframe))
        data.to_csv(destpath, line_terminator=lineterm)
        if delay > 0:
            logger.info('Sleeping for {} second(s)'.format(delay))
            time.sleep(delay)
Beispiel #8
0
def download_contract_data(contract: ContractSpec,
                           timeframe: Timeframe) -> pd.DataFrame:
    exporter = Exporter()
    lookup_df = exporter.lookup(
        name=f"{contract.full_code}({contract.short_code})",
        name_comparator=LookupComparator.EQUALS,
    )
    if len(lookup_df.index) != 1:
        raise ValueError(
            f"Contract lookup failed. Returned {len(lookup_df.index)} rows. "
            f"Names: {', '.join(lookup_df['name'])}")

    today = datetime.date.today()
    start_date = contract.expiration - relativedelta(months=3, day=1)
    end_date = contract.expiration + relativedelta(months=1, day=1)

    if end_date > today:
        end_date = today

    df = exporter.download(
        lookup_df.index[0],
        Market(lookup_df["market"].iloc[0]),
        timeframe=timeframe,
        start_date=start_date,
        end_date=end_date,
    )
    df = df.rename(
        columns={
            "<OPEN>": "open",
            "<HIGH>": "high",
            "<LOW>": "low",
            "<CLOSE>": "close",
            "<VOL>": "volume",
        })
    df.insert(0, "contract", contract.full_code)

    return df
Beispiel #9
0
def get_asset_data(assetCode):
    exporter = Exporter()
    ind = exporter.lookup(code=assetCode)
    assetId = ind.index.values[0]
    assetName, assetCode, market = ind.values[0]
    return assetId, assetName, market