Questions from Beginners MQL5 MT5 MetaTrader 5 - page 404

 

I started to write a wrapper for functions of opening and modification of orders (I thought it would fix my problems with return of values from these operators) but there is the same problem.

Explain what the problem is (no matter what I write, there is always a problem with return of values from OrderSend, OrderSelex and ModifyOrder)

Wrapper code .

int start()

{

if (signal == 1)

{

OrderSendX(Symbol(),OP_BUY,0.1,Ask,0,500,500,"123",123,0,Red);

}

return(0);

}

//-----------------------------------------------

int OrderSendX (string symbol,int cmd, double volume,double price,int slippage,double stoploss,double takeprofit,string comment,

int magic,datetime expiration,color arrow_color)

{

int err=GetLastError();

err = 0;

bool exit_loop = false;

int ticket = -1;

int Retry = 10;

int cnt = 0;

if(cmd == OP_BUY || cmd == OP_SELL) //check the type of order to open

{

while(!exit_loop)

{

ticket = OrderSend(symbol,cmd,volume,price,slippage,stoploss,takeprofit,comment,magic,expiration,arrow_color);

err = GetLastError();

switch(err)

{

case ERR_NO_ERROR:

exit_loop = true;

break;

{ case ERR_SERVER_BUSY:

case ERR_NO_CONNECTION:

case ERR_INVALID_PRICE:

case ERR_BROKER_BUSY:

case ERR_TRADE_CONTEXT_BUSY:

cnt++;

break;

case ERR_PRICE_CHANGED:

case ERR_OFF_QUOTES:

case ERR_REQUOTE:

RefreshRates();

continue;

default:

exit_loop = true;

break;

}

if(cnt>Retry)

exit_loop = true;

if(!exit_loop)

{

Sleep(1000);

RefreshRates();

}

else

{

if(err !=ERR_NO_ERROR)

{

Print("Error : " + err);

}

}

if(err ==ERR_NO_ERROR)

{

OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES);

return(ticket);

}

Print("Error opening order after" + cnt + "attempts");

return(-1);

}

}

}

 
Leanid Aladzyeu:

I started to write a wrapper for functions of opening and modification of orders (I thought it would fix my problems with return of values from these operators) but there is the same problem.

Explain to me what is the problem (no matter what I wrote, I always have problems with return of values from OrderSend, OrderSelex and ModifyOrder)

https://docs.mql4.com/ru/trading/ordermodify

bool res=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TrailingStop,Digits),OrderTakeProfit(),0,Blue);
            if(!res)
               Print("Ошибка модификации ордера. Код ошибки=",GetLastError());
            else
               Print("Цена Stop Loss ордера успешно модифицирована.");

OrderModify() returns yes , no

OrderModify - Документация на MQL4
  • docs.mql4.com
OrderModify - Документация на MQL4
 

No one can explain.

Let's try another example instead of copying the help 100 times to me.

How should it be correct? I will use an example to understand the corrections

{

SL=NormalizeDouble(Bid-TrailingStop*Point,Digits);

if(OrderStopLoss()!=SL)

OrderModify(OrderTicket(),OrderOpenPrice(),SL,0,0);

}

do not insert

"""ticket """= OrderModify(OrderTicket(),OrderOpenPrice(),SL,0,0); every time is correct?

 

And thank you to everyone who tried to explain .

Unfortunately I don't understand how to designate a variable to a variable that is predefined as a function.

 
Leanid Aladzyeu:

And thank you to everyone who tried to explain .

Unfortunately I don't understand how to designate a variable to a variable that is predefined as a function.

DearLeanid. Learn to read the help and work with errors and warnings. If you don't learn how to do it, you won't even program properly. These are the basics. Have you tried to translate what is written in warnings? Do you understand the meaning of functions which "need to be checked"? Help describes a correct example of handling these functions. For instance, OrderModify(). Look at the help:

Return value

It returns true if the function completes successfully or false in case of an error. To get information aboutthe error, you need to callGetLastError() function.

and then follows an example:

bool res=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TrailingStop,Digits),OrderTakeProfit(),0,Blue);
if(!res)
   Print("Ошибка модификации ордера. Код ошибки=",GetLastError());
else
   Print("Цена Stop Loss ордера успешно модифицирована.");

As you can see, the return value of the function is handled in the if-else operator. The res variable is needed "for beauty" for a reason. It is the result of executing a trading function! The function upon which your money depends! Accordingly, to process all possible variants correctly (the processing in this example is not provided), we need the res variable and the error code returned by the GetLastError() function. The developers intentionally included warnings as a reminder not to forget that there are "important" functions to pay attention to processing values! Also note that if the value is not checked there will be no error! I.e., in this case, the function will work as it is, but if an abnormal situation occurs, it will not be processed. The list of non-standard situations are return codes of the GetLastError() function.

I hope it is clearer this way.

Коды возврата торгового сервера - Документация на MQL4
  • docs.mql4.com
Коды возврата торгового сервера - Документация на MQL4
 
Leanid Aladzyeu:

Started to write a wrapper for the order opening and modification functions (thought it would fix my problem with returning values from these operators) but there's the same problem.

And you don't need to write a wrapper in this case. It won't solve problems with warning about checking return value! Look at the examples of these functions and do the same!
 
Tapochun:
And in this case we don't need to write a wrapper. It won't solve the problem with the warning about checking the return value! Have a look at the examples of these functions and do the same!

As indicated in the reference gives an error.

I wrote it this way and it's fine

if(OrderModify(OrderTicket(),OrderOpenPrice(),SL,0,0))

{

Print("Modification was successful.");

}

else Print("Error modifying the order.");

 
Insert code correctly in posts:Insert code correctly in the forum
 
Karputov Vladimir:
Insert code correctly in posts:Insert code correctly in the forum
here you go...
 
Leanid Aladzyeu:

Why doesn't Trawling work for selling? It works for buying!

No errors or warnings , on genetic compilation.

Here is the code:

void Trailing()
  {
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
           {
            if(OrderType()==OP_BUY)
              {
               if(Bid-OrderOpenPrice()>TrailingStop*Point)
                 {
                  if(OrderStopLoss()<Bid-(TrailingStop+TrailingStep)*Point)
                    {
                     SL=NormalizeDouble(Bid-TrailingStop*Point,Digits);
                     if(OrderStopLoss()!=SL)
                        if(OrderModify(OrderTicket(),OrderOpenPrice(),SL,0,0))
                          {
                           Print("Модификация прошла успешно.");
                          }
                     else Print("Ошибка модификации ордера.");
                    }
                 }
              }
            if(OrderType()==OP_SELL)
              {
               if(OrderOpenPrice()-Ask>TrailingStop*Point)
                 {
                  if(OrderStopLoss()>Ask+(TrailingStop+TrailingStep)*Point)
                    {
                     SL=NormalizeDouble(Ask+TrailingStop*Point,Digits);
                     if(OrderStopLoss()!=SL)
                        if(OrderModify(OrderTicket(),OrderOpenPrice(),SL,0,0))
                          {
                           Print("Модификация прошла успешно.");
                          }
                     else Print("Ошибка модификации ордера.");
                    }
                 }
              }
           }
        }
     }
  }

Reason: