Hi guys,
I am trying to build a simple algorithm(as a start!), in which I would like to calculate the % change of two stocks then save them into a list. As a test, I try to print out the calculated change in the log but it does not work.
The following is my code:
def initialize(context):
context.stocks = symbols('DIA', # SPDR Dow Jones Industrial Average ETF
'SPY', # SPDR S&P 500 ETF)
def handle_data(context, data):
prices = history(100, '1d', 'price', ffill=True)
print context.pre
def returns(context,data):
for i in len(context.stocks):
price_cur = prices[context.stocks[i]][len(context.stocks)-1]
price_pre = prices[context.stocks[i]][len(context.stocks)-1-n_short]
context.pre=(price_cur-price_pre)/price_pre
Can any one tell what is wrong with the code? Thanks so much!