I'm trying to get my alert message to print a variable that is defined earlier in my script. Here's the alertcondition()
part of my code:
var bool alerttrigger = true
WaveTrend1val = wt1
strWaveTrend1val = tostring(WaveTrend1val)
alertcondition(alerttrigger, title="Test alert", message=strWaveTrend1val)
//plot(WaveTrend1val, "WaveTrend1val")
Just make the trigger True
all the time for testing purposes. wt1
is a value that is defined within this function:
f_wavetrend(src, chlen, avg, malen, tf) =>
tfsrc = security(syminfo.tickerid, tf, src)
esa = ema(tfsrc, chlen)
de = ema(abs(tfsrc - esa), chlen)
ci = (tfsrc - esa) / (0.015 * de)
wt1 = security(syminfo.tickerid, tf, ema(ci, avg))
wt2 = security(syminfo.tickerid, tf, sma(wt1, malen))
wtVwap = wt1 - wt2
wtOversold = wt2 <= osLevel
wtOverbought = wt2 >= obLevel
wtCross = cross(wt1, wt2)
wtCrossUp = wt2 - wt1 <= 0
wtCrossDown = wt2 - wt1 >= 0
wtCrosslast = cross(wt1[2], wt2[2])
wtCrossUplast = wt2[2] - wt1[2] <= 0
wtCrossDownlast = wt2[2] - wt1[2] >= 0
[wt1, wt2, wtOversold, wtOverbought, wtCross, wtCrossUp, wtCrossDown, wtCrosslast, wtCrossUplast, wtCrossDownlast, wtVwap]
and then it is plotted to my chart using this code:
plot(wtShow ? wt1 : na, style = plot.style_area, title = 'WT Wave 1', color = macdWTColorsShow ? macdWT1Color : colorWT1, transp = 0)
I can easily plot()
the value of wt1
in the last line of the first code block, but when I try to convert the float value to a string and print it in the alert message, I get the following error:
Cannot call 'alertcondition' with 'message'=series[string]. The argument should be of type: const string
I first tried to print the value using {{wt1}}
in the alert message, but I think this only works for the built-in placeholders, such as {{time}}
or {{close}}
. It look like I'm converting the whole series of values of wt1
to a string, but I also tried tostring(wt1[0])
to get only the latest value of wt1
rather than the entire series, but I get the exact same error.
Funny that I've managed to find an answer by myself literally minutes after posting this, but I spent about 3 hours before trying to find and figure out a solution so I'll post my solution here instead of deleting the question, just in case someone else finds it helpful.
The solution I found was to reference the plot itself. So the plot name for the wt1
values is "WT Wave 1". Can reference the plot in the alert message like so:
alertcondition(alerttrigger, title="Test alert", message='TEST: {{plot("WT Wave 1")}}')
This puts in the latest value from the plot into the alert message so that it reads something like "TEST: 43.520", or whatever the value is.
Here's a link to the page that I found the solution on that goes into more detail about the {{placeholders}} used in alert messages: https://kodify.net/tradingview/alerts/alert-variables/