MQL4 TUTORIAL BASICS – 118 SIMPLE LAST ORDER TYPE

video

 

In this video we are going to create an expert advisor that is able to calculate the direction for the last position that was closed. So let’s find out how to do that with MQL4. To get started, please click on a little icon here or press F4 on your keyboard. Now you should see the Meta Editor 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 delete everything above the Ontick function and let’s also remove the two comment lines here. Inside of the Ontick function we want to check if we have no open orders. If that is the case, we need to open test positions. Obviously you wouldn’t do that on a real account, but this is what demo accounts are good for, and we need some positions to calculate the direction of the last one.

 

Therefore, we are going to use order send to open a buy and a sell position. Afterwards we want to create a chart output. That is done by using the comment statement. It will output the text. My last position was followed by the return value for this function here. It is called Get last order type. But it doesn’t exist so far, so we need to create it now. Our user defined function will return a string value. The name is Get last order type. We need a few variables for the details and we use a for loop to go through all the orders in the history.

 

Now we pick one order by using order select for the current counter value. We want to select the position for the mode underscore history. In the next step we want to check if the current order symbol equals the symbol on the chart, and we only want to have the direction for the last position. So we check if it is the last order. That would be true when the counter value equals orders history total minus one. If that is true, we want to calculate the last profit by adding the values for order profit, order swap and order commission.

 

And of course we want to check the order type. If it equals Op underscore buy, the last order type is set to buy order. Otherwise, if the order type equals Op sell, that would mean we have a sell order, so that’s when we assign the word sell order to our last order type. Let’s continue and store the values for the last order type and the last profit in our result variable. Now we close the for loop and return our result to the main module and that’s about it.

 

If this was too fast for you or if you have no idea what all the code here does, you maybe want to watch one of the other videos in the basic video series, or maybe even the premium course on our website might be interesting for you.

 

By the way this was once again a suggestion of a premium course member. And if you are a premium course member and have an idea for a video like this one, please let me know. For now please click on the compile button or press F7 on your keyboard. You shouldn’t get any errors, and if that is the case, you can click on a little button here or press F4 to go back to Meta Trader. And in MetaTrader, please click on View, Strategy Tester or Press Control and R.

 

Please pick the new file simple last order type.ex4, mark the option for the visual mode here and start your test. Here we are, the expert advisor is running and right now the sell trade was closed. It also says that the last position was a sell order with a profit of -4.6 dollars, so let’s click on the results tab. Here it is. So our little expert advisor is working as expected. And in this little video you have learned how to create an expert advisor that is able to calculate the direction for the last position that was closed and you have coded it yourself with a few lines of MQL4 code.