I was looking to trade a certain security on the same day and sell on the same day each year. How do I code this so that I do not need to manually input dates? Testing a seasonality trading algorithm. Some filler with moving average is there, but i just need to know how to do the date buy and sell on the same day and month as prescribed in the code.
import datetime
def initialize(context):
#security to be ordered
context.security = sid(32133)
#list of days to execute algorithm #1
context.alg1_days= [datetime.date(2007,4,20),datetime.date(2008,4,20),datetime.date(2009,4,20),datetime.date(2010,4,20),datetime.date(2011,4,20),datetime.date(2012,4,20)]
#list of days to execute algorithm #2
context.alg2_days= [datetime.date(2007,9,21),datetime.date(2008,9,21),datetime.date(2009,9,21),datetime.date(2010,9,21),datetime.date(2011,9,21),datetime.date(2012,9,21)]
def handle_data(context, data):
fmavg = data[context.security].mavg(12)
smavg = data[context.security].mavg(26)
macd = fmavg - smavg #MACD mavg(12) - mavg(26)
qavg = [macd,9] #MACD avg (9 days) # i dont know if this is right?
y = data[context.security].datetime.year
m = data[context.security].datetime.month
d = data[context.security].datetime.day
event_day = datetime.date(y,m,d)
#algorithm #1 execution
for day in context.alg1_days:
if event_day == day:
order(context.security,+10000)
#algorithm #2 execution
for day in context.alg2_days:
if event_day == day:
order(context.security,-10000)