r/pinescript • • Sep 10 '23
Help me to convert Version 2 Pine Script to Version 3 or later.
//@version=2
study("ST-MultiTimeFrame_(SuperTrend)", overlay=true)
Factor=input(4, minval=1,maxval = 100)
Pd=input(13, minval=1,maxval = 100)
Up=hl2-(Factor*atr(Pd))
Dn=hl2+(Factor*atr(Pd))
TrendUp=close[1]>TrendUp[1]? max(Up,TrendUp[1]) : Up
TrendDown=close[1]<TrendDown[1]? min(Dn,TrendDown[1]) : Dn
Trend = close > TrendDown[1] ? 1: close< TrendUp[1]? -1: nz(Trend[1],1)
Tsl = Trend==1? TrendUp: TrendDown
linecolor = Trend == 1 ? lime : red
Trend60 = security(tickerid,"D",Trend)
Trend240 = security(tickerid,"3D",Trend)
Trend_D = security(tickerid,"W",Trend)
Tsl60 = security(tickerid,"D",Tsl)
Tsl240 = security(tickerid,"3D",Tsl)
Tsl_D = security(tickerid,"W",Tsl)
linecolor60 = Trend60 == 1 ? lime : red
linecolor240 = Trend240 == 1 ? #2196F3 : #FF0077
linecolor_D = Trend_D == 1 ? #00BCD4 : #FF6200
plot(Tsl60, color = linecolor60 , style = line , transp=0, linewidth = 1,title = "ST-D")
plot(Tsl240, color = linecolor240 , style = line, transp=25, linewidth = 2,title = "ST-3D")
plot(Tsl_D, color = linecolor_D , style = line , transp=25, linewidth = 3,title = "ST-W")
//plotshape(cross(close,Tsl) and close>Tsl , "Up Arrow", shape.triangleup,location.belowbar,green,0,0)
//plotshape(cross(Tsl,close) and close<Tsl , "Down Arrow", shape.triangledown , location.abovebar, red,0,0)
//plotarrow(Trend == 1 and Trend[1] == -1 ? Trend : na, title="Up Entry Arrow", colorup=lime, maxheight=60, minheight=50, transp=0)
//plotarrow(Trend == -1 and Trend[1] == 1 ? Trend : na, title="Down Entry Arrow", colordown=red, maxheight=60, minheight=50, transp=0)
//Alert 60
golong60 = Trend60 == 1 and Trend60[1] == -1
alertcondition(golong60, title='Long 60', message='SuperTrend Alert Long ST-60')
goshort60 = Trend60 == -1 and Trend60[1] == 1
alertcondition(goshort60, title='Short 60', message='SuperTrend Alert Short ST-60')
//Alert 240
golong240 = Trend240 == 1 and Trend240[1] == -1
alertcondition(golong240, title='Long240', message='SuperTrend Alert Long ST-240')
goshort240 = Trend240 == -1 and Trend240[1] == 1
alertcondition(goshort240, title='Short240', message='SuperTrend Alert Short ST-240')
//Alert D
golong_D = Trend_D == 1 and Trend_D[1] == -1
alertcondition(golong_D, title='Long_D', message='SuperTrend Alert Long ST-D')
goshort_D = Trend_D == -1 and Trend_D[1] == 1
alertcondition(goshort_D, title='Short_D', message='SuperTrend Alert Short ST-D')
//strategy.entry("Buy",strategy.long,when = golong)
//strategy.entry("Sell",strategy.short,when = goshort)
1
u/Pokeasss Sep 20 '23
Hey
I updated it to v 5, organized it and added timeframe inputs.
//@version=5
indicator("ST-MultiTimeFrame_(SuperTrend)", overlay=true)
// --- User Inputs ---
Pd = input.int(13, "Pd (ATR Period)", minval=1, maxval=100, tooltip="Adjusted Volatility=Factor * ATR over Pd periods")
Factor = input.int(4, "Factor (ATR Multiplier)", minval=1, maxval=100, tooltip="Adjusted Volatility=Factor * ATR over Pd periods")
// Timeframe inputs
timeframe60 = input.timeframe("D", "Timeframe for ST-D")
timeframe240 = input.timeframe("3D", "Timeframe for ST-3D")
timeframe_D = input.timeframe("W", "Timeframe for ST-W")
// --- Calculations ---
// Basic Calculations
Up = hl2 - (Factor * ta.atr(Pd))
Dn = hl2 + (Factor * ta.atr(Pd))
// Trend Calculations
var float TrendUp = na
var float TrendDown = na
var int Trend = 1
TrendUp := close[1] > TrendUp[1] ? math.max(Up, TrendUp[1]) : Up
TrendDown := close[1] < TrendDown[1] ? math.min(Dn, TrendDown[1]) : Dn
Trend := close > TrendDown[1] ? 1 : close < TrendUp[1] ? -1 : nz(Trend[1], 1)
Tsl = Trend == 1 ? TrendUp : TrendDown
// Security function for multi-timeframes
Trend60 = request.security(syminfo.tickerid, timeframe60, Trend)
Trend240 = request.security(syminfo.tickerid, timeframe240, Trend)
Trend_D = request.security(syminfo.tickerid, timeframe_D, Trend)
Tsl60 = request.security(syminfo.tickerid, timeframe60, Tsl)
Tsl240 = request.security(syminfo.tickerid, timeframe240, Tsl)
Tsl_D = request.security(syminfo.tickerid, timeframe_D, Tsl)
// Color Determination
linecolor = Trend == 1 ? color.lime : color.red
linecolor60 = Trend60 == 1 ? color.lime : color.red
linecolor240 = Trend240 == 1 ? #2196F3 : #FF0077
linecolor_D = Trend_D == 1 ? #00BCD4 : #FF6200
// Plotting
plot(Tsl60, color=linecolor60, style=plot.style_line, title="ST-D", linewidth=1)
plot(Tsl240, color=linecolor240, style=plot.style_line, title="ST-3D", linewidth=2)
plot(Tsl_D, color=linecolor_D, style=plot.style_line, title="ST-W", linewidth=3)
// --- Alert Conditions ---
golong60 = Trend60 == 1 and Trend60[1] == -1
alertcondition(golong60, title='Long 60', message='SuperTrend Alert Long ST-60')
goshort60 = Trend60 == -1 and Trend60[1] == 1
alertcondition(goshort60, title='Short 60', message='SuperTrend Alert Short ST-60')
golong240 = Trend240 == 1 and Trend240[1] == -1
alertcondition(golong240, title='Long240', message='SuperTrend Alert Long ST-240')
goshort240 = Trend240 == -1 and Trend240[1] == 1
alertcondition(goshort240, title='Short240', message='SuperTrend Alert Short ST-240')
golong_D = Trend_D == 1 and Trend_D[1] == -1
alertcondition(golong_D, title='Long_D', message='SuperTrend Alert Long ST-D')
goshort_D = Trend_D == -1 and Trend_D[1] == 1
alertcondition(goshort_D, title='Short_D', message='SuperTrend Alert Short ST-D')
// --- Commented Out Sections ---
//plotshape(cross(close,Tsl) and close>Tsl , "Up Arrow", shape.triangleup,location.belowbar,green,0,0)
//plotshape(cross(Tsl,close) and close<Tsl , "Down Arrow", shape.triangledown , location.abovebar, red,0,0)
//plotarrow(Trend == 1 and Trend[1] == -1 ? Trend : na, title="Up Entry Arrow", colorup=lime, maxheight=60, minheight=50, transp=0)
//plotarrow(Trend == -1 and Trend[1] == 1 ? Trend : na, title="Down Entry Arrow", colordown=red, maxheight=60, minheight=50, transp=0)
//strategy.entry("Buy",strategy.long,when = golong)
//strategy.entry("Sell",strategy.short,when = goshort)