Hey traders, welcome to another lesson on Pine Script. In this lesson we’ll be covering operators and conditional structures, which are blocks of code used to execute code based on certain conditions. It will all make sense after we look at a few examples.
Before we dive into user inputs make sure you are subscribed so that you don’t miss any future updates!
Operators
Operators are symbols or keywords that are used to perform operations on values or variables. Operators can be grouped into different categories based on the type of operation they perform.
The three main type of operators are:
Arithmetic
Comparison
Logical
Arithmetic operators are used to perform mathematical operations.
You can also use the “+” to concatenate strings.
Comparison operators are used to compare two values and return a Boolean (true or false) based on the comparison.
Logical operators are used to perform logical operations on Boolean values. These are “and”, “or”, and “not”. To show this best let’s look at some code.
There are two variables, the “close_lower” and “kick_open”. A kicker candle is the combination of both of these variables (the previous days close is less than the open and the current days open is greater than the previous days open) . We declare a kicker with the logical operator “and”. If both conditions are met, it will return true.
Here we have two variables “overbought” and “oversold”. In this case we will plot triangle if either one of the conditions are met.
Lastly, using the same example of an over bought and oversold rsi reading. We only want to plot a triangle if the overbought condition is met and the overbought condition is not true on the previous bar.
Conditional Statements
Now that we have gone over operators, let’s take a look at how they can be used in conditional statements.
If statements
An if statement is used to execute code if a certain condition is met. In this example if the closing price is less than the opening price, the “Color_inbuilt” variable will be reassigned to the color red.
We can reassign variables using the “ := ” operator. It is also important to note, when using an if statement that the next line needs to be indented with a tab or 4 spaces, so the editor knows to only perform the task if the condition is met.
If – Else statements
An if-else statement is used to execute different code blocks based on whether a condition is true or false. In this example, we create a variable called “bullish_candles” with a value of 0. If a candles close is greater than its open, assign “bullish_candles” a value of 1, otherwise (else) assign it a value of -1.
Because Pine Script runs your code on every bar, the “bullish_candles” variable is reset to 0 on every new bar, our indicator will always plot a 1 or -1. We can use the keyword “var” to tell Pine Script that we don’t want this variable to be reset with every bar. If we make that one change to this code we then will have a running count of the “bullish_candles” variable rather than just a simple plus or minus 1 value. Notice how the plot changes.
Ternary Operator
The ternary operator is a shorthand way of writing if-else statements. In the example below, we get the exact same result as the if-else statement above, but we do it only using one line of code. This structure uses a “ ? ” to separate the conditions from the outcomes and “ : ” to differentiate between which code to execute.
Thanks for reading! I hope you found this lesson helpful. If you enjoyed this article please help support my work by doing the following:
Follow me on Twitter @amphtrading
Share this post using the button below
The content presented is for informational and educational purposes only. Nothing contained in this newsletter should be construed as financial advice or a recommendation to buy or sell any security. Please do your own due diligence or contact a licensed financial advisor as participating in the financial markets involves risk.