Incorrect formatting of vaiables in Print() & SendMail() function

 

Hello

I have a snippet of code which should send the Standard Deviation of a currency pair to Print() & SendMail().

Having correctly formatted the figure, having correctly declared the variables, and having included the correct source files at the top of the EA, I fail to see why I get the following result:

2023.05.25 12:26:07.551 experimental EURJPY,H1: The Standard Deviation is 0

The  Standard Deviation is a double variable, which is converted to a string with the %0.0f function.

This is normally the result that is returned when just the variable type is declared, but no clarification is present as to what the variable is supposed to be. I'm an experienced Front Office Developer and I have never had this issue before. 

This is infuriating because this has worked perfectly prior to me transferring this to a separate function, which is then called from the Print() the SendMail() commands.  I do not see any reason as to why this is happening.

I know the number of documents look like overkill but these .mqh documents serve several other documents, so having centralised global declarations is much more efficient. 

I have included the document for you to recreate the problem if you so wish.  

Files:
 
double            standardDeviation;                                    // uninitialized double
int OnInit()
  { 
   string x = standardDev();
⋮
 string standardDev(){
 
   string strStandardDev = StringFormat("The Standard Deviation is %0.0f", standardDeviation); // Using the uninitialized double.

You never call any other code. Variable has garbage.

Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:
  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
 
William Roeder #:

You never call any other code. Variable has garbage.

Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:
  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

Yes, the actual EA does not use any price related or server related functions in OnInit(), this is purely just for test purposes. This showing a 0 result on the OnTick() function once server connection and price data have been established.  

 
William Roeder #:

You never call any other code. Variable has garbage.

Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:
  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

But I do call the code, I called the function via the:\

string x = standardDev();
 
TheHonestPrussian #:

Yes, the actual EA does not use any price related or server related functions in OnInit(), this is purely just for test purposes. This showing a 0 result on the OnTick() function once server connection and price data have been established.  

   			void OnTick(){

			chart_objects(); //source file for standardDeviation & rSquared
   
   
      			if(......){
                                                                                   
                           if(....){
                               
                              if(.....){
                              
                                 if(....){
                                 
                                    if(....){
                                    
                                       if(.... )){
                                       
                                          if(.....){
                                          
                                             if(.....){
                                             
                                                if(rSquared >= minRSquared && rSquared <= maxRSquared){ //rSquared figure is read perfectly
                                                
                                                   if(.....){
                                                   
                                                      if(stringFindJPY  >= 0){ //if the pair has the JPY
                                                         
                                                            if(standardDeviation <= ....&& standardDeviation >= ....){//standardDeviation is not being read.
                                                                                                                        
                                                               automatedSpotShort(); //spot_market_execution.mqh       
                                                               
                                                            }//standardDeviation
                                                            
                                                            else { ....}
                                                      
                                                      } //stringFindJPY 
                                                      
                                                      else { //If the JPY is NOT in the pair.
                                                         
                                                            if(standardDeviation  <= maxStandardDeviationHam && standardDeviation >= minStandardDeviationHam){
   
                                                               automatedSpotShort(); //spot_market_execution.mph
                                                               
                                                                   BarTime=Time[0];
                                                               
                                                            } //standardDeviation
                                                            
                                                            else { 
                                                            
                                                               
... 
                                                            
                                                            }
                                                      
                                                      }
                                                      
                                                   } //OperationMode
                                                   
                                                   else { 
                                                   
                                                      
..... 
                                                    
                                                   }

                                                
                                                } //rSquared
                                                
                                                else {
                                                
                                                   
.... 
        
                                                }
                                                
                                             } //(breakeven * Point >= stopLoss)
                                             
                                             else {
                                             
                                                
.... 
                                             
                                             }           
                                                                                                   
                                          } //(AccountEquity() / marginRequired * 100 >= brokerMarginLevel)
                           
                                          else {
                                          
                                            ....
                                          
                                          }        
                              
                                       }  //(TimeCurrent() < Time[0] + (Period() * 60))
                           
                                       else {
                                       
                                          
.... 
                                       
                                       }          
                                    
                                    } //marginBuffer
                           
                                    else {
                                    
                                       
.... 
                                    
                                    }
                              
                                 } //MaxSpread
                           
                                 else {
                                 
                                    
.... 
                                    
                                 }
                              
                              } //regression
                              
                              else {
                              
                                 
.... 
                              
                              }
                                                                        
                           } // Suspensopn
                           
                           else {
                           
                              
.... 
                                 
                           }
                        
                        } //Opening of Trade
                  
                  } //NewBar
            
               } //Momentum
            
            }//Time
         
         }//OrdersTotal
      
      }//BarTime

}

The source code for the `standardDeviation` % the rSquared is read from the `chart_objects()` function.

Why is the rSquared figure read perfectly, but not the  standardDeviation, despite the presence of the source file. There is no rational explanation for this.