Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Losing more than 100% in backtest

Hi,

I am pretty new here and am a little confused about something. I have an algorithm that only uses order_target_percent and i make sure that the total percent I order sums up to 1. However, for some reason, at some point during the backtest everything seems to start going crazy, up and down, and the total returns end up being far less than 100%. How is this possible if I don't leverage?

Thanks for the help :)

4 responses

You probably keep executing order_target_percent while you have an open order. Easy way to fix this is to add:

if get_open_orders():  
    return  
#Now do stuff  

This will halt the code whenever you have ANY open order that hasn't been filled. You can make it stock-specific as well:

for stock in data:  
    if get_open_orders(stock):  
        return  
    #Now do stuff  

Great. Thank you.

Also, what happens if I have securities I am trying to trade that did not yet exist? For example a stock that started trading in 2010 but my backtest is during 2005. Is there any way to check the existence of a security?

Thanks again

You can set a universe to a percentile of DollarVolume and this will work at all times for all backtests because it's a "rolling list" of securities trading at that time:

set_universe(universe.DollarVolumeUniverse(98,99))  

If you want to test specific stocks, you need to specify the symbol lookup date:

set_symbol_lookup_date('2010-08-31')  
context.stock = symbols('XXXX')   #Replace XXXX with your ticker symbol  

Thanks once again.

I noticed that to get the amount of stocks in your universe you use len(data.keys()). However, this doesn't work in initialize() since there is no "data".
Do you know how I would go about this in initialize()?

Thanks!