简体   繁体   中英

Stop Loss not Triggering on the next candle

When a trade is entered on the close of the day's candle and the stop loss is triggered the next day on the backtest, the trade doesn't exit via stop loss until the close of that day, which far exceeded the stop loss exit. The stop loss exit was possible, and the list of trades recognizes the stop loss condition, but it doesn't make the exit until the close of the day. Leaving you with a 13-14% loss, when it should/could have exited at 4.3% loss. I've seen some questions on this with no working answer that I can find.

    //@version=5

strategy(title="Heikin Ashi Indicator", shorttitle="HA Indicator", overlay=false,calc_on_every_tick=true,calc_on_order_fills=true,process_orders_on_close=true,initial_capital=10000,default_qty_type=strategy.percent_of_equity,default_qty_value=100)
//Rev 10

// Calculation HA Values
haTicker = ticker.heikinashi(syminfo.tickerid)
haOpen = request.security(haTicker, timeframe.period, open)
haHigh = request.security(haTicker, timeframe.period, high)
haLow = request.security(haTicker, timeframe.period, low)
haClose = request.security(haTicker, timeframe.period, close)

plotcandle (haOpen, haHigh, haLow, haClose,title='HA', color=haOpen < haClose ? color.green : color.red, wickcolor=haOpen < haClose ? color.green : color.red, bordercolor=haOpen < haClose ? color.green : color.red)

// Calculation MA Values
len = input.int(14, minval=1, title="MA Length")
src = input(close, title="MA Source")
offset = input.int(title="MA Offset", defval=0, minval=-500, maxval=500)
out = ta.sma(src, len)
plot(out, color=color.blue, title="MA", offset=offset)

ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

typeMA = input.string(title = "Method", defval = "SMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Smoothing")
smoothingLength = input.int(title = "Length", defval = 5, minval = 1, maxval = 100, group="Smoothing")

smoothingLine = ma(out, smoothingLength, typeMA)
plot(smoothingLine, title="Smoothing Line", color=#f37f20, offset=offset, display=display.none)

///
i_dateFilter    = input.bool(false,  "Date Range Filtering On/Off")
i_fromYear      = input.int(1900,   "From Year",    minval = 1900)
i_fromMonth     = input.int(1,      "From Month",   minval = 1, maxval = 12)
i_fromDay       = input.int(1,      "From Day",     minval = 1, maxval = 31)
i_toYear        = input.int(2999,   "To Year",      minval = 1900)
i_toMonth       = input.int(1,      "To Month",     minval = 1, maxval = 12)
i_toDay         = input.int(1,      "To Day",       minval = 1, maxval = 31)

fromDate        = timestamp(i_fromYear, i_fromMonth, i_fromDay, 00, 00)
toDate          = timestamp(i_toYear, i_toMonth, i_toDay, 23, 59)
f_tradeDateIsAllowed() => not i_dateFilter or (time >= fromDate and time <= toDate)

///
ecin1=input.int(3,title="Exit candle 1")
ecin2=input.int(14,title="Exit candle 2")

long=f_tradeDateIsAllowed() and haOpen < haClose and haOpen[1] > haClose[1] and close>open

ex1=haOpen > haClose and haOpen[1] < haClose[1]

upcan=haOpen < haClose?1:0

ex2a = ta.cum( upcan ) - ta.valuewhen( haOpen < haClose and haOpen[1] > haClose[1], ta.cum( upcan ),0 )+1
ex2b=ta.barssince(haOpen < haClose and haOpen[1] > haClose[1])+1

ex2=ex2a==ecin1 and ex2b==ecin1 and haClose<out
ex3=ex2a==ecin2 and ex2b==ecin2 and haClose>out

longexit= ex1 or ex2 or ex3


//bgcolor(ex1?color.red:na)
//bgcolor(ex2?color.aqua:na)
//bgcolor(long?color.yellow:na)

sl_inp = input.float(4.3, title='Stop Loss %',step=0.1)/100
tp_inp = input.float(27.6, title='Take Profit %',step=0.1)/100


stop_level = (not na(strategy.position_avg_price) ? strategy.position_avg_price : open ) * (1 - sl_inp) 
take_level = (not na(strategy.position_avg_price) ? strategy.position_avg_price : open ) * (1 + tp_inp)

if long
    strategy.entry(id="Entry", direction=strategy.long,comment="BUY")


strategy.exit(id="B",from_entry="Entry" ,stop=stop_level, limit=take_level,comment_profit="TP",comment_loss="SL" )

if longexit
    strategy.close(id="Entry",comment="SELL")

我在日线图上测试了你的脚本,我看到 SL 订单是实时的,而不是蜡烛收盘时https://www.tradingview.com/x/PPThf8Cm/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM