Price Data Components
Series array that contains open prices of each barSeries array that contains close prices for each bar
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
XLinesReg
//+------------------------------------------------------------------+
//|                                                     XLinesReg.mq4|
//|                                   excelf@gmail.com, skype: excelf|
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 8

#property indicator_color1 Green
#property indicator_color2 Green
#property indicator_color3 Gold
#property indicator_color4 Gold
#property indicator_color5 Orange
#property indicator_color6 Orange
#property indicator_color7 Red
#property indicator_color8 Red

double Buffer0[];
double Buffer1[];
double Buffer2[];
double Buffer3[];
double Buffer4[];
double Buffer5[];
double Buffer6[];
double Buffer7[];

int price=0;
int myPeriod=PERIOD_D1;

extern int days=90;
extern int period1 = 40;
extern int history = 5000;
extern double step = 0.3;
extern int degree=1;

double avgDayRange;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexBuffer(0,Buffer0);
   SetIndexBuffer(1,Buffer1);
   SetIndexBuffer(2,Buffer2);
   SetIndexBuffer(3,Buffer3);
   SetIndexBuffer(4,Buffer4);
   SetIndexBuffer(5,Buffer5);
   SetIndexBuffer(6,Buffer6);
   SetIndexBuffer(7,Buffer7);

   SetIndexStyle(0,DRAW_ARROW,EMPTY,0);
   SetIndexStyle(1,DRAW_ARROW,EMPTY,0);
   SetIndexStyle(2,DRAW_ARROW,EMPTY,0);
   SetIndexStyle(3,DRAW_ARROW,EMPTY,0);
   SetIndexStyle(4,DRAW_ARROW,EMPTY,0);
   SetIndexStyle(5,DRAW_ARROW,EMPTY,0);
   SetIndexStyle(6,DRAW_ARROW,EMPTY,0);
   SetIndexStyle(7,DRAW_ARROW,EMPTY,0);

