How to convert pinescript code using the tradingview API to v5

156 views Asked by At
study("RDX Score", overlay=true)
len = input(title="length_ADX", type=integer, defval=14)
th = input(title="threshold_ADX", type=integer, defval=14)

TrueRange = max(max(high-low, abs(high-nz(close[1]))), abs(low-nz(close[1])))
DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? max(high-nz(high[1]), 0): 0
DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ? max(nz(low[1])-low, 0): 0

SmoothedTrueRange = nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/len) + TrueRange
SmoothedDirectionalMovementPlus = nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/len) + DirectionalMovementPlus
SmoothedDirectionalMovementMinus = nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1])/len) + DirectionalMovementMinus

DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
DX = abs(DIPlus-DIMinus) / (DIPlus+DIMinus)*100
ADX = sma(DX, len)


length_RSI = input(14 )
priceRSI = close
vrsi = rsi(priceRSI, length_RSI)

m5 = ADX[1]<ADX and vrsi<30
plotshape(m5, title="SCORE -5", text="-5", textcolor=white, style=shape.triangledown, location=location.abovebar, color=red, transp=50, size=size.tiny)

m4 = (ADX[1]>ADX and vrsi<30) or (ADX[1]<ADX and vrsi<35 and vrsi>30)
plotshape(m4, title="SCORE -4", text="-4", textcolor=white, style=shape.triangledown, location=location.abovebar, color=red, transp=50, size=size.tiny)

m3 = (ADX[1]>ADX and vrsi<35 and vrsi>30) or (ADX[1]<ADX and vrsi<40 and vrsi>35)
plotshape(m3, title="SCORE -3", text="-3", textcolor=white, style=shape.triangledown, location=location.abovebar, color=red, transp=50, size=size.tiny)

m2 = (ADX[1]>ADX and vrsi<40 and vrsi>35) or (ADX[1]<ADX and vrsi<45 and vrsi>40)
plotshape(m2, title="SCORE -2", text="-2", textcolor=white, style=shape.triangledown, location=location.abovebar, color=red, transp=50, size=size.tiny)

m1 = ADX[1]>ADX and vrsi<45 and vrsi>40
plotshape(m1, title="SCORE -1", text="-1", textcolor=white, style=shape.triangledown, location=location.abovebar, color=red, transp=50, size=size.tiny)

m0 = vrsi<55 and vrsi>45
//plotshape(m0, title="QQE short", text="0", textcolor=black, style=shape.labeldown, location=location.abovebar, color=yellow, transp=0, size=size.tiny)
//plotshape(m0, title="QQE long", text="0", textcolor=black, style=shape.labelup, location=location.belowbar, color=yellow, transp=0, size=size.tiny)

p1 = ADX[1]>ADX and vrsi>55 and vrsi<60
plotshape(p1, title="SCORE 1", text="1", textcolor=white, style=shape.triangleup, location=location.belowbar, color=green, transp=50, size=size.tiny)

p2 = (ADX[1]>ADX and vrsi>60 and vrsi<65) or (ADX[1]<ADX and vrsi>55 and vrsi<60)
plotshape(p2, title="SCORE 2", text="2", textcolor=white, style=shape.triangleup, location=location.belowbar, color=green, transp=50, size=size.tiny)

p3 = (ADX[1]>ADX and vrsi>65 and vrsi<70) or (ADX[1]<ADX and vrsi>60 and vrsi<65)
plotshape(p3, title="SCORE 3", text="3", textcolor=white, style=shape.triangleup, location=location.belowbar, color=green, transp=50, size=size.tiny)

p4 = (ADX[1]>ADX and vrsi>70) or (ADX[1]<ADX and vrsi>65 and vrsi<70)
plotshape(p4, title="SCORE 4", text="4", textcolor=white, style=shape.triangleup, location=location.belowbar, color=green, transp=50, size=size.tiny)

p5 = ADX[1]<ADX and vrsi>70
plotshape(p5, title="SCORE 5", text="5", textcolor=white, style=shape.triangleup, location=location.belowbar, color=green, transp=50, size=size.tiny)

I'm a novice programmer working on a Trading view Pine script. im trying to convert old code to v5 tried a lot but no use. i tried to convert this old script of RDX SCORE INDICATOR but in the end numbers where not plotting in v5 how can i fix this. highly appreciate your support if anyone can help.

