Having difficulty applying transform columns to a dataframe of fetch'd data. The following is a minimal sample:
import numpy as np
import pandas as pd
import talib
url ='http://www.quandl.com/api/v1/datasets/YAHOO/INDEX_VIX.csv?request_source=python&request_version=2&sort_order=asc&trim_start=2002-01-01'
def fix_vix(df):
df = df.rename(columns={'Close': 'price'})
df.fillna(method='ffill')
df['symbol'] = 'vix'
df['sma'] = talib.SMA(df['price'], timeperiod=5)
df['ema'] = talib.EMA(df['price'], timeperiod=5)
df = df[['price', 'sma', 'ema', 'symbol', 'sid']]
return df
def initialize(context):
fetch_csv(url, symbol='vix', date_column='Date', usecols=['Close'], post_func=fix_vix)
# The following results in an error, irrespective of the use of the 'symbol' param to fetch_csv, or constructing
# a 'symbol' column with the value 'vix' in the post_func.
# context.vix = symbol('vix')
def handle_data(context, data):
if data is None:
return
log.info(data['vix'])
log.info('price: %f' % data['vix'].price)
log.info('price: %f' % data['vix'].ema)
return
The result is:
30 Error Runtime exception: AttributeError: 'SIDData' object has no attribute 'ema'
Despite the SiDData bits being logged clearly showing the 'ema' and 'sma' columns.
2011-01-04handle_data:29INFOSIDData({'ema': 17.448922258336495, '_sid': 'vix', '_freqstr': None, '_initial_len': 3, 'price': 17.38, 'sid': 'vix', 'source_id': 'PandasRequestsCSVb3e026392ead2d14e1bbc7c21b5e8ea4', 'dt': Timestamp('2011-01-04 00:00:00+0000', tz='UTC'), 'type': 9, 'symbol': 'vix', 'sma': 17.508000000000045})
As commented in the code, I've also been unable to get the data into, say, context.vix by the tricks in the docs (at least as well as I did/didn't understand them).
Suggestions?