Integrating indicator code directly into EA

To add comments, please log in or register
Mike
237
Mike  

I have this code for the i-Regr indicator. I would like to integrate it directly into my EA so that I do not have to call iCustom() as it is very slow. It seems simple enough but I cannot figure out how to get last bar's value. Since I only care about the current value and last bar's value I think I can avoid using ArrayCopyRates() (since that function is slow too). Of course if I coded this myself I would have no problem but the lack of descriptive variable names makes it very tough for me to understand what to change. If someone could give me a tip I'd appreciate it.

//----
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 Black
#property indicator_color2 Black
#property indicator_color3 Black
#property indicator_color4 Black
#property indicator_color5 Black
#property indicator_color3 Black


extern int degree =1;
extern double kstd = 2.0;
extern int bars = 30;
extern int shift = 0;


//-----
double fx[],sqh[],sql[];
double fx_h[],sqh_h[],sql_h[];

double ai[10,10],b[10],x[10],sx[20];
double sum;  
int ip,p,n,f;
double qq,mm,tt;
int ii,jj,kk,ll,nn;
double sq;

int i0 = 0;

/*  
void clear()
{
  int total = ObjectsTotal();  
  for (int i=total-1; i >= 0; i--) 
  {
    string name = ObjectName(i);
    if (StringFind(name, prefix) == 0) ObjectDelete(name);
  }
}
*/

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
  SetIndexBuffer(0, fx); // Áóôåðû ìàññèâîâ èíäèêàòîðà
  SetIndexBuffer(1, sqh);
  SetIndexBuffer(2, sql);

  SetIndexStyle(0, DRAW_LINE);
  SetIndexStyle(1, DRAW_LINE);
  SetIndexStyle(2, DRAW_LINE);

  SetIndexEmptyValue(0, 0.0);
  SetIndexEmptyValue(1, 0.0);
  SetIndexEmptyValue(2, 0.0);
  
  SetIndexShift(0, shift);
  SetIndexShift(1, shift);
  SetIndexShift(2, shift);
  
 
  return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{

  //clear();
  return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
  
  
  
  if (Bars < bars) return;
  
   
//---- 
    
  int mi; // ïåðåìåííàÿ èñïîëüçóþùàÿñÿ òîëüêî â start
  ip = bars;
  p=ip; // òèïà ïðèñâàèâàíèå
  sx[1]=p+1; // ïðèìå÷àíèå - [] - îçíà÷àåò ìàññèâ
  nn = degree+1;

  SetIndexDrawBegin(0, Bars-p-1);
  SetIndexDrawBegin(1, Bars-p-1);
  SetIndexDrawBegin(2, Bars-p-1); 
  

//----------------------sx-------------------------------------------------------------------
  for(mi=1;mi<=nn*2-2;mi++) // ìàòåìàòè÷åñêîå âûðàæåíèå - äëÿ âñåõ mi îò 1 äî nn*2-2 
  {
    sum=0;
    for(n=i0;n<=i0+p;n++)
    {
       sum+=MathPow(n,mi);
    }
    sx[mi+1]=sum;
  }  
  //----------------------syx-----------
  for(mi=1;mi<=nn;mi++)
  {
    sum=0.00000;
    for(n=i0;n<=i0+p;n++)
    {
       if(mi==1) sum+=Close[n];
       else sum+=Close[n]*MathPow(n,mi-1);
    }
    b[mi]=sum;
  } 
//===============Matrix=======================================================================================================
  for(jj=1;jj<=nn;jj++)
  {
    for(ii=1; ii<=nn; ii++)
    {
       kk=ii+jj-1;
       ai[ii,jj]=sx[kk];
    }
  }  
//===============Gauss========================================================================================================
  for(kk=1; kk<=nn-1; kk++)
  {
    ll=0;
    mm=0;
    for(ii=kk; ii<=nn; ii++)
    {
       if(MathAbs(ai[ii,kk])>mm)
       {
          mm=MathAbs(ai[ii,kk]);
          ll=ii;
       }
    }
    if(ll==0) return(0);   
    if (ll!=kk)
    {
       for(jj=1; jj<=nn; jj++)
       {
          tt=ai[kk,jj];
          ai[kk,jj]=ai[ll,jj];
          ai[ll,jj]=tt;
       }
       tt=b[kk];
       b[kk]=b[ll];
       b[ll]=tt;
    }  
    for(ii=kk+1;ii<=nn;ii++)
    {
       qq=ai[ii,kk]/ai[kk,kk];
       for(jj=1;jj<=nn;jj++)
       {
          if(jj==kk) ai[ii,jj]=0;
          else ai[ii,jj]=ai[ii,jj]-qq*ai[kk,jj];
       }
       b[ii]=b[ii]-qq*b[kk];
    }
  }  
  x[nn]=b[nn]/ai[nn,nn];
  for(ii=nn-1;ii>=1;ii--)
  {
    tt=0;
    for(jj=1;jj<=nn-ii;jj++)
    {
       tt=tt+ai[ii,ii+jj]*x[ii+jj];
       x[ii]=(1/ai[ii,ii])*(b[ii]-tt);
    }
  } 
//===========================================================================================================================
  for(n=i0;n<=i0+p;n++)
  {
    sum=0;
    for(kk=1;kk<=degree;kk++)
    {
       sum+=x[kk+1]*MathPow(n,kk);
    }
    fx[n]=x[1]+sum;
  } 
//-----------------------------------Std-----------------------------------------------------------------------------------
  sq=0.0;
  for(n=i0;n<=i0+p;n++)
  {
    sq+=MathPow(Close[n]-fx[n],2);
  }
  sq=MathSqrt(sq/(p+1))*kstd;

  for(n=i0;n<=i0+p;n++)
  {
    sqh[n]=fx[n]+sq;
    sql[n]=fx[n]-sq;
  }

  return(0);
}
//+------------------------------------------------------------------+
Mike
237
Mike  

