0

I have followed the answer Here to implement a Stop-Loss and Take-Profit by percentage in one script as a strategy as the code is shown.

However, now I wanna convert this strategy into an indicator v5 (study v4) adding alert functionality to it.

I used this code to trigger an alert once the enterLong condition is met however how to set the TP and SL with the same alert?

Is it possible to create one alert with TP and SL avoiding multiple alerts per trading pair, especially since TradingView's maximum allowed alerts are 400 which will be fully consumed?

Remark I'm using TP% and SL% and might include leverage not only spot trading.

alertcondition(enterLong, title="LONG", message = "long sl=SLPerc tp=TP1Perc")
David Buik
  • 522
  • 1
  • 8
  • 31

1 Answers1

0

If you want to use dynamic messages, I suggest you use the alert() function.

So, you can do something like this:

s = "Entry price: " + str.tostring(entryPrice) + " Stop loss: " + str.tostring(SLPerc) + " Take profit: " + str.tostring(TP1Perc)

if your_condition
    alert(s, alert.freq_once_per_bar_close)

alertcondition() does not support what you are trying to do. If you really want to use the alertcondition(), you should plot those values and use the {{plot_x}} placeholders as described here.

vitruvius
  • 15,740
  • 3
  • 16
  • 26
  • Can I use alert in strategy without the need to re-create it as an indicator (study)? And does it work triggering the alert once the condition is met? – David Buik Jun 10 '22 at 13:51
  • One thing about 'alert()' it doesn't have a title so when using the indicator in TV and when clicking on the alerts button to add one you can't see the title of the alert you want – David Buik Jun 10 '22 at 13:53
  • `alert()` has a `freq` parameter where you can specify the triggering frequency. Yes, it does not have a title so you need to add some if conditions to make sure that it triggers at the correct moment. – vitruvius Jun 10 '22 at 16:44
  • That's true however how to connect it to bots like 3Commas or any other or let's say Telegram? – David Buik Jun 10 '22 at 18:25
  • Like how you do with regular alerts. You need to prepare the bot commands in a string. Make sure that you are calling 'alert()' at the correct time and with the correct message. – vitruvius Jun 10 '22 at 20:47
  • This is will be hard for configuration this way I have to set a configuration per bot type like 3Commas is different than Finandly and any other bots. The aim to be more dynamic and easier. Can't I use multiple conditions in `alertcondition()` so it triggers on entry and TP and SL? – David Buik Jun 10 '22 at 20:55