In this video we are going to create an expert advisor that is able to get the last order type. I hope I can get it done before the building noise around the house starts, so let’s find out how to do that with MQL5. To get started, please click on a little icon here or press F4 on your keyboard. Now you should see the MetaEditor window. And here you want to click on File, new File, Expert Advisor from Template. Continue. I will call this file simple last order type.


Click on Continue, continue and finish. Now you can remove everything above the Ontick function and the two comment lines here. We start with an include statement to include the file Trade.mqh. And that includes the class C trade. We’re going to use an instance of Ctrade that is called Trade. Inside of the Ontick function we need to calculate the ask price and the bid price. That can be done by using symbol info double for the current symbol on the chart.


We use either symbol underscore ask or symbol underscore bid. And with normalize double and underscore digits, we’ll make sure to automatically calculate the right number of digits behind the dot. Now we want to open test positions. If we have no positions, that would be the case when positions total is below one. We are going to use Trade Buy and Trade Sell to open test positions on a test demo account. This would make no sense on a real account, but obviously we need to have some positions to find out the position direction of the last one. And to get the position direction we want to call a function that is called Get Last order type and assign the result to a string variable called My last order type.


And we use a comment statement to output the text my last position was and the result of the function that we are going to create now. So let’s start. The name of the new function will be Get Last order type and it will return a string variable. We need to define a few variables that are required to get the proper history result.


Now we use History Select.


We can pass two time stamps from date and to date. We want to start from the beginning until the current time. Now we use a for loop to go through the total number of deals. Afterwards we check if the ticket number for the current history deal ticket of the counter value is above zero. That would mean we have a real ticket, and in that case we would like to get some data.


First we want to calculate the order profit. That is done by using History deal get double for the current ticket number and we use Deal underscore profit. If you mark that and press F1, you will see that we have so called deal properties. And this is the one we use to get the profit. Let’s continue and get the order type. That is done by using History deal get integer for the current ticket number. And this time we use Deal underscore type. Let’s continue with the current symbol. We use History deal, get string for the current ticket number to get the deal symbol, and then we continue and use History deal get integer for the current ticket number to get the deal entry. Let’s check if the current symbol on the chart equals the symbol of the deal.


And of course, if the deal entry is one, that would mean that the order was closed. And if the close type was order type buy, the original position direction was a sell trade. Otherwise, if the close type was order type sell, the original position direction was a buy trade, let’s assign the code profit, the ticket number and the position direction to our string variable called my result. Now we close the loops and finally we return our result to the main module. So far, so good.


If this was too fast for you, or if you have no idea what all the code here does, maybe you want to watch one of the former starter videos, or maybe even the premium course on our website might be interesting for you. This once again was a suggestion of a premium course member. And if you are already a premium course member and have an idea for a video like this one, just let me know. For now please click on the compile button or Press F7 on your keyboard.


I didn’t get any errors here, and if that is the case, you can click on a little button here or press F4 to go back to Meter Trader. And in MetaTrader, you want to click on View, Strategy TestER or Press Control and R.


Please pick the new file simple last order type dot ex5, mark the option for the visual mode here and start your test. Here we are. We have outputs for buy and sell trades. Now let’s stop that.


So let’s go back to MetaTrader and restart the test. This time we will make a slow version. Here is our first result. It says that the ticket is number four. The closing type was buy, the profit is minus $5.40 and you can see on the chart that the sell position was closed.


Now let’s speed that up a little bit and you see our expert advisor is working. It shows the direction of the last position and you have coded it yourself with a few lines of MQL5 code.