Manage episode 214903399 series 1376757
In this video we are going to calculate the history profit for positions that have been already closed, it will output the text: “My last profit was:” followed by the last profit from the first order in the history, as soon as one of the positions here is closed we will see that we get a value.
Let’s speed that process up a little bit and here we are and now you can see the profit, the ticket number and the position and the direction of the trade and now we want to find out how we can create an Expert Advisor that is going to calculate the order profit for history trades.
To do that please click on the little button here or press F4 on your keyboard and now you should see the Metaeditor and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleOrderHistoryProfit” click on “Continue”, “Continue” and “Finish”, and now you can delete everything above the “OnTick” function and the two comment lines here.
First we need to include the file: “Trade.mqh”, it contains several trading functions so now we can create an instance of the class “CTrade” that will be called: “trade” and we are going to use it to open positions later on.
Inside of the “OnTick” function we are going to calculate the Ask price and the Bid price that is done by using “SymbolInfoDouble” for the symbol on the chart and we use “SYMBOL_ASK” or “SYMBOL_BID” to get the Ask or the Bid price.
“NormalizeDouble” and “_Digits” will help us to calculate the right amount of digits behind the dot – this currency pair has 3 digits and this one has 5 digits – and if we don’t have any open positions – so “PositionsTotal” would be less than one – we will use “trade.Buy” to buy 10 micro lot and “trade.Sell” to sell 10 micro lot to open two testing positions, so please don’t do that on your real account but we need some positions to calculate the last profit. That will be done by calling the function: “GetLastProfit” but we have to code that.
In the “OnTick” function we will use “Comment” to output the text: “My last profit was:” and in a new line the calculated profit that we are getting from the “GetLastProfit” function, so let’s code that function now! It will return a string, the name is: “GetLastProfit” profit, we don’t have any parameters here, first we need to create a few variables.
MQL5 is a little bit complicated, there are deals, orders, positions and ticket numbers.
What does it all mean? Well, if you want something that would be an order. The broker can decide if your order is executed or not, if it is it becomes a deal and in that case you will have an open position that will go in one direction or another, so let’s figure that out!
First we need to get the history by using “HistorySelect”, we want all the data up to the current time (TimeCurrent), now we go through all the deals, here we use an unsigned integer as counter and as long as it is less than the total number of deals (TotalNumberOfDeals) we will increase it.
Now we need to look for a ticket number, that ticket number is the return value for the function: “HistoryDealGetTicket” but we need to use an “if” statement because as long as we have no closed position we don’t have anything inside of the history.
The order profit is calculated by using “HistoryDealGetDouble” for the ticket number and we use “DEAL_PROFIT”, we also need to get the order type by using “HistoryDealGetInteger” for the ticket number and this time we use “DEAL_TYPE”.
Now we need to get the currency pair, this time we are using “HistoryDealGetString” for the ticket number and to get the symbol we use “DEAL_SYMBOL”.
Let’s get the deal entry type to check for close types! Once again “HistoryDealGetInteger” for the current ticket number in combination with “DEAL_ENTRY” will deliver the deal entry type.
Now we want to know if the currency pair fits, so if the currency pair for order in the history is equal to the current currency pair on the chart and if we have a buy or a sell order – so if the order type returns “ORDER_TYPE_BUY” or “ORDER_TYPE_SELL” – and if the order was closed – so if “DealEntry” equals 1 – that is when we want to do something; we set the order type to sell (SELL-TRADE) if the closed type was buy. That sounds a little strange but when you look into the history you will see that we have buy and sell orders and whenever you open a buy order and take the profit that would be a sell, so if you open a buy order and close it that would be a sell trade.
In the other case if you opened a sell trade and close it that becomes a buy trade and in the end we calculate the result – that’s a string, it contains some text – and the calculated values for the order profit, the ticket number and the position direction, so let’s close the brackets.
Finally we want to return the result to the main function, that’s the “OnTick” function here.
Now you can click on the “Compile” button or press F7, that should work without any errors here and in that case you can click on the little button here or press F4 to go back to Metatrader.
In Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, please pick the file: “SimpleOrderHistoryProfit.ex5”, enable the visualization here and start a test.
…and here is our little Expert Advisor at work, so far we have no closed position but as soon as that changes we get the last profit and in this little video you have learned how to calculate the profit for the last position that has been closed in the history and you have coded it yourself with a few lines of MQL5 code.
More MQL5 Tutorial Videos:
- MQL5 Tutorial – Simple Order Cancel Order EA Today we want to talk about a system that is called OCO that is the short term for Order Cancels Order. If you look on…
- MQL5 TUTORIAL – SIMPLE POSITION CLOSE This time we are talking about closing of positions, that happened right now. I have gotten a request to do this from one of the…
- MQL5 TUTORIAL – SIMPLE WHILE LOOP In this video we are going to talk about the “while” statement. Actually “while” is a loop, while “if” is a statement, both are very…
- MQL5 TUTORIAL – SIMPLE RSI STANDALONE EXPERT ADVISOR In this video we are going to automate the Relative Strength Index also known as RSI and we are going to create an Expert Advisor…
- MQL5 TUTORIAL – SIMPLE POSITION INFO In this video we are going to talk about position properties. This chart is about the Australian against the Canadian Dollar. We have a position…
- MQL5 Tutorial – Advanced Library Expert Advisor What you see here is an Expert Advisor that is opening buy stops and we have a calculated profit on the chart and it uses…
- MQL5 Tutorial – Simple trend based sell stop In this video we are talking about a sell stop that is triggered whenever we have a trend that is going down. That is called…
- MQL5 TUTORIAL – SIMPLE DYNAMIC POSITION SIZE MQL5 TUTORIAL – SIMPLE DYNAMIC POSITION SIZE Audio into Text Transcription In this video, we are going to open lots and lots of trades to…
120 episodes available. A new episode about every 8 days averaging 5 mins duration .