Help please

 

Hi All,

I am studying the MQL4 book. I have 2 questions. 

1) In some of the coding I see the "While (true)" being used. I would have thought that within the brackets the condition  is  stated. I would have thought  "True" of itself is not a condition. 

2) Mostly I see only ( )  sometimes I see ( ) ). What does the bracket in the middle signify.

These questions  probably have very simple answers but I have searched and cannot find an explanation.  

Please accept I am in the early stages of learning programming.

Will appreciate your help.

Regards

Bert5 

 
  1. if( 2+2 == 4 ) becomes if( 4 == 4 ) becomes if( true ). if( 2+2 == 5 ) becomes if( 4 == 5 ) becomes if( false ). While(condition) means while(true). The only differences is while(condition) condition will eventually become false and the loop will terminate, while (true) is an infinite loop and must be terminated by another method (break or return) within the loop.
  2. () are parentheses. {} are curly braces. and [] are square brackets. All three are used in mql4. I don't know what you're questioning.
 
WHRoeder:
  1. if( 2+2 == 4 ) becomes if( 4 == 4 ) becomes if( true ). if( 2+2 == 5 ) becomes if( 4 == 5 ) becomes if( false ). While(condition) means while(true). The only differences is while(condition) condition will eventually become false and the loop will terminate, while (true) is an infinite loop and must be terminated by another method (break or return) within the loop.
  2. () are parentheses. {} are curly braces. and [] are square brackets. All three are used in mql4. I don't know what you're questioning.


Thank you WhRoeder.

Please ignore my 2nd question. The last bracket matched with an earlier bracket in the line.

My 1st question came from reading the code below which is an extract from the book.

 Can you please tell me what does the "while (true )" depend on in this case.  

Regards

