brimos
Pine Script Scholar
Pine Script Scholar
Posts: 4
Joined: March 26th, 2023

Simple String VS Const String error

Hi,

I am new to the forum and halfway through the master course. I am working on modifying one of the Zen scripts and I am running into a compile error when I am trying to pass an integer input function maLength1 into the title one of my plots. I generated a new string called L1 and formatted the integer into a string and I am running into this error:

2:45:52 PM Error at 51:76 Cannot call 'plot' with argument 'title'='call 'operator +' (simple string)'. An argument of 'simple string' type was used but a 'const string' is expected.
2:45:52 PM 'MMA' saved.

maLength1 = input.int(title="MA Length #1", defval=50, minval=1, step=10, group=g_length)
string L1 = str.tostring(maLength1)
// Draw MAs
plot(ma1, color=close >= ma1 ? color.green : color.red, linewidth=1, title=""+ L1)

I am not sure the difference between a simple string and const string. Any help would be greatly appreciated.

My full code is:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MTradingTech

//@version=5
indicator("EMA", overlay=true)

// Get MA Lengths
var g_length = "MA Length"
maLength1 = input.int(title="MA Length #1", defval=50, minval=1, step=10, group=g_length)
maLength2 = input.int(title="MA Length #2", defval=100, minval=0, step=10, group=g_length)
maLength3 = input.int(title="MA Length #3", defval=200, minval=0, step=10, group=g_length)
maLength4 = input.int(title="MA Length #4", defval=72, minval=0, step=10, group=g_length)
maLength5 = input.int(title="MA Length #5", defval=89, minval=0, step=10, group=g_length)
// Get MA Types
var g_type = "MA Type"
maType1 = input.string(title="MA Type #1", defval="EMA", options=["EMA", "SMA", "HMA", "WMA", "DEMA", "VWMA", "VWAP"], group=g_type)
maType2 = input.string(title="MA Type #2", defval="EMA", options=["EMA", "SMA", "HMA", "WMA", "DEMA", "VWMA", "VWAP"], group=g_type)
maType3 = input.string(title="MA Type #3", defval="EMA", options=["EMA", "SMA", "HMA", "WMA", "DEMA", "VWMA", "VWAP"], group=g_type)
maType4 = input.string(title="MA Type #4", defval="EMA", options=["EMA", "SMA", "HMA", "WMA", "DEMA", "VWMA", "VWAP"], group=g_type)
maType5 = input.string(title="MA Type #5", defval="EMA", options=["EMA", "SMA", "HMA", "WMA", "DEMA", "VWMA", "VWAP"], group=g_type)
allMaType = input.string(title="ALL MA TYPE", defval="Disabled", options=["Disabled", "EMA", "SMA", "HMA", "WMA", "DEMA", "VWMA", "VWAP"], group=g_type)



// Get selected Moving Average
getMA(_maType, _maLength) =>
if _maLength == 0
na
else
switch allMaType == "Disabled" ? _maType : allMaType
"SMA" => ta.sma(close, _maLength)
"HMA" => ta.hma(close, _maLength)
"WMA" => ta.wma(close, _maLength)
"VWMA" => ta.vwma(close, _maLength)
"VWAP" => ta.vwap
"DEMA" =>
e1 = ta.ema(close, _maLength)
e2 = ta.ema(e1, _maLength)
2 * e1 - e2
=> ta.ema(close, _maLength)

// Get MAs
ma1 = getMA(maType1, maLength1)
ma2 = getMA(maType2, maLength2)
ma3 = getMA(maType3, maLength3)
ma4 = getMA(maType4, maLength4)
ma5 = getMA(maType5, maLength5)

