Home » Data Types In Pinescript » Float Function in Pine Script

Float Function in Pine Script

Photo of author
Published on

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))
Float Function
  1. //@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.
  2. indicator('Price Change using float', shorttitle='PC_f', overlay=true): This line sets up the script’s properties. The indicator 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 with overlay=true.
  3. 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.
  4. var float pChange = na: Here, we declare a variable pChange as a float type, initialized with na (not available). The var keyword ensures the variable’s value persists across bars, and the float keyword declares the variable type as float.
  5. for i = 1 to length by 1: This begins a for loop that starts from 1 and goes up to length, incrementing i by 1 each time.
  6. if na(close[i]): This if condition checks if the closing price of the **i**th previous bar is na. If it is, the following block of code is executed:
    • pChange := na: This line assigns na to pChange.
    • pChange: This line doesn’t have any effect on the script’s execution and seems unnecessary.
  7. else: This block of code is executed when close[i] is not na:
    • pChange := (close - close[i]) / close[i] * 100: Here, we calculate the percentage change of the current closing price (close) from the ith previous closing price (close[i]) and assign the result to pChange.
    • pChange: Similar to the previous mention of pChange, this line doesn’t impact the script’s execution and can be removed.
  8. plot(pChange, color=color.new(color.red, 0)): Finally, the plot function is used to display pChange on the chart. The line color is set to red with color.new(color.red, 0). The 0 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

  1. The float() function in Pine Script is used to convert an argument to a float value.
  2. It is incredibly useful when dealing with data that may sometimes be na, as it allows you to perform calculations without errors.
  3. 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

Leave a Comment