Back to Community
Continue to hold a position as long as a condition is not met

I've used pipeline to look for cheap breakouts (inexpensive valuation combined with large price move on big volume). I want to hold that stock until it breaks down (simplistically defined as falling below 50 day moving average). How best to compare the current price to its 50 day moving average?

Here's the relevant code:

def my_rebalance(context,data):
for security in context.portfolio.positions:
# check to see if our longs have broken down. otherwise, continue owning

    if security.USEquityPricing.price.latest < security.SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=50) and data.can_trade(security):  

for security in context.longs:  
    if data.can_trade(security):  
Loading notebook preview...
Notebook previews are currently unavailable.