Exemplo n.º 1
0
def test_fetch_stock_values():
    now = datetime.utcnow()
    start_date = now - timedelta(days=2)
    end_date = now - timedelta(days=1)
    rows = fetch_stock_values('SPY', start_date, end_date)

    for date, open_, high, low, close_, volume, provider in rows:
        assert isinstance(date, datetime)
        assert isinstance(open_, float)
        assert isinstance(high, float)
        assert isinstance(low, float)
        assert isinstance(close_, float)
        assert isinstance(volume, int)
        assert low <= open_ <= high
        assert low <= close_ <= high
        assert volume >= 0
        assert provider == 'yahoo'
Exemplo n.º 2
0
def fetch_asset_values(code, start_time, end_time):
    try:
        asset = Asset.get_by_symbol(code)
    except AssetNotFoundException:
        log.info('Asset {0} does not exist. Creating an Asset record...', code)
        asset = Asset.create(name=code, code=code, type=AssetType.stock)

    rows = fetch_stock_values(code, start_time, end_time, Granularity.min)

    for date, open_, high, low, close_, volume, source in rows:
        insert_asset_value(asset, date, Granularity.min, open_, high, low,
                           close_, volume, source)

    try:
        db.session.commit()
    except (IntegrityError, InvalidRequestError):
        log.exception('Something went wrong')
        db.session.rollback()
    else:
        log.info('Asset values for {0} have been imported', code)