Back to Community
help with adding moving averages to a fundamental screen

I'm new to Python so this is all a little confusing to me as of now. I'm trying to create a list of stocks from four sectors (consumer cyclicals, tech, consumer defensives an industrials). I'm trying to combine all those stocks in a list and then rank them by return on invested capital (roic) and go long the top 50 and short the bottom 50, rebalanced monthly. I'm pretty sure the attached code does all this

I'm trying to add a wrinkle to it but can't seem to figure it out. I would like to only go long the high roic stocks if the sma 50 > sma 200 and only go short the bottom stocks if sma 200 < sma 50. I would like this new screen/filter to run daily. Any help would be very much appreciated.

Clone Algorithm
3
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5946d1263cceb4697b3d88c6
There was a runtime error.
3 responses

I'd suggest moving the logic into the algorithm and out of pipeline definition. Separate the data from the logic (though here it's still mixed together a bit).
I like using the 'query' method to easily perform logic. Add the SMA factors to the pipeline definition then maybe change your 'before_trading_start' method to something like this.

def before_trading_start(context, data): 

    context.pipe_output = pipeline_output('fundamentals_pipeline')

    #high roic stocks for longs  
    context.longs = (context.pipe_output.  
                     query('longs == True and sma_50 > sma_200').  
                     index.tolist()  
                     )

    # low roic stocks to short  
    context.shorts = (context.pipe_output.  
                     query('shorts == True and sma_50 < sma_200').  
                     index.tolist()  
                     )

Attached is a backtest with that change (note that I shortened the backtest timeframe to reduce run time).

Clone Algorithm
1
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5947eb0968be7b6d7bae2934
There was a runtime error.

awesome, thanks for the quick reply Dan, really appreciate it!

just ran this again and I don't think it's doing exactly what I'm looking for. I would like to sell a long position intra-month as soon as the sma_50 < sma_200 and buy back a short as soon as sma_50 > sma_200.

thanks in advance for the help!