Why is my function returning 0?

 

Hi there guys,

Stuck with this - my swing finding function seems to be returning 0 while inside the loop, but when I set an Alert at the if statements that define variable F they return a value (so I know it works). It's the value is turning to 0 as soon as it enters the while loop. What gives?


//+------------------------------------------------------------------+
//| Find swing function                                              |
//+------------------------------------------------------------------+
int swing()
{
   static int e = broom() + 3;
   static int d = 357;
   static int f;
   if(structure() == 1)
   {
      f = iLowest(NULL,PERIOD_M15,MODE_LOW,d,e);
   // If add Alert((string)f); here, it returns a value
   }
   else if(structure() == 2)
   {
      f = iHighest(NULL,PERIOD_M15,MODE_HIGH,d,e); 
   }
   static int swingIndex = 0;
   static bool swingCheck = true;
   if(newCandle() == true && broom() > 0)
   {
      while(d > e && swingCheck == true)
      {
         // But if I add the same alert here, it returns a value of 0
         if(structure() == 1)
         {
            if((iLow(NULL,PERIOD_M15,f) - iLow(NULL,PERIOD_M15,broom())) >= 0.00010)
            {
               if((iLow(NULL,PERIOD_M15,f) - iLow(NULL,PERIOD_M15,broom())) <= 0.00100)
               {
                  if(iLow(NULL,PERIOD_M15,f) < iLow(NULL,PERIOD_M15,f+1) && iLow(NULL,PERIOD_M15,f) < iLow(NULL,PERIOD_M15,f+2))
                  {
                     if((breaker() - iLow(NULL,PERIOD_M15,broom())) <= 0.00120)
                     {
                        if(closeCounter() < 2)
                        {
                           swingIndex = f;
                           swingCheck = false; 
                        }
                        else
                        {
                           d = d - 1;   
                        }
                     }
                     else
                     {
                        d = d - 1;   
                     }
                  }
                  else
                  {
                     d = d - 1;    
                  }
               }
               else
               {
                  d = d - 1;   
               }
            }
            else
            {
               d = d - 1;
            }
         }
         else if(structure() == 2)
         {
            if((iHigh(NULL,PERIOD_M15,f) - iHigh(NULL,PERIOD_M15,broom())) >= 0.00010)
            {
               if((iHigh(NULL,PERIOD_M15,f) - iHigh(NULL,PERIOD_M15,broom())) <= 0.00100)
               {
                  if(iHigh(NULL,PERIOD_M15,f) < iHigh(NULL,PERIOD_M15,f+1) && iHigh(NULL,PERIOD_M15,f) < iHigh(NULL,PERIOD_M15,f+2))
                  {
                     if((iHigh(NULL,PERIOD_M15,broom()) - breaker()) <= 0.00120)
                     {
                        if(closeCounter() < 2)
                        {
                           swingIndex = f;
                           swingCheck = false;
                           Alert((string)f); 
                        }
                        else
                        {
                           d = d - 1;   
                        }
                     }
                     else
                     {
                        d = d - 1;   
                     }
                  }
                  else
                  {
                     d = d - 1;    
                  }
               }
               else
               {
                  d = d - 1;   
               }
            }
            else
            {
               d = d - 1;
            }
         }
      }
   }   
   return(swingIndex);
}   
 
Harry Wright:

Hi there guys,

