RegressionParabolic

Author: 2023, Shumer3000
Price Data Components
Series array that contains close prices for each bar
Indicators Used
Parabolic Stop and Reverse systemFractals
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
RegressionParabolic
ÿþ//+------------------------------------------------------------------+

//|                                          RegressionParabolic.mq4 |

//|                                       Copyright 2023, Shumer3000 |

//|                                                           v 1.00 |

//+------------------------------------------------------------------+

#property copyright   "2023, Shumer3000"

#property link        "https://www.mql5.com/ru/users/shumer3000"

#property description "Regression Parabolic Stop-And-Reversal system"

#property strict



//--- indicator settings

#property indicator_chart_window

#property indicator_buffers 5

#property indicator_color1  DeepSkyBlue

#property indicator_color2  DeepSkyBlue

#property indicator_color3  Blue

#property indicator_color4  Red

#property indicator_color5  Orange



//--- input parameters

input int    Nwaves=3;           // number of waves

input int    tfw=0;              // TF Waves

input int    fd=10;              // flat range in percent

input double InpSARStep=0.02;    // Step Parabolic

input double InpSARMax=0.2;      // Maximum Parabolic

input bool   al=false;           // Alert ON/OFF



static bool ch;



//---- buffers

double Up[];

double Dn[];

double VUp[];

double VDn[];

double VSt[];



void OnInit()

  {

//--- drawing settings

   IndicatorBuffers(5);

   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);

   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);

   SetIndexStyle(2,DRAW_ARROW,0,1);

   SetIndexArrow(2,233);

   SetIndexStyle(3,DRAW_ARROW,0,1);

   SetIndexArrow(3,234);

   SetIndexStyle(4,DRAW_ARROW,0,1);

   

//---- indicator buffers

   SetIndexBuffer(0,Up);

   SetIndexBuffer(1,Dn);

   SetIndexBuffer(2,VUp);

   SetIndexBuffer(3,VDn);

   SetIndexBuffer(4,VSt);

       

   SetIndexEmptyValue(0,0.0);

   SetIndexEmptyValue(1,0.0);

   SetIndexEmptyValue(2,0.0);

   SetIndexEmptyValue(3,0.0);

   SetIndexEmptyValue(4,0.0);

   

   ch=false;

      

   IndicatorShortName("RegSAR("+DoubleToString(Nwaves,0)+","+DoubleToString(tfw,0)+")");

  }

  

int start()

  {

   int tf,counted_bars=IndicatorCounted();

   if(counted_bars<0) return(-1);

   if(Up[0]!=0) return(-1);

   if(tfw<Period()) tf=Period();

   else tf=tfw;



//---- 

   int i,Nlin,nu,nd,n1=0,n2=0,v1=0,v2=0,x1=0,x2=0,tv1=0,tv2=0,tv_0,tv_1;

   double sar,a1,b1,c1,a2,b2,c2,

   sum1y=0.0,sum2y=0.0,

   sum1x=0.0,sum2x=0.0,

   sum1xy=0.0,sum2xy=0.0,

   sum1x2=0.0,sum2x2=0.0,

   h=0.0,l=0.0;

   

//---- wave counting

   for(i=0; i<Bars-2; i++) {

     if(iSAR(NULL,tf,InpSARStep,InpSARMax,i)>iClose(NULL,tf,i)) {

       n1++;

       n2=0;

       x2=0;

       if(x1==0&&n1>5) { v1++; x1=1; }        

     }

     else {

       n2++;

       n1=0;

       x1=0;

       if(x2==0&&n2>5) { v2++; x2=1; }

     }

     if((v1-v2)==2) v1--;

     if((v2-v1)==2) v2--;

     if(v1>Nwaves||v2>Nwaves) { 

       Nlin=(i-5)*tf/Period();

       break; 

     } 

   }

   

//---- regression calculation

   n1=0;

   n2=0;

   for(i=Nlin-1; i>=0; i--)

     {

       sar=iSAR(NULL,0,InpSARStep,InpSARMax,i);

       if(sar>Close[i])

         {

           sum1x+=i;

           sum1y+=sar;

           sum1xy+=sar*i;

           sum1x2+=i*i;

           n1++;

           nu=i;

           tv_0=(Volume[i]-(Close[i]-Open[i])/Point)/2;        

           tv_1=(Volume[i+1]-(Close[i+1]-Open[i+1])/Point)/2;

           if(tv_0>tv_1)                                       

             tv2+=tv_0;

         }

       else 

         {

           sum2x+=i;

           sum2y+=sar;

           sum2xy+=sar*i;

           sum2x2+=i*i;

           n2++;

           nd=i;

           tv_0=(Volume[i]+(Close[i]-Open[i])/Point)/2;        

           tv_1=(Volume[i+1]+(Close[i+1]-Open[i+1])/Point)/2;

           if(tv_0>tv_1)                                       

             tv1+=tv_0;

         }

     } 

   c1=sum1x2*n1-sum1x*sum1x;

   c2=sum2x2*n2-sum2x*sum2x;

   

   if(c1==0.0 || c2==0.0)

     {

       Alert("Linear regression error!");

       return(-1);

     }

     

   a1=(sum1xy*n1-sum1x*sum1y)/c1;

   b1=(sum1y-sum1x*a1)/n1;

   a2=(sum2xy*n2-sum2x*sum2y)/c2;

   b2=(sum2y-sum2x*a2)/n2;

   if(nu<5) nu=nu+nd;

   else 

     if(nd<5) nd=nd+nu;



 //--- channel boundary definition

   for(i=nu;i<Nlin;i++)

     {

       double LR=a1*i+b1;

       if(Close[i]-LR>h&&iFractals(NULL,0,1,i)!=0) h=Close[i]-LR;

     }

   for(i=nd;i<Nlin;i++)

     {

       double LR=a2*i+b2;

       if(LR-Close[i]>l&&iFractals(NULL,0,2,i)!=0) l=LR-Close[i];

     }

 

 //--- channel building

     for(i=Bars-2;i>=0;i--)

       if(i<Nlin) {

         Up[i]=a1*i+b1+h;

         Dn[i]=a2*i+b2-l;

       }

       else {

         Up[i]=NULL;

         Dn[i]=NULL;

       }



//--- channel check for breakdown

   ch=true;

   for(i=0;i<Nlin;i++) 

     if(VUp[i]>Up[i]||(VUp[i]>0&&VUp[i]<Dn[i])||

        VDn[i]>Up[i]||(VDn[i]>0&&VDn[i]<Dn[i])||

        VSt[i]>Up[i]||(VSt[i]>0&&VSt[i]<Dn[i])) {

       ch=false;

       break;

     }



//--- arrow drawing

   if(ch) 

     if(High[0]>=Up[0]||Low[0]<=Dn[0])    

       if(MathAbs(tv1-tv2)>MathMin(tv1,tv2)*fd/100) 

         if(tv1>tv2) {

           VUp[0]=Low[0]; 

           if(al) Alert("Buy signal");

         }

         else {

           VDn[0]=High[0];

           if(al) Alert("Sell signal");

         }

       else {

           VSt[0]=Close[0];

           if(al) Alert("Stop signal");

       }



   return(0);

  }



//+------------------------------------------------------------------+

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 ---