Rinku
Rinku

Reputation: 133

Pinescript: Use text variable in Alerts

I wish to include a text variable in the alertcondition. I am using alertcondition(scr_label!="" ,title="1.Screener ALert",message="Screener Alert:"+scr_label)

Here scr_label is a dynamic text variable and I want the alert to give me that as the alert message. However if fires an alert without any message.

How can I achieve this?

Upvotes: 1

Views: 4778

Answers (3)

Peter Kreissl
Peter Kreissl

Reputation: 1

Hello fellow Pinescript creators, I found this and it was helpful for me to understand the Alert-context. I tried with creating a label, but it doesn't work. I get this error message: it wants still a constant and not a label. could you please show how to do it properly? Basically I want to get the on signal created initial stop loss as number into the alert header and / or the alert message.

Cannot call 'alertcondition' with argument 'message'='sellalerttext'. An argument of 'series label' type was used but a 'const string' is expected.

this is the portion of the code and the label creation.

string str_buyalert = "long slprice =" + str.tostring(initsl)
string str_sellalert = "short slprice =" + str.tostring(initsl)
label buyalerttext = na
label sellalerttext = na 

if(buysignal[0])
    buyalerttext := label.new(bar_index-10, low, str_buyalert, color = c_green, 
       style = label.style_none, textcolor = #ffffffb3, yloc=yloc.belowbar)

if(sellsignal[0])
    sellalerttext := label.new(bar_index-10, high, str_sellalert, color = c_red, 
       style = label.style_none, textcolor = #ffffffb3, yloc=yloc.abovebar)


alertcondition(buysignal[0], "long", buyalerttext )
alertcondition(sellsignal[0], "short", sellalerttext)

Upvotes: 0

user3136781
user3136781

Reputation: 91

You can use the following placeholders from strategy object:

{{strategy.order.alert_message}} 
{{strategy.order.comment}}
{{strategy.order.id}} 

Just put the string you desire inside any of these properties and it should display in alerts.

Upvotes: 1

AnyDozer
AnyDozer

Reputation: 2568

Detailed description in the documentation

Help Center•Alerts•Alerts settings• How to use a variable value in alert

The ability to generate variable text in alerts is very limited. I couldn't output a string variable in the alerts. In the following snippet, a workaround is how to output a color type variable in an alert.

plot(ColorS == #FF8C00 ? 1 : -1, title="colorA", display=display.none)
alertcondition(cross(LagFF,LagFS) and ColorF==ColorS, title='LagF', message='LagF cross {{ticker}} {{interval}} Up/Down {{plot("colorA")}}' )

Leave a feature request with TradingView via their support ticketing system. I think it is possible to add a placeholder {{label}}.

[ADDED]

Now you can do it like this

if scr_label!=""
    alert("Screener Alert:"+scr_label, alert.freq_once_per_bar_close)

Upvotes: 2

Related Questions