Hello All,
I've been playing with calculating Bollinger Bands but I don't know how to access my calculations in handle_data
import pandas
def process_df(df):
df = df.rename(columns={'Close': 'price'})
df = df.fillna(method='ffill')
#df = df[['price', 'sid']]
#log.info(' \n %s ' , df.head())
df['MA20']=pandas.stats.moments.rolling_mean(df['price'], 20)
df['ABS']=abs(df['price']-df['MA20'])
df['STDDEV']=pandas.stats.moments.rolling_std(df['ABS'], 20)
df['UPPER']=df['MA20']+2*df['STDDEV']
df['LOWER']=df['MA20']-2*df['STDDEV']
log.info(df[18:20])
log.info(df[37:39])
return df
def initialize(context):
fetch_csv('https://raw.github.com/pcawthron/StockData/master/CMG%202011%20Daily%20Close.csv',
date_column='Date',
symbol='CMG',
usecols=['Close'],
post_func = process_df,
date_format='%d/%m/%Y'
)
context.stock = sid(28016)
def handle_data(context, data):
record(CMG=data['CMG'].price)
#print data['CMG'].datetime
if str(data['CMG'].datetime) == "2011-03-31 00:00:00+00:00":
print data['CMG']
# This is what I would like to do!
# current_UPPER = data['CMG']['UPPER']
# record(LowerBB=data['CMG'].LOWER)
I can see that my data is available in data_handler but I'm stuck. Is this completely the wrong approach i.e. should I be using a batch transform?
Also, I don't understand the relationship between 'my' dataframe and the Quantopian dataframe of CMG's prices i.e.
data['CMG']
has my extra fields
but
data[sid(28016)]
does not. My data came from the Yahoo a few months ago.
Regards,
Peter