3

There are 3 answers

0
SpaceX On BEST ANSWER

In this code the timeframe = '' option is replaced by the request.security() function:

//@version=5

indicator('RDX Score', overlay=true)
len = input(title='length_ADX', defval=14)
th = input(title='threshold_ADX', defval=14)
Timeframe = input.timeframe('15', 'Higher timeframe')

TrueRange = math.max(math.max(high - low, math.abs(high - nz(close[1]))), math.abs(low - nz(close[1])))
DirectionalMovementPlus = high - nz(high[1]) > nz(low[1]) - low ? math.max(high - nz(high[1]), 0) : 0
DirectionalMovementMinus = nz(low[1]) - low > high - nz(high[1]) ? math.max(nz(low[1]) - low, 0) : 0

SmoothedTrueRange = 0.0
SmoothedTrueRange := nz(SmoothedTrueRange[1]) - nz(SmoothedTrueRange[1]) / len + TrueRange

SmoothedDirectionalMovementPlus = 0.0
SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - nz(SmoothedDirectionalMovementPlus[1]) / len + DirectionalMovementPlus

SmoothedDirectionalMovementMinus = 0.0
SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) - nz(SmoothedDirectionalMovementMinus[1]) / len + DirectionalMovementMinus

DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
DX = math.abs(DIPlus - DIMinus) / (DIPlus + DIMinus) * 100
ADX = ta.sma(DX, len)

length_RSI = input(14)
priceRSI = close
vrsi =  ta.rsi(priceRSI, length_RSI)

[H_vrsi, H_ADX] =  request.security(syminfo.tickerid, Timeframe, [ta.rsi(priceRSI, length_RSI), ta.sma(DX, len)])

m5 = ADX[1] < ADX and vrsi < 30
plotshape(m5, title='SCORE -5', text='-5', textcolor=color.new(color.white, 50), style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 50), size=size.tiny)

m4 = ADX[1] > ADX and vrsi < 30 or ADX[1] < ADX and vrsi < 35 and vrsi > 30
plotshape(m4, title='SCORE -4', text='-4', textcolor=color.new(color.white, 50), style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 50), size=size.tiny)

m3 = ADX[1] > ADX and vrsi < 35 and vrsi > 30 or ADX[1] < ADX and vrsi < 40 and vrsi > 35
plotshape(m3, title='SCORE -3', text='-3', textcolor=color.new(color.white, 50), style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 50), size=size.tiny)

m2 = ADX[1] > ADX and vrsi < 40 and vrsi > 35 or ADX[1] < ADX and vrsi < 45 and vrsi > 40
plotshape(m2, title='SCORE -2', text='-2', textcolor=color.new(color.white, 50), style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 50), size=size.tiny)

m1 = ADX[1] > ADX and vrsi < 45 and vrsi > 40
plotshape(m1, title='SCORE -1', text='-1', textcolor=color.new(color.white, 50), style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 50), size=size.tiny)

m0 = vrsi < 55 and vrsi > 45
//plotshape(m0, title="QQE short", text="0", textcolor=black, style=shape.labeldown, location=location.abovebar, color=yellow, transp=0, size=size.tiny)
//plotshape(m0, title="QQE long", text="0", textcolor=black, style=shape.labelup, location=location.belowbar, color=yellow, transp=0, size=size.tiny)

p1 = ADX[1] > ADX and vrsi > 55 and vrsi < 60
plotshape(p1, title='SCORE 1', text='1', textcolor=color.new(color.white, 50), style=shape.triangleup, location=location.top, color=color.new(color.green, 50), size=size.tiny)

p2 = ADX[1] > ADX and vrsi > 60 and vrsi < 65 or ADX[1] < ADX and vrsi > 55 and vrsi < 60
plotshape(p2, title='SCORE 2', text='2', textcolor=color.new(color.white, 50), style=shape.triangleup, location=location.top, color=color.new(color.green, 50), size=size.tiny)

p3 = ADX[1] > ADX and vrsi > 65 and vrsi < 70 or ADX[1] < ADX and vrsi > 60 and vrsi < 65
plotshape(p3, title='SCORE 3', text='3', textcolor=color.new(color.white, 50), style=shape.triangleup, location=location.top, color=color.new(color.green, 50), size=size.tiny)