Stuck with this - my swing finding function seems to be returning 0 while inside the loop, but when I set an Alert at the if statements that define variable F they return a value (so I know it works). It's the value is turning to 0 as soon as it enters the while loop. What gives?


 while(d > e && swingCheck == true)
      {

swingIndex = f;
                           swingCheck = false; 

you expect something will stop the loop when swingCheck value is updated while the loop is still running in true condition? either you do recursive function call to self (but you must modify the function) after the value is updated or break the while

//+------------------------------------------------------------------+
//| Find swing function                                              |
//+------------------------------------------------------------------+
int swing()
{
   static int e = broom() + 3;
   static int d = 357;
   static int f;
   if(structure() == 1)
   {
      f = iLowest(NULL,PERIOD_M15,MODE_LOW,d,e);
   // If add Alert((string)f); here, it returns a value
   }
   else if(structure() == 2)
   {
      f = iHighest(NULL,PERIOD_M15,MODE_HIGH,d,e); 
   }
   //--- f = 0
   static int swingIndex = 10;
   static bool swingCheck = true;
   if(newCandle() == true && broom() > 0)
   {

          while(d > e && swingCheck == true)
          {
             // But if I add the same alert here, it returns a value of 0
             if(structure() == 1)
             {
                swingIndex = 1;
                swingCheck = false;
                break;
             }
             else if(structure() == 2)
             {
                swingCheck = false;
                swingIndex = 2;
                break;
            
             }
             swingCheck = false;
             swingIndex = f;
             break;
          }
    
      
   }
   
   return(swingIndex);
}   

i don't use while unless i am sure i know the next iteration stop

 
Sardion Maranatha:

you expect something will stop the loop when swingCheck value is updated while the loop is still running in true condition? either you do recursive function call to self (but you must modify the function) after the value is updated or break the while

i don't use while unless i am sure i know the next iteration stop

Hi, thanks for the replay.

I'm sure not sure I understand your answer.

The script is meant to cycle through the last 357 candles to see find one that matches all the conditions, hence the loop. I guess I could use a for loop instead but wouldn't this just cause the same issue?

 
Harry Wright:

Hi, thanks for the replay.

I'm sure not sure I understand your answer.

The script is meant to cycle through the last 357 candles to see find one that matches all the conditions, hence the loop. I guess I could use a for loop instead but wouldn't this just cause the same issue?

you could use while loop for swingCheck  and then counter decrement to cycle through the last 357, set the next swingCheck value inside the for loop on any index when the conditions are met or  last index if none to stop the swingCheck loop if you need to cycle just once
 
Sardion Maranatha:
you could use while loop for swingCheck  and then counter decrement to cycle through the last 357, set the next swingCheck value inside the for loop on last index to stop the swingCheck loop if you need to cycle just once

Theoretically this should work, right?

//+------------------------------------------------------------------+
//| Find highest/lowest function                                     |
//+------------------------------------------------------------------+
int mostExtreme()
{
   int x = broom() + 3;
   int y = 357;
   int z;
   if(structure() == 1)
   {
      z = iLowest(NULL,PERIOD_M15,MODE_LOW,x,y);  
   }
   else if(structure() == 2)
   {
      z = iHighest(NULL,PERIOD_M15,MODE_HIGH,x,y); 
   }
   else
   {
      z = 0;
   }
   return(z);
}
//+------------------------------------------------------------------+
//| Find swing function                                              |
//+------------------------------------------------------------------+
int swing()
{
   int f;
   int swingIndex = 0;
   if(newCandle() == true && mostExtreme() > 0)
   {
      for(f=mostExtreme();f>(broom()+3);f--)
      {
         if(structure() == 1)
         {
            if((iLow(NULL,PERIOD_M15,f) - iLow(NULL,PERIOD_M15,broom())) >= 0.00010)
            {
               if((iLow(NULL,PERIOD_M15,f) - iLow(NULL,PERIOD_M15,broom())) <= 0.00100)
               {
                  if(iLow(NULL,PERIOD_M15,f) < iLow(NULL,PERIOD_M15,f+1) && iLow(NULL,PERIOD_M15,f) < iLow(NULL,PERIOD_M15,f+2))
                  {
                     if((breaker() - iLow(NULL,PERIOD_M15,broom())) <= 0.00120)
                     {
                        if(closeCounter() < 2)
                        {
                           swingIndex = f;
                           Alert((string)f); 
                           break;
                        }
                     }
                  }
               }
            }            

         }
         else if(structure() == 2)
         {
            if((iHigh(NULL,PERIOD_M15,f) - iHigh(NULL,PERIOD_M15,broom())) >= 0.00010)
            {
               if((iHigh(NULL,PERIOD_M15,f) - iHigh(NULL,PERIOD_M15,broom())) <= 0.00100)
               {
                  if(iHigh(NULL,PERIOD_M15,f) < iHigh(NULL,PERIOD_M15,f+1) && iHigh(NULL,PERIOD_M15,f) < iHigh(NULL,PERIOD_M15,f+2))
                  {
                     if((iHigh(NULL,PERIOD_M15,broom()) - breaker()) <= 0.00120)
                     {
                        if(closeCounter() < 2)
                        {
                           swingIndex = f;
                           Alert((string)f);
                           break; 
                        }
                     }
                  }
               }
            }
         }               
      }
   }   
   return(swingIndex);
} 
 
Yet it still returns 0?? I really don't understand it at all
 
Harry Wright:
Yet it still returns 0?? I really don't understand it at all

zero at all?

FH      0       03:04:35.746    forum350986 (EURUSD,M5) 2020.09.10 04:24:44   swing result is 0
FR      0       03:04:35.747    forum350986 (EURUSD,M5) 2020.09.10 04:24:50   swing result is 0
ED      0       03:04:35.748    forum350986 (EURUSD,M5) 2020.09.10 04:24:52   swing result is 0
KN      0       03:04:35.750    forum350986 (EURUSD,M5) 2020.09.10 04:24:55   swing result is 0
EL      0       03:04:35.752    forum350986 (EURUSD,M5) 2020.09.10 04:25:00   Alert: 339
DH      0       03:04:35.752    forum350986 (EURUSD,M5) 2020.09.10 04:25:00   swing result is 339
PL      0       03:04:35.754    forum350986 (EURUSD,M5) 2020.09.10 04:25:02   swing result is 0
GQ      0       03:04:35.755    forum350986 (EURUSD,M5) 2020.09.10 04:25:06   swing result is 0
EK      0       03:04:35.756    forum350986 (EURUSD,M5) 2020.09.10 04:25:07   swing result is 0
LM      0       03:04:35.757    forum350986 (EURUSD,M5) 2020.09.10 04:25:10   swing result is 0
HG      0       03:04:35.759    forum350986 (EURUSD,M5) 2020.09.10 04:25:16   swing result is 0
EH      0       03:04:35.760    forum350986 (EURUSD,M5) 2020.09.10 04:25:17   swing result is 0
ER      0       03:04:35.761    forum350986 (EURUSD,M5) 2020.09.10 04:25:21   swing result is 0
OD      0       03:04:35.763    forum350986 (EURUSD,M5) 2020.09.10 04:25:21   swing result is 0

or like this? i don't know what's your breaker() so i hard coded to 0.00012 and i hard coded closeCounter() to 1..loop is working per my imaginary try to guess logic..i'm up til here

 
  1. We have no idea what broom() is.
  2. You are looking at the M15 chart but running on the M5. Are you are mixing apples and oranges?

 
William Roeder:
  1. We have no idea what broom() is.
  2. You are looking at the M15 chart but running on the M5. Are you are mixing apples and oranges.

my mistake, i missed to change the last tf i used, already late here, will give a look when i am back, thanks for the correction William
 
Sardion Maranatha:
my mistake, i missed to change the last tf i used, already late here, will give a look when i am back, thanks for the correction William
CG      0       03:49:15.764    forum350986 (EURUSD,M15)        2020.09.10 03:14:36   swing result is 0
QR      0       03:49:15.765    forum350986 (EURUSD,M15)        2020.09.10 03:14:37   swing result is 0
KH      0       03:49:15.766    forum350986 (EURUSD,M15)        2020.09.10 03:14:38   swing result is 0
DF      0       03:49:15.768    forum350986 (EURUSD,M15)        2020.09.10 03:14:39   swing result is 0
HM      0       03:49:15.769    forum350986 (EURUSD,M15)        2020.09.10 03:14:41   swing result is 0
DK      0       03:49:15.771    forum350986 (EURUSD,M15)        2020.09.10 03:14:42   swing result is 0
RF      0       03:49:15.772    forum350986 (EURUSD,M15)        2020.09.10 03:14:45   swing result is 0
ML      0       03:49:15.773    forum350986 (EURUSD,M15)        2020.09.10 03:14:47   swing result is 0
LJ      0       03:49:15.775    forum350986 (EURUSD,M15)        2020.09.10 03:14:48   swing result is 0
RQ      0       03:49:15.776    forum350986 (EURUSD,M15)        2020.09.10 03:14:52   swing result is 0
DO      0       03:49:15.777    forum350986 (EURUSD,M15)        2020.09.10 03:14:57   swing result is 0
MK      0       03:49:15.779    forum350986 (EURUSD,M15)        2020.09.10 03:15:00   Alert: 334
PQ      0       03:49:15.779    forum350986 (EURUSD,M15)        2020.09.10 03:15:00   swing result is 334
GN      0       03:49:15.781    forum350986 (EURUSD,M15)        2020.09.10 03:15:04   swing result is 0
DD      0       03:49:15.782    forum350986 (EURUSD,M15)        2020.09.10 03:15:04   swing result is 0
LR      0       03:49:15.783    forum350986 (EURUSD,M15)        2020.09.10 03:15:07   swing result is 0

i second William's comment, broom() is unknown

 

Here are all the functions referenced in the code. I understand that a for loop would have been better to find the swing but surely my while loop has the same effect? Either way, it always returns 0. What am I doing wrong?

//+------------------------------------------------------------------+
//| New bar function                                                 |
//+------------------------------------------------------------------+
bool newCandle()
{
   static datetime lastCandle;
   datetime currentCandle = Time[0];
   if(lastCandle != currentCandle)
   {
      lastCandle = currentCandle;
      return(true);
   }
   else
   {
      return(false);
   }
}
//+------------------------------------------------------------------+
//| Structure function                                               |
//+------------------------------------------------------------------+
int structure()
{
   int a = 0;
   if(iOpen(NULL,PERIOD_M30,1) < iClose(NULL,PERIOD_M30,1))
   {
      a = 1;  
   }
   else if(iOpen(NULL,PERIOD_M30,1) > iClose(NULL,PERIOD_M30,1))
   {
      a = 2;
   }
   else
   {
      a = 0;
   }   
   return(a);
}
//+------------------------------------------------------------------+
//| Find broom function                                              |
//+------------------------------------------------------------------+
int broom()
{
   int c = 1;
   int d = 1;
   int broomIndex = 0;
   if(structure() == 1)
   {
      while(c < 6)
      {
         if(iLow(NULL,PERIOD_M15, c) == iLow(NULL,PERIOD_M30, 1))
         {
            if(iLowest(NULL,PERIOD_M15,MODE_LOW,3,c) == c)
            {
               broomIndex = c;
               c = 6;
            }
            else
            {  
               c = 6;
            }           
         }
         else
         {
            c = c + 1;
         }
      }   
   }
   else if(structure() == 2)
   {
      while(d < 6)
      {
         if(iHigh(NULL,PERIOD_M15, d) == iHigh(NULL,PERIOD_M30, 1))
         {
            if(iLowest(NULL,PERIOD_M15,MODE_LOW,3,d) == d)
            {
               broomIndex = d;
               d = 6;
            }
            else
            {
               d = 6;
            }           
         }
         else
         {
            d = d + 1;
         }
      }
   }     
   return(broomIndex);   
}
//+------------------------------------------------------------------+
//| Find swing function                                              |
//+------------------------------------------------------------------+
int swing()
{
   static int e = broom() + 3;
   static int d = 357;
   static int f;
   if(structure() == 1)
   {
      f = iLowest(NULL,PERIOD_M15,MODE_LOW,d,e);
   // If add Alert((string)f); here, it returns a value
   }
   else if(structure() == 2)
   {
      f = iHighest(NULL,PERIOD_M15,MODE_HIGH,d,e); 
   }
   static int swingIndex = 0;
   static bool swingCheck = true;
   if(newCandle() == true && broom() > 0)
   {
      while(d > e && swingCheck == true)
      {
         // But if I add the same alert here, it returns a value of 0
         if(structure() == 1)
         {
            if((iLow(NULL,PERIOD_M15,f) - iLow(NULL,PERIOD_M15,broom())) >= 0.00010)
            {
               if((iLow(NULL,PERIOD_M15,f) - iLow(NULL,PERIOD_M15,broom())) <= 0.00100)
               {
                  if(iLow(NULL,PERIOD_M15,f) < iLow(NULL,PERIOD_M15,f+1) && iLow(NULL,PERIOD_M15,f) < iLow(NULL,PERIOD_M15,f+2))
                  {
                     if((breaker() - iLow(NULL,PERIOD_M15,broom())) <= 0.00120)
                     {
                        if(closeCounter() < 2)
                        {
                           swingIndex = f;
                           swingCheck = false; 
                        }
                        else
                        {
                           d = d - 1;   
                        }
                     }
                     else
                     {
                        d = d - 1;   
                     }
                  }
                  else
                  {
                     d = d - 1;    
                  }
               }
               else
               {
                  d = d - 1;   
               }
            }
            else
            {
               d = d - 1;
            }
         }
         else if(structure() == 2)
         {
            if((iHigh(NULL,PERIOD_M15,f) - iHigh(NULL,PERIOD_M15,broom())) >= 0.00010)
            {
               if((iHigh(NULL,PERIOD_M15,f) - iHigh(NULL,PERIOD_M15,broom())) <= 0.00100)
               {
                  if(iHigh(NULL,PERIOD_M15,f) < iHigh(NULL,PERIOD_M15,f+1) && iHigh(NULL,PERIOD_M15,f) < iHigh(NULL,PERIOD_M15,f+2))
                  {
                     if((iHigh(NULL,PERIOD_M15,broom()) - breaker()) <= 0.00120)
                     {
                        if(closeCounter() < 2)
                        {
                           swingIndex = f;
                           swingCheck = false;
                        }
                        else
                        {
                           d = d - 1;   
                        }
                     }
                     else
                     {
                        d = d - 1;   
                     }
                  }
                  else
                  {
                     d = d - 1;    
                  }
               }
               else
               {
                  d = d - 1;   
               }
            }
            else
            {
               d = d - 1;
            }
         }
      }
   }   
   return(swingIndex);
}   
//+------------------------------------------------------------------+
//| Find breaker function                                            |
//+------------------------------------------------------------------+
double breaker()
{
   int f;
   double g;
   bool breakerCheck = true;
   if(structure() == 1)
   {
      f = iLowest(NULL,PERIOD_M1,MODE_LOW,30,0);
      if(iLow(NULL,PERIOD_M1,f) == iLow(NULL,PERIOD_M15,broom()))
      {
         f = f + 1;
         while(f < 200 && breakerCheck == true)
         {
            if(iOpen(NULL,PERIOD_M1,f) < iClose(NULL,PERIOD_M1,f))
            {
               g = iHigh(NULL,PERIOD_M1,f);
               breakerCheck = false;
            }
            else if(iOpen(NULL,PERIOD_M1,f) == iClose(NULL,PERIOD_M1,f))
            {
                  f = f + 1;   
            }
            else
            {
               f = f + 1;   
            }
         }     
      }
   }
   else if(structure() == 2)
   {
      f = iHighest(NULL,PERIOD_M1,MODE_HIGH,30,0);
      if(iHigh(NULL,PERIOD_M1,f) == iHigh(NULL,PERIOD_M15,broom()))
      {
         f = f + 1;
         while(f < 200 && breakerCheck == true)
         {
            if(iOpen(NULL,PERIOD_M1,f) > iClose(NULL,PERIOD_M1,f))
            {
               g = iLow(NULL,PERIOD_M1,f);
               breakerCheck = false;
            }
            else if(iOpen(NULL,PERIOD_M1,f) == iClose(NULL,PERIOD_M1,f))
            {
               f = f + 1;            
            }
            else
            {
               f = f + 1;   
            }
         }       
      }  
   }
   else
   {
      breakerCheck = false;
   }           
   return(g);
}
//+------------------------------------------------------------------+
//| Close counter function                                           |
//+------------------------------------------------------------------+
int closeCounter()
{
   int h = 0;
   int closeIndex =  broom();
   while(closeIndex < swing())
   {
      if(structure() == 1)
      {
         if(iClose(NULL,PERIOD_M15,closeIndex) < iLow(NULL,PERIOD_M15,swing()))
         {
            h = h + 1;
            closeIndex = closeIndex + 1;
         }
         else
         {
            closeIndex = closeIndex + 1;   
         }
      }
      else if(structure() == 2)
      {
         if(iClose(NULL,PERIOD_M15,closeIndex) > iHigh(NULL,PERIOD_M15,swing()))
         {
            h = h + 1;
            closeIndex = closeIndex + 1;
         }
         else
         {
            closeIndex = closeIndex + 1;    
         }
      }              
   }  
   return(h);
}
Reason: