Hello guys,
I'm facing an issue with my code: I want to check profit for a closed order, using OrderProfit().
In MT4 app, this trade is like this in history:
In logs here are information coming from my code:
In my code I use OrderSelect() in history with ticket ID (OrderSelect(ID, SELECT_BY_TICKET, MODE_HISTORY)).
Then I check OrderProfit().
Why is it negative while it was closed ith positive profit?
Thanks.
double positive_value = OrderProfit(); double value = 0; if(positive_value > 0) value = MathAbs(positive_value);
Try this
pool=MODE_TRADES
[in] Optional order pool index. Used when the selected parameter is SELECT_BY_POS. It can be any of the following values:
MODE_TRADES (default)- order selected from trading pool(opened and pending orders),
MODE_HISTORY - order selected from history pool (closed and canceled order).
https://docs.mql4.com/trading/orderselect
So in case SELECT_BY_TICKET, it is not needed to use MODE_HISTORY. I guess you do not need the third input parameter at all.
- docs.mql4.com
Just re-checked my code and its actually quite bad. OrderProfit() returns a trailing value if called upon every tick when a position is open or not.
Here is a much better code, compile is successful and tested, a much better version that makes more sense than the previous code I put out.
void PrintProfitIfTakeProfitReached()
{
int totalOrders = OrdersHistoryTotal();
for (int i = 0; i < totalOrders; i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
double takeProfitLevel = OrderTakeProfit();
double profitValue = OrderProfit();
if (MathAbs(takeProfitLevel <= Bid) || MathAbs(takeProfitLevel <= Ask)) {
Print("Order Profit:", profitValue);
}
}
}
}
Just re-checked my code and its actually quite bad. OrderProfit() returns a trailing value if called upon every tick when a position is open or not.
Here is a much better code, compile is successful and tested, a much better version that makes more sense than the previous code I put out.
Thanks bro!
I changed my code to check close time, but I pin your code which seems ok too.
++
int totalOrders = OrdersHistoryTotal();
for (int i = 0; i < totalOrders; i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
MT4:
-
Do not assume history has only closed orders.
OrderType() == 6, 7 in the history pool? - MQL4 programming forum #4 and #5 (2017) -
Do not assume history is ordered by date, it's not.
Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020) -
Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
"balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)Broker History FXCM Commission - «TICKET»
Rollover - «TICKET»>R/O - 1,000 EUR/USD @0.52 #«ticket» N/A OANDA Balance update
Financing (Swap: One entry for all open orders.)
MT4:
-
Do not assume history has only closed orders.
OrderType() == 6, 7 in the history pool? - MQL4 programming forum #4 and #5 (2017) -
Do not assume history is ordered by date, it's not.
Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020) -
Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
"balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)Broker History FXCM Commission - «TICKET»
Rollover - «TICKET»>R/O - 1,000 EUR/USD @0.52 #«ticket» N/A OANDA Balance update
Financing (Swap: One entry for all open orders.)
3. The code is fine for someone who does not consider commission or swaps, what if someone who reads this are not paying for commission and have a standard account and close before the end of the day? No, just get ahead of yourself bud.
2. This code is intended to get the last recent orders profit, works for me, works perfectly fine actually.
1. History can potentially have opened, closed & probably deleted orders too, I don't know I haven't tested that.
For someone who is looking for something basic like this is better than telling them to pay for a freelancer or copy & paste your 1990s link about how to rtfm. Thanks though.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello guys,
I'm facing an issue with my code: I want to check profit for a closed order, using OrderProfit().
In MT4 app, this trade is like this in history:
In logs here are information coming from my code:
In my code I use OrderSelect() in history with ticket ID (OrderSelect(ID, SELECT_BY_TICKET, MODE_HISTORY)).
Then I check OrderProfit().
Why is it negative while it was closed ith positive profit?
Thanks.