How does this Ordersend(request, result) work without sending it??

 
#define EXPERT_MAGIC 123456   // MagicNumber of the expert
//+------------------------------------------------------------------+
//| Closing all positions                                            |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- declare and initialize the trade request and result of trade request
   MqlTradeRequest request;
   MqlTradeResult  result;
   int total=PositionsTotal(); // number of open positions   

//--- iterate over all open positions
   for(int i=total-1; i>=0; i--)
     {
      //--- parameters of the order
      ulong  position_ticket=PositionGetTicket(i);                                      // ticket of the position
      string position_symbol=PositionGetString(POSITION_SYMBOL);                        // symbol 
      int    digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS);              // number of decimal places
      ulong  magic=PositionGetInteger(POSITION_MAGIC);                                  // MagicNumber of the position
      double volume=PositionGetDouble(POSITION_VOLUME);                                 // volume of the position
      ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);    // type of the position

      //--- output information about the position
      PrintFormat("#%I64u %s  %s  %.2f  %s [%I64d]",
                  position_ticket,
                  position_symbol,
                  EnumToString(type),
                  volume,
                  DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits),
                  magic);

      //--- if the MagicNumber matches
      if(magic==EXPERT_MAGIC)
        {
         //--- zeroing the request and result values
         ZeroMemory(request);
         ZeroMemory(result);
         //--- setting the operation parameters
         request.action   =TRADE_ACTION_DEAL;        // type of trade operation
         request.position =position_ticket;          // ticket of the position
         request.symbol   =position_symbol;          // symbol 
         request.volume   =volume;                   // volume of the position
         request.deviation=5;                        // allowed deviation from the price
         request.magic    =EXPERT_MAGIC;             // MagicNumber of the position
         //--- set the price and order type depending on the position type
         if(type==POSITION_TYPE_BUY)
           {
            request.price=SymbolInfoDouble(position_symbol,SYMBOL_BID);
            request.type =ORDER_TYPE_SELL;
           }
         else
           {
            request.price=SymbolInfoDouble(position_symbol,SYMBOL_ASK);
            request.type =ORDER_TYPE_BUY;
           }
         //--- output information about the closure
         PrintFormat("Close #%I64d %s %s",position_ticket,position_symbol,EnumToString(type));

         //--- send the request
         if(!OrderSend(request,result)) <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<-- HERE
            PrintFormat("OrderSend error %d",GetLastError());  // if unable to send the request, output the error code


         //--- information about the operation   
         PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
         //---
        }
     }
  }
//+------------------------------------------------------------------+


https://www.mql5.com/en/docs/constants/structures/mqltraderequest


hello guys im just trying to learn MQL5 Coding by myself..

but i need some help here..

In this mql reference doesnt use Ordersend(request, result)
but only !Ordersend(request,result)
how does it work?

i mean,

Where does it know TRUE value if Ordersend() is not made? 



and below is the function im trying to make 'Close all position by magic number'.
where should i put  Ordersend() if i have to? or where should i fix more?


void CloseAll(ulong CloseTicket, double CloseLots, double ClosePrice, int Slippage, ulong magic)
{
//--- declare and initialize the trade request and result of trade request
   MqlTradeRequest request;
   MqlTradeResult  result;
   int total=PositionsTotal(); // number of open positions 
   
//--- iterate over all open positions
   for(int i=total-1; i>=0; i--)
     {
      //--- parameters of the order
      ulong  CloseTicket      = PositionGetTicket(i);                                      // ticket of the position
      string position_symbol  = PositionGetString(POSITION_SYMBOL);                        // symbol 
      int    digits           = (int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS);     // number of decimal places
      ulong  magic            = PositionGetInteger(POSITION_MAGIC);                        // MagicNumber of the position
      double CloseLots        = PositionGetDouble(POSITION_VOLUME);                        // volume of the position
      double ClosePrice       = SymbolInfoDouble(_Symbol, SYMBOL_BID)
      int    Slippage         = Slippage
      ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);    // type of the position
      
            //--- if the MagicNumber matches
      if(magic==gMagicNumber)
        {
         //--- zeroing the request and result values
         ZeroMemory(request);
         ZeroMemory(result);
         
         //--- setting the operation parameters
         request.action    = TRADE_ACTION_DEAL;       // type of trade operation
         request.position  = CloseTicket;             // ticket of the position
         request.symbol    = position_symbol;         // symbol 
         request.volume    = CloseLots;               // volume of the position
         request.deviation = Slippage;                // allowed deviation from the price
         request.magic     = magic;                   // MagicNumber of the position
         //--- set the price and order type depending on the position type
         if(type==POSITION_TYPE_BUY)
           {
            request.price=SymbolInfoDouble(position_symbol,SYMBOL_BID);
            request.type =ORDER_TYPE_SELL;
           }
         else
           {
            request.price=SymbolInfoDouble(position_symbol,SYMBOL_ASK);
            request.type =ORDER_TYPE_BUY;
           }
         if(!OrderSend(request,result))
            PrintFormat("OrderSend error %d",GetLastError());
         }
      }
}      



Thank you for helping..!

Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Request Structure
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Request Structure
  • www.mql5.com
Interaction between the client terminal and a trade server for executing the order placing operation is performed by using trade requests. The...
 
Simon Jeon:
Where does it know TRUE value if Ordersend() is not made? 
 if(!OrderSend(request,result)) <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<-- HERE
            PrintFormat("OrderSend error %d",GetLastError()); 

OrderSend function returns a boolean value (true/false) true if the function worked, false if the function failed in some way.

so:    if(OrderSend(request,result))  will execute the if statement if OrderSend returns True

and   if(!OrderSend(request,result))  will execute the if statement if OrderSend returns False

the ! basically means NOT so if (NOT OrderSend())   means if it doesn't work and returns False.

you could rewrite it to this

if(OrderSend(request,result))
{
   // do stuff here if OrderSend returns True
}
else
{
   // do stuff here if false
}

but if you are only interested in capturing the False (and therefore an error) then it is shorter to use !OrderSend

Note: the true/false refers to the function execution and not the result of the trade request.
 
Paul Anscombe #:

OrderSend function returns a boolean value (true/false) true if the function worked, false if the function failed in some way.

so:    if(OrderSend(request,result))  will execute the if statement if OrderSend returns True

and   if(!OrderSend(request,result))  will execute the if statement if OrderSend returns False

the ! basically means NOT so if (NOT OrderSend())   means if it doesn't work and returns False.

you could rewrite it to this

but if you are only interested in capturing the False (and therefore an error) then it is shorter to use !OrderSend

Note: the true/false refers to the function execution and not the result of the trade request.


right.. so i understand now this Ordersend() returns true or false valuse,

but how do i make an order?? i thoguht this mqlrequest thing was going to make an order..?

this request. request. things are not just making list to make an order??

but how do i EXECUTE and SEND the order??

i want to let the program ORDER THE ORDER but i complied success and backtested it wont order..

 
Simon Jeon #:


right.. so i understand now this Ordersend() returns true or false valuse,

but how do i make an order?? i thoguht this mqlrequest thing was going to make an order..?

this request. request. things are not just making list to make an order??

but how do i EXECUTE and SEND the order??

i want to let the program ORDER THE ORDER but i complied success and backtested it wont order..

It will make the order based upon the request and return the output into the result structure

You should perhaps look at some articles on how to code the trade operations, also the documentation and you could also look at the CTrade class


 
Paul Anscombe #:
It will make the order based upon the request and return the output into the result structure

You should perhaps look at some articles on how to code the trade operations, also the documentation and you could also look at the CTrade class


ok thank you!
i understood this part but i will study more and look what u told me