The document discusses the five arithmetic operators in Pine Script: addition, subtraction, multiplication, division, and modulo. Examples are provided to demonstrate how each operator works on integer and float values, including unary operators, string concatenation, and real-life use cases like calculating percentage change, averages, and converting times. Modulo is used to extract hours, minutes, and seconds from timestamps.
The document discusses the five arithmetic operators in Pine Script: addition, subtraction, multiplication, division, and modulo. Examples are provided to demonstrate how each operator works on integer and float values, including unary operators, string concatenation, and real-life use cases like calculating percentage change, averages, and converting times. Modulo is used to extract hours, minutes, and seconds from timestamps.
The document discusses the five arithmetic operators in Pine Script: addition, subtraction, multiplication, division, and modulo. Examples are provided to demonstrate how each operator works on integer and float values, including unary operators, string concatenation, and real-life use cases like calculating percentage change, averages, and converting times. Modulo is used to extract hours, minutes, and seconds from timestamps.
num = 1 unary_sub = - num // Negates the expression unary_add = + num // does nothing added just for the symmetry with the unary - operator // plot(unary_add, color=color.green, title='unary add') // plot(unary_sub, color=color.red, title='unary subtract')
// Example Use Case: Finding the range
range = high - low change_range = range - range[4] // Difference between todays range and yesterdays range change_range_bi = change(range, 4) // Built in function that does same thing as above // plot(range, title=concatenation, color=color.red) // plot(change_range, title="change " + string_2, color=color.orange ) // plot(change_range_bi, title="change " + string_2 + " bi", color=color.yellow )
// DIVISION & MULTIPLICATION
_int_div = 2 / 2 // const int = 1 _int_mult = 2 * 2 // const int = 4 _float_div = 2.0 / 2.0 // const float = 1.0 _float_mult = 2.0 * 2.0 // const float = 4.0 _float_div_ = 2 / 2.0 // const float = 1.0 series_div = close / 4 // series float = close[n] / 4 series_mult = close * 4 // series float = close[n] * 4 // Note* Result is a float because built-in "close" variable is a series[float] // If one operand is na then the result will be na always_na_div = 2 / na // na always_na_mult = na(2 * na) // na // plot(always_na_div)
// impossible_div = 1 / 0 // This will throw an error
bad_fraction = 1 / 2 // This does not equal 0.5 because these are both ints so the result is an integer good_fraction = 1.0 / 2 // Automatically casts to float better_fraction = 1.0 / 2.0 // plot(bad_fraction)