look this https://www.mql5.com/en/code/19702
and this https://www.mql5.com/en/code/21434
- www.mql5.com
I need to migrate an EA from Mql4 [ OrderProfit(),OrderClosePrice(),OrderOpenPrice()] to Mql5.
Forum on trading, automated trading systems and testing trading strategies
How to figure out the close price when position ran into stop loss?
fxsaber, 2018.07.10 11:46
#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006 void OnStart() { const int Total = OrdersHistoryTotal(); for (int i = 0; i < Total; i++) if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) OrderPrint(); }
I'm trying to get the open, close prices and the profit of an ORDER in the history section, but I do not know how to achieve this. I'm trying with the following code:
But the tickets of DEALS and ORDERS are different and I do not know if there is a way to get the open price of a DEAL or the close price and the profit of an ORDER.
I need to migrate an EA from Mql4 [ OrderProfit(),OrderClosePrice(),OrderOpenPrice()] to Mql5.
Thanks in advance.
hi you can use this one :
void history_checker() { int margines=0; string ordertyp="NULL"; double prft=0; for( int TradeCount=OrdersHistoryTotal()-1;TradeCount>=0;TradeCount--) { if ( OrderSelect(TradeCount,SELECT_BY_POS,MODE_HISTORY)) { if(OrderType()==0) ordertyp="BUY"; else if (OrderType()==1) ordertyp="SELL"; else if (OrderType()>1) ordertyp="NULL"; prft=(OrderSwap()+OrderProfit())-OrderCommission(); //margines:=iBarShift(OrderSymbol(),PERIOD_W1,OrderOpenTime()); //OrderType(); OrderProfit() ; datetime OrderCloseTime() ; if(OrderType()<=1) { Print("Order:"+ ordertyp +":"+"Order Profit:"+DoubleToStr(prft,2)+":"+"Order Open Time:"+TimeToStr(OrderOpenTime(),TIME_DATE|TIME_SECONDS)+":"+"Order Close Time:"+TimeToStr(OrderCloseTime(),TIME_DATE|TIME_SECONDS) ); } } }
Jhojan, I am pretty sure that you already found your way around the problem, as it is now more than 6 months since your initial post :)
Still, to keep other people aware of the solution (and the search engines, of course), I'd like to demonstrate, how I solved this problem.
First, you need to make sure, you understand a couple of concepts:
1. Order is something you send to the broker.
2. Deal is something that is executed in the market on your behalf by the broker.
3. Within one order there are opening deal, closing deal and some other kinds of deals.
4. Order tickets numbers and deal ticket numbers are different (as you pointed out correctly).
5. To close order, essentially an opposite deal is executed on the market.
What you have to do in MQL5 code:
1. First you need to select a history of all deals that are related to your order number. Order number is the number that you can find on the History tab in MT5 terminal, in the column called "Ticket".
HistorySelectByPosition(1234567890); // where 1234567890 is a ticket number of your ORDER
2. Iterate over all deals related to the order; find the one with entry type DEAL_ENTRY_OUT (meaning, this is the deal for position closing); get price of that deal.
I've created a function for convenience and sharing it here with you:
To get answers to other questions, you can just follow same principle and consult MQL5 API documentation a bit.
Jhojan, I am pretty sure that you already found your way around the problem, as it is now more than 6 months since your initial post :)
Still, to keep other people aware of the solution (and the search engines, of course), I'd like to demonstrate, how I solved this problem.
First, you need to make sure, you understand a couple of concepts:
1. Order is something you send to the broker.
2. Deal is something that is executed in the market on your behalf by the broker.
3. Within one order there are opening deal, closing deal and some other kinds of deals.
4. Order tickets numbers and deal ticket numbers are different (as you pointed out correctly).
5. To close order, essentially an opposite deal is executed on the market.
What you have to do in MQL5 code:
1. First you need to select a history of all deals that are related to your order number. Order number is the number that you can find on the History tab in MT5 terminal, in the column called "Ticket".
HistorySelectByPosition(1234567890); // where 1234567890 is a ticket number of your ORDER
2. Iterate over all deals related to the order; find the one with entry type DEAL_ENTRY_OUT (meaning, this is the deal for position closing); get price of that deal.
I've created a function for convenience and sharing it here with you:
To get answers to other questions, you can just follow same principle and consult MQL5 API documentation a bit.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I'm trying to get the open, close prices and the profit of an ORDER in the history section, but I do not know how to achieve this. I'm trying with the following code:
But the tickets of DEALS and ORDERS are different and I do not know if there is a way to get the open price of a DEAL or the close price and the profit of an ORDER.
I need to migrate an EA from Mql4 [ OrderProfit(),OrderClosePrice(),OrderOpenPrice()] to Mql5.
Thanks in advance.