is a new variable/constant required once it is used for a separate comparison ?

 
Hi, 
My thoughts were this: why can't I reuse histo in the same way as I use "i" ? 

I use histo for comparison for up/down cross. 
histo  = iCustom(NULL,0,"MACD True",2,1);
I use histo_1 for counting
void histo_up_count()
   {
   for (int i=0; i < 50; i++)
      {
      histo_1 = iCustom(NULL,0,"MACD True",2,i);
      if (histo_1 > 0)
         {
         histoupcount = i;
         }
     
      else return;
              
      }   
      return;
    }

I'm concerned that reusing histo; will interfere with my comparison of histo<0 and histo>0 comparisons. Since histo value will change during the void function() ? 

I guess the question is: Is it customary to keep creating variables/constants for others comparisons like this or what should be done to avoid such duplication etc. 

Please advise
Thanks
 
Agent86: My thoughts were this: why can't I reuse histo in the same way as I use "i" ?  I use histo for comparison for up/down cross. I use histo_1 for counting. I'm concerned that reusing histo; will interfere with my comparison of histo<0 and histo>0 comparisons. Since histo value will change during the void function() ?  I guess the question is: Is it customary to keep creating variables/constants for others comparisons like this or what should be done to avoid such duplication etc. Please advise. Thanks

In your example code you are not reusing any variable. All the variables in your example a named differently so there is no problem.

However, "histo_1" does not seem to be locally declared and you probably meant the following:

