Conversion of PineScript strategy (TradingView)
Forums › ProRealTime English forum › ProBuilder support › Conversion of PineScript strategy (TradingView)
- This topic has 1 reply, 2 voices, and was last updated 9 months ago by Iván.
-
-
12/11/2023 at 6:27 PM #225124
I have a trading strategy developed in PineScript language, but I want to be able to backtest it on ProRealTime. It is not a particularly complex strategy, it is based around a single indicator on TradingView. A lot of the code is related to how to display the strategy on a chart which isn’t needed, I just want the entry signals, profit targets and stop loss exits converted so I can backtest. I am attaching a screenshot of how the strategy is displayed on a chart, but as I say, I don’t need all this.
One thing about the trade exit on a loss to point out, exit is not at the actual value of the calculated stop loss, exit only happens if a price bar closes below the value of the set stop loss, exit is then at the open of the next price bar. On the other hand, trade exit at a profit should happen immediately when the price touches the preset target profit (TP) level.
I am pasting the code below that is working in TradingView, is there anyone that can convert this to the code for a system I can probacktest in ProRealTime?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275//@version=1strategy(“test pivot strategy”,overlay = true,max_bars_back = 500,max_lines_count = 500, initial_capital = 1000000 , currency = currency.USD, default_qty_value = 5, default_qty_type = strategy.percent_of_equity, slippage = 0, calc_on_every_tick = true, calc_on_order_fills = true, use_bar_magnifier = true)bool ChartTime = time > chart.left_visible_bar_time and time < chart.right_visible_bar_timestring CORE = “➞ Core Settings 🔸”var bool TradeisON = falsevar bool LongTrade = falsevar bool ShortTrade = falsevar float TP = 0.0var float SL = 0.0int BarTIME = time – time[1]var line tpLine = navar line slLine = navar label LAB = navar int UpdatedX = 0var float UpdatedY = 0.0var float UpdatedSLP = 0.0var int UpdatedXLow = 0var float UpdatedYLow = 0.0var float UpdatedSLPLow = 0.0int Period = input.int(10, title=’ Period ➞’,group = CORE,inline = “001”)bool Trendtype = input.string(title = “ Type ➞”,defval=’Wicks’,options=[‘Wicks’, ‘Body’],group = CORE,inline = “001”)== ‘Wicks’string Extensions = input.string(title=’ Extend ➞’,defval=’ 25′,options=[‘ 25’, ‘ 50’, ‘ 75’],group = CORE,inline = “001”)color LineCol1 = input.color(color.rgb(109, 111, 111, 19),””,group = CORE,inline = “001”)bool ShowTargets = input.bool(true,”Show Targets”,group = CORE,inline = “002”)ExtenSwitcher(ex) =>switch ex‘ 25’ => 1 ,‘ 50’ => 2 ,=> 3WidthSwitcher(ex) =>switch ex‘1’ => 1 ,‘2’ => 2 ,=> 3StyleSwitcher(style) =>switch style‘Dashed’ => line.style_dashed ,‘Dotted’ => line.style_dotted ,=> line.style_solidmethod volAdj(int len)=>math.min(ta.atr(len) * 0.3, close * (0.3/100)) [20] /2Zband = volAdj(30)method Trendlines(float src, int timeIndex,bool dir) =>var int Start = 1 , var int End = 0 , var int TIME = 1var float YEnd = 0, var float YStart = 0 , var float Slope = 0var line Line1 = line.new(na,na,na,na)var line Line2 = line.new(na,na,na,na)var line Line3 = line.new(na,na,na,na)SCR = fixnan(src)if ta.change(SCR) != 0TIME := time[timeIndex]YStart := SCR[1]Start := TIME[1]Slope := (SCR – YStart) / (TIME – Start)SlopeEXTime = ExtenSwitcher(Extensions) * BarTIME * 25End := TIME + EXTimeYEnd := SCR + EXTime * Slopeif ta.change(SCR) != 0 and not TradeisON[1]LineCond = Slope * time < 0 ? dir ? na : color.rgb(11, 139, 7, 53) : dir ? color.rgb(212, 46, 0, 54) : naif not na(LineCond) //and ChartTimeLine1 := line.new(Start,YStart,End,YEnd,xloc.bar_time,extend.none,color=color.new(color.white,100))Line2:=line.new(Start,YStart – (Zband * 2),End,YEnd – (Zband * 2),xloc.bar_time,extend.none,color=color.new(color.black,100))Line3:=line.new(Start,YStart – (Zband * 1),End,YEnd – (Zband * 1),xloc.bar_time,extend.none,color=color.new(color.black,100))linefill.new(Line3,Line2,color= LineCol1)linefill.new(Line3,Line1,color= LineCond)// linefill.new(Line,Line2,color= color.rgb(28, 15, 2, 76))[Start, YStart, Slope]PH = ta.pivothigh(Trendtype ? high : close > open ? close : open, Period, Period / 2)PL = ta.pivotlow(Trendtype ? low : close > open ? open : close, Period, Period / 2)method GetlinePrice(int TIME, float Price, float SLOP, int LookB) =>var float Current = 0.0EsTime = time – TIMECurrent := Price + (EsTime – LookB * BarTIME) * SLOPCurrentmethod CheckCross(float Price, int StartTime, float StartPrice, float SLP) =>var float Current = 0.0var float Previous = 0.0if StartPrice[Period] != StartPriceCurrent := GetlinePrice(StartTime, StartPrice, SLP, 0)Previous := GetlinePrice(StartTime, StartPrice, SLP, 1)Crossover = Price[1] < Previous and Price > Current ? 1 : Price[1] > Previous – (Zband*0.1) and Price < Current – (Zband*0.1) ? -1 : 0Crossover[Xx, XZ, SLPXZ] = Trendlines(PH, Period / 2,false)[XxL, XZL, SLPXZL] = Trendlines(PL, Period / 2, true)if ta.change(fixnan(PH)) != 0UpdatedX := XxUpdatedY := XZUpdatedSLP := SLPXZUpdatedSLPif ta.change(fixnan(PL)) != 0UpdatedXLow := XxLUpdatedYLow := XZLUpdatedSLPLow := SLPXZLUpdatedSLPLowLong = not (UpdatedSLP * time > 0)and CheckCross(close, UpdatedX, UpdatedY, UpdatedSLP)== 1and not TradeisONShort = not (UpdatedSLPLow * time < 0)and CheckCross(close, UpdatedXLow, UpdatedYLow, UpdatedSLPLow)==-1and not TradeisONTradeFire = Long or Shortif Long and not TradeisONLongTrade:= trueShortTrade:= falseif Short and not TradeisONLongTrade:= falseShortTrade:= trueif trueif TradeFire and not TradeisONTP := switchLong => high + (Zband *20)Short => low – (Zband *20)SL := switchLong => low – (Zband *20)Short => high + (Zband *20)if Longstrategy.entry(“Long”, strategy.long)strategy.exit(“Long Exit”, “Long”, limit = TP)if Shortstrategy.entry(“Short”, strategy.short)strategy.exit(“Short Exit”, “Short”, limit = TP)TradeisON:= trueif ShowTargetsline.new(bar_index,Long ? high : low,bar_index,TP,width=2,color = color.rgb(154, 103, 20),style= line.style_dashed)tpLine:= line.new(bar_index,TP,bar_index+2,TP,style= line.style_dashed,color = color.rgb(154, 103, 20))slLine:= line.new(bar_index,SL,bar_index+2,SL,style= line.style_dashed,color = color.new(color.red, 50))LAB:=label.new(bar_index,TP,“Target”,color = color.rgb(154, 103, 20),style= label.style_label_left,size=size.small,textcolor = color.white)if TradeisONline.set_x2(tpLine,bar_index)line.set_x2(slLine,bar_index)label.set_x(LAB,bar_index+1)if LongTrade and TradeisONif high >= TPlabel.set_color(LAB,color.rgb(6, 128, 10, 37))TradeisON:=falseif close <= SLlabel.set_color(LAB,color.new(color.rgb(246, 7, 7),70))TradeisON:=falsestrategy.close(“Long”, comment = “Long Stop-Loss”)else if ShortTrade and TradeisONif low <= TPlabel.set_color(LAB,color.rgb(6, 128, 10, 37))TradeisON:=falseif close >= SLlabel.set_color(LAB,color.new(color.rgb(246, 7, 7),70))TradeisON:=falsestrategy.close(“Short”, comment = “Short Stop-Loss”)plotshape(Long and not TradeisON[1],size = size.small,color = color.rgb(46, 192, 6, 11),location = location.belowbar,style = shape.labelup ,text = “”,textcolor = color.white)plotshape(Short and not TradeisON[1],size = size.small,color = color.rgb(241, 2, 2, 11),location = location.abovebar,style = shape.labeldown ,text = “”,textcolor = color.white)// — END — .03/14/2024 at 5:08 PM #229776Hello. I can't translate this code because it is not copied correctly. I've tried to fix it but it's too much… If you want me to try to translate the code, send it to me in the correct format so I can read it and insert it into Tradingview to check it. Greetings
-
AuthorPosts
Find exclusive trading pro-tools on