Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Momentum day trading strategy

Hi, new to Quantopian so I apologise if this may seem a stupid question

I am trying to implement a simple Momentum day trading strategy.
Basically, each day I narrow down to about 4 securities through the screening process and I open up the positions every day. I also just want to try out closing each position at the end of the day i.e. I don't want to hold any positions overnight.

However, there seems to be a problem with my code and I can't seem to fix the issue - I am not clearing my positions at the end of the day when I check through the transactions and I actually start off the backtest with shorting securities which is very peculiar and I seem to be stacking on huge leverage.

Please help! Thank you.

3 responses

Update: it would appear that by changing the code to context.long instead of context.portfolio positions, the first problem is solved i.e. I manage to clear my positions by the end of the day. However, I still start off with short positions which is not what I want.

@ Ethan

Try to use order_target_percent():

def open_all(context, data):  
    for security in context.long:  
        order_target_percent(security, 0.25)

def close_all(context, data):  
    for security in context.portfolio.positions:  
        order_target_percent(security, 0)  

@Vladimir
Thank you so much:)