// SetIndexStyle(0, DRAW_LINE, EMPTY, 0);
// SetIndexStyle(1, DRAW_LINE, EMPTY, 0);
// SetIndexStyle(2, DRAW_LINE, EMPTY, 0);
// SetIndexStyle(3, DRAW_LINE, EMPTY, 0);
// SetIndexStyle(4, DRAW_LINE, EMPTY, 0);
// SetIndexStyle(5, DRAW_LINE, EMPTY, 0);
// SetIndexStyle(6, DRAW_LINE, EMPTY, 0);
// SetIndexStyle(7, DRAW_LINE, EMPTY, 0);


   SetIndexArrow(0,159);
   SetIndexArrow(1,159);
   SetIndexArrow(2,159);
   SetIndexArrow(3,159);
   SetIndexArrow(4,159);
   SetIndexArrow(5,159);
   SetIndexArrow(6,159);
   SetIndexArrow(7,159);
   double summ=0;
   for(int i=days-1; i>=0; i--) 
     {
      summ+=MathAbs(iClose(NULL,myPeriod,i)-iOpen(NULL,myPeriod,i));
     }
   avgDayRange=summ/days;

   Print("avgDayRange: "+DoubleToStr(avgDayRange/Point,0));
   IndicatorDigits(Digits);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   if(counted_bars==0) limit-=1+1;

   if(history>0) 
     {
      if(limit>history) { limit=history; }
     }

   int period=((double)period1)*50*5/Period();

   double PP;
   for(int i=limit; i>=0; i--)
     {
      if(degree==1) 
        {
         PP=regression_LRMA(period,i,price);
           } else if(degree==2) {
         PP=regression_QRMA(period,i,price);
           } else {
         PP=regression_regressionPolynomial(i,period,degree,price);
        }

      Buffer0[i] = PP + (avgDayRange * step);
      Buffer1[i] = PP - (avgDayRange * step);

      Buffer2[i] = PP + (avgDayRange * step * 2);
      Buffer3[i] = PP - (avgDayRange * step * 2);

      Buffer4[i] = PP + (avgDayRange * step * 3);
      Buffer5[i] = PP - (avgDayRange * step * 3);

      Buffer6[i] = PP + (avgDayRange * step * 4);
      Buffer7[i] = PP - (avgDayRange * step * 4);
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double regression_QRMA(int period,int shift,int price) 
  {
   double lwma= iMA(NULL,0,period,0,MODE_LWMA,0,shift);
   double sma = iMA(NULL,0,period,0,MODE_SMA,0,shift);
   double qwma= ma_qwma(period,shift,price);
   double value=3.0*sma+qwma *(10-15/(period+2))-lwma *(12-15/(period+2));
   return(value);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double ma_qwma(int period,int shift,int price) 
  {
   double sum=0;
   int j,i;
   for(j=shift,i=1;j<shift+period; j++,i++) 
     {
      sum+=switch_getPrice(price,shift)*MathPow(period-i+1,2);
     }
   double value=6.0/(period *(period+1) *(2*period+1));
   return(value*sum);
  }

double a[10,10];
double b[10];
double x[10];
double sx[20];
int n;
int i;
int k;
double t;
int degree1;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double regression_regressionPolynomial(int shift,int period,int degree,int priceCase) 
  {
   sx[1]=period+1;
   degree1=degree+1;

   for(i=1;i<=degree1*2-2; i++) 
     {
      sx[i+1]=0;
      for(n=0;n<=period; n++)
        {
         sx[i+1]+=MathPow(n,i);
        }
     }
   double t;
   for(i=1; i<=degree1; i++)
     {
      b[i]=0;
      for(n=0; n<=period; n++)
        {
         if(i==1) 
           {
            b[i]+=switch_getPrice(priceCase,n+shift);
              } else {
            b[i]+=switch_getPrice(priceCase,n+shift)*MathPow(n,i-1);
           }
        }
     }

   for(int j = 1;j <= degree1; j++)
     {
      for(i = 1; i <= degree1; i++)
        {
         k=i+j-1;
         a[i,j]=sx[k];
        }
     }

   for(k=1; k<=degree1-1; k++)
     {
      int l=0;
      double mm=0;
      for(i=k; i<=degree1; i++)
        {
         if(MathAbs(a[i,k])>mm)
           {
            mm= MathAbs(a[i,k]);
            l = i;
           }
        }
      if(l==0) 
        {
         return(0);
        }
      if(l!=k)
        {
         for(j=1; j<=degree1; j++)
           {
            t = a[k,j];
            a[k, j] = a[l, j];
            a[l, j] = t;
           }
         t=b[k];
         b[k] = b[l];
         b[l] = t;
        }
      double div=0;
      for(i=k+1;i<=degree1; i++)
        {
         div=a[i,k]/a[k,k];
         for(j=1;j<=degree1; j++)
           {
            if(j==k) 
              {
               a[i,j]=0;
                 } else {
               a[i,j]=a[i,j]-div*a[k,j];
              }
           }
         b[i]=b[i]-div*b[k];
        }
     }

   x[degree1]=b[degree1]/a[degree1,degree1];
   for(i=degree;i>=1; i--)
     {
      t = 0;
      for(j=1;j<=degree1-i; j++)
        {
         t = t+a[i,i+j] * x[i+j];
         x[i]=(1/a[i,i]) *(b[i]-t);
        }
     }

   double value=x[1];
   for(i=1;k<=degree; i++)
     {
      value+=x[i+1] *MathPow(n,i);
     }
   return(value);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double regression_LRMA(int period,int shift,int price)
  {
   return(3*iMA(NULL,0,period,0,MODE_LWMA,price,shift)-2*iMA(NULL,0,period,0,MODE_SMA,price,shift));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double switch_getPrice(int price,int shift) 
  {
   switch(price) 
     {
      case 0:
         return(Close[shift]);
      case 1:
         return(Open[shift]);
      case 2:
         return(High[shift]);
      case 3:
         return(Low[shift]);
      case 4:
         return((High[shift]+Low[shift])/2);
      case 5:
         return((High[shift]+Low[shift]+Close[shift])/3);
      case 6:
         return((High[shift]+Low[shift]+Close[shift]+Close[shift])/4);
      default:
         return(Close[shift]);
     }
  }
//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---