检索最后的买入或卖出信号

问题描述 投票:0回答:1

我已经使用 pinescript 好几天了,正在寻求社区的支持。

我的策略有条件自动发出

buy
sell
警报。 我正在综合利用 3 个差异指标。我被阻止的是 UT BOT Indicator

  • 我需要找到一种方法来检索指示器最后闪烁的信号何时以及是否为
    buy
    sell
  • 仅供参考,这些是我策略中的另外两个指标:线性回归蜡烛stc

目前警报的工作方式应为:

如果出现以下情况,则发出购买警报:

  • UT Bot 指标发出买入信号
  • STC指示灯为绿色
  • 线性回归蜡烛收盘于信号白线上方

如果出现以下情况,则发出卖出警报:

  • UT 机器人指标发送卖出信号
  • STC指示灯为红色
  • 线性回归蜡烛收盘于信号白线下方

边缘情况:

  • 当 UT Bot 指标闪烁“买入”或“卖出”时,不会触发警报,但不满足 STC 和线性回归蜡烛条件。

问:

  • 一旦满足其他条件(STC 和线性回归):在历史中查找 UT Bot 指标上次闪烁的位置和信号(买入或卖出)的蜡烛...然后相应地发出警报()。

请向下滚动到代码末尾以查看我迄今为止的尝试:

//@version=5

strategy(title='Combined indicators', shorttitle='COMBIND', overlay=true)
// UT BOT indicator START
// Inputs
a = input(1, title='Key Vaule. \'This changes the sensitivity\'')
c = input(10, title='ATR Period')
h = input(false, title='Signals from Heikin Ashi Candles')

xATR = ta.atr(c)
nLoss = a * xATR

src = h ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close, lookahead=barmerge.lookahead_off) : close

xATRTrailingStop = 0.0
iff_1 = src > nz(xATRTrailingStop[1], 0) ? src - nLoss : src + nLoss
iff_2 = src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0) ? math.min(nz(xATRTrailingStop[1]), src + nLoss) : iff_1
xATRTrailingStop := src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0) ? math.max(nz(xATRTrailingStop[1]), src - nLoss) : iff_2

pos = 0
iff_3 = src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0) ? -1 : nz(pos[1], 0)
pos := src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0) ? 1 : iff_3

xcolor = pos == -1 ? color.red : pos == 1 ? color.green : color.blue

ema = ta.ema(src, 1)
above = ta.crossover(ema, xATRTrailingStop)
below = ta.crossover(xATRTrailingStop, ema)

buy = src > xATRTrailingStop and above
sell = src < xATRTrailingStop and below

barbuy = src > xATRTrailingStop
barsell = src < xATRTrailingStop

plotshape(buy, title='Buy', text='Buy', style=shape.labelup, location=location.belowbar, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), size=size.tiny)
plotshape(sell, title='Sell', text='Sell', style=shape.labeldown, location=location.abovebar, color=color.new(color.red, 0), textcolor=color.new(color.white, 0), size=size.tiny)

barcolor(barbuy ? color.green : na)
barcolor(barsell ? color.red : na)

alertcondition(buy, 'UT Long', 'UT Long')
alertcondition(sell, 'UT Short', 'UT Short')

// ******** My Logic *********
// First Approach: Fails to trigger Alert if conditions are met after the chart continues plotting, because the `buy/sell` signal was flashed x candles ago

// send Bot alert
if sell and close < signal and stcRed // only focus on the sell variable here
    alert("UT Sell+ LIN REG bellow + STC is RED", alert.freq_once_per_bar_close)

if buy and close > signal and stcGreen // only focus on the buy variable here
    alert("UT Buy + LIN REG above + STC is GREEN", alert.freq_once_per_bar_close)


// Second Approach: Change State of buy and sell variables via a boolean check. It fails because once set to true, these will trigger alerts on every single bar close.

var bool buy_signal = false
var bool sell_signal = false 

if buy 
    buy_signal := true 
    sell_signal:= false
    

if sell 
    sell_signal := true
    buy_signal := false


log.info("Buy is: " + str.tostring(buy_signal))
log.info("Sell is: " + str.tostring(sell_signal))



// send Bot alert
if sell_signal and close < signal and stcRed
    alert("UT Sell+ LIN REG bellow + STC is RED", alert.freq_once_per_bar_close)

if buy_signal and close > signal and stcBuy
    alert("UT Buy + LIN REG above + STC is GREEN", alert.freq_once_per_bar_close)
    

如果没有正确的解决方案,该策略就会存在缺陷,并且很容易遭受损失。请参阅下图的真实案例场景 enter image description here enter image description here

pine-script pine-script-v5 tradingview-api
1个回答
0
投票

只需使用

var
变量来跟踪即可。

var ut_was_long = false

if (buy)  // New buy signal from UT Bot
    ut_was_long := true
else if (sell)
    ut_was_long := false

它将在每次迭代之间保留其值,以便您可以使用该标志来找出最后一个信号是什么。

© www.soinside.com 2019 - 2024. All rights reserved.