p4 = ADX[1] > ADX and vrsi > 70 or ADX[1] < ADX and vrsi > 65 and vrsi < 70
plotshape(p4, title='SCORE 4', text='4', textcolor=color.new(color.white, 50), style=shape.triangleup, location=location.top, color=color.new(color.green, 50), size=size.tiny)

p5 = ADX[1] < ADX and vrsi > 70
plotshape(p5, title='SCORE 5', text='5', textcolor=color.new(color.white, 50), style=shape.triangleup, location=location.top, color=color.new(color.green, 50), size=size.tiny)






H_m5 = H_ADX[1] < H_ADX and H_vrsi < 30
plotshape(H_m5, title='SCORE -5', text='-5', textcolor=color.new(color.white, 0), style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 25), size=size.normal)

H_m4 = H_ADX[1] > H_ADX and H_vrsi < 30 or H_ADX[1] < H_ADX and H_vrsi < 35 and H_vrsi > 30
plotshape(H_m4, title='SCORE -4', text='-4', textcolor=color.new(color.white, 0), style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 25), size=size.normal)

H_m3 = H_ADX[1] > H_ADX and H_vrsi < 35 and H_vrsi > 30 or H_ADX[1] < H_ADX and H_vrsi < 40 and H_vrsi > 35
plotshape(H_m3, title='SCORE -3', text='-3', textcolor=color.new(color.white, 0), style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 25), size=size.normal)

H_m2 = H_ADX[1] > H_ADX and H_vrsi < 40 and H_vrsi > 35 or H_ADX[1] < H_ADX and H_vrsi < 45 and H_vrsi > 40
plotshape(H_m2, title='SCORE -2', text='-2', textcolor=color.new(color.white, 0), style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 25), size=size.normal)

H_m1 = H_ADX[1] > H_ADX and H_vrsi < 45 and H_vrsi > 40
plotshape(H_m1, title='SCORE -1', text='-1', textcolor=color.new(color.white, 0), style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 25), size=size.normal)

H_m0 = H_vrsi < 55 and H_vrsi > 45

H_p1 = H_ADX[1] > H_ADX and H_vrsi > 55 and H_vrsi < 60
plotshape(H_p1, title='SCORE 1', text='1', textcolor=color.new(color.white, 0), style=shape.triangleup, location=location.belowbar, color=color.new(color.green, 25), size=size.normal)

H_p2 = H_ADX[1] > H_ADX and H_vrsi > 60 and H_vrsi < 65 or H_ADX[1] < H_ADX and H_vrsi > 55 and H_vrsi < 60
plotshape(H_p2, title='SCORE 2', text='2', textcolor=color.new(color.white, 0), style=shape.triangleup, location=location.belowbar, color=color.new(color.green, 25), size=size.normal)

H_p3 = H_ADX[1] > H_ADX and H_vrsi > 65 and H_vrsi < 70 or H_ADX[1] < H_ADX and H_vrsi > 60 and H_vrsi < 65
plotshape(H_p3, title='SCORE 3', text='3', textcolor=color.new(color.white, 0), style=shape.triangleup, location=location.belowbar, color=color.new(color.green, 25), size=size.normal)

H_p4 = H_ADX[1] > H_ADX and H_vrsi > 70 or H_ADX[1] < H_ADX and H_vrsi > 65 and H_vrsi < 70
plotshape(H_p4, title='SCORE 4', text='4', textcolor=color.new(color.white, 0), style=shape.triangleup, location=location.belowbar, color=color.new(color.green, 25), size=size.normal)

H_p5 = H_ADX[1] < H_ADX and H_vrsi > 70
plotshape(H_p5, title='SCORE 5', text='5', textcolor=color.new(color.white, 0), style=shape.triangleup, location=location.belowbar, color=color.new(color.green, 25), size=size.normal)
1
SpaceX On

If the indicator will display signals as if it's on a higher timeframe (ex 15 min) you will receive one signal every 15 minutes.

To achieve this you can use the timeframe = '' option:

indicator('RDX Score', overlay=true, timeframe = '')

enter image description here

If you want

3
SpaceX On

enter image description hereHere is the converted code to version 5.

//@version=5

indicator('RDX Score', overlay=true)
len = input(title='length_ADX', defval=14)
th = input(title='threshold_ADX', defval=14)