void histo_up_count()
   {
   for (int i=0; i < 50; i++)
      {
      double histo_1 = iCustom(NULL,0,"MACD True",2,i);
      if (histo_1 > 0)
...

Local or temporary variables in a function should not collide with globally declared variables because that would interfere with the functionality. That is why you use should not "reuse" globally scoped variables for locally scoped functionality.

In my own programming I use a prefix of "g_" on globally scoped variables so as to differentiate them and prevent conflict, but you can use whatever naming convention you want as long as you understand the deferent variable scopes.


 

Agent86:
Hi, 
My thoughts were this: why can't I reuse histo in the same way as I use "i" ? 

void histo_up_count()
   {
   for (int i=0; i < 50; i++)
      {
      histo_1 = iCustom(NULL,0,"MACD True",2,i);
      if (histo_1 > 0)
         {
         histoupcount = i;
         }
     
      else return;
              
      }   
      return;
    }

What are you trying to do?

Are you trying to find the first bar that the condition is true or are you trying to count the number of bars where the condition is true?

Why do you have the return in there?

If the condition is not true on the first check, it will return and so the function achieves nothing

 
Fernando Carreiro #:

In your example code you are not reusing any variable. All the variables in your example a named differently so there is no problem.

However, "histo_1" does not seem to be locally declared and you probably meant the following:

Local or temporary variables in a function should not collide with globally declared variables because that would interfere with the functionality. That is why you use should not "reuse" globally scoped variables for locally scoped functionality.

In my own programming I use a prefix of "g_" on globally scoped variables so as to differentiate them and prevent conflict, but you can use whatever naming convention you want as long as you understand the deferent variable scopes.


AHHH ! I think I see now. I can cut out a lot of code by locally declared variables of the same name. 
I have some thinking and reading to do on this. Thanks Big Help

 
Keith Watford #:

What are you trying to do?

Are you trying to find the first bar that the condition is true or are you trying to count the number of bars where the condition is true?

Why do you have the return in there?

If the condition is not true on the first check, it will return and so the function achieves nothing

So in this case it counts i++ up to 50 bars to find the point where histo is no longer >0 this is my reference bar for my other functions.
As far as the return goes, I think this was just left over from things I was working back and forth between functions. I'll clean this up too thanks. 

Fernando's post about global vs local declarations will help me clean a lot of this up. 

Thanks all for the responses it really helps. 


 
Agent86 #: AHHH ! I think I see now. I can cut out a lot of code by locally declared variables of the same name. I have some thinking and reading to do on this. Thanks Big Help


No, that is not what I stated. I stated the exact opposite. Do not use the same name for local and global variables.

I even gave you my example of using the "g_"prefix on global variables so as not to be the same as local variables.

You can however use the same name for local variables in different functions.

Learn the difference between globally scoped and locally scoped variables.

 
So I cleaned this up and am confused about the "return" or the "else return".
else return; functions as expected, but return has differences. 
no return or else return: counts way back in time and does function as expected.  

 

void histo_up_count()
   {
   for (int i=0; i < 50; i++)
      {
      double histo_1 = iCustom(NULL,0,"MACD True",2,i);
      if (histo_1 > 0) histoupcount = i;
        
      //else return;
       }
    }

//                     ------------Counting the BAR number that crossed below 0 historgram used to find fractals.
void histo_down_count()
   {
   for (int i=0; i < 50; i++)
      {
      double histo_1 = iCustom(NULL,0,"MACD True",2,i);
      if (histo_1 < 0) histodowncount = i;
      //else return;
      }    

    }


And to be clear. Declaration of local variables of the same name will not collide right ? Thanks 
 

I asked you before

Keith Watford #:

What are you trying to do?

Are you trying to find the first bar that the condition is true or are you trying to count the number of bars where the condition is true?

You replied

Agent86 #:

So in this case it counts i++ up to 50 bars to find the point where histo is no longer >0 this is my reference bar for my other functions.

Your latest code

Agent86 #:
So I cleaned this up and am confused about the "return" or the "else return".
else return; functions as expected, but return has differences. 
no return or else return: counts way back in time and does function as expected.  
void histo_up_count()
   {
   for (int i=0; i < 50; i++)
      {
      double histo_1 = iCustom(NULL,0,"MACD True",2,i);
      if (histo_1 > 0) histoupcount = i;
        
      //else return;
       }
    }
 

Both your function name and the variable name suggests that you are counting something, but you are looking for a bar index, correct? Use meaningful function and variable names to avoid confusion.

if you are trying to find the first bar where the histo is NOT >0

void histo_up_count()
   {
   for (int i=0; i < 50; i++)
      {
      double histo_1 = iCustom(NULL,0,"MACD True",2,i);
      if (histo_1 <= 0) 
        {        
         histoupcount = i;        
         return;
        }
      }
    }
 
Fernando Carreiro #:

No, that is not what I stated. I stated the exact opposite. Do not use the same name for local and global variables.

I even gave you my example of using the "g_"prefix on global variables so as not to be the same as local variables.

You can however use the same name for local variables in different functions.

Learn the difference between globally scoped and locally scoped variables.

Yes, I think I understood this. What I meant was that I got rid of the global variable that I was attempting to reuse and opted for a locally declared variable for each of my functions. 
Do I have this wrong still ? 
Thanks

 
Keith Watford #:

I asked you before

You replied

Your latest code

Both your function name and the variable name suggests that you are counting something, but you are looking for a bar index, correct? Use meaningful function and variable names to avoid confusion.

if you are trying to find the first bar where the histo is NOT >0

I misunderstood your question. 
Yes the first bar where histo==true meaning first bar crossing up above 0. 
This is the reference bar for counting in either direction. I count forward to get the first UPPER fractal and backwards to get the first LOWER fractal. 

So once that bar is found the bar number is set to histoupcount=i

Then I use this to find fractals UPPER / LOWER

bool A_low()
   {  
      {
      if(histo())
         {
         val2=0;
         for (int i=histoupcount; val2==0; i++)
            {
            val2=iFractals(NULL, 0, MODE_LOWER, i);
            A = val2;
            atime=Time[i];
            }
            if(A!=0)
               {
               
               //atime=Time[i];
               //ObjectSet("B3",OBJPROP_BGCOLOR,clrLime);
               ObjectSet("B16",OBJPROP_TIME1,atime);
               ObjectSet("B16",OBJPROP_PRICE1,A);
               ObjectSet("B16",OBJPROP_ANCHOR,ANCHOR_UPPER);
               ObjectSetString(0,"B16",OBJPROP_TOOLTIP,"A Low = "+DoubleToStr(A,4));
               //Print(A, " A Low Located at bar ",i);               
   
               return(true);
               }
            }
       }

//another snip for UPPER
bool B_high()
   {
      {
      if(histo())
         {
         for (int i=histoupcount; i!=0; i--)
            {
            val1=iFractals(NULL, 0, MODE_UPPER, i);
            B = val1; //Print(B, " B = val1");
            if(B!=0)
               {
               
               btime = Time[i];              
               //ObjectSet("B2",OBJPROP_BGCOLOR,clrLime);
               ObjectSet("B17",OBJPROP_TIME1,btime);
               ObjectSet("B17",OBJPROP_PRICE1,B);
               ObjectSet("B17",OBJPROP_ANCHOR,ANCHOR_LOWER);
               ObjectSetString(0,"B17",OBJPROP_TOOLTIP,"B High = "+DoubleToStr(B,4));
               //Print(B, " B high Located at bar ", i);
               
               return(true);
               }
             }
            }
        }
    //ObjectSet("B2",OBJPROP_BGCOLOR,clrBlack);
    return(false);                 
   }

That is the general idea to use histo crossed bar as a reference bar and count to the fractal I want from the reference bar. Either forward or back to the fractal I want.

It's not a good code but I'm learning.


Thanks for all the answers. I have learned a lot but there is still much foggy areas for me.

 
The thing about this code is that it functions as I want, the problem really arises from putting objects on the code. This is where I these global declarations makes it hard to make changes because so much other code is linked to that global. 
I was attempting to trim the fat and also make easier to work on for TEXT objects that move on the chart etc.  

Aside from the TEXT variables the other OBJ_Labels and things work as expected. 

I may be going about it all wrong but it's what lead me to ask about "reusing" the variables. Mostly the question was answered but may not actually be the question I should have been asking. 

I think I may have worked my self in a corner from too many global variables making it too time consuming to make changes every time I think of a new way of writing the functions better and shorter. 

So I'll keep working on this some more you all have given me a bunch to think about already. 

Thanks again and every bit helps a lot.