添加RSI Pine脚本中的警报条件。

huangapple go评论49阅读模式
英文:

adding alert condition in rsi pine script

问题

I'm building my strategy for my trading bot using pine script.
Everything seems fine on the RSI signals.
But my issues are on the alerts.
I want to add alert RSI crossover, please see the bellow image to understand it.

英文:

I'm building my strategy for my trading bot using pine script.

Everything seems fine on the RSI signals.
But my issues are on the alerts.
I want to add alert RSI crossover, please see the bellow image to understand it.
here is the image
https://i.gyazo.com/96c7c2624044dd306d9160f2f995e1a4.png

here is the code

//input
len = input.int(title='RSI len', defval=14, inline='1')
src = input.source(title='Source', defval=close, inline='1')
upli = input.int(title='Up line limit', defval=70, inline='2')
lowli = input.int(title='Down line limit', defval=30, inline='2')
theme = input.string(title='Mode', defval='Dark', options=['Dark', 'Light'])

//rsi
rsilevel = ta.rsi(src, len)

//calculations RSI lines
ob = 100
upper = ob - lowli
lower = ob - upli
center = ob - 50
zero = 0

//mode settings
color colorred = na
color colorgreen = na
color coloryellow = na
color colorwhite = na
color coloraqua = na

modeDark = if theme == 'Dark'
    colorred := color.red
    colorgreen := color.green
    coloryellow := color.yellow
    colorwhite := color.white
    coloraqua := color.aqua
    coloraqua
modeLight = if theme == 'Light'
    colorred := color.red
    colorgreen := color.green
    coloryellow := color.blue
    colorwhite := color.green
    coloraqua := color.yellow
    coloraqua

//display RSI lines (center, overbought, oversold)

upline = plot(upper, color=colorred, title='Up line', linewidth=2)
dnline = plot(lower, color=colorgreen, title='Down line', linewidth=2)
ctr = plot(center, color=colorwhite, title='Center line', transp=50)

//display rsi
rsiover = plot(rsilevel, color=color.rgb(148, 41, 148), title='RSI', transp=50)

//calculations RSI overbought and oversold lines for filling
lineupper = rsilevel > upper ? upper : rsilevel
linelower = rsilevel < lower ? lower : rsilevel
lu = plot(lineupper, color=colorred, title='Overbought', transp=100)
ll = plot(linelower, color=colorgreen, title='Oversold', transp=100)

//filling lines
fill(ll, rsiover, color=colorgreen, title='Fill background oversold zone', transp=20)
fill(lu, rsiover, color=colorred, title='Fill background overbought zone', transp=20)

fill(ctr, upline, color=colorred, title='Fill background from center line to Up line', transp=100)
fill(ctr, dnline, color=colorgreen, title='Fill background from center line to Down line', transp=100)


ma1(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "Bollinger Bands1" => ta.sma(source, length)
        "EMA1" => ta.ema(source, length)
        "SMMA (RMA)1" => ta.rma(source, length)
        "WMA1" => ta.wma(source, length)
        "VWMA1" => ta.vwma(source, length)

rsiLengthInput = input.int(100, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
maTypeInput1 = input.string("SMA1", title="MA Type", options=["SMA1", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
maLengthInput1 = input.int(14, title="MA Length", group="MA Settings")
bbMultInput1 = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="MA Settings")

up1 = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down1 = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi1 = down1 == 0 ? 100 : up1 == 0 ? 0 : 100 - (100 / (1 + up1 / down1))
rsiMA1 = ma1(rsi1, maLengthInput1, maTypeInput1)
isBB1 = maTypeInput1 == "Bollinger Bands"

plot(rsi1, "RSI 2", color.rgb(199, 226, 47))

rsiUpperBand = hline(70, "RSI Upper Band", color=#787B86)
hline(50, "RSI Middle Band", color=color.new(#787B86, 50))
rsiLowerBand1 = hline(30, "RSI Lower Band", color=#787B86)
fill(rsiUpperBand, rsiLowerBand1, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")
bbUpperBand1 = plot(isBB1 ? rsiMA1 + ta.stdev(rsi1, maLengthInput1) * bbMultInput1 : na, title = "Upper Bollinger Band", color=color.green)
bbLowerBand1 = plot(isBB1 ? rsiMA1 - ta.stdev(rsi1, maLengthInput1) * bbMultInput1 : na, title = "Lower Bollinger Band", color=color.green)
fill(bbUpperBand1, bbLowerBand1, color= isBB1 ? color.new(color.green, 90) : na, title="Bollinger Bands Background Fill")



if ta.crossover(upli,70)
    label.new(bar_index, 75, text = 'Sell', color= color.rgb(201, 60, 55), textcolor = color.rgb(252, 249, 249), style=label.style_label_down)
if ta.crossover(30, len)
    label.new(bar_index, 20, text = 'Buy', color= color.rgb(40, 180, 45), textcolor = #ffffff, style=label.style_label_up)

答案1

得分: 1

你可以在你的if语句中添加警报,以绘制你的标签。

if ta.crossover(upli, rsilevel)
    label.new(bar_index, 75, text = '卖', color= color.rgb(201, 60, 55), textcolor = color.rgb(252, 249, 249), style=label.style_label_down)
    alert('卖', alert.freq_once_per_bar)
if ta.crossover(30, rsilevel)
    label.new(bar_index, 20, text = '买', color= color.rgb(40, 180, 45), textcolor = #ffffff, style=label.style_label_up)
    alert('买', alert.freq_once_per_bar)
英文:

You can add alerts to your if statements that plot your labels

if ta.crossover(upli,rsilevel)
    label.new(bar_index, 75, text = 'Sell', color= color.rgb(201, 60, 55), textcolor = color.rgb(252, 249, 249), style=label.style_label_down)
    alert('Sell', alert.freq_once_per_bar)
if ta.crossover(30, rsilevel)
    label.new(bar_index, 20, text = 'Buy', color= color.rgb(40, 180, 45), textcolor = #ffffff, style=label.style_label_up)
    alert('Buy', alert.freq_once_per_bar)

huangapple
  • 本文由 发表于 2023年5月15日 03:52:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249414.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定