Conversion TrailingSL from TradingView
- This topic has 4 replies, 2 voices, and was last updated 1 year ago by .
Viewing 5 posts - 1 through 5 (of 5 total)
Viewing 5 posts - 1 through 5 (of 5 total)
Similar topics:
Forums › ProRealTime English forum › ProBuilder support › Conversion TrailingSL from TradingView
Hi there!
I was wondering if any coding expert could help convert this code from TradingView.
Thanks in advance!
Cheers,
A
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
//@version=5 indicator("1.5 ATR From High and -1.5 ATR From Low", shorttitle = "TSL", overlay=true) // Input for ATR length atrLength = input.int(21, title="ATR Length") // Calculate the ATR atrValue = ta.atr(atrLength) // Variables to store the last higher low, last lower high, and their corresponding ATR lines var float lastHigherLow = na var float lastLowerHigh = na var float atrLineLow = na var float atrLineHigh = na // Detect higher low and lower high if (low > low[1] and (na(lastHigherLow) or low > lastHigherLow)) lastHigherLow := low atrLineLow := lastHigherLow - 1.5 * atrValue if (high < high[1] and (na(lastLowerHigh) or high < lastLowerHigh)) lastLowerHigh := high atrLineHigh := lastLowerHigh + 1.5 * atrValue // Recalculate the lines if they're broken by a bar if (low < atrLineLow) lastHigherLow := low atrLineLow := lastHigherLow - 1.5 * atrValue if (high > atrLineHigh) lastLowerHigh := high atrLineHigh := lastLowerHigh + 1.5 * atrValue // Plot the -1.5 ATR from the low when a higher low occurs plot(na(atrLineLow) ? na : atrLineLow, color=color.red, linewidth=2, title="-1.5 ATR From Low") // Plot the 1.5 ATR from the high when a lower high occurs plot(na(atrLineHigh) ? na : atrLineHigh, color=color.blue, linewidth=2, title="1.5 ATR From High") |
For future posts use the Add PRT Code button.
here you have the code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
once lastlowerhigh=high once lasthigherlow=low atrLength=21 atrValue=averagetruerange[atrLength](close) if low>low[1] and low>lastHigherlow then lastHigherLow=low atrLineLow=lastHigherLow-1-5*atrValue endif if high<high[1] and high<lastLowerhigh then lastlowerhigh=high atrlinehigh=lastlowerhigh+1.5*atrvalue endif return atrlinelow as "-1.5 ATR from low"coloured("red"), atrlinehigh as "+1.5 ATR from High" coloured("blue") |
It's strange because the code I sent you matches the one on Tradingview. Since I didn't have an image of the indicator, I installed it on the TV and the results were identical… Ok. Now with the image you attached I understand what you want to program. Here is the code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
atrLength=21 atrValue=averagetruerange[atrLength](close) //---ATR Line Low once atrLineLow=0 if low < atrLineLow[1] then atrLineLow=low-1.5*atrValue elsif low-1.5*atrValue>atrLineLow[1] then atrLineLow=low-1.5*atrValue else atrLineLow=atrLineLow endif //---ATR Line High once atrLineHigh=999999 if high>atrLineHigh[1] then atrLineHigh=high+1.5*atrValue elsif high+1.5*atrValue<atrLineHigh[1] then atrLineHigh=high+1.5*atrValue else atrLineHigh=atrLineHigh endif //---Plot Lines when defined if atrLineLow<>0 and atrLineHigh<>999999 then LineLow=atrLineLow LineHigh=atrLineHigh else LineLow=undefined LineHigh=undefined endif //--------------------------------------------// return LineLow coloured("red"), LineHigh coloured("blue") |
Find exclusive trading pro-tools on 