Bert5 

 int start() // Special function 'start'

  {
   string Symb=Symbol();                        // Symbol
   double Dist=1000000.0;                       // Presetting
   int Real_Order=-1;                           // No market orders yet
   double Win_Price=WindowPriceOnDropped();     // The script is dropped here
//-------------------------------------------------------------------------------- 2 --
   for(int i=1; i<=OrdersTotal(); i++)          // Order searching cycle
     {
      if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available
        {                                       // Order analysis:
         //----------------------------------------------------------------------- 3 --
         if (OrderSymbol()!= Symb) continue;    // Symbol is not ours
         int Tip=OrderType();                   // Order type
         if (Tip>1) continue;                   // Pending order  
         //----------------------------------------------------------------------- 4 --
         double Price=OrderOpenPrice();         // Order price
         if (NormalizeDouble(MathAbs(Price-Win_Price),Digits)< //Selection
            NormalizeDouble(Dist,Digits))       // of the closest order       
           {
            Dist=MathAbs(Price-Win_Price);      // New value
            Real_Order=Tip;                     // Market order available
            int Ticket=OrderTicket();           // Order ticket
            double Lot=OrderLots();             // Amount of lots
           }
         //----------------------------------------------------------------------- 5 --
        }                                       //End of order analysis
     }                                          //End of order searching
//-------------------------------------------------------------------------------- 6 --
   while(true)                                  // Order closing cycle
     {
      if (Real_Order==-1)                       // If no market orders available
        {
         Alert("For ",Symb," no market orders available");
         break;                                 // Exit closing cycle        
        }

 

You did not post enough of the while loop.
It seems, it is a loop to close orders and as soon as all orders are closed, Real_Order is set to -1. Once in that if condition, it 'breaks' the while(true) loop with break. 
while(true) does run forever until you leave the loop with the break instruction.

 
kronin:

You did not post enough of the while loop.
It seems, it is a loop to close orders and as soon as all orders are closed, Real_Order is set to -1. Once in that if condition, it 'breaks' the while(true) loop with break. 
while(true) does run forever until you leave the loop with the break instruction.


Thank you Kronin,

I am posting the complete code from the book.

I just need to know exactly the condition "while(true)" is referring to.

regards

Bert5

 int start() // Special function 'start'

  {
   string Symb=Symbol();                        // Symbol
   double Dist=1000000.0;                       // Presetting
   int Real_Order=-1;                           // No market orders yet
   double Win_Price=WindowPriceOnDropped();     // The script is dropped here
//-------------------------------------------------------------------------------- 2 --
   for(int i=1; i<=OrdersTotal(); i++)          // Order searching cycle
     {
      if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available
        {                                       // Order analysis:
         //----------------------------------------------------------------------- 3 --
         if (OrderSymbol()!= Symb) continue;    // Symbol is not ours
         int Tip=OrderType();                   // Order type
         if (Tip>1) continue;                   // Pending order  
         //----------------------------------------------------------------------- 4 --
         double Price=OrderOpenPrice();         // Order price
         if (NormalizeDouble(MathAbs(Price-Win_Price),Digits)< //Selection
            NormalizeDouble(Dist,Digits))       // of the closest order       
           {
            Dist=MathAbs(Price-Win_Price);      // New value
            Real_Order=Tip;                     // Market order available
            int Ticket=OrderTicket();           // Order ticket
            double Lot=OrderLots();             // Amount of lots
           }
         //----------------------------------------------------------------------- 5 --
        }                                       //End of order analysis
     }                                          //End of order searching
//-------------------------------------------------------------------------------- 6 --
   while(true)                                  // Order closing cycle
     {
      if (Real_Order==-1)                       // If no market orders available
        {
         Alert("For ",Symb," no market orders available");
         break;                                 // Exit closing cycle        
        }
      //-------------------------------------------------------------------------- 7 --
      switch(Real_Order)                        // By order type
        {
         case 0: double Price_Cls=Bid;          // Order Buy
            string Text="Buy ";                 // Text for Buy
            break;                              // Из switch
         case 1: Price_Cls=Ask;                 // Order Sell
            Text="Sell ";                       // Text for Sell
        }
      Alert("Attempt to close ",Text," ",Ticket,". Awaiting response..");
      bool Ans=OrderClose(Ticket,Lot,Price_Cls,2);// Order closing
      //-------------------------------------------------------------------------- 8 --
      if (Ans==true)                            // Got it! :)
        {
         Alert ("Closed order ",Text," ",Ticket);
         break;                                 // Exit closing cycle
        }
      //-------------------------------------------------------------------------- 9 --
      int Error=GetLastError();                 // Failed :(
      switch(Error)                             // Overcomable errors
        {
         case 135:Alert("The price has changed. Retrying..");
            RefreshRates();                     // Update data
            continue;                           // At the next iteration
         case 136:Alert("No prices. Waiting for a new tick..");
            while(RefreshRates()==false)        // To the new tick
               Sleep(1);                        // Cycle sleep
            continue;                           // At the next iteration
         case 146:Alert("Trading subsystem is busy. Retrying..");
            Sleep(500);                         // Simple solution
            RefreshRates();                     // Update data
            continue;                           // At the next iteration
        }
      switch(Error)                             // Critical errors
        {
         case 2 : Alert("Common error.");
            break;                              // Exit 'switch'
         case 5 : Alert("Old version of the client terminal.");
            break;                              // Exit 'switch'
         case 64: Alert("Account is blocked.");
            break;                              // Exit 'switch'
         case 133:Alert("Trading is prohibited");
            break;                              // Exit 'switch'
         default: Alert("Occurred error ",Error);//Other alternatives   
        }
      break;                                    // Exit closing cycle
     }
//------------------------------------------------------------------------------- 10 --
   Alert ("The script has finished operations -----------------------------");
   return;                                      // Exit start()
  }
 
  {
   string Symb=Symbol();                        // Symbol
   double Dist=1000000.0;                       // Presetting
   int Real_Order=-1;                           // No market orders yet
   double Win_Price=WindowPriceOnDropped();     // The script is dropped here
//-------------------------------------------------------------------------------- 2 --
   for(int i=1; i<=OrdersTotal(); i++)          // Order searching cycle
     {
      if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available
        {                                       // Order analysis:
         //----------------------------------------------------------------------- 3 --
         if (OrderSymbol()!= Symb) continue;    // Symbol is not ours
         int Tip=OrderType();                   // Order type
         if (Tip>1) continue;                   // Pending order  
         //----------------------------------------------------------------------- 4 --
         double Price=OrderOpenPrice();         // Order price
         if (NormalizeDouble(MathAbs(Price-Win_Price),Digits)< //Selection
            NormalizeDouble(Dist,Digits))       // of the closest order       
           {
            Dist=MathAbs(Price-Win_Price);      // New value
            Real_Order=Tip;                     // Market order available
            int Ticket=OrderTicket();           // Order ticket
            double Lot=OrderLots();             // Amount of lots
           }
         //----------------------------------------------------------------------- 5 --
        }                                       //End of order analysis
     }                                          //End of order searching
//-------------------------------------------------------------------------------- 6 --
   while(true)                                  // Order closing cycle
     {
      if (Real_Order==-1)                       // If no market orders available
        {
         Alert("For ",Symb," no market orders available");
         break;                                 // Exit closing cycle        
        }
      //-------------------------------------------------------------------------- 7 --
      switch(Real_Order)                        // By order type
        {
         case 0: double Price_Cls=Bid;          // Order Buy
            string Text="Buy ";                 // Text for Buy
            break;                              // Из switch
         case 1: Price_Cls=Ask;                 // Order Sell
            Text="Sell ";                       // Text for Sell
        }
      Alert("Attempt to close ",Text," ",Ticket,". Awaiting response..");
      bool Ans=OrderClose(Ticket,Lot,Price_Cls,2);// Order closing
      //-------------------------------------------------------------------------- 8 --
      if (Ans==true)                            // Got it! :)
        {
         Alert ("Closed order ",Text," ",Ticket);
         break;                                 // Exit closing cycle
        }
      //-------------------------------------------------------------------------- 9 --
      int Error=GetLastError();                 // Failed :(
      switch(Error)                             // Overcomable errors
        {
         case 135:Alert("The price has changed. Retrying..");
            RefreshRates();                     // Update data
            continue;                           // At the next iteration
         case 136:Alert("No prices. Waiting for a new tick..");
            while(RefreshRates()==false)        // To the new tick
               Sleep(1);                        // Cycle sleep
            continue;                           // At the next iteration
         case 146:Alert("Trading subsystem is busy. Retrying..");
            Sleep(500);                         // Simple solution
            RefreshRates();                     // Update data
            continue;                           // At the next iteration
        }
      switch(Error)                             // Critical errors
        {
         case 2 : Alert("Common error.");
            break;                              // Exit 'switch'
         case 5 : Alert("Old version of the client terminal.");
            break;                              // Exit 'switch'
         case 64: Alert("Account is blocked.");
            break;                              // Exit 'switch'
         case 133:Alert("Trading is prohibited");
            break;                              // Exit 'switch'
         default: Alert("Occurred error ",Error);//Other alternatives   
        }
      break;                                    // Exit closing cycle
     }
//------------------------------------------------------------------------------- 10 --
   Alert ("The script has finished operations -----------------------------");
   return;                                      // Exit start()
  }

before starting the loop we set real order =  -1 

in the loop we look to all orders to find one of the EA

if one found real order becomes > -1

if no order found when loop finished  then it stays -1   and we break our while function 

 
Bert5: I am posting the complete code from the book.

Play video
Please edit your post.
For large amounts of code, attach it.
 
deVries:

before starting the loop we set real order =  -1 

in the loop we look to all orders to find one of the EA

if one found real order becomes > -1

if no order found when loop finished  then it stays -1   and we break our while function 

  

 

 


Thank you deVries

I understand now. You have re shown the code with the functions colored which makes it readable. How did you do that. I copied and pasted and lost the colour. 

I know WHRoeder has said that code must be inserted using the SRC button. This I will do in the future.

It is a question of getting used to the site and how to use it properly.

Thanks again. 

 
WHRoeder:

Play video
Please edit your post.
For large amounts of code, attach it.


WHRoeder,

Sorry for not doing it properly. I will use the SRC button  in the future to add code.

 I understand the "while(true)" now from DeVries  response.

Again thank you for your help. It is very much appreciated.

Bert5 

 

This link Play video   you can see also in the post of WHRoeder you open by clicking on it

It will show you how to post a code using 'SRC' button inside a message

After the code attaching is done we can give some text background color

with using  'ab_pencil' button you can find in the same line as the 'SRC' button

 
deVries:

This link Play video   you can see also in the post of WHRoeder you open by clicking on it

It will show you how to post a code using 'SRC' button inside a message

After the code attaching is done we can give some text background color

with using  'ab_pencil' button you can find in the same line as the 'SRC' button


deVries

Much appreciated.

Thanks. 

Reason: