Indicator Question - page 3

 
Agent86:
.

So anyhow, I was trying to make the high fractals only form when another condition occurs such as iMACD crosses, or EMA crosses or some other indicator crosses.
So that the fractals will only show on the indicator when all the conditions are met.


i wasnt sure exactly what your trying to do but try this, notice the external inputs.

#property indicator_chart_window      extern bool condition1 = true;
#property indicator_buffers 3         extern bool condition2 = true;
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 White

//---- buffers
double v1[];
double v2[];
double v3[];
double val1;
double val2;
double val3;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
//----
   IndicatorBuffers(3);

   SetIndexArrow(0, 111);
   SetIndexStyle(0,DRAW_ARROW,STYLE_DOT,1,Blue);
   SetIndexBuffer(0, v1);
   SetIndexLabel(0,"Resistance");

   SetIndexArrow(1, 111);
   SetIndexStyle(1,DRAW_ARROW,STYLE_DOT,1,Red);
   SetIndexBuffer(1, v2);
   SetIndexLabel(1,"Support");

   SetIndexArrow(2, 111);
   SetIndexStyle(2,DRAW_ARROW,STYLE_DOT,1,White);
   SetIndexBuffer(2, v3);
   SetIndexLabel(2,"High A"); 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
    
   double   faster = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL
           
            
   int bars = Bars;
//----
  
   for(int i=bars; i>=0; i--)
    {
     if(condition1)
       {
        val1=iFractals(NULL, 0, MODE_UPPER,i);
        if (val1 > 0) v1[i]=High[i];       
       }   
     if(condition2)
       {      
        val2=iFractals(NULL, 0, MODE_LOWER,i);
        if (val2 > 0) v2[i]=Low[i];
       }
     }    
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
WHRoeder:
Exactly how did you come to that conclusion and what is the functional difference?

I came to that conclusion, because likely, i was looking at from OOP perspectives. yes, this is not true for the case of MQL4, I recall now. Though it can be good practice?



 

Or maybe something like this ?

you really ought to use IndicatorCounted() though, because if you do it like this your indicator is redrawing all those objects at every new tick instead of drawing all them just one time and adding new ones as new bars are formed

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 White

//---- buffers
double v1[];
double v2[];
double v3[];
double val1;
double val2;
double val3;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
//----
   IndicatorBuffers(3);

   SetIndexArrow(0, 111);
   SetIndexStyle(0,DRAW_ARROW,STYLE_DOT,1,Blue);
   SetIndexBuffer(0, v1);
   SetIndexLabel(0,"Resistance");

   SetIndexArrow(1, 111);
   SetIndexStyle(1,DRAW_ARROW,STYLE_DOT,1,Red);
   SetIndexBuffer(1, v2);
   SetIndexLabel(1,"Support");

   SetIndexArrow(2, 111);
   SetIndexStyle(2,DRAW_ARROW,STYLE_DOT,1,White);
   SetIndexBuffer(2, v3);
   SetIndexLabel(2,"High A"); 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
    
   double   faster=0;
   double   slower=0;         
   int      bars = Bars;
//----
  
   for(int i=bars; i>=0; i--)
    {
     faster = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,i); //MODE_MAIN
     slower = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,i); //MODE_SIGNAL
     val1=iFractals(NULL, 0, MODE_UPPER,i);
     val2=iFractals(NULL, 0, MODE_LOWER,i);
     
     if(faster > 0)
      {
       if (val1 > 0) v1[i]=High[i];
      }
      
     if(faster < 0)
      {
       if (val2 > 0) v2[i]=Low[i];
      }        
    }   
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
diostar:
I came to that conclusion, because likely, i was looking at from OOP perspectives. yes, this is not true for the case of MQL4, I recall now. Though it can be good practice?

It's always good practice to limit scope. it's always good practice to define variable/object where used and to initialize them at that point.

Defining an object outside the loop results in a default construction plus N assignments. Defined inside the loop results in N constructions with value - usually quicker.

Never worry about optimizations until you can prove the change makes a difference.

 
WHRoeder:

It's always good practice to limit scope. it's always good practice to define variable/object where used and to initialize them at that point.

Defining an object outside the loop results in a default construction plus N assignments. Defined inside the loop results in N constructions with value - usually quicker.

Never worry about optimizations until you can prove the change makes a difference.

This should apply to its iterator as well. for (int i=0;...
 
diostar:
This should apply to its iterator as well. for (int i=0;...
I aggree, but i didn't write that code.
 
Ok, I seem to have some variations of a working code now thanks.

Although, I am slightly confused as to why the declaring of the variables (faster and slower) outside the loop is no good while it's ok inside the loop

And yet the int i = Bars will works outside the loop or inside the loop ?


Anyhow good to see that some of my ideas I was working on are already posted in this thread so this is good news and means I'm getting a little closer to being able to code something on my own someday. So a little progress is better then non. Thanks

So now I can work on comparing some of the indicator times, I'll try to figure this out now.

Such as:
Compare v1[i] to v2[i] Currently formed indicator Times && / || if(v1[i] time is > v2[i] time) and other comparisons like this.

Thanks for all the tips everyone this has been a great help.
 
WHRoeder:

It's always good practice to limit scope. it's always good practice to define variable/object where used and to initialize them at that point.

Defining an object outside the loop results in a default construction plus N assignments. Defined inside the loop results in N constructions with value - usually quicker.

Never worry about optimizations until you can prove the change makes a difference.

I'm assuming this is true for all, and not just indicators / custom indicators ?
 
Agent86:
Ok, I seem to have some variations of a working code now thanks.

Although, I am slightly confused as to why the declaring of the variables (faster and slower) outside the loop is no good while it's ok inside the loop

And yet the int i = Bars will works outside the loop or inside the loop ?


You have to consider what it is you are doing, when you create an indicator you are applying your code to each and every bar on the chart, this means you need to get the macd indicator value as it was for each of those historical bars.

in your original code you did:

double   faster = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1), 
the last parameter 1 is the value of the macd as it was at Bar 1 of the chart, the bar previous to the current bar which is indexed as bar 0

Obviously you do not want to use that single value of the macd in your conditional operator over the whole historical chart .

You need to index the macd to the same bar index as each bar so for instance at bar 500 you need this

faster = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,500) 

that last parameter must change to match the bar number your indicator is applying its algorithms to.

this is why you need it inside the loop, so you can use the loop cycle iterator ( i ) for the last parameter therfore you get your macd value as it was for each bar of the historical chart.

faster = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,i), 

i hope that helps to clear things up a little.

 
WHRoeder:
Try this change
Why for(int i = Bars-1 ?

And why is this better then for(int i = Bars ??

Please advise thanks