退出交易信号过多(指标)

Excessive signals on exit trade (Indicator)

我已经编译了这个脚本(指标),但它一直显示错误。交易退出应对应于“SL”或“LongExit”水平。 "LongExit" 起作用,而 "SL" 不会在每笔交易中发出一个信号,而是在每次价格突破 "SL" 时发出信号。我尝试了几种替代方法,但 none 成功地消除了这个错误。

    //@version=5
indicator("", overlay = true)

// ------------------------------ ALGO SETTING SSL INDICATOR ---------------------------{

len     = input(title = 'Period', defval = 10)
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))

// }

// -------------- LONG INDICATOR INPUT FOR 3COMMAS -----------------{

LongEntry = ta.crossover(sslUp,  sslDown) and barstate.isconfirmed
plotshape(LongEntry, color = color.yellow)
alertcondition(LongEntry, "BUY LONG", "message")

var int bar     = 0
var int count   = 0

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

// ------------------ ALGO SETTING LONG EXIT ------------------- {

LongExit = ta.crossunder(sslUp, sslDown) and barstate.isconfirmed
alertcondition(LongExit, "LONG CLOSE REVERSE", "message")

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)

LongStopLoss    = ta.crossunder(close, SL)
Condition       = (LongExit or LongStopLoss)

alertcondition(LongStopLoss, "CLOSE TRADE", "message")
plotshape(Condition, color = color.blue)

// }

它每次都会发出 SL 信号,因为您没有告诉脚本何时以及为什么不应触发它。为什么不使用策略脚本?否则,您将不得不手动遵循订单并使用您的自定义逻辑来管理它们,而 built-in 回溯测试引擎可以代替它。

作为解决方法,创建一个 SL 中断事件计数器并将其重置为进入条件,我还添加了一个金字塔输入来过滤序列中 SL 信号的数量。图表上的红色交叉 - SL 事件,白色交叉 - 使用计数器和金字塔过滤的 SL 事件:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © e2e4mfck

//@version=5
indicator("indicator() sl")

LongEntry = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
bgcolor(LongEntry ? color.new(color.red, 80) : na, title = 'EntrySignal / ResetSLCounter')
LongStopLoss = close < ta.sma(close, 14)
plotshape(LongStopLoss, color = color.red, title = 'LongStopLoss')

var int LongStopLossCounter = 0
if LongStopLoss
    LongStopLossCounter += 1
else if LongEntry
    LongStopLossCounter := 0

pyramid = input.int(1, 'Pyramiding', minval = 1)

plotshape((LongStopLossCounter <= pyramid) and LongStopLoss, color = color.white, size = size.huge, title = 'LongStopLoss filtered')