0 Views
0 Downloads
0 Favorites
Channel_v2
ÿþ//+------------------------------------------------------------------+

//|                                                      Channel.mq4 |

//|                                                                  |

//|                                                                  |

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

#property version   "1.00"

#property strict

#property indicator_chart_window

#property indicator_buffers 4

#property indicator_plots   4

//--- plot bHigh

#property indicator_label1  "bHigh"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrCornflowerBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2

//--- plot bLow

#property indicator_label2  "bLow"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrLightCoral

#property indicator_style2  STYLE_SOLID

#property indicator_width2  2

//--- plot bMedium

#property indicator_label3  "bMedium"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrPurple

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot SMA

#property indicator_label4  "SMA"

#property indicator_type4   DRAW_LINE

#property indicator_color4  clrSeaGreen

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1

//--- input parameters

input int       Lfv=4;  // MA1 length (period)

input int       LF=20;  // MA2 length (period)

input int       CL=2;   // MA3 length (period)

input int       NF=10;  // MA4 length (period) < LF

input int       Gint=80;//gap value

input double    Ae=0.4; //filter function coeff (~1/reaction)

input double    Be=0.04;//narrow function coeff (~reaction)

input int       St=4;   //correction step

//--- indicator buffers

double         bHighBuffer[];

double         bLowBuffer[];

double         bMediumBuffer[];

double         sFilt[];

double         bFilt[];

//--- bars minimum for calculation

#define DATA_LIMIT  50

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- 1 additional buffer used for counting.

   IndicatorBuffers(5);

//--- indicator buffers mapping

   SetIndexBuffer(0,bHighBuffer);

   SetIndexBuffer(1,bLowBuffer);

   SetIndexBuffer(2,bMediumBuffer);

   SetIndexBuffer(3,sFilt);



   SetIndexDrawBegin(0,DATA_LIMIT);

   SetIndexDrawBegin(1,DATA_LIMIT);

   SetIndexDrawBegin(2,DATA_LIMIT);

   SetIndexDrawBegin(3,DATA_LIMIT);

//--- work buffer

   SetIndexBuffer(4,bFilt);



//--- counting like timeseries

   ArraySetAsSeries(bHighBuffer,true);

   ArraySetAsSeries(bLowBuffer,true);

   ArraySetAsSeries(bMediumBuffer,true);

   ArraySetAsSeries(bFilt,true);

   ArraySetAsSeries(sFilt,true);



   return(INIT_SUCCEEDED);

  }

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

