Once you have the "deal entry out", you need to use the Position ID, to find corresponding "deal entry in".
gringoh:
Hi,
Many thanks for your help Alain, May I ask you how and where to use the Position ID?
Thanks,
G
HistoryDealGetInteger(deal_ticket,DEAL_POSITION_ID);
Once you have it, then loop the deals to find the ones with same PositionID but DEAL ENTRY IN.
I managed to get the correct information thanks to your help ;-)
If it can help other coders, below is the code that I am using. It is a much longer process and need more resources than with mq4, is there a best way to get this information?
//Variables
datetime deal_close_time = 0;
double deal_close_price = 0;
datetime deal_entry_time = 0;
double deal_entry_Price = 0;
double deal_profit = 0;
string Symbol_temp = "";
long pos_id = 0;
ENUM_DEAL_ENTRY entry_type;
long deal_nature = 0;
//Start loop
HistorySelect(0, TimeCurrent());
for (int i = HistoryDealsTotal(); i >= 0; i--)
{
if (HistoryDealsTotal() == 0) return;
ulong deal_ticket=HistoryDealGetTicket(i);
if ( (ProfitMN != -1) && ( HistoryDealGetInteger(deal_ticket, DEAL_MAGIC) != ProfitMN ) ) continue;
if (ProfitSYMBOL != "") { if (HistoryDealGetString(deal_ticket, DEAL_SYMBOL)!= ProfitSYMBOL) continue; }
Symbol_temp = HistoryDealGetString(deal_ticket,DEAL_SYMBOL);
if(Symbol_temp!="")
{
entry_type=(ENUM_DEAL_ENTRY)HistoryDealGetInteger(deal_ticket,DEAL_ENTRY);
if(entry_type==DEAL_ENTRY_OUT)
{
deal_nature = HistoryDealGetInteger(deal_ticket,DEAL_TYPE);
pos_id = HistoryDealGetInteger(deal_ticket,DEAL_POSITION_ID);
deal_close_time = (datetime)HistoryDealGetInteger( deal_ticket, DEAL_TIME );
deal_close_price = HistoryDealGetDouble( deal_ticket, DEAL_PRICE);
deal_profit = HistoryDealGetDouble(deal_ticket,DEAL_PROFIT) + HistoryDealGetDouble(deal_ticket,DEAL_SWAP) + HistoryDealGetDouble(deal_ticket,DEAL_COMMISSION);
for (int y = HistoryOrdersTotal(); y >= 0; y--)
{
ulong open_ticket = HistoryOrderGetTicket(y);
//Print("Open tiket: " + open_ticket);
if(open_ticket != pos_id) continue;
deal_entry_time = (datetime)HistoryOrderGetInteger( open_ticket, ORDER_TIME_DONE );
deal_entry_Price = HistoryOrderGetDouble( open_ticket, ORDER_PRICE_CURRENT);
Print("Deal: " + deal_ticket + " pos_id: " + pos_id + " " + i + " entry: " + deal_entry_Price + " exit: " + deal_close_price);
Print("Deal: " + deal_ticket + " pos_id: " + pos_id + " " + i + " entry t: " + TimeToString(deal_entry_time, TIME_DATE|TIME_SECONDS) + " exit t: " + TimeToString(deal_close_time, TIME_DATE|TIME_SECONDS));
}//End for
}//End DEAL_ENTRY_OUT
}//End (Symbol_temp!="")
}//End for
datetime deal_close_time = 0;
double deal_close_price = 0;
datetime deal_entry_time = 0;
double deal_entry_Price = 0;
double deal_profit = 0;
string Symbol_temp = "";
long pos_id = 0;
ENUM_DEAL_ENTRY entry_type;
long deal_nature = 0;
//Start loop
HistorySelect(0, TimeCurrent());
for (int i = HistoryDealsTotal(); i >= 0; i--)
{
if (HistoryDealsTotal() == 0) return;
ulong deal_ticket=HistoryDealGetTicket(i);
if ( (ProfitMN != -1) && ( HistoryDealGetInteger(deal_ticket, DEAL_MAGIC) != ProfitMN ) ) continue;
if (ProfitSYMBOL != "") { if (HistoryDealGetString(deal_ticket, DEAL_SYMBOL)!= ProfitSYMBOL) continue; }
Symbol_temp = HistoryDealGetString(deal_ticket,DEAL_SYMBOL);
if(Symbol_temp!="")
{
entry_type=(ENUM_DEAL_ENTRY)HistoryDealGetInteger(deal_ticket,DEAL_ENTRY);
if(entry_type==DEAL_ENTRY_OUT)
{
deal_nature = HistoryDealGetInteger(deal_ticket,DEAL_TYPE);
pos_id = HistoryDealGetInteger(deal_ticket,DEAL_POSITION_ID);
deal_close_time = (datetime)HistoryDealGetInteger( deal_ticket, DEAL_TIME );
deal_close_price = HistoryDealGetDouble( deal_ticket, DEAL_PRICE);
deal_profit = HistoryDealGetDouble(deal_ticket,DEAL_PROFIT) + HistoryDealGetDouble(deal_ticket,DEAL_SWAP) + HistoryDealGetDouble(deal_ticket,DEAL_COMMISSION);
for (int y = HistoryOrdersTotal(); y >= 0; y--)
{
ulong open_ticket = HistoryOrderGetTicket(y);
//Print("Open tiket: " + open_ticket);
if(open_ticket != pos_id) continue;
deal_entry_time = (datetime)HistoryOrderGetInteger( open_ticket, ORDER_TIME_DONE );
deal_entry_Price = HistoryOrderGetDouble( open_ticket, ORDER_PRICE_CURRENT);
Print("Deal: " + deal_ticket + " pos_id: " + pos_id + " " + i + " entry: " + deal_entry_Price + " exit: " + deal_close_price);
Print("Deal: " + deal_ticket + " pos_id: " + pos_id + " " + i + " entry t: " + TimeToString(deal_entry_time, TIME_DATE|TIME_SECONDS) + " exit t: " + TimeToString(deal_close_time, TIME_DATE|TIME_SECONDS));
}//End for
}//End DEAL_ENTRY_OUT
}//End (Symbol_temp!="")
}//End for
Regards,
G

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi,
I am currently migrating some of my tools from mq4 to mq5 and I am struggling to get the pip/tick count of closed orders.
My only issue is to get the ENTRY PRICE of closed orders.
Here is the piece of code that I am using
double deal_close_price = 0;
double deal_entry_Price = 0;
string Symbol_temp = "";
HistorySelect(0, TimeCurrent());
for (int i = HistoryDealsTotal(); i >= 0; i--)
{
if (HistoryDealsTotal() == 0) return;
ulong deal_ticket=HistoryDealGetTicket(i);
if ( HistoryDealGetInteger(deal_ticket, DEAL_MAGIC) != ProfitMN ) continue;
if ( HistoryDealGetString(deal_ticket, DEAL_SYMBOL)!= ProfitSYMBOL ) continue;
Symbol_temp = HistoryDealGetString(deal_ticket,DEAL_SYMBOL);
if(Symbol_temp!="")
{
ENUM_DEAL_ENTRY entry_type=(ENUM_DEAL_ENTRY)HistoryDealGetInteger(deal_ticket,DEAL_ENTRY);
if(entry_type==DEAL_ENTRY_OUT)
{
deal_close_time = (datetime)HistoryDealGetInteger( deal_ticket, DEAL_TIME );
deal_close_price = HistoryDealGetDouble( deal_ticket, DEAL_PRICE);
deal_entry_Price = ????????????????????????
Print("Deal: " + deal_ticket + " " + i + " entry: " + deal_entry_Price + " exit: " + deal_close_price);
}//End DEAL_ENTRY_OUT
}//End (Symbol_temp!="")
}//End for
Any help to get the entry price value and/or to improve my code would be much appreciated.
Regards,
G