How to work correctly in MT5 with OrderSend - page 2

 
fxsaber:
This is already a terminology argument. The bad situation has been described in detail above. Standard libraries don't even take that into account.

No.

But keep persisting. I won't explain it any further.

 
Renat Fatkhullin:

No.

But keep persisting. I won't explain it any further.

People write about it on the forum. And more than once. The reaction is just that.

 
fxsaber:

People write about it on the forum. And more than once. The reaction is just like this.

People don't want to think for themselves, but want others to do it for them... Unfortunately this is the case.
 
prostotrader:

Do you want me to help you bypass these mishaps (from personal experience of trading with MT5 robots on a real account)?

Added

Absolutely free :)

Add your own to the piggy bank as well.

Forum on trading, automated trading systems and strategy testing

Waiting for server response after sending OrderSend()

Denis Sartakov, 2016.10.08 15:43

The function is synchronous, just the history is not updated synchronously with the completion of this function,

I'm doing it this way, I think it's easier than tinkering with OnTrade(), and other stuff:

bool ClosePosition()
{
   if (!PositionSelect(_Symbol))
   {
      return(false);
   }

   bool   b_Result;
   
   b_Result = m_trade.PositionClose(_Symbol,mi_Deviation);
   
   if(!b_Result)
   {
      Print("The PositionClose() method has failed. Return code=",m_trade.ResultRetcode(),
            ". Code description: ",m_trade.ResultRetcodeDescription());
      return(false);
   }
   
   if(b_Result)
   {
      if (m_trade.ResultRetcode() == TRADE_RETCODE_DONE)
      {
         while(PositionSelect(_Symbol))
         {
         }
         
         Print("The PositionClose() has been successfully executed. Return code=",m_trade.ResultRetcode(),
               " (",m_trade.ResultRetcodeDescription(),")");
         return(true);
      }
      if (m_trade.ResultRetcode() != TRADE_RETCODE_DONE)
      {
         Print("The PositionClose() failed. Return code=",m_trade.ResultRetcode(),
               " (",m_trade.ResultRetcodeDescription(),")");
         return(false);
      }
   }
   return(false);
}      
//+------------------------------------------------------------------+

 
prostotrader:
People don't want to think for themselves, but want others to do it for them... unfortunately this is the case.

People not only think but also share their solutions. The developers are silent on this, unfortunately.

Not a single example in the Documentation on this topic. And this is with the main function.

 
fxsaber:

People not only think but also share their solutions. The developers are silent on this, unfortunately.

Not a single example in the Documentation on this topic. And that's with the main function.

The example you gave above is, excuse me, complete nonsense.

That's not the place to start!

 
prostotrader:

The example you gave above is, excuse me, complete nonsense.

That's not the place to start!

My variant

Forum on trading, automated trading systems and strategy testing

Bugs, bugs, questions

fxsaber, 2016.10.09 09:39

If an OrderSend is sent, you have to REMEMBER (this is where the potential vulnerability is) that it is done. Then disregard the current history (trading environment) until the appropriate OnTrade message arrives. Once it arrives, REMEMBER to FORGET.

When there is no REMEMBER, you can trust the history as in MT4.


 
//+------------------------------------------------------------------+
//|                                                    OrderSend.mq5 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//
bool is_busy;
ulong order_ticket;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   is_busy=false; 
   order_ticket=0;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(!is_busy && (order_ticket==0)) //Только при выполнении данного условия мы продолжаем работать(кроме отложенного ордера)
   {
     MqlTradeRequest request={0};
     MqlTradeResult  result={0};
     
     if(PositionSelect(Symbol()))
     {
       if(OrderSend(request, result)
       {
         if(result.retcode==TRADE_RETCODE_PLACED) //для биржи TRADE_RETCODE_PLACED
         {
           if(result.order>0)
           {
             is_busy=true;
             order_ticket=result.order;
             Print("Order plased, ticket = ",result.order);
           }
           else
           {
             Print("Internal error!");
           }  
         }
         else
         {
           //смотрим причину result.retcode
         }
       }
       else
       {
         Print("Order NOT send!");
       }
     }
     else
     {
     }
   }   
  }
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
  {
//--- В зависимости от того, квкой тип ордера мы устанавливали, смотрим, что с ордером произошло
// и только после полного понимания снимаем флаг is_busy=false; order_ticket=0;
// вот и все премудрости OrderSend
   
  }
//+------------------------------------------------------------------+

Take advantage.

 
prostotrader:

Use it.

So how is it different from what I wrote above? A good man even sent me a serious bible on the subject.

People are finding solutions. It was about the fact that the developers themselves don't even have it in the SB.

 
fxsaber:

So how is this different from what I wrote above? I even had a good man drop me a serious bible on the subject.

People are finding solutions. It was about the fact that the developers themselves don't even have it in the SB.

If you don't like it, don't use it.
Reason: