Indicator Question - page 7

 
It's about scope and last set value. All these variables have a scope of at least the Function they are in, if they are declared globally then they have scope outside of the Function. If the value of a variable changes 3 times within a loop you can't get the 2nd value it was set to from outside the loop because by the time the code outside the loop is executed the variable is set to it's 3rd value.
 
If I use something like this

And lets say I declare val1 with shift 3
and then the loop of course is shift i

If I refer to val1 outside the loop, will I be talking about val1 global or val1 looped ?

I guess I'm wondering if I should add a separate global fractal for my if statements like val3 and val4 or something

I'm sort of getting stuck here, but seems like the ABCD scheme is at least making a little progress

Anyhow here is the code any ideas ?

//+------------------------------------------------------------------+
//|                                                  Agent86_5min.mq4 |
//|                                                    Unfinished POS |
//|                      my attempt at an ABCD scheme and fib retrace | 
//+------------------------------------------------------------------+
#property copyright "Unfinished POS by Agent86"


//---- input parameters
extern double    TakeProfit=20.0;
extern double    Lots=0.1;
extern double    StopLoss=10.0;
extern int MagicNumber=123486;

double val1;
double val2;


//++++ These are adjusted for 5 digit brokers.

int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)

    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   if (Digits == 5 || Digits == 3)
   {    // Adjust for five (5) digit brokers.
      pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
   } 
   else 
    {    
      pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; 
    }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
     
   
    
//---- 

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
   
    
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   
        
   int i=0;                
   val1=iFractals(NULL, 0, MODE_UPPER,3);
   val2=iFractals(NULL, 0, MODE_LOWER,3); 
  


for(i=0; val1==0 || val2==0; i++)
     {
     if(iFractals(NULL, 0, MODE_UPPER,i) > 0 && val1 == 0) 
     val1 = iFractals(NULL, 0, MODE_UPPER, i);
     if(iFractals(NULL, 0, MODE_LOWER, i) > 0 && val2 == 0)
     val2 = iFractals(NULL, 0, MODE_LOWER, i);
     double A = val2;
     if(A !=0) 
     Print (A, " A loop down");
     double a = val1;
     if(a !=0)
     Print (a, " a loop up");
      
     //Print (" Last UPPER Fractal at ", val1, " Last LOWER Fractal at ", val2);
         
     }   
                            
      if(val1 > 0)
         {
         //Print (val1, " val1");
         //Print(TimeToStr(Time[3],TIME_DATE|TIME_MINUTES), " = val1 time");      
         //datetime b = Time[3];
         ////Print (A, " A if");
         double Z=(val1-A)/2; //my attempt at a .50 retrace in pips but can be any fib level
         double C=A+Z; //using the calc above to get the retrace value at .50
         //Print (C, " C");
         if(Bid<=C && Bid > A) //C will be somewhere in between here
         Print (Low[0], " Low [0] ", C, " = C");
         // some other signal here to trade at retrace
         }        
         
         

      if(val2 < 0)
         {
         //Print (val2, " val2");
         //Print(TimeToStr(Time[3],TIME_DATE|TIME_MINUTES), " = val2 time");
         //datetime c = Time[3];
         //Print(a, " a");
         double z=(a-val2)/2; //my attempt at a .50 retrace in pips but can be any fib level
         double c=a+z; //using the calc above to get the retrace value at .50
         //Print (c, " c");
         if(Bid<=c && c < a) //c will be somewhere in between here
         Print (Low[0], " Low [0] ", c, " = c");
         //some other signal here to trade at retrace
         }
         
                   
                
   return(0);
  }    

//+------------------------------------------------------------------+


So the A would be the previous low and the val1 should be the current shift 3, and same with a and val2

I have to retest some more to be sure that A and a are referring to the correct values because I added the !=0 statements so that it would not print the empty values.
Seems to be ok at first look, but I need to re-confirm this

Anyhow to restate this:
Does the val1 in my if(statement) properly refer to the correct condition or do I need a completely different iFractal to refer to shift 3
I can't really tell if it's working right or not, it appears to be working but something doesn't seem right and I can't quit put my finger on it.

