Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Why is my code selling securities even though they are still in the context.ouput of my pipeline??

I have been working on this pipeline for some time now and am ready to start implementing orders with it. This is my first attempt and to my surprise it seems to be buying them correctly. However, the next day it sells all of the securites it bought the day before even though they are still in the context.output and should only sell when they are no longer in context.output.

Any help is much appreciated.

I tried my hardest not to ask for help Dan...... lol

1 response

@John R

The problem is in the statement


    for security in context.portfolio.positions:  
        if security not in context.output and data.can_trade(security):  
            order_target_percent(security, 0)

This should be

    for security in context.portfolio.positions:  
        if security not in context.output.index and data.can_trade(security):  
            order_target_percent(security, 0)

Note the change to 'context.output.index'. You need to specify the index (which is the list of the securities). Without specifying the index, the statement never finds any securities and therefore orders a target percent of zero (ie sells everything).

Attached is a backtest with this change. That's at least a start...