#I want to detect when the candle closes...
The problem here is that we don't know when the current candle will be closed. Yes, we can guess - in the last second, just before the period of time ends. But the thing is that our Expert Advisors are driven by ticks and we don't know when the last tick will come. It may come not 1, but 2 seconds before the end of the period. So, if no tick comes in the last second, what will we detect?
Yes, we have the Timer event and we can set it up for 1 second and do what we want on the last second of the period each time. But there is an issue - the Timer event is not very accurate in the Strategy Tester. See, the Tester actually doesn't have such event, because it doesn't work in real time. The Tester generates fake ticks and drives the Expert Advisor this way. There is no guarantee that in every candle the last tick will come at the last second. So we are asking for problems and very inconsisten results in the Tester and in the live trading.
How to do it? Better use that Once per bar block under the on Tick event to detect the beginning of every new candle.
#Thinking that you need to connect the blocks in one row
Take a look at this project and guess what is wrong with it:
The connection between blocks 3 and 4 should not be there. You might think that each block waits until is passes, but this is not the case, blocks doesn't wait for each other. Instead, blocks (at least those on top level) run on every tick because of the on Tick event.
Well, there is actually a way to make certain blocks to "wait", take a look at the example below. To force a block to wait, right-click on it and select "Wait to Pass". But you might find this way of working confusing and not very intuitive, contrary to your initial expectations.
#Don't forget the "For each..." block
Click here if you don't know what "For each..." means.
So, the category with the pink blocks is a little bit different. "For each..." blocks are designed to create a loop of trades or pending orders and the rest of the pink blocks are designed to manipulate one trade or pending order at a time.
People tend to forget that in MetaTrader 4, and now also in MetaTrader 5 we can have multiple trades at a time. Given that fact, you can't just use let's say the pink check profit block without any context. This block is there to check the profit of a particular trade (or pending order), but it can't magically choose which one is the right one when you have 2 or more trades (or orders) at the same time.
#Don't put blue blocks in a "For each..." loop
This is not mandatory, but be careful and make sure you know what you are doing.
Again, a "For each..." block creates a loop and below this block you should put other pink blocks, specifically designed to work in that context. To work with a single trade (or pending order) at a time.
There is no need to connect let's say For each Trade -> Close trades or maybe For each Trade -> Trailing stop, because those blocks (Close trades and Trailing stop) are blocks who already have loops in them. In other words, they are already designed to find all available trades and do the same for each one of them, they can work alone. While the pink close block for example can only close a trade (or pending order) that was previously selected in a loop made with one of the For each... blocks.
#Watch out for the "Close Trades" block
When you have blocks like Buy now and Close trades under the Tick event, be very careful to not allow Close trades to run under the same conditions as Buy now. Otherwise what happens is that after a trade us created, it is immediately closed and you wonder why you see so many trades opening and closing.
The example below is correct and shows how Close trades can be used in a safe way. You may want to do something different, but in any case, be careful when you are using this block and make sure that it runs only when it's needed. You should always put blocks like No trade (with certain settings) above it or other blocks for checking and filtering, to limit the chance for the block to run at inappropriate times.
