Olivia Moore
Olivia Moore

Reputation: 1

Pinescript multiple alerts for one condition

I am using my script with 3commas. I am trying to set alerts to when it flips from one direction to another like in the picture. What is the best way to write that in code? It seems that I cant send a close alert for previous trade to 3c when this situation happens. Please Help :)

entry code:

//entry & exit
strategy.entry('LONG', direction=strategy.long, comment='LONG', when=Long_signal, alert_message = entry_long_alert)
strategy.entry('SHORT', direction=strategy.short, comment='SHORT', when=Short_signal, alert_message = entry_short_alert)

exit code:

// exit long
if strategy.position_size > 0 
    if (Short_signal) 
        strategy.close('LONG', alert_message = close_long_alert)
    else 
        strategy.exit(id = "Close", stop = longstoppercent, limit = longtakeprofit, alert_message = close_long_alert)

// exit short
if strategy.position_size < 0
    if (Long_signal)
        strategy.close('SHORT', alert_message = close_short_alert)
    else
        strategy.exit(id = "Close", stop = shortstoppercent, limit = shorttakeprofit, alert_message = close_short_alert)

Upvotes: 0

Views: 446

Answers (1)

vitruvius
vitruvius

Reputation: 21121

You can specifically check if the position direction has changed by using the strategy.position_size built-in variable.

If the value is > 0, the market position is long. If the value is < 0, the market position is short.

long_to_short = (strategy.position_size[1] > 0) and (strategy.position_size < 0)
short_to_long = (strategy.position_size[1] < 0) and (strategy.position_size > 0)

Then I would use the alert() function with those variables.

if (long_to_short)
    alert("Long to Short")

if (short_to_long)
    alert("Short to Long")

Upvotes: 0

Related Questions