I actually figured it out.. sorta.

This is what I have so far:

void Regression (int timeframe)
{
   static double fx[],sqh[],sql[];
   static double fx_h[],sqh_h[],sql_h[];

   static double ai[10,10],b[10],x[10],sx[20];
   static double sum;  
   static int ip,p,n,f;
   static double qq,mm,tt;
   static int ii,jj,kk,ll,nn;
   static double sq;

int i0 = 0;
   int IBARS = iBars(Symbol(), timeframe);
   if(ArraySize(fx) < IBARS)
     {
       ArraySetAsSeries(fx, false);
       //----  
       ArrayResize(fx, IBARS); 
       //----
       ArraySetAsSeries(fx, true);
     } 
     
  int mi; // ïåðåìåííàÿ èñïîëüçóþùàÿñÿ òîëüêî â start
  ip = Regr.bars;
  p=ip; // òèïà ïðèñâàèâàíèå
  sx[1]=p+1; // ïðèìå÷àíèå - [] - îçíà÷àåò ìàññèâ
  nn = Regr.degree+1;
  

//----------------------sx-------------------------------------------------------------------
  for(mi=1;mi<=nn*2-2;mi++) // ìàòåìàòè÷åñêîå âûðàæåíèå - äëÿ âñåõ mi îò 1 äî nn*2-2 
  {
    sum=0;
    for(n=i0;n<=i0+p;n++)
    {
       sum+=MathPow(n,mi);
    }
    sx[mi+1]=sum;
  }  
  //----------------------syx-----------
  for(mi=1;mi<=nn;mi++)
  {
    sum=0.00000;
    for(n=i0;n<=i0+p;n++)
    {
       if(mi==1) sum+=Close[n];
       else sum+=Close[n]*MathPow(n,mi-1);
    }
    b[mi]=sum;
  } 
//===============Matrix=======================================================================================================
  for(jj=1;jj<=nn;jj++)
  {
    for(ii=1; ii<=nn; ii++)
    {
       kk=ii+jj-1;
       ai[ii,jj]=sx[kk];
    }
  }  
//===============Gauss========================================================================================================
  for(kk=1; kk<=nn-1; kk++)
  {
    ll=0;
    mm=0;
    for(ii=kk; ii<=nn; ii++)
    {
       if(MathAbs(ai[ii,kk])>mm)
       {
          mm=MathAbs(ai[ii,kk]);
          ll=ii;
       }
    }
    if(ll==0) return(0);   
    if (ll!=kk)
    {
       for(jj=1; jj<=nn; jj++)
       {
          tt=ai[kk,jj];
          ai[kk,jj]=ai[ll,jj];
          ai[ll,jj]=tt;
       }
       tt=b[kk];
       b[kk]=b[ll];
       b[ll]=tt;
    }  
    for(ii=kk+1;ii<=nn;ii++)
    {
       qq=ai[ii,kk]/ai[kk,kk];
       for(jj=1;jj<=nn;jj++)
       {
          if(jj==kk) ai[ii,jj]=0;
          else ai[ii,jj]=ai[ii,jj]-qq*ai[kk,jj];
       }
       b[ii]=b[ii]-qq*b[kk];
    }
  }  
  x[nn]=b[nn]/ai[nn,nn];
  for(ii=nn-1;ii>=1;ii--)
  {
    tt=0;
    for(jj=1;jj<=nn-ii;jj++)
    {
       tt=tt+ai[ii,ii+jj]*x[ii+jj];
       x[ii]=(1/ai[ii,ii])*(b[ii]-tt);
    }
  } 
//===========================================================================================================================
  for(n=i0;n<=i0+p;n++)
  {
    sum=0;
    for(kk=1;kk<=Regr.degree;kk++)
    {
       sum+=x[kk+1]*MathPow(n,kk);
    }
    fx[n]=x[1]+sum;
  } 
//-----------------------------------Std-----------------------------------------------------------------------------------
  sq=0.0;
  for(n=i0;n<=i0+p;n++)
  {
    sq+=MathPow(Close[n]-fx[n],2);
  }
  sq=MathSqrt(sq/(p+1))*Regr.kstd;


    R.U0=fx[0]+sq;
    R.L0=fx[0]-sq;
    R.U1=fx[1]+sq;
    R.L1=fx[1]-sq;
    R.M0=fx[0];
    R.M1=fx[1];
  

  return(0);
}

