mjune
mjune

Reputation: 37

How to Trigger Alert only once per Buy Signal until a Sell Signal?

I want to trigger the alert condition only once per BuySignal until a SellSignal occurs. I used this flag solution by @vitruvius from here, and it worked if I had 1 BuySignal and 1 SellSignal.

But now I have 5 Buy Signals and 1 Sell Signal. I want the alert condition to trigger only once per Buy Signal until the Sell Signal occurs.

Lets assume I have Buy Signals like these:

buySignal_1 = ta.crossover(a, b)

buySignal_2 = ta.crossover(c, d)

buySignal_3 = ta.crossover(e, f)

buySignal_4 = ta.crossover(g, h)

buySignal_5 = ta.crossover(i, j)

and 1 Sell Signal:

sellSignal_1 = ta.crossunder(k, l)

each Buy Signal can trigger only once till a Sell Signal is occurred. So I can have a maximum of only 5 Signals occur till a Sell Signal occurs. And once a Sell Signal occurs, the flag is reset and the cycle is repeated.

I tried to convert the code but it became too confusing for me. I would appreciate it if someone can help. Thanks in advance.

Upvotes: 0

Views: 1545

Answers (1)

vitruvius
vitruvius

Reputation: 21342

When things get complicated, think about a brute force solution.

In this case, you can have five var flags to see if the respective alert has been triggered. Then trigger your alarm if it has not been triggered yet. Reset the flags when there is a sell signal.

var buy_signal_1_alert_triggered = false
var buy_signal_2_alert_triggered = false
var buy_signal_3_alert_triggered = false

buySignal_1 = ta.crossover(a, b)
buySignal_2 = ta.crossover(c, d)
buySignal_3 = ta.crossover(e, f)

// Trigger alerts if they have not been triggered yet
alert_1 = not buy_signal_1_alert_triggered and buySignal_1
alert_2 = not buy_signal_2_alert_triggered and buySignal_2
alert_3 = not buy_signal_3_alert_triggered and buySignal_3

// Update the flags
buy_signal_1_alert_triggered := alert_1 ? true : buy_signal_1_alert_triggered
buy_signal_2_alert_triggered := alert_2 ? true : buy_signal_2_alert_triggered
buy_signal_3_alert_triggered := alert_3 ? true : buy_signal_3_alert_triggered

// Reset the flags
if (sellSignal_1)
    buy_signal_1_alert_triggered := false
    buy_signal_2_alert_triggered := false
    buy_signal_3_alert_triggered := false

I haven't tested this but it should work.

Upvotes: 1

Related Questions