Pine Script is a domain-specific language for coding custom technical analysis indicators and strategies on TradingView. Among the numerous functions available in Pine Script, one often overlooked but very useful function is float()
.
What is the Float Function?
The float()
function in Pine Script is a casting function that is used to convert an argument into a float type value. In simple terms, it is used to ensure that the variable or data you are working with is in the float format, even if the original input was na
or not available.
The syntax for the float()
function can be represented as:
float(x) → const float float(x) → input float float(x) → simple float float(x) → series float
In this syntax, x
is the argument that is being converted to a float. This argument can be a constant, input, simple, or series type variable.
Use Cases of the Float Function
The float()
function is incredibly versatile and can be applied to a variety of scenarios in Pine Script. It can be particularly useful when dealing with data that may sometimes be unavailable, or na
. By converting na
values to float, you can prevent errors that may occur when executing mathematical operations.
Let’s look at a unique use case to see the float()
function in action.
Example: Calculating the Simple Moving Average with Float Function
In this example, we’ll calculate a simple moving average (SMA) for a series of prices. We’ll also ensure that our calculation handles na
values correctly using the float()
function.
Here’s the code:
//@version=5 indicator('Price Change using float', shorttitle='PC_f', overlay=true) length = input.int(14, minval=1) var float pChange = na for i = 1 to length by 1 if na(close[i]) pChange := na pChange else pChange := (close - close[i]) / close[i] * 100 pChange plot(pChange, color=color.new(color.red, 0))
//@version=5
: This is a directive to use version 5 of Pine Script. Version directives ensure your script is interpreted correctly as the language evolves.indicator('Price Change using float', shorttitle='PC_f', overlay=true)
: This line sets up the script’s properties. Theindicator
function declares the script as an indicator script. It is given a title “Price Change using float”, a short title “PC_f”, and is set to overlay the plot on the price chart withoverlay=true
.length = input.int(14, minval=1)
: This line of code creates an input field on the settings panel of the indicator. The initial value is 14, and it can be any integer greater than or equal to 1.var float pChange = na
: Here, we declare a variablepChange
as a float type, initialized withna
(not available). Thevar
keyword ensures the variable’s value persists across bars, and thefloat
keyword declares the variable type as float.for i = 1 to length by 1
: This begins afor
loop that starts from 1 and goes up tolength
, incrementingi
by 1 each time.if na(close[i])
: Thisif
condition checks if the closing price of the **i
**th previous bar isna
. If it is, the following block of code is executed:pChange := na
: This line assignsna
topChange
.pChange
: This line doesn’t have any effect on the script’s execution and seems unnecessary.
else
: This block of code is executed whenclose[i]
is notna
:pChange := (close - close[i]) / close[i] * 100
: Here, we calculate the percentage change of the current closing price (close
) from thei
th previous closing price (close[i]
) and assign the result topChange
.pChange
: Similar to the previous mention ofpChange
, this line doesn’t impact the script’s execution and can be removed.
plot(pChange, color=color.new(color.red, 0))
: Finally, theplot
function is used to displaypChange
on the chart. The line color is set to red withcolor.new(color.red, 0)
. The0
is the transparency level (0 means fully opaque).
This script will calculate the percentage price change over the past length
bars and plot the values on the chart. Any bars where close[i]
is na
will also be plotted as na
.
Key Takeaways
- The
float()
function in Pine Script is used to convert an argument to a float value. - It is incredibly useful when dealing with data that may sometimes be
na
, as it allows you to perform calculations without errors. - The function can take a constant, input, simple, or series type argument and return a float value.
Conclusion
In conclusion, the float()
function is a powerful tool in the Pine Script toolkit. It provides a flexible way to handle na
values, enabling you to write robust and error-free code. Whether you’re calculating moving averages, or dealing with other complex technical analysis scenarios, float()
function