I wonder if there is a way to trim it down/speed it up some more.

Ray
2653
Ray  

Parts of it are ttoooo hhaarrdd ffoorr mmee ttoo rreeaadd ssoo II''ll ppaass.

derek
98
derek  

hi Mfurlend, can you halp do the same thing this with my indicator over at my thread..

much thanks

help would be appreciated,

Huslterscreed

Mike
237
Mike  
Yeah phy I agree with you (I didn't write it so its not my fault lol)
Mike
237
Mike  
hustlerscreed:

hi Mfurlend, can you halp do the same thing this with my indicator over at my thread..

much thanks

help would be appreciated,

Huslterscreed


I can't seem to find it. Send me a link please.
derek
98
derek  

the forum name is in the recent forum list.. look for my name as the starter

whroeder1
17946
whroeder1  
mfurlend:

I actually figured it out.. sorta.

This is what I have so far:

I wonder if there is a way to trim it down/speed it up some more.

Inefficient code, try:
extern int      COG.Bars            = 240;
extern int      COG.Degree          =   4;
                                            #define COG.Degree.MAX  10
                                            #define COG.Degree.2MAX 20  // 2*MAX
//+------------------------------------------------------------------+
//| Compute Center Of Gravity values                                 |
//+------------------------------------------------------------------+
void ComputeCOG() {
/* The no repainting variant of the COG indicator "JB Center of Gravity.mq4"
 * was created by "ANG3110@latchess.com and Jürgen Bouché"
 * http://juergen.bouche.de Original indicator code from NG3110@latchess.com
 *
 * This creates a Nth degree least squares polynominal
 *     0       1     2     3     4      5     6     7     8
 * EXn E1=bars EX    E(X2) E(X3) E(X4)  E(X5) E(X6) E(X7) E(X8)
 *
 * Aii 0       1     2     3     4          Bi
 * 0   E1=bars Ex    E(x2) E(x3) E(x4)      0   EY              (   )( ) (  )
 * 1   Ex      E(x2) E(x3) E(x4) E(x5)      1   E(YX)           (   )( ) (  )
 * 2   E(x2)   E(x3) E(x4) E(x5) E(x6)      2   E(YX2)          (Aii)(X)=(Bi)
 * 3   E(x3)   E(x4) E(x5) E(x6) E(x7)      3   E(YX3)          (   )( ) (  )
 * 4   E(x4)   E(x5) E(x6) E(x7) E(x8)      4   E(YX4)          (   )( ) (  )
 ******************************************************************************/
    double   Bi[COG.Degree.MAX];
    double  EXn[COG.Degree.2MAX];       int COG.Degree2 = COG.Degree*2;
    double  Aii[COG.Degree.MAX, COG.Degree.MAX];
    for(int ii = 0; ii <= COG.Degree;  ii++)             Bi[ii] = 0;
    for(    ii = 0; ii <= COG.Degree2; ii++)            EXn[ii] = 0;

    int maxBar = MathMin(Bars,shift+COG.Bars)-1;
    for(int i = shift; i <= maxBar; i++) {      // Count up for maximum accuracy.
        double  c = Close[i], Xn=1;     // Change Open/change RMS
        for(ii = 0; ii <= COG.Degree;  ii++, Xn *= i) { Bi[ii]  += Xn * c;
                                                        EXn[ii] += Xn;      }
        for(      ; ii <= COG.Degree2; ii++, Xn *= i)   EXn[ii] += Xn;
    }
    double  mean=Bi[0]/EXn[0];
    for(int jj = 0; jj <= COG.Degree; jj++) {
        for(ii = 0; ii <= COG.Degree; ii++) {   Aii[ii, jj] = EXn[ii + jj]; }
    }

    for(int kk = 0; kk <= COG.Degree; kk++) {           // Gaussian Elimination
        int ll = kk;    double mm = MathAbs(Aii[ll, kk]);
        for(ii = kk+1; ii <= COG.Degree; ii++) {        // Largest magintude.
            double tt = MathAbs(Aii[ii, kk]);
            if (tt > mm) {  ll = ii;    mm = tt;    }
        }

        if (ll != kk) {                                 // Pivot.
            for(jj = 0; jj <= COG.Degree; jj++) {
                tt          = Aii[kk, jj];
                Aii[kk, jj] = Aii[ll, jj];
                Aii[ll, jj] = tt;
            }
            tt = Bi[kk]; Bi[kk] = Bi[ll]; Bi[ll] = tt;
        }
        for(ii = kk + 1; ii <= COG.Degree; ii++) {      // Reduce.
            double qq; qq = Aii[ii, kk] / Aii[kk, kk];  // Aii[ii,kk]=0
            for(jj = kk+1; jj <= COG.Degree; jj++) {
                Aii[ii, jj] -= qq * Aii[kk, jj];
            }
            Bi[ii] -= qq * Bi[kk];
    }   }
    double  x[COG.Degree.MAX];                          // Back substitution
        x[COG.Degree] = Bi[COG.Degree] / Aii[COG.Degree, COG.Degree];
        for (ii = COG.Degree-1; ii >= 0; ii--) {
                tt = Bi[ii];
                for(jj = ii+1; jj <= COG.Degree; jj++){
                        tt -= Aii[ii, jj] * x[jj];
                }
                x[ii] = tt/Aii[ii, ii];
        }
    // Least squares completed.  Compute residual and final.
    double  ssr.p = 0,  ssr.m = 0;
    for(i = maxBar; i >= shift; i--) {  // Count down so at end, sum = Y(shift).
        double                                              sum  = x[0]; //X0==1
        for(kk = 1, Xn=i; kk <= COG.Degree; kk++, Xn *= i)  sum += x[kk] * Xn;
        double res.p = Close[i] - sum,  res.m = Close[i] - mean;
        ssr.p += res.p*res.p;           ssr.m += res.m*res.m;
    }
    double  rms = MathSqrt(ssr.p / (maxBar-shift+1)),
            std = MathSqrt(ssr.m / (maxBar-shift+1));
    COG[COGCENTR]    = sum;  // sum = Y(shift).
    COG[COGM2STD]    = sum - std * 2;    COG[COGP2STD]    = sum + std * 2;
    COG[COGM1STD]    = sum - std;        COG[COGP1STD]    = sum + std;
    COG[COGM3RMS]    = sum - rms * 3;    COG[COGP3RMS]    = sum + rms * 3;
    COG[COGM2RMS]    = sum - rms * 2;    COG[COGP2RMS]    = sum + rms * 2;
    COG[COGM1RMS]    = sum - rms;        COG[COGP1RMS]    = sum + rms;
    return(0);
}   // ComputeCOG

Mike
237
Mike  
Wow WHRoeder, that is some professional looking code! Did you write that yourself?
To add comments, please log in or register