通过指标条件确定的价格交易

发布于 2025-01-23 12:44:07 字数 1762 浏览 1 评论 0原文

我正在尝试为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")`

I am trying to enter a stop loss for a Pine Script V5 strategy. The stop loss is -0.10% of the red line of the indicator. To find this level I have created a (white) line. I must then necessarily close the trade at the exact price of the white line. At the moment I have tried thousands of solutions all of which close only after the breakout candle (this is great for repainting but not sufficient for the solution I am looking for now). Can anyone tell me how to get a precise exit on the white line ?

`//@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")`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红焚 2025-01-30 12:44:07

使用 sl 变量作为stop =参数,而不是 close ,它返回na没有na code> 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")

Use the SL variable as a stop= argument instead of the Close, which returns na when there is no ta.crosunder() condition met:

//@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")

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文