Metatrader 5: How to get take profit level of closed position?
Metatrader 5: How to get take profit level of closed position?
Loading saved threads...
Jason Smith · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Jason Smith
Original post: https://stackoverflow.com/questions/70557487
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
In the MQL5 language, to get the current TP of an open position (hedging mode), I use
if (PositionSelectByTicket(positionId)) {
return PositionGetDouble(POSITION_TP);
}
Once the position is closed, I search for the corresponding ENTRY_IN deal, and query the corresponding order for the TP:
if (HistorySelectByPosition(positionId)) {
for (int i = HistoryDealsTotal()-1; i >= 0; i--) {
ulong dealId = HistoryDealGetTicket(i);
if (HistoryDealGetInteger(dealId, DEAL_POSITION_ID) != positionId) continue;
if ((ENUM_DEAL_ENTRY)HistoryDealGetInteger(dealId, DEAL_ENTRY) != DEAL_ENTRY_IN) continue;
ulong orderId = HistoryDealGetInteger(dealId, DEAL_ORDER);
if (HistoryOrderSelect(orderId)) {
return HistoryOrderGetDouble(orderId, ORDER_TP);
}
}
}
However, this only works if the TP was set directly while opening the position. If the TP was set later, this does not work. I examined all deals and orders corresponding to the
positionId
, but was not able to find the TP anywhere.
What is a robust method to obtain the TP of a closed position?
Quote
Report
Jason Smith · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: Jason Smith
Original post: https://stackoverflow.com/a/70585400
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Via the MQL5 forum I got the following suggestion from @fxsaber: the DEAL_TP property of the DEAL_ENTRY_OUT deal contains the last TP set. That indeed works!
if (HistorySelectByPosition(positionId)) {
for (int i = HistoryDealsTotal()-1; i >= 0; i--) {
ulong dealId = HistoryDealGetTicket(i);
if (HistoryDealGetInteger(dealId, DEAL_POSITION_ID) != positionId) continue;
if ((ENUM_DEAL_ENTRY)HistoryDealGetInteger(dealId, DEAL_ENTRY) != DEAL_ENTRY_OUT) continue;
return HistoryDealGetDouble(dealId, DEAL_TP);
}
}
Quote
Report
McDiver · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: McDiver
Original post: https://stackoverflow.com/a/77386316
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
mt5.history_deals_get(position=result.order)[1].profit
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: Jason Smith Source score (net votes, not local likes): 1 Original post: https://stackoverflow.com/a/70585400 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Via the MQL5 forum I got the following suggestion from @fxsaber: the DEAL_TP property of the DEAL_ENTRY_OUT deal contains the last TP set. That indeed works! if (HistorySelectByPosition(positionId)) { for (int i = HistoryDealsTotal()-1; i >= 0; i--) { ulong dealId = HistoryDealGetTicket(i); if (HistoryDealGetInteger(dealId, DEAL_POSITION_ID) != positionId) continue; if ((ENUM_DEAL_ENTRY)HistoryDealGetInteger(dealId, DEAL_ENTRY) != DEAL_ENTRY_OUT) continue; return HistoryDealGetDouble(dealId, DEAL_TP); } }
Checking account access…