由于 calc_on_order_fills,策略退出限制未正确触发

时间:2021-07-11 18:43:42

标签: pine-script

保留 calc_on_order_fills 为真的目的是我希望它在当前蜡烛图期间达到目标或止损时退出交易。

但在实践中,当发生这种情况时,无论何时要退出利润,它都会在进入交易的烛台的最大值处退出,而不是退出目标,并且当达到止损时,即使是在同一根蜡烛中,止损被正确触发,就好像止损设置正确,并且限制设置 在入场蜡烛的最大值

但这很奇怪,因为如果您将 calc_on_order_fills 设置为 false,它总是会正确计算值

//@version=4
strategy(title="Stochastic Slow Bar Strategy",initial_capital=100000, shorttitle="Stoch - 1 Buy", format=format.price, precision=2,overlay=true,calc_on_order_fills=true)
// Parameters
src                   = input("K"         , group="Indicator"           , title="Based on :", options=["K","D"]              )
length0               = input(11          , group="Indicator"           , title="%K length"                                  )
length1               = input(3           , group="Indicator"           , title="%K Smoothing"                               )
length2               = input(8           , group="Indicator"           , title="%D Smoothing"                               )
i_indicatorBuy        = input(35          , group="Indicator"           , title="Buy under: "                                )
b_media               = input(true        , group="EMA Filter"          , title="EMA Filter?"                                )
i_media               = input(72          , group="EMA Filter"          , title="Length?"                                    )
b_minOfLastCandles    = input(true        , group="Stop Rules"          , title="based on the minimum of the last X candles?")
i_minOfLastCandles    = input(2           , group="Stop Rules"          , title="How many?"                                  )
target                = input(2.0         , group="Strategy Rules"      , title="Target"                                     )
i_risk                = input(1           , group="Strategy Rules"      , title="Risk in % based on Total Capital"           )
b_betItAll            = input(true        , group="Strategy Rules"      , title="Reinvest all profit?"                       )
signalColor           = input(color.yellow, group="Color of the Candles", title="Signal Candle Color"                        )
entryColor            = input(color.lime  , group="Color of the Candles", title="Entry Candle Color"                         )

// Estrategy calculation formulas
minOfLastCandles = lowest(low,i_minOfLastCandles)
deltaCandle      = b_minOfLastCandles ? (high - minOfLastCandles) : (high - low)
longTarget       = high + deltaCandle * target
long_stopBuy     = high

// Stop formulas
long_stopLoss     = if b_minOfLastCandles
    long_stopLoss = minOfLastCandles[1]
if not b_minOfLastCandles    
    long_stopLoss := low    

// market position formulas
opened   = strategy.position_size[0] != 0 and strategy.position_size[1] == 0
inMarket = strategy.opentrades > strategy.opentrades[1]

// Target and Stop Verifier
checkLongBuy      = inMarket ?  long_stopBuy[1] : na
checkLongTarget   = inMarket ?  longTarget[1]   : na
checkLongstopLoss = inMarket ?  long_stopLoss   : na


// Risk Management formulas
long_stopLossRisk     = if b_minOfLastCandles
    long_stopLossRisk = minOfLastCandles
if not b_minOfLastCandles    
    long_stopLossRisk := low    
    
operationRisk = (long_stopLossRisk/long_stopBuy-1)*-1
risk = i_risk / 100
riskManagement = b_betItAll ? risk / operationRisk * strategy.equity : risk / operationRisk * strategy.initial_capital

maxQTY = riskManagement / long_stopBuy

//Indicador
periodK        = length0
smoothK        = length1
periodD        = length2
k              = sma(stoch(close, high, low, periodK), smoothK)
d              = sma(k, periodD)

// Indicator Check Formula
indicator      = if src == "K"
    indicator  = k
if src == "D"
    indicator := d
    
// Trend variables
bullishTrend = close > ema(close,i_media)
indicatorBuy = indicator < i_indicatorBuy

// Strategy
if b_media 
    if bullishTrend and indicatorBuy
        strategy.entry("Entry", strategy.long,qty=maxQTY, comment="Entry on Bullish Stoch", stop=long_stopBuy)
    if inMarket 
        strategy.exit("Exit","Entry",comment="Stoch Exit",limit=checkLongTarget, stop=long_stopLoss)
    if bullishTrend[2] and indicatorBuy[2] and not opened or bullishTrend[1] and not bullishTrend[0] or indicatorBuy[1] and not indicatorBuy[0]
        strategy.cancel("Entry",when=close)
    if strategy.closedtrades > strategy.closedtrades[1]
        strategy.cancel_all()
        
if not b_media
    if indicatorBuy
        strategy.entry("Entry", strategy.long,qty=maxQTY, comment="Entry", stop=high[1])
    if inMarket 
        strategy.exit("Exit","Entry",comment="Stoch Exit",limit=checkLongTarget, stop=long_stopLoss)
    if indicatorBuy[2] and not opened or indicatorBuy[1] and not indicatorBuy[0]
        strategy.cancel("Entry",when=close)
    if strategy.closedtrades > strategy.closedtrades[1]
        strategy.cancel_all()

//Plot the target and stop lines on the chart
valuewhen_Long        = valuewhen(bullishTrend and indicatorBuy, longTarget, 0)
valuewhen_Stop        = valuewhen(bullishTrend and indicatorBuy, long_stopLoss, 0)
valuewhen_Buy         = valuewhen(bullishTrend and indicatorBuy, long_stopBuy, 0)
valuewhen_checkTarget = valuewhen(checkLongTarget, longTarget[1], 0)
valuewhen_checkStop   = valuewhen(checkLongstopLoss, long_stopLoss, 0)

plot(strategy.opentrades != 0 ? valuewhen_checkTarget : na,style=plot.style_circles,color=color.lime,linewidth=2)
plot(strategy.opentrades != 0 ? valuewhen_checkStop   : na,style=plot.style_circles,color=color.red,linewidth=2)

plot(checkLongTarget,                    style=plot.style_circles, color=color.white ,linewidth=4)
plot(checkLongstopLoss,                  style=plot.style_circles, color=color.red   ,linewidth=4)
plot(b_media ? ema(close,i_media) : na , style=plot.style_line   , color=color.orange,linewidth=4)

//Plot signal Candles and Entry Candles on the chart
o = open
Open =  bullishTrend and indicatorBuy ? o : na
h = high
High =  bullishTrend and indicatorBuy ? h : na
l = low
Low =   bullishTrend and indicatorBuy ? l : na
c = close
Close = bullishTrend and indicatorBuy ? c : na

plotcandle(Open,High,Low,Close,"Signal Candle Color "  ,signalColor,signalColor,bordercolor=signalColor)
plotcandle(Open[1]  and inMarket ? open : na,High[1]  and inMarket ? high : na,Low[1]   and inMarket ? low : na,Close[1] and inMarket ? close : na,"Entry Candle Color",entryColor,entryColor,bordercolor=entryColor)

示例:

  1. https://www.tradingview.com/x/RUdODw4O/
  2. https://www.tradingview.com/x/k8FEAGD6
  3. https://www.tradingview.com/x/H7zRTxR0/

1 个答案:

答案 0 :(得分:0)

<块引用>

保留 calc_on_order_fills true 的目的是我希望它 如果当前目标或止损被击中,则退出交易 蜡烛。

calc_on_order_fills 的目的是在订单成交后立即强制计算策略,而无需等待柱线关闭。 strategy.exit 也会立即与 calc_on_order_fills=false(默认情况下)关闭交易。