What are loops?
Loops are referred to as the method of finding historical data points (such as a bearish candle and its high price) by looping through all historical data until the conditions for your data point have been met. This means that you don't have to identify the exact candle ID on which you want to find the bearish candle, assuming that you don't know exactly where this bearish candle could be located. A loop will run through all candles, one-by-one, until it finds the bearish candle you are looking for.
Why would you use loops?
Let's have a look at the chart example above. If we keep the example of wanting to find the first bearish candle, it means that we have to first iterate through 6 bullish candles. You can obviously build this in Profectus using 7 trade rule blocks, given that the specific candle you are looking for is no further away than 6 candles.
This process is time-consuming and illogical, as you never know exactly where the candle could be located. For this reason, you want to use a simple look to find the correct candle in one go and store it in a variable to use it later on.
Building loops the easy way
The process of building loops using the example of finding the first bearish candle looks like this:
Initialize variables: Begin by creating two variables named candleid and bearishcandlehigh. Set candleid to 1 and initialize bearishcandlehigh with a value of 0.
Define the loop condition: Within the loop, check whether the close price of the candle at
candleidis below its open price. This condition identifies a bearish candle.Update the variable: If the condition is not satisfied (meaning the candle is not bearish), increase the value of
candleidby 1.Repeat the process: Continue looping and comparing candles using the updated
candleiduntil a bearish candle is found.Store the result: Once a bearish candle is detected, exit the loop and assign the corresponding
candleidvalue to thebearishcandlehighvariable for later use.
Sounds complex? Don't worry, let me show you what it looks like on the Profectus canvas.
Let's go through the blocks:
Run per candle. This indicates that we want to run the loop again for every new candle that is being created.
Trade rule block 1. Here, we first check if there is not yet a bearish candle high found and in play. Without this rule, you would only be able to hold the stored value for the bearish candle ID for 1 candle, and then it would find the next one.
bearishcandlehigh= 0Trade rule block 2. Here, we check whether the close price of the candle
candleidis below its open price. This condition identifies a bearish candle.Modify variable block 1. If the bearish candle condition is not satisfied on the previous candle, we add +1 to the value of
candleidto tell the EA to look for the next candle. It will repeat this process in the same tick until the bearish candle is found. The value ofcandleidat that point shows on which exact candle ID the bearish candle was found. This block is connected with the false output of the 2nd Trade Rule block.
The output of this counter block goes back into the 2nd Trade rule block to check the conditions again and repeat the process.Modify variable block 2. If a bearish candle has been found, the true output gets triggered, and we assign the corresponding
candleidvalue to thebearishcandlehighvariable for the high price of this specific candle for later use.
Assigning the corresponding value to the bearishcandlehigh variable looks like this:
Using the loop in a trading strategy
Your loop is now completed and ready for the next step to add your other strategy rules and trade order execution blocks. The last variable modification is used as our trigger to tell the rest of the EA that it can move on with the rest of the strategy.
You now simply tell your EA to check if the variable bearishcandlehigh is not equal to 0 before moving on and taking a trade.
You can see I have now added a small strategy element that uses the bearishcandlehigh, checks an extra condition (candle close cid 1 > 50 EMA), and that places a buy pending order on the assigned bearishcandlehigh high price.
Resetting your loop
The final step of the looping process that can’t be forgotten is to reset the variables back to their default values after a trade has been taken. If you don’t do this, your EA will only take one trade and get stuck. Resetting the loop in this example means setting the variables back to:
candleid = 1
bearishcandlehigh = 0
We reset our loop in 2 situations:
1. If the other trade conditions have not been met, we want to look for new opportunities, and that requires a reset of the loop. You can see in the example above how there are 2 false outputs connected to the "Reset" Modify variables block.
2. We also want to reset the loop after a trade has been placed because the whole cycle is now completed, and we want to look for new opportunities again.
Building more strategies using loops
Loops are one of the gateways that open up the world to automating basically any trading strategy. With loops, you can:
-Find complex candlestick patterns back in time
-Find support/resistance levels back in time
-Find accurate breakout levels
-Find the highest/lowest prices over varying time periods
And so much more. This example will help you to understand the basics. Access the loop example template as used in this article.




