How do I open a market order correctly?

 

I recently raised a topic on reopening orders. Thought I had solved the problem, as everything was working very smoothly for a week. But today I have encountered a similar problem only when opening.
A lot of orders opened on Finam, and 9 on Otkritie...although only one order should have opened in all cases.

Here is a code snippet from the EA

    if(OpenOrders<1)
    {
        Coment="Open Sell "+string(OpenOrders+1);
        ret=OpenSellPosition(_Symbol,volume,Coment,Sleeppage,Filling);
    }
    
    
  if(ret)
  {
    OpenOrders++;
    PriceLastOpen=price;
  }

bool OpenSellPosition(string symbol, double volume, string comment="", ulong deviation=10, ENUM_ORDER_TYPE_FILLING filling=ORDER_FILLING_FOK)
{
  MqlTradeRequest Request;
  MqlTradeResult Results;
  ZeroMemory(Request);
  ZeroMemory(Results);
  Request.price=SymbolInfoDouble(_Symbol,SYMBOL_BID);
  Request.action=TRADE_ACTION_DEAL;
  Request.type=ORDER_TYPE_SELL;
  Request.symbol=symbol;
  Request.volume=volume;      
  Request.deviation=deviation;
  Request.comment=comment;  
  Request.type_filling=filling;
  bool res=false;
  res=OrderSend(Request,Results);
  if(res)
  {
    if(Results.deal>0) return(true);
    else return(false);
  }
  return(false);
}

i.e. we can see from the code that if the operation is successful, the variableOpenOrders increases, which initially equals 0

If it is higher than 0, there should be no further opening of the order, but the entire pile of orders is opened with the comment Order1.

I check if there is a positive response in the function opening the order and if the order ticket has been received, but for some reason this function returns false, even though the order is actually set.

Explain what is wrong, how to solve this problem?

 
I assume that the information about the deal has not yet arrived. Here (highlighted in red) you are relying on luck. And she is a capricious lady :-))

bool OpenSellPosition(string symbol, double volume, string comment="", ulong deviation=10, ENUM_ORDER_TYPE_FILLING filling=ORDER_FILLING_FOK)
{
  MqlTradeRequest Request;
  MqlTradeResult Results;
  ZeroMemory(Request);
  ZeroMemory(Results);
  Request.price=SymbolInfoDouble(_Symbol,SYMBOL_BID);
  Request.action=TRADE_ACTION_DEAL;
  Request.type=ORDER_TYPE_SELL;
  Request.symbol=symbol;
  Request.volume=volume;      
  Request.deviation=deviation;
  Request.comment=comment;  
  Request.type_filling=filling;
  bool res=false;
  res=OrderSend(Request,Results);
  if(res)
  {
    if(Results.deal>0) return(true);
    else return(false);

  }
  return(false);
}
 
Gennady Mazur:

I recently raised the issue of re-opening orders. I thought I had solved the problem because everything was working very well for a week. But today I have encountered a similar problem only with opening.
A very large number of orders opened on Finam, and 9 on Otkritie... although in all cases only one order should have opened.

Here is a code snippet from the EA

    if(OpenOrders<1)
    {
        Coment="Open Sell "+string(OpenOrders+1);
        ret=OpenSellPosition(_Symbol,volume,Coment,Sleeppage,Filling);
    }
    
    
  if(ret)
  {
    OpenOrders++;
    PriceLastOpen=price;
  }

bool OpenSellPosition(string symbol, double volume, string comment="", ulong deviation=10, ENUM_ORDER_TYPE_FILLING filling=ORDER_FILLING_FOK)
{
  MqlTradeRequest Request;
  MqlTradeResult Results;
  ZeroMemory(Request);
  ZeroMemory(Results);
  Request.price=SymbolInfoDouble(_Symbol,SYMBOL_BID);
  Request.action=TRADE_ACTION_DEAL;
  Request.type=ORDER_TYPE_SELL;
  Request.symbol=symbol;
  Request.volume=volume;      
  Request.deviation=deviation;
  Request.comment=comment;  
  Request.type_filling=filling;
  bool res=false;
  res=OrderSend(Request,Results);
  if(res)
  {
    if(Results.deal>0) return(true);
    else return(false);
  }
  return(false);
}

i.e. we can see from the code that if the operation is successful, the variableOpenOrders increases, which initially equals 0

If it is higher than 0, there should be no further opening of the order, but the entire pile of orders is opened with the comment Order1.

I check if there is a positive response in the function opening the order and if the order ticket has been received, but for some reason this function returns false, even though the order is actually set.

Can you explain what is wrong and how to fix this problem?

Bad forex legacy.

You should checkResults.order.

If the order is placed, it does not mean that the deal has already been done.

 
Sergey Chalyshev:

Bad forex legacy.

We have to check it out.order.

If an order is placed, it does not mean that a trade has already been executed.

You would be right if I were placing pending orders, but I work by market and here the ticket is obtained withResults.deal
 
Dennis Kirichenko:
I assume that the information about the deal has not yet arrived. Here (highlighted in red) you are relying on luck. And she is a capricious lady :-))

