Cause
A variable's type is fixed when it is declared. Here level is a float, and a later := tries to store a string in it.
Broken code
TradingView reports the error on line 5.
//@version=6
indicator("Level and side")
float level = 0.0
if close > open
level := "above"
plot(level)Fixed code
The same script, corrected for Pine Script v6. Paste it into the Pine Editor and save.
//@version=6
indicator("Level and side")
float level = 0.0
string side = "below"
if close > open
level := close
side := "above"
plot(level)
if barstate.islast
label.new(bar_index, level, side)Why it works
Use one variable per kind of value: a float for the price and a string for the description. Pine doesn't convert between types when you assign, with one exception: an int can be assigned to a float variable. To store a number as text, convert it with str.tostring(); to read a number from text, use str.tonumber().
Older Pine versions worded the same problem differently (a variable "declared with" one type that can't take an expression of another), and the fix is identical.
The same error with int and float
int count = 0 followed by count := count + 0.5 fails with Cannot assign a value of the "float" type to the "count" variable. The variable is declared with the "int" type. Declare the variable as a float, or convert the value with int(), math.round(), math.floor() or math.ceil().
Questions
How do I convert a number to a string in Pine Script?
Use str.tostring(value), optionally with a format: str.tostring(close, format.mintick) rounds to the symbol's tick size and str.tostring(value, "#.##") keeps two decimals.
Can I assign an int to a float variable?
Yes, ints are converted to float automatically. The reverse isn't: assigning a float to an int variable needs int(), math.round(), math.floor() or math.ceil().
The scripts on this page are minimal examples written to show the error and its fix, not trading systems. Trading involves substantial risk of loss and is not suitable for every investor. Edgoro is a software tool, not a broker or financial adviser: nothing it produces is investment advice, and past or backtested performance does not guarantee future results.