string L1 = str.tostring(maLength1)
// Draw MAs
plot(ma1, color=close >= ma1 ? color.green : color.red, linewidth=1, title=""+ L1)
plot(maLength2 == 0 ? na : ma2, color=#1d75bc, linewidth=2, title="100")
plot(maLength3 == 0 ? na : ma3, color=#175a91, linewidth=3, title="200")
a4 = plot(maLength4 == 0 ? na : ma4, color=#124773, linewidth=1, title="72")
a5 = plot(maLength5 == 0 ? na : ma5, color=#072d4c, linewidth=1, title="89")
fill(a4,a5, title="Jlines",color=color.new(color.gray,60))

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: Simple String VS Const String error

You have converted L1 to a string which will have automatically embedded quotation marks. Try the following:

plot(ma1, color=close >= ma1 ? color.green : color.red, linewidth=1, title=L1)

brimos
Pine Script Scholar
Pine Script Scholar
Posts: 4
Joined: March 26th, 2023

Re: Simple String VS Const String error

Steve,
I am still getting the error:

2:12:04 PM Error at 26:76 Cannot call 'plot' with argument 'title'='L1'. An argument of 'simple string' type was used but a 'const string' is expected.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MTradingTech

//@version=5
indicator("D EMA", timeframe = "D", overlay=true)

// Get MA Lengths
var g_length = "MA Length"
maLength1 = input.int(title="MA Length #1", defval=50, minval=1, step=10, group=g_length)
maLength2 = input.int(title="MA Length #2", defval=100, minval=0, step=10, group=g_length)
maLength3 = input.int(title="MA Length #3", defval=200, minval=0, step=10, group=g_length)
maLength4 = input.int(title="MA Length #4", defval=0, minval=0, step=10, group=g_length)
maLength5 = input.int(title="MA Length #5", defval=0, minval=0, step=10, group=g_length)


// Get MAs
ma1 = ta.ema(close, maLength1)
ma2 = ta.ema(close, maLength2)
ma3 = ta.ema(close, maLength3)
ma4 = ta.ema(close, maLength4)
ma5 = ta.ema(close, maLength5)
int intL1 = 10

string L1 = str.tostring(maLength1)
// Draw MAs
plot(ma1, color=close >= ma1 ? color.green : color.red, linewidth=5, title=L1)
plot(maLength2 == 0 ? na : ma2, color=color.purple, linewidth=5, title="100")
plot(maLength3 == 0 ? na : ma3, color=color.yellow, linewidth=5, title="200")
plot(maLength4 == 0 ? na : ma2, color=color.blue, linewidth=5, title="??")
plot(maLength5 == 0 ? na : ma3, color=color.lime, linewidth=5, title="??")



I came across this that the "const" tyope is the lowest type:

Forms

Pine Script™ forms identify when a variable’s value is known. They are:

“const” for values known at compile time (when adding an indicator to a chart or saving it in the Pine Script™ Editor)
“input” for values known at input time (when values are changed in a script’s “Settings/Inputs” tab)
“simple” for values known at bar zero (when the script begins execution on the chart’s first historical bar)
“series” for values known on each bar (any time during the execution of a script on any bar)

Forms are organized in the following hierarchy: const < input < simple < series, where “const” is considered a weaker form than “input”, for example, and “series” stronger than “simple”. The form hierarchy translates into the rule that, whenever a given form is required, a weaker form is also allowed.

An expression’s result is always of the strongest form used in the expression’s calculation. Furthermore, once a variable acquires a stronger form, that state is irreversible; it can never be converted back to a weaker form. A variable of “series” form can thus never be converted back to a “simple” form, for use with a function that requires arguments of that form.

Note that of all these forms, only the “series” form allows values to change dynamically, bar to bar, during the script’s execution over each bar of the chart’s history. Such values include close or hlc3 or any variable calculated using values of “series” form. Variables of “const”, “input” or “simple” forms cannot change values once execution of the script has begun.



I do not know how to generate a "Const" type and it seems that a higher type can't be converted to a lower type. Not sure where to go here?

Thanks,
Brian

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: Simple String VS Const String error

Brian,

I now know why you received this error. It is because you are associating the title of a plot with a string variable which happens when you use string L1 = str.tostring(maLength1) just before the plots.

Titles need to be constant strings so change the 1st plot to plot(ma1, color=close >= ma1 ? color.green : color.red, linewidth=5, title="maLength1") or similar. Unfortunately you can't have the title change on the fly as it requires a constant

brimos
Pine Script Scholar
Pine Script Scholar
Posts: 4
Joined: March 26th, 2023

Re: Simple String VS Const String error

Thank you Steve!

Return to “Pine Script Q&A”