bool OpenSellPosition(string symbol, double volume, string comment="", ulong deviation=10, ENUM_ORDER_TYPE_FILLING filling=ORDER_FILLING_FOK)
{
  MqlTradeRequest Request;
  MqlTradeResult Results;
  ZeroMemory(Request);
  ZeroMemory(Results);
  Request.price=SymbolInfoDouble(_Symbol,SYMBOL_BID);
  Request.action=TRADE_ACTION_DEAL;
  Request.type=ORDER_TYPE_SELL;
  Request.symbol=symbol;
  Request.volume=volume;      
  Request.deviation=deviation;
  Request.comment=comment;  
  Request.type_filling=filling;
  bool res=false;
  res=OrderSend(Request,Results);
  if(res)
  {
    if(Results.deal>0) return(true);
    else return(false);

  }
  return(false);
}
No, this result is present, this is not a ticket order on the server, this is a temporary ticket operation, which will later be replaced by a ticket position
 
Dennis Kirichenko:
I assume that the information about the deal has not yet arrived. Here (highlighted in red) you are relying on luck. And she is a capricious lady :-))

bool OpenSellPosition(string symbol, double volume, string comment="", ulong deviation=10, ENUM_ORDER_TYPE_FILLING filling=ORDER_FILLING_FOK)
{
  MqlTradeRequest Request;
  MqlTradeResult Results;
  ZeroMemory(Request);
  ZeroMemory(Results);
  Request.price=SymbolInfoDouble(_Symbol,SYMBOL_BID);
  Request.action=TRADE_ACTION_DEAL;
  Request.type=ORDER_TYPE_SELL;
  Request.symbol=symbol;
  Request.volume=volume;      
  Request.deviation=deviation;
  Request.comment=comment;  
  Request.type_filling=filling;
  bool res=false;
  res=OrderSend(Request,Results);
  if(res)
  {
    if(Results.deal>0) return(true);
    else return(false);

  }
  return(false);
}
And I was assured in a previous thread, the guru of this forum, that this info always comes ... almost instantly ... and if the server responded positively the ticket is not issued, the order has failed
 

You need to log all trades. For example, like this:

first in the "header" we connect the CTrade trade class

#include <Trade\Trade.mqh>
CTrade         m_trade;                      // trading object

and here is an example of Buy operation:

   if(m_trade.Buy(lots,NULL,m_symbol.Ask(),sl,tp))
     {
      if(m_trade.ResultDeal()==0)
         Print("Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),
               ", description of result: ",m_trade.ResultRetcodeDescription());

      else
         Print("Buy -> true. Result Retcode: ",m_trade.ResultRetcode(),
               ", description of result: ",m_trade.ResultRetcodeDescription());
     }
   else
      Print("Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),
            ", description of result: ",m_trade.ResultRetcodeDescription());

so you can get two errors: first level - operation didn't pass basic check and second level - check of trade's ticket.

Then, if there is a problem, you can use the logs to figure it out.

Added: the above is true for the SYNC mode of operation.

 

Now the gurus here will teach you too ))

Объясните что не так, как решить данную проблему? 

With your experience, it's unseemly to ask such questions here.

 
Vladimir Karputov:

You need to log all trades. For example, like this:

first in the "header" we connect the CTrade trade class

#include <Trade\Trade.mqh>
CTrade         m_trade;                      // trading object

and here is an example of Buy operation:

   if(m_trade.Buy(lots,NULL,m_symbol.Ask(),sl,tp))
     {
      if(m_trade.ResultDeal()==0)
         Print("Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),
               ", description of result: ",m_trade.ResultRetcodeDescription());

      else
         Print("Buy -> true. Result Retcode: ",m_trade.ResultRetcode(),
               ", description of result: ",m_trade.ResultRetcodeDescription());
     }
   else
      Print("Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),
            ", description of result: ",m_trade.ResultRetcodeDescription());

so you can get two errors: first level - operation didn't pass basic check and second level - check of trade's ticket.

Then, if there is a problem, you can use the logs to figure it out.

Added: this is true for the SYNC mode of operation.

That's exactly how I did it.

  bool res=false;
  res=OrderSend(Request,Results);
  if(res)
  {
    if(Results.deal>0) return(true);
    else return(false);
  }
  return(false);
only with my own code
 
Vladimir Karputov:

You need to log all trades. For example, like this:

first in the "header" we connect the CTrade trade class

#include <Trade\Trade.mqh>
CTrade         m_trade;                      // trading object

and here is an example of Buy operation:

   if(m_trade.Buy(lots,NULL,m_symbol.Ask(),sl,tp))
     {
      if(m_trade.ResultDeal()==0)
         Print("Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),
               ", description of result: ",m_trade.ResultRetcodeDescription());

      else
         Print("Buy -> true. Result Retcode: ",m_trade.ResultRetcode(),
               ", description of result: ",m_trade.ResultRetcodeDescription());
     }
   else
      Print("Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),
            ", description of result: ",m_trade.ResultRetcodeDescription());

so you can get two errors: first level - operation didn't pass basic check and second level - check of trade's ticket.

Then, if there is a problem, you can use the logs to figure it out.

Added: this is true for the SYNC mode of operation.

Oh and another thing, does the standard function support execution of orders, their filing, because I used to use SeaTrade for opening, until I got to an exchange where orders did not want to be executed as it was impossible to determine the filing
 
Gennady Mazur:
That's exactly what I've done.

  bool res=false;
  res=OrderSend(Request,Results);
  if(res)
  {
    if(Results.deal>0) return(true);
    else return(false);
  }
  return(false);
but with my own code.
And the logging? You have to output both the successful completion of the operation and, especially, the failure with the error code.
Reason: