Average distance indicator

 

Dear Forum,

I'm trying to code a custom indicator which draws a line which is the average distance between two pairs. Below is the code. I don't know why it's not working. I know the Averages function is working correctly:

#property indicator_separate_window
#property indicator_buffers 2       // Number of buffers
#property indicator_color1 Yellow     // Color of the 1st line
#property indicator_color2 Red      // Color of the 2nd line

extern int     Bars2Compare      = 50;
extern string  MainPair          = "GBPUSD";
extern string  SecondPair        = "EURUSD";
extern string     TimeFrame         = "M1";

double Buf_0[],Buf_1[];

double MainClose_1[];
double MainClose_5[]; 
double MainClose_15[]; 
double MainClose_30[]; 
double MainClose_60[];
double MainClose_240[];
double MainClose_1440[];

double M_SumAllClose_1;
double M_SumAllClose_5;
double M_SumAllClose_15;
double M_SumAllClose_30;
double M_SumAllClose_60;
double M_SumAllClose_240;
double M_SumAllClose_1440;

double M_FinalAverage_1;
double M_FinalAverage_5;
double M_FinalAverage_15;
double M_FinalAverage_30;
double M_FinalAverage_60;
double M_FinalAverage_240;
double M_FinalAverage_1440;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
      SetIndexBuffer(0,Buf_0);         // Assigning an array to a buffer
   SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);// Line style
   SetIndexBuffer(1,Buf_1);         // Assigning an array to a buffer
   SetIndexStyle (1,DRAW_LINE,STYLE_SOLID,1);// Line style
   return;  
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
         int      counted_bars=IndicatorCounted();
         int      i;                           // Bar index
         int      Counted_bars;                // Number of counted bars

         Counted_bars=IndicatorCounted(); // Number of counted bars
         i=Bars-Counted_bars-1;           // Index of the first uncounted
         while(i>=0)                      // Loop for uncounted bars
         {
                  Buf_0[i+1]=Averages(MainPair,SecondPair,TimeFrame, i);             // Value of 0 buffer on i bar
                  //Buf_1[i]=Low[i];              // Value of 1st buffer on i bar
                  i--;                          // Calculating index of the next bar
         }
         //Print(Averages(MainPair,SecondPair,TimeFrame))
         return(0);
  }
//+------------------------------------------------------------------+

double Averages(string MainPair, string SecondPair, string TF, int shift)
{

         for(int i=1+shift; i<=Bars2Compare+shift; i++)
         {
               if(M_SumAllClose_1!=0)
                     M_SumAllClose_1=0;
               if(M_SumAllClose_5!=0)
                     M_SumAllClose_5=0;
               if(M_SumAllClose_15!=0)
                     M_SumAllClose_15=0;                     
               if(M_SumAllClose_30!=0)
                     M_SumAllClose_30=0;
               if(M_SumAllClose_60!=0)
                     M_SumAllClose_60=0;
               if(M_SumAllClose_240!=0)
                     M_SumAllClose_240=0;
               if(M_SumAllClose_1440!=0)
                     M_SumAllClose_1440=0;                    
                                    
               MainClose_1[i]    =  MathAbs(iClose(MainPair,1,i)    - iClose(SecondPair,1,i));
               MainClose_5[i]    =  MathAbs(iClose(MainPair,5,i)    - iClose(SecondPair,5,i));
               MainClose_15[i]   =  MathAbs(iClose(MainPair,15,i)   - iClose(SecondPair,15,i));
               MainClose_30[i]   =  MathAbs(iClose(MainPair,30,i)   - iClose(SecondPair,30,i));                                 
               MainClose_60[i]   =  MathAbs(iClose(MainPair,60,i)   - iClose(SecondPair,60,i)); 
               MainClose_240[i]  =  MathAbs(iClose(MainPair,240,i)  - iClose(SecondPair,240,i)); 
               MainClose_1440[i] =  MathAbs(iClose(MainPair,1440,i) - iClose(SecondPair,1440,i));                
                          
         }
         for(int j=1;j<=Bars2Compare;j++)
         {
               M_SumAllClose_1    = M_SumAllClose_1   +  MainClose_1[j];
               M_SumAllClose_5    = M_SumAllClose_5   +  MainClose_5[j];
               M_SumAllClose_15   = M_SumAllClose_15  +  MainClose_15[j]; 
               M_SumAllClose_30   = M_SumAllClose_30  +  MainClose_30[j];
               M_SumAllClose_60   = M_SumAllClose_60  +  MainClose_60[j];
               M_SumAllClose_240  = M_SumAllClose_240 +  MainClose_240[j];
               M_SumAllClose_1440 = M_SumAllClose_240 +  MainClose_1440[j];
         }
                        
         M_FinalAverage_1    =  M_SumAllClose_1   / Bars2Compare;
         M_FinalAverage_5    =  M_SumAllClose_5   / Bars2Compare;
         M_FinalAverage_15   =  M_SumAllClose_15  / Bars2Compare; 
         M_FinalAverage_30   =  M_SumAllClose_30  / Bars2Compare; 
         M_FinalAverage_60   =  M_SumAllClose_60  / Bars2Compare; 
         M_FinalAverage_240  =  M_SumAllClose_240 / Bars2Compare;          
         M_FinalAverage_1440 =  M_SumAllClose_1440/ Bars2Compare;         

         

                                 
         if(TF=="M1")
               return(M_FinalAverage_1);
         else if(TF=="M5")
               return(M_FinalAverage_5);
         else if(TF=="M15")
               return(M_FinalAverage_15);
         else if(TF=="M30")
               return(M_FinalAverage_30);
         else if(TF=="H1")
               return(M_FinalAverage_60);
         else if(TF=="H4")
               return(M_FinalAverage_240);
         else if(TF=="D1")
               return(M_FinalAverage_1440);

}

I'd appreciate your input. Thanks

 

average on distance eurousd_gbpusd

example

make an indicator display difference of EURUSD and GBPUSD

put moving average on indicators data and you get the moving average of the difference

same as iMAonArray

 

Thanks, but could you please tell what is wrong with the code above(if you know)?

Also, can you share this indicator?

Regards

 

I'm not that good at working with arrays, so I am not sure whether the fact that you do not size the MainClose arrays will cause problems or not.

Instead of all the calculating in the function, why not just use an additional buffer array and work with whatever time-frame is entered in the external parameters?

 
I got it working, for Buf_0, but now the problem is in Buf_1 when I try to make an average of the last 50 values of the array.
int start()
  {
         int      counted_bars=IndicatorCounted();
         int      i;                           // Bar index
         int      Counted_bars;                // Number of counted bars

         Counted_bars=IndicatorCounted(); // Number of counted bars
         i=Bars-Counted_bars-1;           // Index of the first uncounted
         while(i>=0)                      // Loop for uncounted bars
         {
                  
                  
                  
                  Buf_0[i]=  iClose(MainPair,TimeFrame,i)    - iClose(SecondPair,TimeFrame,i);           
                                
                  Buf_1[i]= iMAOnArray(Buf_0,0,50,0,MODE_SMA,i);
                  i--;                          
         }
         
         return(0);
  }
 

do it like this

int start()
  {
   int      i,	Counted_bars=IndicatorCounted();

//-	---- loop to accumulate values ----
   i=Bars-Counted_bars-1;			
   while(i>=0)                       		
   {
    Buf_0[i] =  iClose(MainPair,TimeFrame,i) - iClose(SecondPair,TimeFrame,i);
    i--;
   }
//-	----    Loop for iMAOnArray    ----
   i=Bars-Counted_bars-1;           
   while(i>=0)                      		
   {
    Buf_1[i]= iMAOnArray(Buf_0,0,50,0,MODE_SMA,i);
    i--;                                     
   }
         
   return(0);
  }
 

Thank you SDC. It worked. G-d bless you.

I see that each Buffer required a different loop. I thought it could be done in one same loop, but that was the very problem.

Thank you again.

 

Yes it is not very clear in the function description. When it says "data must be previously prepared" that is what it means.

 
Buf_0[i]=  iClose(MainPair,TimeFrame,i)    - iClose(SecondPair,TimeFrame,i);

You are also mixing apples and oranges and grapes. "i" is the index for the current pair/current time frame, but you are using it with other pairs/other time frames. Won't work.

datetime when  = Time[i];
int      iMain = iBarShift(MainPair,TimeFrame,when),
         iScnd = iBarShift(SecondPair,TimeFrame,when);
Buf_0[i]=  iClose(MainPair,TimeFrame,iMain)    - iClose(SecondPair,TimeFrame,iScnd);
 

Thanks WHRoeder.

Like I have it coded now is like SDC instructed me above:

extern int     Bars2Compare      = 50;
extern string  MainPair          = "GBPUSD";
extern string  SecondPair        = "EURUSD";
extern int     TimeFrame         = 1;

double Buf_0[],Buf_1[];

double Aux_0[],Aux_1[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexBuffer(0,Buf_0);         
   SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);
   SetIndexLabel(0,"Difference of Closing");
   SetIndexBuffer(1,Buf_1);        
   SetIndexStyle (1,DRAW_LINE,STYLE_SOLID,1);
   SetIndexLabel(1,"Average of Difference");
   IndicatorShortName("Average between " + MainPair + " and " + SecondPair + " of Timeframe " + TimeFrame + " Bars2Compare:" + Bars2Compare);   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int      counted_bars=IndicatorCounted();
   int      i;
   int      Counted_bars;

   Counted_bars=IndicatorCounted();

//-     ----loop to accumulate values----
   i=Bars-Counted_bars-1;                       
   while(i>=0)                                  
   {
    Buf_0[i] =  iClose(MainPair,TimeFrame,i) - iClose(SecondPair,TimeFrame,i);
    i--;
   }
//-   ----Loop for iMAOnArray----
   i=Bars-Counted_bars-1;           
   while(i>=0)                                  
   {
    Buf_1[i]= iMAOnArray(Buf_0,0,Bars2Compare,0,MODE_SMA,i);
    i--;                                     
   }
         
   return(0);
  }

And this is what I got:

It's being correctly calculated for the other time frames?

 
No
Reason: