Back

Slope of moving average

Hi all,

I'm looking to add a criterion to my algorithm so it only executes when the simple moving average is upward sloping (for a buy) or downward sloping (for a sell). 

Is there an easy way of checking whether the slope is upward or downward? I imagine it'd be to do with whether the current value of the moving average is greater than or less than the value one step prior, but not sure how to do that.

Thanks
Hasan

Update Backtest







In order to check if the current value of the moving average is greater than or less than the value one step prior, you will need to use a Rolling Window. Rolling windows are arrays of data in which the most recent item added in the window is in the first index. These are popularly paired with indicators such that one can look back and analyze a recent trend, as seen here

The example in the documentation shows how to implement a rolling window that saves the past 5 simple moving averages.

0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed