战略和研究中价值图的差异

Differences in the values plots in strategy and study

我正在把这个策略变成一项研究。 Noro's RiskDonchian Strategy

我现在的剧本

//@version=4
//strategy(title = "Noro's RiskDonchian Indicator", shorttitle = "RiskDonchian indi", overlay = true, default_qty_type = strategy.percent_of_equity, initial_capital = 100, default_qty_value = 100, commission_value = 0.1)
study(title = "Noro's RiskDonchian Indicator", shorttitle = "RiskDonchian indi", overlay = true)

//Settings
needlong = input(true, title = "Long")
needshort = input(true, title = "Short")
tp = input(3.0, minval = 1, title = "Take-profit, %")
tptype = input(defval = "2. Fix", options = ["1. None", "2. Fix", "3. Trailing"], title = "Take-profit type")
sltype = input(defval = "2. Center", options = ["1. None", "2. Center"], title = "Take-profit type")
risklong  = input(1.0, minval = 0.0, maxval = 99.9, title = "Risk size for long, %")
riskshort = input(1.0, minval = 0.0, maxval = 99.9, title = "Risk size for short, %")
pclen = input(20, minval = 1, title = "Price Channel Length")
showll = input(true, title = "Show lines")
showbg = input(false, title = "Show Background")
showof = input(false, title = "Show Offset")
showlabel = input(true, title = "Show label")
fromyear = input(1900, minval = 1900, maxval = 2100, title = "From Year")
toyear = input(2100, minval = 1900, maxval = 2100, title = "To Year")
frommonth = input(01, minval = 01, maxval = 12, title = "From Month")
tomonth = input(12, minval = 01, maxval = 12, title = "To Month")
fromday = input(01, minval = 01, maxval = 31, title = "From day")
today = input(31, minval = 01, maxval = 31, title = "To day")

var  position = 0 // analogue strategy.position_size

//Price Channel
h = highest(high, pclen)
l = lowest(low, pclen)
center = (h + l) / 2

//Take-profit
tpl = 0.0
tpl := tptype == "2. Fix" and position > 0 ? tpl[1] : h * (100 + tp) / 100

//Stop-loss
tps = 0.0
tps := tptype == "2. Fix" and position < 0 ? tps[1] : l * (100 - tp) / 100

//Lines
tplcol = showll and needlong and tptype != "1. None" ? color.lime : na
pclcol = showll and needlong ? color.blue : na
sllcol = showll and needlong and sltype != "1. None" ? color.red : na
tpscol = showll and needshort and tptype != "1. None" ? color.lime : na
pcscol = showll and needshort ? color.blue : na
slscol = showll and needshort and sltype != "1. None" ? color.red : na
offset = showof ? 1 : 0
plot(tpl, offset = offset, color = tplcol, title = "TP Long")
plot(h, offset = offset, color = pclcol, title = "Channel High")
plot(center, offset = offset, color = sllcol, title = "SL Long")
plot(center, offset = offset, color = slscol, title = "SL Short")
plot(l, offset = offset, color = pcscol, title = "Channel Low")
plot(tps, offset = offset, color = tpscol, title = "TP Short")

//Background
size = position
bgcol = showbg == false ? na : size > 0 ? color.lime : size < 0 ? color.red : na
bgcolor(bgcol, transp = 70)

//Lot size
// risksizelong = -1 * risklong
// risklonga = ((center / h) - 1) * 100
// coeflong = abs(risksizelong / risklonga)
// lotlong = (strategy.equity / close) * coeflong
// risksizeshort = -1 * riskshort
// riskshorta = ((center / l) - 1) * 100
// coefshort = abs(risksizeshort / riskshorta)
// lotshort = (strategy.equity / close) * coefshort

//Trading
truetime = time > timestamp(fromyear, frommonth, fromday, 00, 00) and time < timestamp(toyear, tomonth, today, 23, 59)
mo = 0
mo := position != 0 ? 0 : high >= center[1] and low <= center[1] ? 1 : mo[1]

if h > 0 and position[1] == 0 and needlong and truetime and high >= h[1]
    position := 1

if position[1] == 1 and needlong and truetime and (high >= tpl[1] or low <= center[1])
    position := 0

if h > 0 and position[1] == 0 and needshort and truetime and low <= l[1]
    position := -1

if position[1] == -1 and needshort and truetime and (low <= tps[1] or high >= center[1])
    position := 0

plotshape(position[1] == 0 and position[0] == 1, style=shape.triangleup, color=color.green, location=location.belowbar, text="Long", size=size.tiny)
plotshape(position[1] == 1 and position[0] == 0, style=shape.xcross, color=color.green, text="Exit Long", size=size.tiny)

plotshape(position[1] == 0 and position[0] == -1, style=shape.triangledown, color=color.red, text="Short", size=size.tiny)
plotshape(position[1] == -1 and position[0] == 0, style=shape.xcross, color=color.red, location=location.belowbar, text="Exit Short", size=size.tiny)

转换后,通道的形成存在差异。当策略中存在未平仓头寸时,就会出现差异。屏幕截图显示,石灰色通道不匹配,蓝色通道不匹配,只有细微差别。 造成这种差异的原因可能是什么?

可能是因为:

  1. 策略仅在收盘时执行
  2. 仅在 strategy.entry/exit 执行后(满足条件)
  3. 的下一根柱线开仓(订单成交)