RegressionParabolic

Author: Copyright © 2023, Shumer3000
17 Views
0 Downloads
0 Favorites
RegressionParabolic
ÿþ//+------------------------------------------------------------------+

//|                                          RegressionParabolic.mq5 |

//|                                     Copyright © 2023, Shumer3000 |

//|                         https://www.mql5.com/ru/users/shumer3000 |

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

//------------------------------------------------------------------

#property copyright   "Copyright © 2023, Shumer3000"

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

#property version     "1.00"

//------------------------------------------------------------------

#property indicator_chart_window

#property indicator_buffers 8

#property indicator_plots   5

#property indicator_label1  "High line"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrDeepSkyBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2

#property indicator_label2  "Low line"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrDeepSkyBlue

#property indicator_style2  STYLE_SOLID

#property indicator_width2  2

#property indicator_label3  "Up"

#property indicator_type3   DRAW_ARROW

#property indicator_color3  clrBlue

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

#property indicator_label4  "Down"

#property indicator_type4   DRAW_ARROW

#property indicator_color4  clrRed

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1

#property indicator_label5  "Stop"

#property indicator_type5   DRAW_ARROW

#property indicator_color5  clrOrange

#property indicator_style5  STYLE_SOLID

#property indicator_width5  1

//--- input parameters

input int                Nwaves=3;           // number of waves

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

input double             InpSARStep=0.02;    // Step Parabolic

input double             InpSARMax=0.2;      // Maximum Parabolic

//---

static bool ch;

int sar, fr;

//---- buffers

double Up[];

double Dn[];

double VUp[];

double VDn[];

double VSt[];

double SAR[];

double FR1[];

double FR2[];

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   SetIndexBuffer(0,Up,INDICATOR_DATA);

   SetIndexBuffer(1,Dn,INDICATOR_DATA);

   SetIndexBuffer(2,VUp,INDICATOR_DATA);

   PlotIndexSetInteger(2,PLOT_ARROW,233);

   PlotIndexSetInteger(2,PLOT_ARROW_SHIFT,10);

   SetIndexBuffer(3,VDn,INDICATOR_DATA);

   PlotIndexSetInteger(3,PLOT_ARROW,234);

   PlotIndexSetInteger(3,PLOT_ARROW_SHIFT,-10);

   SetIndexBuffer(4,VSt,INDICATOR_DATA);

   SetIndexBuffer(5,SAR,INDICATOR_CALCULATIONS);

   SetIndexBuffer(6,FR1,INDICATOR_CALCULATIONS);

   SetIndexBuffer(7,FR2,INDICATOR_CALCULATIONS);

//---

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(4,PLOT_EMPTY_VALUE,0.0);

//--- create handle of the indicator SAR

   sar=iSAR(_Symbol,_Period,InpSARStep,InpSARMax);

//--- if the handle sar0 is not created

   if(sar==INVALID_HANDLE)

     {

      //--- tell about the failure and output the error code

      PrintFormat("Failed to create handle of the SAR indicator");

      //--- the indicator is stopped early

       return(INIT_SUCCEEDED);

     }

//--- create handle of the indicator Fractals 

   fr=iFractals(_Symbol,_Period);

//--- if the handle fr is not created

   if(fr==INVALID_HANDLE)

     {

      //--- tell about the failure and output the error code

      PrintFormat("Failed to create handle of the Fractals indicator");

      //--- the indicator is stopped early

       return(INIT_SUCCEEDED);

     }

//--- name for DataWindow and indicator subwindow label

   string short_name=StringFormat("RegSAR(Waves %d)",Nwaves);

   IndicatorSetString(INDICATOR_SHORTNAME,short_name);

   return(0);

  }

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

