Global variable issue

 
double ffUpperChannelValue;
double ffLowerChannelValue;


int start(){
   
  
   //Controls number of Bars
   if(Bars < wideChannel) 
      return;
    
   if(barHasChanged()) 
      setValuesOnBarChange();  //set values for global variables 
       
/*      
   //EXIT
   if(OrdersTotal()> 0)
      checkForExitBo();
*/   
   //ENTRY
   if(OrdersTotal() == 0){
   
      //Finds out if there has been a bo and in which direction
      if(boHit()){
         //Finds out if it is a valid trading signal
         Print("TEMP: ","start ",ffUpperChannelValue," ",ffLowerChannelValue); 
         if(validTradingSignal()){
            //enterTrade();   
         }//end valid signal logic
      }
      //end bo logic
   }  
  
   
   return(0);
}


void setValuesOnBarChange(){

   ffUpperChannelValue = iHigh(NULL, 0, iHighest(NULL, 0, MODE_HIGH, wideChannel, 1));
   ffLowerChannelValue = iLow(NULL, 0, iLowest(NULL, 0, MODE_LOW, wideChannel, 1));
   twentyUpperChannelValue = iHigh(NULL, 0, iHighest(NULL, 0, MODE_HIGH, standardChannel, 1));
   twentyLowerChannelValue = iLow(NULL, 0, iLowest(NULL, 0, MODE_LOW, standardChannel, 1));
   
   //Print("TEMP: ","setChannels",ffUpperChannelValue," ",ffLowerChannelValue);
}

bool validTradingSignal(){
   
   Print("TEMP: ","validTradingSignal");  
   Print("TEMP: ","func ",ffUpperChannelValue," ",ffLowerChannelValue);  
   
      
   double currentRiskReward = getCurrentRiskReward();

   return (false);
}

double getCurrentRiskReward(){

    Print("TEMP: ","getCurrentRiskReward"); 
    Print("TEMP: getCurrentRiskReward ",ffUpperChannelValue," ",ffLowerChannelValue);
    
    //The moment this calculation happens the values of ffUpperChannelValue and ffLowerChannelValue turn to 0 and everything fails, if I comment the line everything is fine, but I need to make calculations
    double currentRiskReward =  ((ffUpperChannelValue - ffLowerChannelValue) / ffLowerChannelValue) * 100;
    Print("TEMP: ",currentRiskReward); 
    
    //sets risk reward type
    setTradeRewardType(currentRiskReward);
    
    return (currentRiskReward);
    //return (0);
}

Hello everyone

I have a strange situation that I would really appreciate any help with


This is the set up

  1. On start I use a function that recognizes when a bar changes, when it does change, it calls a custom function that sets new values for global variables
  2. Global variables are set
  3. Once the global variables are set I call a function that makes calculations with global variables
  4. The weird thing is that calculations are ok if I add or substract, but if I divide doubles, the values of the involved doubles turn to zero, the global variable values are lost

I have tried NormalizeDouble, send the global variables as parameters so they are not directly involved in the calculations but every time I try to use them for calculation the global doubles turn to zero

The basic code to understand the issue is posted

If someone can help I would really appreciate it

Thanks

 
felipecj:

This is the set up

  1. On start I use a function that recognizes when a bar changes, when it does change, it calls a custom function that sets new values for global variables
  2. Global variables are set
  3. Once the global variables are set I call a function that makes calculations with global variables
  4. The weird thing is that calculations are ok if I add or substract, but if I divide doubles, the values of the involved doubles turn to zero, the global variable values are lost

5. I have tried NormalizeDouble, send the global variables as parameters so they are not directly involved in the calculations but every time I try to use them for calculation the global doubles turn to zero

1. You are not using Global Variables you are using globally declared variables, it's an important distinction to make to avoid confusion.

4 & 5 I assume you are talking about this code ? it is the only code you show that has a division . . .

double currentRiskReward =  ((ffUpperChannelValue - ffLowerChannelValue) / ffLowerChannelValue) * 100;
    Print("TEMP: ",currentRiskReward); 

lets work this code through with some typical EURUSD values . . .

1.2646 - 1.2545 = 0.0101

0.0101 / 1.2646 = 0.00798

0.00798 * 100 = 0.798

Print only shows 4 digits, if your upper and lower channel values are very close to each other then it could be possible for your resultant value to be less than 0.0001 and printing as 0.0, to avoid this use DoubleTo Str and read the documentation for Print()

Doers this line of code give you the correct values ?

Print("TEMP: ","setChannels",ffUpperChannelValue," ",ffLowerChannelValue);
Reason: