Mathematical formula indicator

 

Dear Forum,

I want to create an indicator which shows the value for each bar of a mathematical formula. There is something wrong as it's not being painted right. What it could be?

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


double Buf_0[];

int init()
  {
   SetIndexBuffer(0,Buf_0);         
   SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);
   return(0);
  }



int start()
  {
   
   int      i;
   int      Counted_bars;

   Counted_bars=IndicatorCounted();


   i=Bars-Counted_bars-1;                       
   while(i>=0)                                  
   {
    Buf_0[i] =  Math(MainPair,SecondPair,TimeFrame,i);
    i--;
   }

         
   return(0);
  }

double Math(string MainPair, string SecondPair, int TF, int index)
{
      double VectorMain[500];
      double VectorSecond[500];
      
      double XperY[500];
      
      double XperY_Summatory;
      double First_Term;
      
      double Summatory_x;
      double Summatory_y;
      
      double Second_Term;
      
      double Summatory_x_3;
      double Summatory_y_3;
      double NperSumX_3;
      
      double Summatory_x_3_1;
      double Squared_Summatory_x_3_1;
      
      double Third_Term;
      
      double Summatory_x_4_1;      
      double Summatory_y_4;
      double NperSumY_4;
      
      double Summatory_y_4_1;
      double Squared_Summatory_y_4_1;      
      
      double Fourth_Term;
      
      double Result;
      
////////Calculate First Term----
      
      for(int i=1+index; i<=Bars2Compare+index; i++)
      {
            
            VectorMain[i]   = iClose(MainPair,TF,i);
            VectorSecond[i] = iClose(SecondPair,TF,i);

      }
      for(int j=1+index; j<=Bars2Compare+index; j++)
      {
            XperY[j]   = VectorMain[j]*VectorSecond[j];
      }
      
      if(XperY_Summatory!=0)
            XperY_Summatory=0;
            
      for(int k=1+index; k<=Bars2Compare+index; k++)
      {
            XperY_Summatory   = XperY_Summatory + XperY[k];
      }
      
      First_Term = Bars2Compare * XperY_Summatory;
      
      
///////Calculate Second Term----
      
      if(Summatory_x!=0)
            Summatory_x=0;
            
      for(k=1+index; k<=Bars2Compare+index; k++)
      {
            Summatory_x   = Summatory_x + VectorMain[k];
      }
      
      if(Summatory_y!=0)
            Summatory_y=0;
            
      for(k=1+index; k<=Bars2Compare+index; k++)
      {
            Summatory_y   = Summatory_y + VectorSecond[k];
      }
      Second_Term = Summatory_x * Summatory_y;
      
      //Print("Second_Term= ",Second_Term);
///////Calculate Third Term----  
      
      if(Summatory_x_3!=0)
            Summatory_x_3=0;
            
      for(k=1+index; k<=Bars2Compare+index; k++)
      {
            Summatory_x_3   = Summatory_x_3 + (VectorMain[k]*VectorMain[k]);
      }
      NperSumX_3 = Bars2Compare*Summatory_x_3;
      
      //Print("NperSumX_3= ",NperSumX_3);
      
      if(Summatory_x_3_1!=0)
            Summatory_x_3_1=0;
            
      for(k=1+index; k<=Bars2Compare+index; k++)
      {
            Summatory_x_3_1   = Summatory_x_3_1 + VectorMain[k];
      }
      Squared_Summatory_x_3_1 = Summatory_x_3_1*Summatory_x_3_1;  
      
      Third_Term = NperSumX_3 - Squared_Summatory_x_3_1;
      
      //Print("Third_Term= ",Third_Term);
      
///////Calculate Fouth Term----

      if(Summatory_y_4!=0)
            Summatory_y_4=0;
            
      for(k=1+index; k<=Bars2Compare+index; k++)
      {
            Summatory_y_4   = Summatory_y_4 + (VectorSecond[k]*VectorSecond[k]);
      }
      NperSumY_4 = Bars2Compare*Summatory_y_4;
      
      if(Summatory_x_4_1!=0)
            Summatory_x_4_1=0;
            
      for(k=1+index; k<=Bars2Compare+index; k++)
      {
            Summatory_y_4_1   = Summatory_y_4_1 + VectorSecond[k];
      }
      Squared_Summatory_y_4_1 = Summatory_y_4_1*Summatory_y_4_1;  
      
      Fourth_Term = NperSumY_4 - Squared_Summatory_y_4_1; 
      
      //Print("Fourth_Term= ",Fourth_Term);
      
//////Final Calculation

      Result = (First_Term-Second_Term)/MathSqrt(Third_Term*Fourth_Term);
      return(Result);           
}


Any help would be greatly appreciated!

Regards.

 
Asked and answered TWICE ALREADY
 
No, this is other indicator much different. Not related.
 

EXACTLY THE SAME PROBLEM

   Buf_0[i] =  Math(MainPair,SecondPair,TimeFrame,i);
:
double Math(string MainPair, string SecondPair, int TF, int index){
:
      for(int i=1+index; i<=Bars2Compare+index; i++)
      {
            
            VectorMain[i]   = iClose(MainPair,TF,i);
            VectorSecond[i] = iClose(SecondPair,TF,i);

in start() i is the current pair/current TF

in Math() index is the current pair/current TF. Therefor Math()'s i is ALSO current pair/current TF

iClose( other than current pair/TF, i) is BOGUS

 

Thanks for your patience. Is this the correct way? exactly the same problem, exactly the same solution...

   while(i>=0)                                  
   {
    
    datetime   when0  = Time[i];
    int        iMain0 = iBarShift(MainPair,TimeFrame,when0);
    
    Buf_0[i] =  Math(MainPair,SecondPair,TimeFrame,iMain0);
    i--;
   }
:
double Math(string MainPair, string SecondPair, int TF, int  index){
:
      for(int i=1+index; i<=Bars2Compare+index; i++)
      {

            datetime   when1  = Time[i];
            int        iMain1 = iBarShift(MainPair,TimeFrame,when1);
            int        iScnd1 = iBarShift(SecondPair,TimeFrame,when1); 
              
            VectorMain[i]   = iClose(MainPair,TF,iMain1);
            VectorSecond[i] = iClose(SecondPair,TF,iScnd1);

      }