0

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)

Brenda L.
  • 1
  • 3

1 Answers1

0

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")
vitruvius
  • 15,740
  • 3
  • 16
  • 26
  • oh nice okok! how would I include the "alert_message" that I am using? It seems the alert function doesnt allow that – Brenda L. Nov 17 '22 at 21:26
  • Build your message like you did with `entry_long_alert` for example. Then pass this to `alert()`. – vitruvius Nov 17 '22 at 21:29
  • will this work if im using {{strategy.order.alert_message}} in my alert message? – Brenda L. Nov 17 '22 at 21:55
  • 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) 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) if (long_to_short) alert(close_long_alert, alert.freq_once_per_bar_close) if (short_to_long) alert(close_short_alert, alert.freq_once_per_bar_close) – Brenda L. Nov 17 '22 at 21:59