'Delay indicator alert based on conditions
My first question here is from a very beginner pine programmer. I have created an indicator and want to alert either a few bars or a few minutes later than a condition is met.
Let's take a simple RSI, I want to create an alert (as an example) either 5 bars or 10 minutes later after the RSI reaches a value lower than 20. As a next step, I'd like to combine the 2nd condition: if RSI is < 20 within the last 5 bars or 10 minutes and then MFI is below 20 now, create the alert.
Below is a sample code (for the first example) but it's missing the delay element in it:
//@version=5
indicator(title="RSI-MFI-%B", shorttitle="RSI-MFI", format=format.price, precision=2, timeframe="", timeframe_gaps=true)
ob = input.int(title="OverBought", defval=70, minval=1, maxval=100)
os = input.int(title="Oversold", defval=30, minval=1, maxval=100)
// RSI
rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "RSI Source", group="RSI Settings")
rsiob = input.int(title="OverBought", defval=68, minval=1, maxval=100, group="RSI Settings")
rsios = input.int(title="Oversold", defval=24, minval=1, maxval=100, group="RSI Settings")
up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
plot(rsi, "RSI", color.blue, display=display.none)
rsiUpperBand = hline(70, "Upper Band", color=#787B86)
hline(50, "Middle Band", color=color.new(#787B86, 50))
rsiLowerBand = hline(30, "Lower Band", color=#787B86)
fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")
// MFI
mfilength = input.int(14, minval=1, title="MFI Length", group="MFI Settings")
mfisrc = input.source(close, "MFI Source", group="MFI Settings")
mfiob = input.int(title="OverBought", defval=75, minval=1, maxval=100, group="MFI Settings")
mfios = input.int(title="Oversold", defval=20, minval=1, maxval=100, group="MFI Settings")
mf = ta.mfi(mfisrc, mfilength)
plot(mf, "MFI", color.white, display=display.none)
// Signal
buy = rsi<os
sell = rsi>ob
plotshape(buy,"Buy", shape.cross, location.bottom, color.yellow, size = size.tiny)
plotshape(sell,"Sell", shape.cross, location.top, color.yellow, size = size.tiny)
alertcondition(buy, title="Low Price Detected", message="Low Price Detected")
alertcondition(sell, title="High Price Detected", message="High Price Detected")
Thanks again for any help
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
