Download MetaTrader 5
To add comments, please log in or register
Improve your rating. Execute orders in Freelance service!
Jim
20
Jim 2016.06.09 04:51 
//+------------------------------------------------------------------+
//|                                            Torinex BarsSince.mq4 |
//|                                       Torinex Consulting Pty Ltd |
//|                                          https://www.torinex.com |
//+------------------------------------------------------------------+

   #property copyright "Torinex Consulting Pty Ltd"
   #property link      "https://www.torinex.com"
   #property version   "1.00"
   #property strict
   #property indicator_separate_window
   #property indicator_buffers 4
   #property indicator_plots   4

//--- plot 1st Indicator
   #property indicator_label1  "Last Hihger"
   #property indicator_type1   DRAW_HISTOGRAM
   #property indicator_color1  clrBlue
   #property indicator_style1  STYLE_SOLID
   #property indicator_width1  4

//--- plot 2nd Indicator
   #property indicator_label2  "Last Lower"
   #property indicator_type2   DRAW_HISTOGRAM
   #property indicator_color2  clrRed
   #property indicator_style2  STYLE_SOLID
   #property indicator_width2  4

//--- plot 1st Indicator
   #property indicator_label3  "Cumulative Higher"
   #property indicator_type3   DRAW_LINE
   #property indicator_color3  clrBlue
   #property indicator_style3  STYLE_SOLID
   #property indicator_width3  1

//--- plot 2nd Indicator
   #property indicator_label4  "Cumulative Lower"
   #property indicator_type4   DRAW_LINE
   #property indicator_color4  clrRed
   #property indicator_style4  STYLE_SOLID
   #property indicator_width4  1

   
//--- input parameters
   input double                Inp_DecayRate  = 0.5   ;     //Decay Rate

//--- declare indicator buffers
   double        
   Last_Higher[],
   Last_Lower[],
   Cumulative_Higher[],
   Cumulative_Lower[];


   
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Last_Higher);
   SetIndexBuffer(1,Last_Lower);
   SetIndexBuffer(2,Cumulative_Higher);
   SetIndexBuffer(3,Cumulative_Lower);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
   int OnCalculate(const int rates_total,
                   const int prev_calculated,
                   const datetime &time[],
                   const double &open[],
                   const double &high[],
                   const double &low[],
                   const double &close[],
                   const long &tick_volume[],
                   const long &volume[],
                   const int &spread[])
  {
  
//--- set all buffers as Time Series (i.e. current =[0] and previous=[1])       
        ArraySetAsSeries(Last_Higher,true);
        ArraySetAsSeries(Last_Lower,true);
        ArraySetAsSeries(Cumulative_Higher,true);
        ArraySetAsSeries(Cumulative_Lower,true);
        ArraySetAsSeries(high,true);
        ArraySetAsSeries(low,true);
        ArraySetAsSeries(open,true);
        ArraySetAsSeries(close,true);
        
//--- declare variables 
        int
        i,
        CountBack;
        

        
        
//---Indicator Loop



        int bars = rates_total - 1;
        if(prev_calculated > 0) bars = rates_total - prev_calculated;
   //for(i=bars; i >= 0; i--)
   
//--------------------------------------------------------
   for(i=0; i <= Bars; i++)
      {     
         for(CountBack=1; CountBack<=bars; CountBack++)
            {
               if(High[CountBack+i] >= High[i])
                  {
                     Last_Higher[i] = CountBack;
                     break;
                  }   
            }   

         for(CountBack=1; CountBack<=bars; CountBack++)
            {
               if(Low[CountBack+i] <= Low[i])
                  {
                     Last_Lower[i] = -CountBack;
                     break;
                  }   
            }   
      } 
   
//--------------------------------------------------------
   for(i=0-1; i <= Bars; i++)
      {     
         if(Last_Higher[i] > 1)Cumulative_Higher[i+1] = Cumulative_Higher[i+1] + Last_Higher[i];
            else Cumulative_Higher[i] = Cumulative_Higher[i+1] * Inp_DecayRate;     

         if(Last_Lower[i] < -1)Cumulative_Lower[i] = Cumulative_Lower[i+1] - Last_Lower[i];
            else Cumulative_Lower[i] = Cumulative_Lower[i+1] * Inp_DecayRate;     
      }      
   
   
                 
               
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
Jim
20
Jim 2016.06.09 04:55  

Hi Programmers;

 

I've written a Custom Indicator that determines how many bars back was the last higher and lower High[] and Low[]. This counts from the most recent bar back in time to Bars. This part seems to work fine.

The second part tries to use a cumulative count with a decay rate.  This tries to count from the oldest bar at Bars to the most recent. The "for" loop does not seem to run at all. I'm probably missing something obvious so if anyone can point my error out I would be grateful.

 

thanks all 

h3nrixd
4
h3nrixd 2016.06.09 09:47  
TorinexTrading:

Hi Programmers;

 

I've written a Custom Indicator that determines how many bars back was the last higher and lower High[] and Low[]. This counts from the most recent bar back in time to Bars. This part seems to work fine.

The second part tries to use a cumulative count with a decay rate.  This tries to count from the oldest bar at Bars to the most recent. The "for" loop does not seem to run at all. I'm probably missing something obvious so if anyone can point my error out I would be grateful.

 

thanks all 

int lastBar  = (Bars - 1);
int firstBar = 0;

// Loop forward
for(i = 0; i <= (Bars - 1); i++)
{
    // code
}

// Loop backwards
for(i = (Bars - 1); i >= 0; i--)
{
    // code
}
whroeder1
13602
whroeder1 2016.06.10 12:30  
TorinexTrading:
        int bars = rates_total - 1;
        if(prev_calculated > 0) bars = rates_total - prev_calculated;

   for(i=0; i <= Bars; i++){     
         for(CountBack=1; CountBack<=bars; CountBack++){
               if(High[CountBack+i] >= High[i])
:
   return(rates_total);

Do your lookbacks correctly.
Jim
20
Jim 2016.06.11 05:47  

Thanks WHRoder and h3nrixd. Very helpful.

cheers 

Jim
20
Jim 2016.06.12 04:41  

WHRoeder;

 

Thanks for pointing me to your link on counting up and down lookbacks correctly.

Whilst I stress that I am not a programmer, I am not seeing the point: If the array is set as a time series (ArraySetAsSeries) then I would assume that I can go through it backwards or forwards. Or am I missing something?

 

Thanks again. 

/
To add comments, please log in or register