Back

The Super Trend Indicator(Python)

I needed the atr indicator as trailing stop, but I couldnt find super trend in QC. So I tried to build one. And here it is.

Since I am not 100% sure the code is correct, please review the code before you use it. If theres any bugs or super trend already in QC, please let me know.

And the code is based on tradingview super trend in pinescript, and here is the code:

study("abc", overlay=true)
Periods = input(title="ATR Period", type=input.integer, defval=10)
Multiplier = input(title="ATR Multiplier", type=input.float, step=0.1, defval=2.0)

atr = atr(Periods)
atrDown = hl2 - (Multiplier*atr)
atrDown := close[1] > atrDown[1] ? max(atrDown, atrDown[1]) : atrDown
atrUp = hl2 + (Multiplier*atr)
atrUp := close[1] < atrUp[1] ? min(atrUp, atrUp[1]) : atrUp

atrTrend = 0
atrTrend := nz(atrTrend[1], atrTrend)
atrTrend := close > atrUp[1] ? 1 : close < atrDown[1] ? -1 : atrTrend[1]

upPlot = plot(atrTrend == -1 ? atrUp : na, style=plot.style_linebr, linewidth=2, color=color.red)
downPlot = plot(atrTrend == 1 ? atrDown : na, style=plot.style_linebr, linewidth=2, color=color.green)
closePlot = plot(close)
fill(upPlot, closePlot, color=color.red)
fill(downPlot, closePlot, color=color.green)

 

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.


Hi, please ignore the self.test in Update method of SuperTrend, it is for debugging only

2

You could try searching the forums for an implementation. I think someone has already tried using ATR trailing stop in their code here. 

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