Deal information is accessed at a different time to position (active position) information.
You have the deal scan loop inside the position scan loop, it shouldn't be like that.
This needs to be outside the position scan loop in its own scope:
// Loop through the history to find the deal for (int i = 0; i < HistoryDealsTotal(); i++) { ulong deal_ticket = HistoryDealGetTicket(i); if (deal_ticket == positionTicket) { // Get the close time of the deal datetime close_time = (datetime)HistoryDealGetInteger(deal_ticket, DEAL_TIME); if(close_time) { Print("Close Time",close_time); priceReEnteredRange = false; DrawOrUpdateLine(slLineName, open_time_ticket, slLevel, close_time, slLevel, clrBlueViolet, "SL_Line"); DrawOrUpdateLine(tpLineName, open_time_ticket, tpLevel, close_time, tpLevel, clrYellowGreen, "TP_Line"); DrawOrUpdateRectangle(tpRectName, open_time_ticket, price_start, close_time, tpLevel, clrGreen); DrawOrUpdateRectangle(slRectName, open_time_ticket, slLevel, close_time, price_start, clrRed); } else { Print("Close Time incorrect",close_time); } } else { Print("Close Ticket not Found"); } }
You also NEED to use "HistorySelect" or "HistorySelectByPosition" as it says in the documentation:
Documentation on MQL5: HistoryDealGetTicket / Trade Functions
- www.mql5.com
The function selects a deal for further processing and returns the deal ticket in history. Prior to calling HistoryDealGetTicket(), first it is...
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
I am totally new here and trying to code something like a simple open and close.
However the EA is hitting the TP it is not continuing with anything else in the code.
The part of the code to check and close is not even triggered as I don't see the print messages in the journal.
Can someone explain the mechanic/logic when the trade position hits the TP?
Much appreciated