Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Best way to compare relative SMAs over time

I want to look at an X day SMA and compare it to a Y day SMA on the same universe of equities. This is the code I am using;

    context.bigSMA = 50  
    context.littleSMA = 15  
    historyPrice = history(context.bigSMA*2,'1d','price')  
    historyOpen = history(context.bigSMA,"1d","open_price",ffill=True)  
    historyClose = history(context.bigSMA,"1d","close_price",ffill=True)  
    priceBigMean = pd.rolling_mean(historyPrice, window=context.bigSMA, min_periods=1)[context.bigSMA:context.bigSMA*2]  
    priceLittleMean = pd.rolling_mean(historyPrice, window=context.littleSMA, min_periods=1)[context.bigSMA:context.bigSMA*2]  
    littleMeanMoreThanBigMean = (priceLittleMean >= priceBigMean)

I can then loop through littleMeanMoreThanBigMean to see closing prices where the shorter SMA is more than the Longer SMA.

Does this look reasonable?

1 response

The purpose of historyOpen and historyClose, are to then make note of the daily profit/loss on days where the littleSMAis > then the littleSMA.