TrueRange = math.max(math.max(high - low, math.abs(high - nz(close[1]))), math.abs(low - nz(close[1])))
DirectionalMovementPlus = high - nz(high[1]) > nz(low[1]) - low ? math.max(high - nz(high[1]), 0) : 0
DirectionalMovementMinus = nz(low[1]) - low > high - nz(high[1]) ? math.max(nz(low[1]) - low, 0) : 0

SmoothedTrueRange = 0.0
SmoothedTrueRange := nz(SmoothedTrueRange[1]) - nz(SmoothedTrueRange[1]) / len + TrueRange

SmoothedDirectionalMovementPlus = 0.0
SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - nz(SmoothedDirectionalMovementPlus[1]) / len + DirectionalMovementPlus

SmoothedDirectionalMovementMinus = 0.0
SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) - nz(SmoothedDirectionalMovementMinus[1]) / len + DirectionalMovementMinus

DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
DX = math.abs(DIPlus - DIMinus) / (DIPlus + DIMinus) * 100
ADX = ta.sma(DX, len)


length_RSI = input(14)
priceRSI = close
vrsi = ta.rsi(priceRSI, length_RSI)

m5 = ADX[1] < ADX and vrsi < 30
plotshape(m5, title='SCORE -5', text='-5', textcolor=color.new(color.white, 50), style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 50), size=size.tiny)

m4 = ADX[1] > ADX and vrsi < 30 or ADX[1] < ADX and vrsi < 35 and vrsi > 30
plotshape(m4, title='SCORE -4', text='-4', textcolor=color.new(color.white, 50), style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 50), size=size.tiny)

m3 = ADX[1] > ADX and vrsi < 35 and vrsi > 30 or ADX[1] < ADX and vrsi < 40 and vrsi > 35
plotshape(m3, title='SCORE -3', text='-3', textcolor=color.new(color.white, 50), style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 50), size=size.tiny)

m2 = ADX[1] > ADX and vrsi < 40 and vrsi > 35 or ADX[1] < ADX and vrsi < 45 and vrsi > 40
plotshape(m2, title='SCORE -2', text='-2', textcolor=color.new(color.white, 50), style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 50), size=size.tiny)

m1 = ADX[1] > ADX and vrsi < 45 and vrsi > 40
plotshape(m1, title='SCORE -1', text='-1', textcolor=color.new(color.white, 50), style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 50), size=size.tiny)

m0 = vrsi < 55 and vrsi > 45
//plotshape(m0, title="QQE short", text="0", textcolor=black, style=shape.labeldown, location=location.abovebar, color=yellow, transp=0, size=size.tiny)
//plotshape(m0, title="QQE long", text="0", textcolor=black, style=shape.labelup, location=location.belowbar, color=yellow, transp=0, size=size.tiny)

p1 = ADX[1] > ADX and vrsi > 55 and vrsi < 60
plotshape(p1, title='SCORE 1', text='1', textcolor=color.new(color.white, 50), style=shape.triangleup, location=location.belowbar, color=color.new(color.green, 50), size=size.tiny)

p2 = ADX[1] > ADX and vrsi > 60 and vrsi < 65 or ADX[1] < ADX and vrsi > 55 and vrsi < 60
plotshape(p2, title='SCORE 2', text='2', textcolor=color.new(color.white, 50), style=shape.triangleup, location=location.belowbar, color=color.new(color.green, 50), size=size.tiny)

p3 = ADX[1] > ADX and vrsi > 65 and vrsi < 70 or ADX[1] < ADX and vrsi > 60 and vrsi < 65
plotshape(p3, title='SCORE 3', text='3', textcolor=color.new(color.white, 50), style=shape.triangleup, location=location.belowbar, color=color.new(color.green, 50), size=size.tiny)

p4 = ADX[1] > ADX and vrsi > 70 or ADX[1] < ADX and vrsi > 65 and vrsi < 70
plotshape(p4, title='SCORE 4', text='4', textcolor=color.new(color.white, 50), style=shape.triangleup, location=location.belowbar, color=color.new(color.green, 50), size=size.tiny)

p5 = ADX[1] < ADX and vrsi > 70
plotshape(p5, title='SCORE 5', text='5', textcolor=color.new(color.white, 50), style=shape.triangleup, location=location.belowbar, color=color.new(color.green, 50), size=size.tiny)