MQL5 Tutorial – How to spot a doji on your chart
Is it possible to use MQL5 to detect something like a Doji?
So if you don’t know what a Doji is this would be one, it’s a candle where the open price and the close prize are exactly the same.
If you visit Wikipedia you will learn that a Doji is formed when opening and closing prices are virtually the same and if that happens on the lower end of the candle it would be a Grave Stone Doji and when it happens on the upper end of the candle it would be a Dragon-Fly Doji.
So let’s create a simple Expert Advisor in MQL5 that is able to find a Doji on your chart.
To do that please click on the little button here in your Meta Trader or press you F4 key, that will bring up the MetaEditor here, and here you want to select File, New, Expert Advisor from Template, Continue, I will call this Expert Advisor Simple Doji, Continue, Continue and finish.
And now you can delete everything above the OnTick function here and let’s remove the two comment lines.
Now we will use MQLrates to create an array for price information and with arraysetasseries we will sort our price information array from the current candle to the oldest candle.
Now we want to copy our price data into the array. To do that we use copy rates for the current symbol on the chart and the currently selected period.
We will start with the current candle, that is candle zero and we will collect all the bars that are currently on the chart for that symbol and that period we have selected here.
All that price data will be stored in the price information array you have created here.
And now we want to calculate if the last open price equals the last close price.
That is done by using the IF statement and if open price for candle one in our price information array equals the close price of candle one in our price information array, we use comment to output on our chart ‘Candle one is a Doji.’
Now in the other case when the open price of candle one in our price information array doesn’t equal the close price of candle one in our price information array, we would create a chart output that candle one is not a Doji.
Now let’s compile the code here and that was successful, we don’t have any errors and any warnings so now we can click on a little button here to go back to Meta Trader.
And in Meta Trader you want to click on View, Strategy Tester or press Control and R on your keyboard to bring up the Strategy Tester panel.
Here you want to select the Simple Doji.ex5 file, pick any currency here and please enable the visualisation option before you start your test.
And this is how it looks like. You will see the text ‘Candle One is not a Doji’ but the text will change every time when we see a Doji on our chart.
So this would be one and this would be another one so now you know how to spot a Doji on your chart and you have coded it in five minutes with a few lines of MQL 5 codes.