PositionsTotal() return 0.
Actually I have different result. For some broker accounts it returns 1 and other broker accounts it return 0.
After sending the order you receive a market order (OrdersTotal() > 0) with ORDER_STATE_STARTED and OrderGetInteger(ORDER_POSITION_ID) == 0.
Details are described here in Russian.
Look at the description for this EA.
//+------------------------------------------------------------------+ //| Возвращает "неопределённое" состояние торгового окружения | //+------------------------------------------------------------------+ bool IsUncertainStateEnv(const string symbol_name,const ulong magic_number) { if(MQLInfoInteger(MQL_TESTER)) return false; int total=OrdersTotal(); for(int i=total-1; i>WRONG_VALUE; i--) { if(OrderGetTicket(i)==0) continue; if(OrderGetInteger(ORDER_TYPE)>ORDER_TYPE_SELL) continue; if(OrderGetInteger(ORDER_MAGIC)!=magic_number) continue; if(!OrderGetInteger(ORDER_POSITION_ID) && OrderGetString(ORDER_SYMBOL)==symbol_name) return true; } return false; }
I myself write like this (works correctly)
#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006 if(OrderSend(_Symbol,OP_BUY,size,openPrice,slippage,0,0,RobotName+" "+timeframeToString(_Period)) == -1) { lastErrorMessage="Position open failed. Return code="+IntegerToString(MT4ORDERS::LastTradeResult.retcode); lastDateErrorMessage=TimeCurrent(); Print("code retour du trade:",MT4ORDERS::LastTradeResult.retcode); return -1; } else { Print("PositionsTotal:",PositionsTotal()); }
Hi,
** When a trading order is sent to a server, an occasional delay in execution may lead to incorrectly countinging the number of market positions. If such an "undefined" state is detected, the Expert Advisor waits for the specified number of seconds and then reads the environment again.
Which function are you using to check if the order is executed ?
Should I use the function ResultDeal ?
If ResultDeal is 0 then does it means the order is not yet executed ?
Regards,
Dorian
I'm using a complex solution. You can see it in the source code of MT4Orders.
Yes, that's right.
Ok thanks for your help. As you said your solution seems to be complex for what I need.
I have also checked the code from the EA: https://www.mql5.com/en/code/20497
I do not think this piece of code will work :
//+------------------------------------------------------------------+ //| Возвращает "неопределённое" состояние торгового окружения | //+------------------------------------------------------------------+ bool IsUncertainStateEnv(const string symbol_name,const ulong magic_number) { if(MQLInfoInteger(MQL_TESTER)) return false; int total=OrdersTotal(); for(int i=total-1; i>WRONG_VALUE; i--) { if(OrderGetTicket(i)==0) continue; if(OrderGetInteger(ORDER_TYPE)>ORDER_TYPE_SELL) continue; if(OrderGetInteger(ORDER_MAGIC)!=magic_number) continue; if(!OrderGetInteger(ORDER_POSITION_ID) && OrderGetString(ORDER_SYMBOL)==symbol_name) return true; } return false; }
since after I call PositionOpen with ORDER_TYPE_BUY the function OrderTotal returns 0 since it has not opened a pending order.
after I call PositionOpen with ORDER_TYPE_BUY the function OrderTotal returns 0 since it has not opened a pending order.
ServerName?
ServerName?
ICMarkets-MT5
When a trading order is sent to a server, an occasional delay in execution may lead to incorrectly countinging the number of market positions.
Which function should I use to check if an order (not pending) is excuted on the server side ?
After making some test on ResultDeal I notice that it always return 0 after the execution order.
So my question is which function should I use to test if the order has been executed.
You must wait until Result.deal != 0
static bool HistoryDealSelect( MqlTradeResult &Result ) { // Заменить HistorySelectByPosition на HistorySelect(PosTime, PosTime) if (!Result.deal && Result.order && ::HistorySelectByPosition(::HistoryOrderGetInteger(Result.order, ORDER_POSITION_ID))) for (int i = ::HistoryDealsTotal() - 1; i >= 0; i--) { const ulong DealTicket = ::HistoryDealGetTicket(i); if (Result.order == ::HistoryDealGetInteger(DealTicket, DEAL_ORDER)) { Result.deal = DealTicket; break; } } return(::HistoryDealSelect(Result.deal)); }
There are many pitfalls in the platform. This one is the simplest. So I do not use a standard library and I use my own (MQL4-style).
ICMarkets-MT5
It's Live. Try this Demo for Result.deal == 0 - ForexTimeFXTM-Demo01 (or FXOpen-MT5).
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
After running the following code block:
PositionsTotal() return 0.
Actually I have different result. For some broker accounts it returns 1 and other broker accounts it return 0.
What is the root cause of this behavior and how to get right number of open orders.
Note I am using only hedging account.
Regards,
Dorian