//| 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[])

  {

//---

   ArraySetAsSeries(close,true);

   ArraySetAsSeries(low,true);

   ArraySetAsSeries(high,true);



   int i,pos;

   static int Gp=0;

   double dl,dh,Ye,Xh,Xl,temp;   

//---  gap position 

   Gp=rates_total; 



   if(rates_total<LF)

      return(0);





//--- starting calculation

   if(prev_calculated>1)

      pos=rates_total-prev_calculated;

   else

      pos=rates_total-1;

//--- main cycle

//--- match timeframe, specific SET is needed 

   if(true)//_Period==PERIOD_H1)

     {



      for(i=pos; i>=0 && !IsStopped(); i--)

        {



         if(i<Gp-LF)

           {

            sFilt[i] =Mean_Func(Close,i,Lfv);

            bFilt[i] =Mean_Func(Close,i,LF);

//--- search for cross points Xh,Xl and check division by zero

            temp=bHighBuffer[i+1]-bHighBuffer[i+CL]-sFilt[i]+sFilt[i+CL];

            if(temp==0)

               temp=1e-10;

            Xh = (sFilt[i]-bHighBuffer[i+1])*CL/temp;

            Ye = MathExp(-Ae*MathAbs(Xh));

            bHighBuffer[i]=bHighBuffer[i+1]+Ye*(sFilt[i]-sFilt[i+1])+(1-Ye)*(bFilt[i]-bFilt[i+1]);



            temp=bLowBuffer[i+1]-bLowBuffer[i+CL]-sFilt[i]+sFilt[i+CL];

            if(temp==0)

               temp=1e-10;

            Xl = (sFilt[i]-bLowBuffer[i+1])*CL/temp;

            Ye = MathExp(-Ae*MathAbs(Xl));

            bLowBuffer[i]=bLowBuffer[i+1]+Ye*(sFilt[i]-sFilt[i+1])+(1-Ye)*(bFilt[i]-bFilt[i+1]);

//--- new gap coordinate 

            if(MathAbs(Close[i]-Close[i+1])>Gint*_Point*10)

               Gp=i;

           }

         else

           {

            bHighBuffer[i]=High[i];

            bLowBuffer[i]=Low[i];

            bFilt[i]=Close[i];

            sFilt[i]=Close[i];

            if(i==Gp-LF)

              {

               bHighBuffer[i]=Max_Func(High,i,int(MathRound(LF)))*0.1+0.9*bHighBuffer[i+1]; // 8A?@028BL =07=0G5=85 =0G 7=0G5=89

               bLowBuffer[i]=Min_Func(Low,i,int(MathRound(LF)))*0.1+0.9*bLowBuffer[i+1];  // 8A?@028BL A;565=85 5A;8 1K; 2KE>4K 3@0=8F 87 480?07>=0

               bFilt[i]=Mean_Func(Close,i,LF);

               sFilt[i]=Mean_Func(Close,i,Lfv);

              }

           }

//--- correct only previous value 

         if(i<Gp-MathMax(LF,NF) && i>0)

           {

            dh = bHighBuffer[i]-Max_Func(High,i,NF);

            dl =  Min_Func(Low,i,NF)-bLowBuffer[i];

            if(dh<0)

               dh=0;

            if(dl<0)

               dl=0;

//--- x10 - for 5 digit 

//--- modification  of channel borders       

            bHighBuffer[i]=bHighBuffer[i]-(1-MathExp(-Be*dh/_Point/10))*St*_Point*10;

            bLowBuffer[i]=bLowBuffer[i]+(1-MathExp(-Be*dl/_Point/10))*St*_Point*10;





            dh = sFilt[i]-bHighBuffer[i];

            dl = bLowBuffer[i]-sFilt[i];

            if(dh<0)

               dh=0;

            if(dl<0)

               dl=0;

//--- x10 - for 5 digit 

            bHighBuffer[i]=bHighBuffer[i]+(1-MathExp(-Be*dh/_Point/10))*St*_Point*10;

            bLowBuffer[i]=bLowBuffer[i]-(1-MathExp(-Be*dl/_Point/10))*St*_Point*10;



           }



         bMediumBuffer[i]=(bHighBuffer[i]+bLowBuffer[i])/2;



        }

     }

//   Print(" 07<5@ ?C=:B0 B5:CI53> 8=AB@C<5=B0 2 20;NB5 :>B8@>2:8=",_Point); 

//   Print("=0G5=85 B09<D@59<0 B5:CI53> 3@0D8:0=",_Period); 

//--- return value of prev_calculated for next call

   return(rates_total);



  }

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



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

//| Calculate Mean Value                                             |

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

double Mean_Func(const double &price[],int position,int period)

  {//--- variables

   double Mean_Tmp=0.0;

   double sum=0.0;

//--- calcualte Mean

   for(int i=0; i<period; i++)

     {sum+=price[position+i];}

   Mean_Tmp=sum/period;



//--- return calculated value

   return(Mean_Tmp);

  }

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

//| Calculate Min Value                                              |

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

double Min_Func(const double &price[],int position,int period)

  {//--- variables

   double Min_Tmp=0.0;

//--- calcualte Min

   Min_Tmp=price[position];

   for(int i=0; i<period; i++)

     {

      if(Min_Tmp>price[position+i])

         Min_Tmp=price[position+i];

     }



//--- return calculated value

   return(Min_Tmp);

  }

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

//| Calculate Max Value                                              |

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

double Max_Func(const double &price[],int position,int period)

  {//--- variables

   double Max_Tmp=0.0;

//--- calcualte Max

   Max_Tmp=price[position];

   for(int i=0; i<period; i++)

     {

      if(Max_Tmp<price[position+i])

         Max_Tmp=price[position+i];

     }

//--- return calculated value

   return(Max_Tmp);

  }

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

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