Please advise

Thanks

Although this is really not an indicator topic anymore, should I move this to a new topic ?

 
Agent86:
If I use something like this

And lets say I declare val1 with shift 3
and then the loop of course is shift i

If I refer val1 outside the loop will I be talking about val1 global or val1 looped ?

I guess I'm wondering if I should add a separate global fractal for my if statements like val3 and val4 or something

val1 & val2 are declared with global scope, i is declared locally within start().

val1 and val2 inside or outside the loop have the last value that was assigned to them . . . that happens before the loop and then the values may be changed inside the loop . . . once the loop is exited the values are what they are and remain that way until start() is called the next time, then they are reset and then possibly modified in the loop . . . etc. etc.

 
RaptorUK:

val1 & val2 are declared with global scope, i is declared locally within start().

val1 and val2 inside or outside the loop have the last value that was assigned to them . . . that happens before the loop and then the values may be changed inside the loop . . . once the loop is exited the values are what they are and remain that way until start() is called the next time, then they are reset and then possibly modified in the loop . . . etc. etc.

Hmmm, so decisions decisions.

I could try to go after the values while in the loop and just redeclare val1 and val2 =iFractal,,,3 again. To reset things but only after I declare A=val2 and a=val1 so I have a value for these which hopefully is my previous fractal if I have this right.

Or I could redeclare val 1 and val2 outside the loop, but I don't want to loose the value for A or a so I just have to work it out at the proper times I think.

I also could be going about this all together wrong but no matter. Gotta start somewhere, so I picked this for now;and i can adjust it as I get more experience.

Thanks for the help
 
So these are sort of irrelevant ?

val1=iFractals(NULL, 0, MODE_UPPER,3);
val2=iFractals(NULL, 0, MODE_LOWER,3);

 

I'm guessing I should probably changed these to val3 and val4 in order to use the actual iFractal for trading and only reference and use (A) and (a) for to reference my previous fractal.


I'll keep working on it

Thanks

 
Ok

After a few days thinking about it and trying to work through the logic I think I have a better working code now although not perfect but it's progressing

//+------------------------------------------------------------------+
//|                                                  Agent86_5min.mq4 |
//|                                              Unfinished POS slate |
//|                                          See progress notes below |
//+------------------------------------------------------------------+
#property copyright "Unfinished POS slate by Agent86"


//---- input parameters
extern double    TakeProfit=20.0;
extern double    Lots=0.1;
extern double    StopLoss=10.0;
extern int MagicNumber=123486;

double val1;
double val2;
bool traded = false;

//++++ These are adjusted for 5 digit brokers.

int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)

    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   if (Digits == 5 || Digits == 3)
   {    // Adjust for five (5) digit brokers.
      pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
   } 
   else 
    {    
      pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; 
    }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
     
   
    
//---- 

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
    
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   
        
   int i=0;
   int ticket,total,result;
   total = OrdersTotal();                 
   val1=iFractals(NULL, 0, MODE_UPPER,3);
   val2=iFractals(NULL, 0, MODE_LOWER,3); 


     
     if(val1 > 0 && traded == false)
      { 
      // in the absence of an && indicator above I get continous print statements
      // with indicator added && indicator above, then it prints A and B once as it should ???
      // I need to work this out so it won't print all the time aka trade all the time
      // A and B first values seems to initialize the whole block
      // just a reminder that they time stamps coordinate after this FYI
      // now work on C retrace and possibly signals
      // add options for user to select some candle patterns
      // add options to select indicators
      // add bells and whistles to make things fun
      // alerts,sounds,maybe poppup video animation or something
      
      double B = val1;
      Print(B, " B high");
         if(val2==0)
            {
            for (i=0; val2==0; i++)
               {
               val2=iFractals(NULL, 0, MODE_LOWER, i);
               double A = val2;
                  if(A!=0)
                     {
                     Print(A, " A low");
                     }
               }
             }  
      traded=true;
      }
     
     if(val2 > 0 && traded == true)
      {
      B = val2;
      Print(B, " B low");
         if(val1==0)
            {
            for (i=0; val1==0; i++)
               {
               val1=iFractals(NULL, 0, MODE_UPPER, i);
               A = val1;
                  if(A!=0)
                     {
                     Print(A, " A high");
                     }
               }
             }  
      traded=false;
      }
     
                    
                
   return(0);
  }    

