분류
예제
슈퍼트렌드 백테스트 스크립트
페이지 정보
본문
https://youtu.be/sL7gxwDKQV8 에 사용된 스크립트입니다.
요청이 있어 올립니다.
ver1.0
//@version=2 //Author - Rajandran R //www.marketcalls.in //Edit - kimchulho //1.0 - Basic strategy //1.1 - ADD Long and short //1.2 - Backtest period strategy("Supertrend strategy kimchulho ed 1.1", shorttitle="Supertrend STG 1.1", overlay=true, initial_capital=100000000, default_qty_type=strategy.percent_of_equity, default_qty_value=100) Factor = input(3, minval=1, maxval=100) Pd = input(7, minval=1, maxval=100) isLong = input(title="Long", type=bool, defval=true) isShort = input(title="Short", type=bool, defval=false) 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 ? green : red plot(Tsl, color=linecolor, style=line , linewidth=2, title="SuperTrend") 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) //plot(Trend==1 and Trend[1]==-1,color = linecolor, style = circles, linewidth = 3,title="Trend") 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) //////////////////////////////////////////////////////////////////////////////// // Backtesting //////////////////////////////////////////////////////////////////////////////// startY = input(2018, "Start Y") startM = input(1, "Start M") startD = input(1, "Start D") startP = timestamp(startY, startM, startD, 0, 0) stopY = input(9999, "Stop Y") stopM = input(12, "Stop M") stopD = input(31, "Stop D") stopP = timestamp(stopY, stopM, stopD, 0, 0) periodBg = input(title="Background Color", type=bool, defval=true) periodBgColor = periodBg and (time >= startP) and (time <= stopP) ? #00FF00 : na testPeriod() => time >= startP and time <= stopP ? true : false if testPeriod() longCondition = cross(close,Tsl) and close>Tsl shortCondition = cross(Tsl,close) and close<Tsl strategy.entry("BUY", true, when = longCondition and isLong) strategy.close("BUY", when = shortCondition and not isShort) strategy.entry("SELL", false, when = shortCondition and isShort) strategy.close("SELL", when = longCondition and not isLong)
ver1.1
//@version=2 //Author - Rajandran R //www.marketcalls.in //Edit - kimchulho //1.0 - Basic strategy //1.1 - ADD Long and short //1.2 - Backtest period strategy("Supertrend strategy kimchulho ed 1.1", shorttitle="Supertrend STG 1.1", overlay=true, initial_capital=100000000, default_qty_type=strategy.percent_of_equity, default_qty_value=100) Factor = input(3, minval=1, maxval=100) Pd = input(7, minval=1, maxval=100) isLong = input(title="Long", type=bool, defval=true) isShort = input(title="Short", type=bool, defval=false) 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 ? green : red plot(Tsl, color=linecolor, style=line , linewidth=2, title="SuperTrend") 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) //plot(Trend==1 and Trend[1]==-1,color = linecolor, style = circles, linewidth = 3,title="Trend") 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) //////////////////////////////////////////////////////////////////////////////// // Backtesting //////////////////////////////////////////////////////////////////////////////// startY = input(2018, "Start Y") startM = input(1, "Start M") startD = input(1, "Start D") startP = timestamp(startY, startM, startD, 0, 0) stopY = input(9999, "Stop Y") stopM = input(12, "Stop M") stopD = input(31, "Stop D") stopP = timestamp(stopY, stopM, stopD, 0, 0) periodBg = input(title="Background Color", type=bool, defval=true) periodBgColor = periodBg and (time >= startP) and (time <= stopP) ? #00FF00 : na testPeriod() => time >= startP and time <= stopP ? true : false if testPeriod() longCondition = cross(close,Tsl) and close>Tsl shortCondition = cross(Tsl,close) and close<Tsl strategy.entry("BUY", true, when = longCondition and isLong) strategy.close("BUY", when = shortCondition and not isShort) strategy.entry("SELL", false, when = shortCondition and isShort) strategy.close("SELL", when = longCondition and not isLong) bgcolor(periodBgColor, title="Backtest BG", color=green, transp=95)
ver1.2
//@version=2 //Author - Rajandran R //www.marketcalls.in //Edit - kimchulho //1.0 - Basic strategy //1.1 - ADD Long and short //1.2 - Backtest period strategy("Supertrend strategy kimchulho ed 1.2", shorttitle="Supertrend STG 1.2", overlay=true, initial_capital=100000000, default_qty_type=strategy.percent_of_equity, default_qty_value=100) Factor = input(3, minval=1, maxval=100) Pd = input(7, minval=1, maxval=100) isLong = input(title="Long", type=bool, defval=true) isShort = input(title="Short", type=bool, defval=false) 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 ? green : red plot(Tsl, color=linecolor, style=line , linewidth=2, title="SuperTrend") 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) //plot(Trend==1 and Trend[1]==-1,color = linecolor, style = circles, linewidth = 3,title="Trend") 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) //////////////////////////////////////////////////////////////////////////////// // Backtesting //////////////////////////////////////////////////////////////////////////////// startY = input(2018, "Start Y") startM = input(1, "Start M") startD = input(1, "Start D") startP = timestamp(startY, startM, startD, 0, 0) stopY = input(9999, "Stop Y") stopM = input(12, "Stop M") stopD = input(31, "Stop D") stopP = timestamp(stopY, stopM, stopD, 0, 0) periodBg = input(title="Background Color", type=bool, defval=true) periodBgColor = periodBg and (time >= startP) and (time <= stopP) ? #00FF00 : na testPeriod() => time >= startP and time <= stopP ? true : false if testPeriod() longCondition = cross(close,Tsl) and close>Tsl shortCondition = cross(Tsl,close) and close<Tsl strategy.entry("BUY", true, when = longCondition and isLong) strategy.close("BUY", when = shortCondition and not isShort) strategy.entry("SELL", false, when = shortCondition and isShort) strategy.close("SELL", when = longCondition and not isLong) bgcolor(periodBgColor, title="Backtest BG", color=green, transp=95)