How do I open a market order correctly? - page 5

 
Gennady Mazur:

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?

Additionally, you can check the opening of a position by trying to select it. And it is better to try several times.

         int n = 0;
          do
           {
            n++;
           }
          while(!PositionSelectByTicket(tradeResult.deal) && n < 5);
There is both a delay and confirmation that the position is already in place.

I also noticed that there is no check OrderCheck(Request, checkResult), which is bad.
 

Oh, and also in market order - price = 0

Request.price=SymbolInfoDouble(_Symbol,SYMBOL_BID); forget it

bool SendOrderSyncMode()
  {
   MqlTradeRequest request={0};
   MqlTradeResult  result={0};
   order_ticket=0;
   request.action = TRADE_ACTION_DEAL;
   request.magic  = 9876543211;
   request.symbol = Symbol();
   request.volume = 1;
   request.price  = 0;
   request.type=ORDER_TYPE_BUY;
   request.comment="Sync mode";
   request.type_filling=ORDER_FILLING_FOK;
   request.type_time=ORDER_TIME_DAY;
   if(OrderSend(request,result))
     {
      if((result.retcode==TRADE_RETCODE_DONE) || (result.retcode==TRADE_RETCODE_PLACED))
        {
         if(result.order>0)
           {
            order_ticket=result.order;
            Print(__FUNCTION__," Order sent in sync mode");
            return(true);
           }
         else
           {
            Print(__FUNCTION__," Error order sent in sync mode! Retcode = ",result.retcode);
           }
        }
      else
        {
         Print(__FUNCTION__," Error order sent in sync mode! Retcode = ",result.retcode);
        }
     }
   else
     {
      Print(__FUNCTION__," Order not sent in sync mode.");
     }
   return(false);
  }
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
   switch(trans.type)
     {
      case TRADE_TRANSACTION_HISTORY_ADD:
        if((order_ticket > 0) && (trans.order == order_ticket))
      {
        //Вот здесь и смотрим что произошло
      }
      break;
     }
  }
 
Alexey Viktorov:
Additionally, it is possible to check the position opening by trying to select it. And it is better to try several times

         int n = 0;
          do
           {
            n++;
           }
          while(!PositionSelectByTicket(tradeResult.deal) && n < 5);
There is both delay and confirmation that the position has already been opened.

I also noticed that there is no check OrderCheck(Request, checkResult), which is bad.

What if the position already existed?

Or if there is already a position but the order is not filled completely?

 
Alexey Viktorov:
Additionally, one can check the position opening by trying to select it. And several attempts are better.

         int n = 0;
          do
           {
            n++;
           }
          while(!PositionSelectByTicket(tradeResult.deal) && n < 5);
There is both delay and confirmation that the position is already in place.

Also I noticed that there is no check OrderCheck(Request, checkResult) and this is bad.

Well, maybe I will insertOrderCheck(Request, checkResult)
Thank you

But I don't particularly like such a loop

 
prostotrader:

Oh, and also in market order - price = 0

Request.price=SymbolInfoDouble(_Symbol,SYMBOL_BID); forget it

bool SendOrderSyncMode()
  {
   MqlTradeRequest request={0};
   MqlTradeResult  result={0};
   order_ticket=0;
   request.action = TRADE_ACTION_DEAL;
   request.magic  = 9876543211;
   request.symbol = Symbol();
   request.volume = 1;
   request.price  = 0;
   request.type=ORDER_TYPE_BUY;
   request.comment="Sync mode";
   request.type_filling=ORDER_FILLING_FOK;
   request.type_time=ORDER_TIME_DAY;
   if(OrderSend(request,result))
     {
      if((result.retcode==TRADE_RETCODE_DONE) || (result.retcode==TRADE_RETCODE_PLACED))
        {
         if(result.order>0)
           {
            order_ticket=result.order;
            Print(__FUNCTION__," Order sent in sync mode");
            return(true);
           }
         else
           {
            Print(__FUNCTION__," Error order sent in sync mode! Retcode = ",result.retcode);
           }
        }
      else
        {
         Print(__FUNCTION__," Error order sent in sync mode! Retcode = ",result.retcode);
        }
     }
   else
     {
      Print(__FUNCTION__," Order not sent in sync mode.");
     }
   return(false);
  }
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
   switch(trans.type)
     {
      case TRADE_TRANSACTION_HISTORY_ADD:
        if((order_ticket > 0) && (trans.order == order_ticket))
      {
        //Вот здесь и смотрим что произошло
      }
      break;
     }
  }
Thanks so much for such a detailed description...and explanation....
And about the price.... yes, more used to MT4, and there you always have to put the price...
 
Sergey Chalyshev:

Repeat

Apart from that, you should also check the status of the order and whether it has already appeared in the history or not.

The exchange does not work with positions, only orders.

Really?
 
Gennady Mazur:
You would be right if I were to place pending orders, but I work by market, and here the ticket is obtained withResults.deal
Still, the order is sent first, then it is executed.
 
Sergey Chalyshev:

I don't understand what your smile means?

I didn't expect to hear you say that. What pauses, or are you joking?

Pause after OrderSend() so that market information and history can be updated.
 
Sergey Chalyshev:

Bad forex legacy.

...

You might want to be careful how you phrase it. Otherwise we might as well talk about the genetic mutation of the adherents of FOORTS.
 
Sergey Chalyshev:

What if the position already existed?

Or is there already a position but the order is not filled completely?

Yeah. And I'm all about my hedge. I admit I wasn't quite right. Or totally wrong. :-)
Reason: