根据指标条件确定的价格关闭交易

Close trade on price determined by indicator condition

我正在尝试为 Pine Script V5 策略输入止损。止损为指标红线的-0.10%。为了找到这个水平,我创建了一条(白色)线。然后我必须以白线的确切价格关闭交易。目前我已经尝试了数以千计的解决方案,所有这些解决方案都只在突破蜡烛后关闭(这对于重新绘制非常有用,但对于我现在正在寻找的解决方案来说还不够)。谁能告诉我如何在白线上精确退出?

`//@version=5
strategy("", overlay = true, currency = currency.EUR,
     initial_capital = 1000, commission_type = strategy.commission.percent, commission_value = 0.03, 
     default_qty_value = 100, default_qty_type = strategy.percent_of_equity)


len     = input(title = 'PERIOD', defval = 10, group = "SETTING INDICATOR")
smaHigh = ta.sma(high, len)
smaLow  = ta.sma(low,  len)
Hlv     = int(na)
Hlv := close > smaHigh ? 1 : close < smaLow ? -1 : Hlv[1]
sslDown = Hlv < 0 ? smaHigh : smaLow
sslUp   = Hlv < 0 ? smaLow : smaHigh

plot(sslDown, linewidth = 2, color = color.new(color.red, 0))
plot(sslUp,   linewidth = 2, color = color.new(color.lime, 0))

LongEntry = ta.crossover (sslUp,  sslDown) and barstate.isconfirmed
LongExit  = ta.crossunder(sslUp,  sslDown) and barstate.isconfirmed

isLong    = LongEntry //and inDateRange

if (isLong and barstate.isconfirmed)
    strategy.entry(id = "BUY",  direction  = strategy.long, qty = 100)

var int bar     = 0
var int count   = 0

if LongEntry
    bar := bar_index
    bar
count := bar_index - bar + 1

isExit = if LongExit 
    close 

StopLoss = input.float(defval = 0.10, title = "STOP LOSS VALUE", minval = 0.00)
distance = (sslDown * StopLoss) / 100
Value    = (sslDown - distance)

SL      = ta.valuewhen(LongEntry, Value, 0)

plot(SL, color = color.white)

Close = if ta.crossunder(close, SL)
    close

strategy.exit("CLOSE LONG", from_entry = "BUY", stop = Close)
    //strategy.close_all(when = SL)
    //strategy.close_all(when = isExitStopLoss, comment = "CLOSE TRADE STOP LOSS")`

使用 SL 变量作为 stop= 参数而不是 Close,returns na当没有ta.crosunder()条件满足时:

//@version=5
strategy("", overlay = true, currency = currency.EUR,
     initial_capital = 1000, commission_type = strategy.commission.percent, commission_value = 0.03, 
     default_qty_value = 100, default_qty_type = strategy.percent_of_equity)


len     = input(title = 'PERIOD', defval = 10, group = "SETTING INDICATOR")
smaHigh = ta.sma(high, len)
smaLow  = ta.sma(low,  len)
Hlv     = int(na)
Hlv := close > smaHigh ? 1 : close < smaLow ? -1 : Hlv[1]
sslDown = Hlv < 0 ? smaHigh : smaLow
sslUp   = Hlv < 0 ? smaLow : smaHigh

plot(sslDown, linewidth = 2, color = color.new(color.red, 0))
plot(sslUp,   linewidth = 2, color = color.new(color.lime, 0))

LongEntry = ta.crossover (sslUp,  sslDown) and barstate.isconfirmed
LongExit  = ta.crossunder(sslUp,  sslDown) and barstate.isconfirmed

isLong    = LongEntry //and inDateRange

if (isLong and barstate.isconfirmed)
    strategy.entry(id = "BUY",  direction  = strategy.long, qty = 100)

var int bar     = 0
var int count   = 0

if LongEntry
    bar := bar_index
    bar
count := bar_index - bar + 1

isExit = if LongExit 
    close 

StopLoss = input.float(defval = 0.10, title = "STOP LOSS VALUE", minval = 0.00)
distance = (sslDown * StopLoss) / 100
Value    = (sslDown - distance)

SL      = ta.valuewhen(LongEntry, Value, 0)

plot(SL, color = color.white)

// Close = if ta.crossunder(close, SL)
//     close
//plot(Close) // <- returns 'na' if there is no crossunder

strategy.exit("CLOSE LONG", from_entry = "BUY", stop = SL)
    //strategy.close_all(when = SL)
    //strategy.close_all(when = isExitStopLoss, comment = "CLOSE TRADE STOP LOSS")