//+------------------------------------------------------------------+


      

So I have my A and B values and while an indicator statement is placed in the initial if(statements && indicators) the values print once for example MACD faster > slower or some such indicator statements.

So I get only A and B as I would like

I'm not sure why it prints multiple times when the indicator options are taken away from my if(statements)

Anyhow I'm getting a little closer with this ABCD type of scheme which can be used on other indicators too not just fractals etc. for future reference

Please advise

Thanks

 

This might help you out. It's a indicator to access the last fractals and the shift so you could use to work out your ABCD.

iCustom(Symbol(),0,"Fractal Store",0,"",0,0)

That will give upper fractals. Change "" to "L" for lower fractals.

iCustom(Symbol(),0,"Fractal Store",1,"",0,0)

Will give the fractal before the most recent one.

Files:
 
heelflip43:

This might help you out. It's a indicator to access the last fractals and the shift so you could use to work out your ABCD.

That will give upper fractals. Change "" to "L" for lower fractals.

Will give the fractal before the most recent one.

Yep, I see that

prior to my latest code in this thread I had worked out some various (for) statements with some help and it was also an indicator I was able to draw what I wanted in the buffer, however this brought me to another problem of trying to extract the values and only printing those values once to be used or referenced.

Since these various for statements cause problems for me and those are similar to the code you posted.
I was never able to get the values without constantly printing the value each and everytime it becomes true which is continuously.
I've worked out some of this but still never really learned how to stop once a condition is true.

I see in your code one thing that might help is the static datetime block and I've used that only once before to refer to the condition only when a new bar and new bar time comes in.
This part might be helpful with what I'm working. And although my current code does produce the current fractal and the previous fractal currently it's working

The only problem I mostly have is that once the condition is true it prints out the statement continuously which could be ok, but this means it will also probably place a trade continuously once I get to that point.

Perhaps I need more experience on how to use iCustom indicators and this might solve a lot of my troubles, but I am not that far in my learning yet.

I'll review this code some more, and the EMPTY_VALUE parts. Now that I see that in use I can learn from that as well. It was suggested before but I never understood how to use it.

I'll keep working on it
Thanks for the help
 
One last think I noticed that when I add lets say MACD to the if(val1 > 0 && faster > slower && traded == false)

Then it works and only gives exactly what I want which is print the A low, B high, then and/or B low and A high

But if I take out the faster > slower and faster < slower on both these if statements then I notice it NEVER prints the A high and also continuously prints the others which I'm confused about


I'll keep working on, i'm sure that I need to get it to print the statement once with no other indicators statements in the EA so I know that indicators won't interfere with the ABCD parts of the code

Back to the drawing board


 

I think your trouble is in you haven't reset val1 and val2. So once you've found the high and last low you need to reset them:

double B = val1;
      Print(B, " B high");
         if(val2==0)
            {
            for (i=0; val2==0; i++)
               {
               //Print("i "+i);
               val2=iFractals(NULL, 0, MODE_LOWER, i);
               double A = val2;
                  if(A!=0)
                     {
                     Print(A, " A low");
                     }
               }
             }  
      traded=true;
      }
     
   val1=iFractals(NULL, 0, MODE_UPPER,3);
   val2=iFractals(NULL, 0, MODE_LOWER,3); 

     if(val2 > 0 && traded == true)
      {
      B = val2;
      Print(B, " B low");
         if(val1==0)
            {
            for (i=0; val1==0; i++)
               {
               val1=iFractals(NULL, 0, MODE_UPPER, i);
               A = val1;
                  if(A!=0)
                     {
                     Print(A, " A high");
                     }
               }
             }  
      traded=false;
      }
Hopefully that will help.
Reason: