Why is my variable resetting to zero?

 

Hi, I'm new to coding and have been trying to pick my way through it. I've run into something that has me stumped so would appreciate some advice.


I'm trying to find the highest price whilst the stochastic is oversold.


Using "if" I set a variable when the stoch first crosses 80 and another when it crosses back under and then want to find the highest price between the two points. My problem is that when the second condition is true, the variable set in the first condition is re set to zero. I don't understand why this is happening. I thought that a variable would hold it's value until instructed to change it. Hopefully the following code can explain. There are a load of alerts in there that I have been using to try and figure out what is going on... I've obviously done something stupid, but what am I missing??? any comments? Thanks in advance.


#property copyright ""
#property link      ""

#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   
//----
int    counted_bars=IndicatorCounted();

//---- check for possible errors
if (counted_bars<1) return(-1);

//---- last counted bar will be recounted
if (counted_bars>0) counted_bars--;

int pos=Bars-counted_bars;

//---- main calculation loop

while(pos>=0)
{

// Stochastic
      double stoch=iStochastic(NULL,0,5,3,3,0,0,0,1);             // Get Stochastic of last close
      double pstoch=iStochastic(NULL,0,5,3,3,0,0,0,2);            // Get Stochastic of previouse bar
    
      if ( stoch>80 && pstoch <80)                               // Find the bar when the stoch crosses over 80 
         {
         int hsta=counted_bars;                                  // Set a bar ID index High A
         Alert ("hsta: ", hsta);           
         }
      
      if ( stoch<80 && pstoch >80)                               // find the bar when stoch crosses back under 80...
         {
         int hstb=counted_bars;                                  // Set a bar ID index High B
         Alert ("hsta: ", hsta, " hstb: ", hstb);
         }
      
      if ( hsta<hstb)                                            // make sure the range has completed
         {
         int hcount = hstb-hsta;                                 // Count how many bars between A and B
         int hshift= counted_bars-hstb;                          // Count how many bars have passed since last cross below 80
         Alert(" count: ", hcount, " shift:  " , hshift," hsta: ", hsta," hstb: ", hstb);
         
         int stochhighbar=iHighest(NULL,0,MODE_HIGH,hcount,hshift);// Find the bar between A-B with highest price 
         double stochhigh= iHigh(NULL,0,stochhighbar);           // find the high price   

}
         
                
       pos--;  
    }     
//----
   return(0);
  }
//+------------------------------------------------------------------+
 

Hi

I think you have to declare fisrt all your variable and not in start() nor in a while loop.

Each loop reset all your variable

 
Matutin:

Hi

I think you have to declare fisrt all your variable and not in start() nor in a while loop.

Each loop reset all your variable

Ah!! that did it, I declared them all as global variables first and now it's holding the results.. Many thanks

Reason: