Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Seasonal Algo

I would like a create an algo which buys TLT in January and February and from May to September, and buys QQQ in March and April and from October to December. (TLT is sold when QQQ is bought and vice-verse.) Does anyone know how to do this? I am not quite sure how to utilize the datetime fuction to code this idea. Thanks for any help in advance.

20 responses

Here you go.

Thank you so much Simon! This is exactly what I was looking for.

Another version.

Oh wow even more concise. Kind of strange how there is a slight difference in performance though. Anyways, thank you Jeremy for the input!

and so simple... why would this give a consistent edge? @YoungQuant tell us about the story behind it...

Like you said, pretty amazing how brief, can be boiled down to a mere 14 lines of code.

This is definitely positive yet a slight bit less impressive if you consider that it spent $1,383,106 to profit $5,015,961 for an effective 363%. Max leverage/margin was early on 2002-10-09 so fairly easy to check into and maybe remedy, or just be aware of it.

@Peter The algo is based on seasonality trends and "myths" such as "sell in May and go away" and the "Halloween Effect." In reality, these well known stock market sayings are not myths, and the time of the year actually does influence the market. In general, winter is more profitable than summer.

@garyha I'll look into that and try to remove any leverage and see if the algo still gets good results.

winter is more profitable then summer.... is this a factor of the number of market participants in the market or as a proxy the volume created? In other words is there a relationship between the volume and returns? Just trying to quantify myths....

@Peter To be honest, I am not entirely sure. It's just a long standing pattern worth investigating. Some of the December success most likely has to do with Christmas.

I would like a create an algo which buys TLT in Monday and sell on Tuesday.. and from Wednesday buy QQQ then sell on Thursday... and buys SPY on Friday ... Does anyone know how to do this? I am not quite sure how to utilize the date time function to code this idea. Thanks for any help in advance.

John this should be really simple, modify mine to look,up on weekday instead of year month.

@Simon... I took a stab.. on it and encounter.. some Errors... KeyError: 6 wat am I.. missing...it stil wont work on a weekday... instead of year month. heres the code...

def initialize(context):
context.spy = sid(8554)
context.tlt = sid(23921)
context.week_ports = { 1: {context.tlt: 1.0, context.tlt: 0.0},
2: {context.spy: 1.0, context.spy: 0.0},
3: {context.tlt: 1.0, context.tlt: 0.0},
4: {context.spy: 1.0, context.spy: 0.0},
5: {context.tlt: 1.0, context.tlt: 0.0},

                   }  

schedule_function(func=do_allocation,
date_rule=date_rules.week_start(),
time_rule=time_rules.market_close(),
half_days=False
)

def handle_data(context, data):
record(spy=context.portfolio.positions[context.spy].amount)
record(tlt=context.portfolio.positions[context.tlt].amount)

def do_allocation(context, data):
week = get_datetime().week
port = context.week_ports[week]
for (sid, weight) in port.iteritems():
if sid in data:
order_target_percent(sid, weight)

have you tried establishing a hedge/long-short strategy with this algo?

John looks like you are probably missing allocations for saturday and Sunday, you might need to schedule daily also. Maybe add some logging to verify you have the right portfolios on each weekday.

Simon...I just need.. monday to friday... the logging is nice.. to have... but I dont hav any knowledge of it, on how to do it or structure right now... if it can that can run as is... i'll be happy... we'll add the logging stuff later on... can you correct my work cheers... ;)

Then figure it out. See why it wants to do something on day six. Maybe timezones.

wat is email... mayb we can.. collaborate... ;) help a buddy out... since I've practiclly exhausted all my effort... and the bugs still wont disappear...

Ask me in a month, I am very busy right now sorry.

Check if this helps!! Modify time/date offset as desired. Thanks!

Thanks Shiv.... any help... from the community... is well... appreciated. tnxs ;)