Download MetaTrader 5

calculating lowest/highest indicator values over a certain period

To add comments, please log in or register
nilihijiadbab
29
nilihijiadbab  

Hi,

I'm trying to create a custom indicator that calculates the lowest highs and highest lows over a certain period and shows it on the chart as a line.

It's based on an arrow indicator I've created myself that shows dots as support and resistance until a new high/low is formed. (you can replace it by iFractals() with a few changes beceause fractals aren't always seen on the chart)

The idea is to show two lines: line 1 at 50period lowest high and line 2 at 50period highest low. I just couldn't get it right. And I know it should be EASY...

Please help me out here!! I'm new at coding so an explanation would be nice too.

Thanks!

//+------------------------------------------------------------------+
//|                                                                  |
//|        1BRKR                                                     |
//|                                                                  |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Red
input int PER     =50;                 //period 
input int history =3000;

double high[]; 				//indicator value of arrow indicator                                                 
double low[];
double LowestHigh;                     	
double HighestLow;
double HighLine[];			//array for LowestHigh and HighestLow
double LowLine[];
int i;
  
void init()
{

   SetIndexBuffer(0,HighLine);			
   SetIndexStyle(0,DRAW_LINE,2,2);
   SetIndexEmptyValue(0,0.0);  
  
   SetIndexBuffer(1,LowLine);
   SetIndexStyle(1,DRAW_LINE,2,2);
   SetIndexEmptyValue(1,0.0);

return;
}

int start()
  {
   LowestHigh=iCustom(NULL,0,"MS_SR",0,0);            //assigning latest high and low as highesthigh and lowestlow
   HighestLow=iCustom(NULL,0,"MS_SR",1,0);
   i=history;
   while(i>=0)
   {
   for (int cnt=0;cnt<PER;cnt++)                      //loop for determining lowesthigh and highestlow
   {  high[cnt]=iCustom(NULL,0,"MS_SR",0,cnt);
      low[cnt]=iCustom(NULL,0,"MS_SR",1,cnt);
      if (LowestHigh>high[cnt])LowestHigh=high[cnt];
      if (HighestLow<low[cnt])HighestLow=low[cnt];
   }
   HighLine[cnt]=LowestHigh;                         //assigning 50period highs and lows to indicator array
   LowLine[cnt]=HighestLow;
   i--;
   }  
   return(0);
  }
   
//+------------------------------------------------------------------+
Keith Watford
Moderator
12229
Keith Watford  
double STH[];
double STL[];
double LBRKR;
double SBRKR;
double BRKR_L[];
double BRKR_S[];

Use descriptive variable names so that others can easier understand what you are trying to do.

You are more likely to get help then.

nilihijiadbab
29
nilihijiadbab  
Keith Watford:

Use descriptive variable names so that others can easier understand what you are trying to do.

You are more likely to get help then.

Thanks! I think it should be more readable now.
Naguisa Unada
253
Naguisa Unada  

I do not know exactly what you are trying to do, but I think that this may be good.

int start()
  {
   int cnt, i=history-PER;
   double price_high, price_low;
   //LowestHigh=iCustom(NULL,0,"MS_SR",0,0);            //assigning latest high and low as highesthigh and lowestlow
   //HighestLow=iCustom(NULL,0,"MS_SR",1,0);
   //i=history;
   
   while(i>=0)
     {
      LowestHigh=iCustom(NULL,0,"MS_SR",0,i);
      HighestLow=iCustom(NULL,0,"MS_SR",1,i);
        
      //for(int cnt=0;cnt<PER;cnt++) //loop for determining lowesthigh and highestlow
      for(cnt=1;cnt<PER;cnt++)
        {
         //high[cnt]=iCustom(NULL,0,"MS_SR",0,cnt);
         //low[cnt]=iCustom(NULL,0,"MS_SR",1,cnt);
         price_high=iCustom(NULL,0,"MS_SR",0,i+cnt);
         price_low =iCustom(NULL,0,"MS_SR",1,i+cnt);
         
         //if(LowestHigh>high[cnt])LowestHigh=high[cnt];
         //if(HighestLow<low[cnt])HighestLow=low[cnt];
         if(LowestHigh>price_high)
            LowestHigh=price_high;
                
         if(HighestLow<price_low)
            HighestLow=price_low;
        }
        
      //HighLine[cnt]=LowestHigh;                         //assigning 50period highs and lows to indicator array
      //LowLine[cnt]=HighestLow;
      HighLine[i]=LowestHigh; 
      LowLine[i] =HighestLow;
      
      i--;
     }
     
   return(0);
  }
nilihijiadbab
29
nilihijiadbab  
Naguisa Unada:

I do not know exactly what you are trying to do, but I think that this may be good.

Thank you! This is what I've been trying to accomplish.

I've tried another solution yesterday...

int start()
  {
   i=history;
   for (int cnt=history-1;cnt>=0;cnt--)                      //loop for determining lowesthigh and highestlow
   {  high[cnt]=iCustom(NULL,0,"MS_SR",0,cnt);
      low[cnt]=iCustom(NULL,0,"MS_SR",1,cnt);
      HighLine[cnt]=high[ArrayMinimum(high,PER,cnt)];
      LowLine[cnt]=low[ArrayMaximum(low,PER,cnt)];    
   } 
   return(0);
  }
   

but this is giving me problems when I implement the indicator into an EA 

"incorrect start position for ArrayMaximum function"

Naguisa Unada
253
Naguisa Unada  
nilihijiadbab:

but this is giving me problems when I implement the indicator into an EA 

"incorrect start position for ArrayMaximum function"

Maybe buffers are not prepared.

nilihijiadbab
29
nilihijiadbab  
Naguisa Unada:

Maybe buffers are not prepared.

what do you mean with that? can you explain?
Naguisa Unada
253
Naguisa Unada  
nilihijiadbab :
what do you mean with that? can you explain?

You defined 4 arrays in the 1st post, but only 2 buffers were prepared.

nilihijiadbab
29
nilihijiadbab  
Naguisa Unada:

You defined 4 arrays in the 1st post, but only 2 buffers were prepared.

they were all prepared... but the error still occurs... 

//+------------------------------------------------------------------+
//|                                                                  |
//|        1BRKR                                                     |
//|                                                                  |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Green
#property indicator_color2 Red
input int PER     =50;                 //period 
input int history =3000;

double high[];                                                  
double low[];
double HighLine[3000];
double LowLine[];
int i;

void init()
{

   SetIndexBuffer(0,HighLine);
   SetIndexStyle(0,DRAW_LINE,2,2);
   SetIndexEmptyValue(0,0.0);  
  
   SetIndexBuffer(1,LowLine);
   SetIndexStyle(1,DRAW_LINE,2,2);
   SetIndexEmptyValue(1,0.0);
   
   SetIndexBuffer(2,high);
   SetIndexStyle(2,EMPTY,EMPTY,EMPTY,clrNONE);
   SetIndexBuffer(3,low);
   SetIndexStyle(3,EMPTY,EMPTY,EMPTY,clrNONE);
return;
}

int start()
  {
   i=history;
   for (int cnt=history-1;cnt>=0;cnt--)                      //loop for determining lowesthigh and highestlow
   {  high[cnt]=iCustom(NULL,0,"MS_SR",0,cnt);
      low[cnt]=iCustom(NULL,0,"MS_SR",1,cnt);
      HighLine[cnt]=high[ArrayMinimum(high,PER,cnt)];
      LowLine[cnt]=low[ArrayMaximum(low,PER,cnt)];    
   } 
   return(0);
  }
   
//+------------------------------------------------------------------+
Sergey Golubev
Moderator
103886
Sergey Golubev  

Forum on trading, automated trading systems and testing trading strategies

Press review

Sergey Golubev, 2018.03.11 05:55

Abandoned Baby candlestick pattern (based on the article)

andlestick pattern

  • "The Bullish Abandoned Baby candlestick pattern is a reversal pattern. The pattern has three candles. It forms at the bottom of a trend. In this pattern, the first candle is any long and bearish candle. The second candle is a small and bearish candle—or a Doji. The second candle doesn’t overlap the first or third candle. The third candle is any long and bullish candle."
  • "The Bearish Abandoned Baby pattern is a reversal pattern. The pattern has three candles. It forms at the peak of a trend. In this pattern, the first candle is long and bullish. The second candle is a small and bullish candle—or a Doji. The second candle doesn’t overlap the first or third candle. The third candle is long and bearish."


To add comments, please log in or register