Hi there!
I was wondering if any coding expert could help convert this code from TradingView.
Thanks in advance!
Cheers,
A
//@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
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")
Thank you, but unfortunately, it doesn’t work. See the attachment for the differences between the TradingView and ProRealTime indicators.
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:
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")
Now it works perfectly, thank you so much!