In pine script, the operators which are used to do mathematical calculations are called arithmetic operators. Basically, inputs are given to the operator. It performs the operation and gives output. Input values are called operands. In the pine script programming language, there are five types of arithmetic operators used.
It works same as mathematical algebra.
Addition and Subtraction operator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © ForexBee //@version=4 study("My Script") int_1 = 3+3 // return int value 6 float_1= 3.0 + 3.0 // return float value 6.0 Float_2= 3.0 + 3 // return float value 6.0 series_2 = open + 1 plot(series_2)
In the above code, the values 3, 3.0 are operands and the + symbol is the operator. In the case of na operand, the return value will always be na.
I have used the addition operator to add 1 in the open price of every candle on the chart. The open function indicates the series of the opening prices of every candle on the chart. Copy and paste the code in pine editor and then click add to the chart. You will understand the use of the addition operator.
At least two inputs are given to the arithmetic operator but in the case of subtraction and addition operators, one input can also be given.
Let’s assume two variables a and b. Assign variable a with a value of 2 and assign variable b with a value of -3.
Division and multiplication operators
The ( / ) operator is used for division operator and ( * ) is used for multiplication operator or star operator.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © ForexBee //@version=4 study("My Script") int_1 = 3 / 3 // return int value 1 float_1= 3.0 * 3.0 // return float value 9.0 Float_2= 3.0 / 3 // return float value 1.0 series_2 = open / 2 plot(series_2)
Copy and paste the above code and add to the chart and check the behavior of indicator for learning purposes.
In the case of division, 1/0 will give an error. Any infinite number of infinite series in pine script will give an error. Avoid such types of fractions.
1 / 2 in the case of division operator will not return 0.5. But it will return integer 1 because 1 and 2 are integers and it will only return an integer value. Keep in mind to use float values only during the use of the division operator.
- / 2.0 is a best example of fraction, and it will return 0.5, a float value.
Modulo returns remainder value. For example, in 7 / 4. 1 will be quotient and 3 will be remainder.
int_1 = 7 % 4 // return int value 3 float_1= 7.0 % 4.0 // return float value 3.0
In the next article, we will make a simple indicator based on the parameters we have learned from previous tutorials of pine script.