Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Sell and buy logic with a profit taking portion

Hi All,

I am new to quantopian and giving a go at one of my first algos. The issue that I am encountering is the sell and buy logic. The parameters are simple I would like to buy when the prediction is above 1 and sell when below as well as when the the same occurs for my MA's. When I run the code it seems the logic I have is conflicting and keep getting a in and out affect.

Below is what I have:

if prediction > 0 and context.ma_50 > context.ma_200:  
            context.trade_price = data.current(context.assets,'price')  
            order_target_percent(context.assets,1)  
            if prediction < 0:  
                order_target_percent(context.assets, 0)  
            if context.trade_price*1.0025 < data.current(context.assets,'price'):  
                order_target_percent(context.assets,0)  
        else:  
            if prediction < 0 and context.ma_50 < context.ma_200:  
                order_target_percent(context.assets, -1.0)  
            if prediction > 0:  
                order_target_percent(context.assets, 0)  
            if context.trade_price*.9975 > data.current(context.assets,'price'):  
                order_target_percent(context.assets, 0)  

I have toyed with trying to have the Algo recognize when there is a position already present but had no luck with that. Any help would be great!!

Thanks in advance for your help