首页 > 解决方案 > 回测 pinescript 策略时没有数据错误。在我错的地方有什么帮助吗?

问题描述

嗨,我还在学习,我结合了两种不同策略的数据,一种是 superz rafeal zioni(来自 tradingview),另一种是我在这里找到的,我试图明智地连接它,但没有出现数据错误。代码如下 1) 从 rafel zioni supertrend 获取买卖条件。2)增加了时间限制,因为我只想在上午 9:30 到下午 3:00 之间进行交易。3)添加止损和获利(作为输入),但在我的例子中是相对于初始金额1000计算的,它根据风险百分比计算订单头寸,然后将利润作为R的1.5倍的因子R. 4)最后一部分是,它将所有这些作为评论发送,因为我想将信号作为评论发送到 api 桥,因此符号、数量等 5)我真的不知道我到底哪里出错了,任何帮助都会受到赞赏。提前致谢。

下面是我的代码

//@version=4
  strategy("Super Z strategy - Thanks to Rafael Zioni", shorttitle="Super Z 75 ", 
  overlay=true,precision=2, initial_capital=1000,calc_on_every_tick=false, pyramiding=0, 
  default_qty_type=strategy.cash, default_qty_value=10, process_orders_on_close=true)
 src5 = input(close)
 /////
 tf = input(1440)
 len5 = timeframe.isintraday and timeframe.multiplier >= 1 ? 
    tf / timeframe.multiplier * 7 : 
    timeframe.isintraday and timeframe.multiplier < 60 ? 
    60 / timeframe.multiplier * 24 * 7 : 7

 ma = ema(src5*volume, len5) / ema(volume, len5)

 //script taken from https://www.tradingview.com/script/kChCRRZI-Hull-Moving-Average/
 src1 = ma
 p(src1, len5) =>
     n = 0.0
     s = 0.0
     for i = 0 to len5 - 1
         w = (len5 - i) * len5
         n := n + w
         s := s + src5[i] * w
     s / n

 hm = 2.0 * p(src1, floor(len5 / 2)) - p(src1, len5)
 vhma = p(hm, floor(sqrt(len5)))
 lineColor = vhma > vhma[1] ? color.lime : color.red
 plot(vhma, title="VHMA", color=lineColor ,linewidth=3)
 hColor = true,vis = true
 hu = hColor ? (vhma > vhma[2] ? #00ff00 : #ff0000) : #ff9800

 vl = vhma[0]
 ll = vhma[1]
 m1 = plot(vl, color=hu, linewidth=1, transp=60)
 m2 = plot(vis ? ll : na,  color=hu, linewidth=2, transp=80)
 fill(m1, m2,  color=hu, transp=70)
 //
 b = timeframe.isintraday and timeframe.multiplier >= 1 ? 
    60 / timeframe.multiplier * 7 : 
    timeframe.isintraday and timeframe.multiplier < 60 ? 
    60 / timeframe.multiplier * 24 * 7 : 7
 //
 res5 = input("75", type=input.resolution)
 o = security(syminfo.tickerid, res5, open, barmerge.gaps_off, barmerge.lookahead_on)
 c = security(syminfo.tickerid, res5, close, barmerge.gaps_off, barmerge.lookahead_on)
 hz = security(syminfo.tickerid, res5, high, barmerge.gaps_off, barmerge.lookahead_on)
 l = security(syminfo.tickerid, res5, low, barmerge.gaps_off, barmerge.lookahead_on)
 col = c >= o ? color.lime : color.red
 ppo = plot(b ? o >= c ? hz : l : o, color=col, title="Open", style=plot.style_stepline, transp=100)
 ppc = plot(b ? o <= c ? hz : l : c, color=col, title="Close", style=plot.style_stepline, transp=100)
 plot(b and hz > c ? hz : na, color=col, title="High", style=plot.style_circles, 
 linewidth=2,transp=60)
 plot(b and l < c ? l : na, color=col, title="Low", style=plot.style_circles,linewidth=2, transp=60)
 fill(ppo, ppc, col)

 // INPUTS //
 st_mult   = input(1,   title = 'SuperTrend Multiplier', minval = 0, maxval = 100, step = 0.01)
 st_period = input(50, title = 'SuperTrend Period',     minval = 1)

 // CALCULATIONS //
 up_lev =l - (st_mult * atr(st_period))
 dn_lev = hz + (st_mult * atr(st_period))

 up_trend   = 0.0
 up_trend   := c[1] > up_trend[1]   ? max(up_lev, up_trend[1])   : up_lev

 down_trend = 0.0
 down_trend := c[1] < down_trend[1] ? min(dn_lev, down_trend[1]) : dn_lev

 // Calculate trend var
 trend = 0
 trend := c > down_trend[1] ? 1: c < up_trend[1] ? -1 : nz(trend[1], 1)

 // Calculate SuperTrend Line
 st_line = trend ==1 ? up_trend : down_trend

 //INTRADAY timeframe
 s=input(title="INTRA DAY TRADE SESSION",type=input.session,defval="0915-1430")
 st=time(timeframe.period,s)
 e=input(title="END SESSION",type=input.session,defval="1445-1500")
 et=time(timeframe.period,e)
 symbol=syminfo.ticker

 // Plotting
 //plot(st_line[1], color = trend == 1 ? color.green : color.red , style = plot.style_cross, 
 linewidth = 2, title = "SuperTrend")
 buy_Condition = crossover( c, st_line)
 sell_Condition = crossunder(c, st_line)
 signal = input(false)
 ////////////////////////////////////////
 var initial_Capital = strategy.initial_capital
 /////////////// Plotting /////////////// 
 plotshape(signal and buy_Condition, style=shape.triangleup, size=size.normal, 
 location=location.belowbar, color=color.lime)
 plotshape(signal and sell_Condition, style=shape.triangledown, size=size.normal, 
 location=location.abovebar, color=color.red)
 ///////////////////////////////////////////////////////////////
 Percent_Stop = input(title="% of Risk to Starting Equity Use to Size Positions", defval=1.0, 
 type=input.float) / 100
 TP_Factor = input(title="Factor of stop determining target profit", defval=1.5, type=input.float)
 //////////////////////////////////////////////////////////////
 // STOP LOSS
 float Long_SL_Price = na
 Long_SL_Price := buy_Condition ? lowest(low, 11)[1] : Long_SL_Price[1]
 float short_SL_Price= na
 short_SL_Price:=sell_Condition ? highest(high,11)[1] : short_SL_Price[1]

 // TAKE PROFIT
 //for long entry
 Long_Entry_Price = close//set to close
 R_for_Long = abs(Long_Entry_Price - Long_SL_Price) // close/entryprice-long_SL_Price
 float TP_for_Long = na
 TP_for_Long := buy_Condition ? Long_Entry_Price + (TP_Factor * R_for_Long) : TP_for_Long[1]

 //for short entry
 Short_Entry_Price= close//set to close
 R_for_short= abs(short_SL_Price -Short_Entry_Price)// short sl price is higher thats why
 float TP_for_short=na//set to na
 TP_for_short:=sell_Condition ? Short_Entry_Price  - (TP_Factor * R_for_short) : TP_for_short[1]
 //
 Value_of_Long_Position = (initial_Capital * Percent_Stop )/ (R_for_Long / Long_Entry_Price)
 Quantity_for_Long_trade = Value_of_Long_Position / Long_Entry_Price//positionSizelong 

 Value_of_Short_Position=( initial_Capital * Percent_Stop) / (R_for_short / Short_Entry_Price)
 Quantity_for_short_trade =  Value_of_Short_Position / Short_Entry_Price//positionSizeshort 

 //////////////////////////////////////////////////////////////
 le="TYPE:LE " + " :SYMBOL: " + syminfo.ticker + " :QTY: " + tostring(Quantity_for_Long_trade )
 se="TYPE:SE " + " :SYMBOL: " + syminfo.ticker + " :QTY: " + tostring(Quantity_for_short_trade )
 lx="TYPE:LX " + " :SYMBOL: " + syminfo.ticker + " :QTY: " + tostring(Quantity_for_Long_trade )
 sx="TYPE:SX " + " :SYMBOL: " + syminfo.ticker + " :QTY: " + tostring(Quantity_for_short_trade )
 lxse="TYPE: LX" + " :SYMBOL: " + syminfo.ticker + " :QTY: " + tostring(Quantity_for_Long_trade ) + " 
 :TYPE:SE " + " :SYMBOL: " + syminfo.ticker + " :QTY: " + tostring(Quantity_for_short_trade )
 sxle="TYPE: SX" + " :SYMBOL: " + syminfo.ticker + " :QTY: " + tostring(Quantity_for_short_trade ) + 
 " :TYPE:LE " + " :SYMBOL: " + syminfo.ticker + " :QTY: " + tostring(Quantity_for_Long_trade )
 //
 if (buy_Condition and  strategy.position_size==0) 
     strategy.entry("LE", strategy.long, comment=le,when=st)

 if (sell_Condition and  strategy.position_size==0) 
     strategy.entry("SE", strategy.short,comment=se, when=st)

 if(buy_Condition and strategy.position_size!=0)
     strategy.entry("LE2",strategy.long,comment=sxle, when=st)

 if(sell_Condition and strategy.position_size!=0) 
     strategy.entry("SE2",strategy.short,comment=lxse,when=st)

 // Submit exit orders
 strategy.exit(id="exitlong", from_entry="LE",stop=Long_SL_Price, limit=TP_for_Long,comment=lx)
 strategy.exit(id="exitshort", from_entry="SE",stop=short_SL_Price, limit=TP_for_short,comment=sx)
 strategy.exit(id="exitlong1", from_entry="LE2",stop=Long_SL_Price, limit=TP_for_Long,comment=lx)
 strategy.exit(id="exitshort2", from_entry="SE2",stop=short_SL_Price, limit=TP_for_short,comment=sx)

 //ADDING SQUAREOFF TIME
 strategy.close("LE",when=et,comment=lx)
 strategy.close("LE2",when=et,comment=lx)
 strategy.close("SE",when=et,comment=sx)
 strategy.close("SE2",when=et,comment=sx)

 //strategy.close_all(when=et)

标签: pine-script

解决方案


推荐阅读