//| Custom indicator iteration function                              |

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

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {   

   if(Bars(_Symbol,_Period)<rates_total)

      return(-1);

   ArraySetAsSeries(SAR,true);

   ArraySetAsSeries(FR1,true);

   ArraySetAsSeries(FR2,true);

   ArraySetAsSeries(Up,true);

   ArraySetAsSeries(Dn,true);  

   ArraySetAsSeries(VUp,true);

   ArraySetAsSeries(VDn,true);

   ArraySetAsSeries(VSt,true); 

//----

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

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

   sum1y = 0.0, sum2y = 0.0,

   sum1x = 0.0, sum2x = 0.0,

   sum1xy = 0.0, sum2xy = 0.0,

   sum1xx = 0.0, sum2xx = 0.0,

   h = 0.0, l = 0.0;  

//----

   if(!FillArrayFromBuffer(SAR,0,sar,rates_total))

       return(0);

   if(!FillArrayFromBuffer(FR1,0,fr,rates_total))

       return(0);

   if(!FillArrayFromBuffer(FR2,1,fr,rates_total))

       return(0);

//---- wave counting

   for(int i = 0; i < Bars(_Symbol,_Period)-1; i++) {

     if(SAR[i] > iClose(_Symbol,_Period,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;

       break; 

     } 

   } 

//---- regression calculation

   n1=0;

   n2=0;

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

     { 

       if(SAR[i] > iClose(_Symbol,_Period,i))

         {

           sum1x += i;

           sum1y += SAR[i];

           sum1xy += SAR[i] * i;

           sum1xx += i * i;

           n1++;

           nu = i;

           tv_0 = (iVolume(_Symbol,_Period,i) - (iClose(_Symbol,_Period,i) - iOpen(_Symbol,_Period,i)) / _Point) / 2;        

           tv_1 = (iVolume(_Symbol,_Period,i+1) - (iClose(_Symbol,_Period,i+1) - iOpen(_Symbol,_Period,i+1)) / _Point) / 2;

           if(tv_0 > tv_1)                                       

             tv2 += tv_0;

         }

       else 

         {

           sum2x += i;

           sum2y += SAR[i];

           sum2xy += SAR[i] * i;

           sum2xx += i * i;

           n2++;

           nd = i;

           tv_0 = (iVolume(_Symbol,_Period,i) + (iClose(_Symbol,_Period,i) - iOpen(_Symbol,_Period,i)) / _Point) / 2;        

           tv_1 = (iVolume(_Symbol,_Period,i+1) + (iClose(_Symbol,_Period,i+1) - iOpen(_Symbol,_Period,i+1)) / _Point) / 2;

           if(tv_0 > tv_1)                                       

             tv1 += tv_0;

         }

     } 

   c1 = sum1xx * n1 - sum1x * sum1x;

   c2 = sum2xx * 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(int i = nu; i < Nlin; i++)

     {

       double LR = a1 * i + b1; 

       if(iClose(_Symbol,_Period,i) - LR > h && FR1[i] > 0.01) h = iClose(_Symbol,_Period,i) - LR;

     }

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

     {

       double LR = a2 * i + b2; 

       if( LR - iClose(_Symbol,_Period,i) > l && FR2[i] > 0.01) l = LR - iClose(_Symbol,_Period,i);

     } 

 //--- channel building

   for(int i = Bars(_Symbol,_Period)-1; 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(int 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( iHigh(_Symbol,_Period,0) >= Up[0] || iLow(_Symbol,_Period,0) <= Dn[0])    

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

         if(tv1 > tv2) 

           VUp[0] = iLow(_Symbol,_Period,0); 

         else 

           VDn[0] = iHigh(_Symbol,_Period,0);

       else 

           VSt[0] = iClose(_Symbol,_Period,0);



   return(rates_total); 

  }

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

//| Filling indicator buffers from the indicator                     |

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

bool FillArrayFromBuffer(double &values[],  // indicator buffer 

                         int buff,          // buffer

                         int ind_handle,    // handle 

                         int amount)        // number of copied values

  {

//--- reset error code

   ResetLastError();

//--- fill a part of the Buffer array with values from the indicator buffer that has 0 index

   if(CopyBuffer(ind_handle,buff,0,amount,values)<0)

     {

      //--- if the copying fails, tell the error code

      PrintFormat("Failed to copy data from the indicator, error code %d",GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated

      return(false);

     }

//--- everything is fine

   return(true);

  }

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

Comments