iK_LinReg_v1

Author: Ivan Katsko
Price Data Components
Series array that contains open time of each bar
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
iK_LinReg_v1
//+------------------------------------------------------------------+
//|                                                                  |
//|       Èíäèêàòîð óòðåííåãî ôëåòà íà îñíîâå                        |
//|       êàíàëîâ ëèíåéíîé ðåãðåññèè                                 |
//|                             http://www.mql4.com/ru/users/ikatsko |
//+------------------------------------------------------------------+
#property copyright "Ivan Katsko"
#property link      "ICQ:372739628"

//---- indicator settings
#property  indicator_chart_window
#property  indicator_buffers 8
#property indicator_color1 DeepSkyBlue
#property indicator_color2 DeepSkyBlue
#property indicator_color3 FireBrick
#property indicator_color4 DeepSkyBlue
#property indicator_color5 DeepSkyBlue
#property indicator_color6 Yellow
#property indicator_color7 HotPink
#property indicator_color8 LawnGreen 

//---- buffers
double B0[];
double B1[];
double Stop[];
double B3[];
double B4[];
double Up[];
double Dn[];
double Target[];

extern string S3="Öåëü ïðè ïðîáîå îòí. øèðèíû êàíàëà";
extern double t0=1.3;
extern string S4="Èñïîëüçîâàòü äëÿ ðàñ÷¸òà Close";
extern bool useClose=true;
extern string S5="Ìàêñèìàëüíûé SL â ïóíêòàõ";
extern int MaxSL=800;
extern string StartTime = "20:00";     //Íà÷àëî ðàáîòû
extern string FinishTime = "01:00";    //Êîíåö ðàáîòû
extern int PeriodSilense = 12;

string StTime, FinTime;
bool   New_Bar=false,                  // Ôëàã íîâîãî áàðà 
       FindR0, isChannel;
double a0;
double b0;
double range0;
double MaxAggres, MinAggres;
int r0, Nlin, loopbeg, NumMaxAggres;
static datetime New_Time;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   FindR0=false;
   MaxAggres=0;
   MinAggres=0;
        
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
   SetIndexBuffer(0, B0);      
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
   SetIndexBuffer(1, B1);      
   SetIndexStyle(2,DRAW_ARROW,0,2);
   SetIndexBuffer(2, Stop);      
   SetIndexStyle(3,DRAW_LINE,STYLE_DOT);
   SetIndexBuffer(3, B3);      
   SetIndexStyle(4,DRAW_LINE,STYLE_DOT);
   SetIndexBuffer(4, B4);      

   SetIndexStyle(5,DRAW_ARROW,0,1);
   SetIndexBuffer(5, Up);      
   SetIndexArrow(5, 233);        
   SetIndexStyle(6,DRAW_ARROW,0,1);
   SetIndexBuffer(6, Dn);      
   SetIndexArrow(6, 234);        
   SetIndexStyle(7,DRAW_ARROW,0,1);
   SetIndexBuffer(7, Target);      
   SetIndexArrow(7, 231);        

   SetIndexEmptyValue(0,0.0);   
   SetIndexEmptyValue(1,0.0);   
   SetIndexEmptyValue(2,0.0);   
   SetIndexEmptyValue(3,0.0);   
   SetIndexEmptyValue(4,0.0);   
   SetIndexEmptyValue(5,0.0);   
   SetIndexEmptyValue(6,0.0);   
   SetIndexEmptyValue(7,0.0);   

//---- èìÿ èíäèêàòîðà è ïîäñêàçêè äëÿ ëèíèé
   SetIndexLabel(0,"Channel Low");
   SetIndexLabel(1,"Channel High");
   SetIndexLabel(3,"Channel Low Extended");
   SetIndexLabel(4,"Channel High Extended");
   SetIndexLabel(5,"BUY Signal");
   SetIndexLabel(6,"SELL Signal");
   SetIndexLabel(2,"Stop/Reverse");
   SetIndexLabel(7,"Target");

   isChannel=false;

   return(0);
  } 

int deinit()
  {
  Comment("");
  } 

int start() {
  if(Bars-IndicatorCounted()==0) return(0);
  Fun_New_Bar();                       // Îïðåäåëåíèå íà÷àëà íîâîãî áàðà
  
  int i,j,k;
  if(New_Bar){
    // Íàéäåì StTime 
    if(Time[0]==StrToTime(TimeToStr(Time[0], TIME_DATE)+" "+"01:00")) {
      int flatbeg=(Time[0]-StrToTime(TimeToStr(Time[0]-86400, TIME_DATE)+" "+StartTime))/60/Period();
      int flatnull=(Time[0]-StrToTime(TimeToStr(Time[0], TIME_DATE)+" "+"00:00"))/60/Period();
      MaxAggres=0;
      for(i=flatnull+1;i<=flatbeg;i++){
        double m1=iCustom(0, 0, "Silense",PeriodSilense,288,0,i);
        double m2=iCustom(0, 0, "Silense",PeriodSilense,288,0,i+1);
        double m3=iCustom(0, 0, "Silense",PeriodSilense,288,0,i+2);
        if(m2>m1 && m2>m3 && m2>MaxAggres){
          MaxAggres=m2;
          MinAggres=0;
          NumMaxAggres=i+1;
        }
        if(MaxAggres>50) break;
      }
      if(MaxAggres>0) {StTime=TimeToStr(Time[NumMaxAggres],TIME_MINUTES); FindR0=false;}
        else {StTime=StartTime;FindR0=false;}
    }
    else if(Time[0]<=StrToTime(TimeToStr(Time[0], TIME_DATE)+" "+FinishTime)) return(0);

    if(MinAggres==0){
      if(Time[0]>=StrToTime(TimeToStr(Time[0], TIME_DATE)+" "+FinishTime)) {
        m1=iCustom(0, 0, "Silense",PeriodSilense,288,0,1);
        m2=iCustom(0, 0, "Silense",PeriodSilense,288,0,2);
        m3=iCustom(0, 0, "Silense",PeriodSilense,288,0,3);
        double n1=iCustom(0, 0, "Silense",PeriodSilense,288,1,1);
        double n3=iCustom(0, 0, "Silense",PeriodSilense,288,1,3);
        //Alert("Time[0]=",TimeToStr(Time[0]-60*2*Period(),TIME_MINUTES)," m2<m1=",m1-m2," m2<m3=",m3-m2," n3<n1=",n1-n3);
        if(m2<m1 && m2<m3 && n3<n1) MinAggres=m2;
        if(MinAggres>0) FinTime=TimeToStr(Time[2],TIME_MINUTES); 
        else{
          if(Time[0]>=StrToTime(TimeToStr(Time[0], TIME_DATE)+" "+FinishTime))
            FinTime=FinishTime;
            else {/*Alert("FinTime=7:00");*/return(0);}
        }
      }
    }
    Nlin=(StrToTime(TimeToStr(TimeCurrent(), TIME_DATE)+" "+FinTime)
          -StrToTime(TimeToStr(TimeCurrent()-86400, TIME_DATE)+" "+StTime))
          /60/Period();                                                // ×èñëî áàðîâ âî ôëýòå
  }
  
  double a,b,c,
         sumy=0.0,
         sumx=0.0,
         sumxy=0.0,
         sumx2=0.0,
         h=0.0,l=0.0,
         range = 0.0;
  
  if(Time[0]==(StrToTime(TimeToStr(Time[0], TIME_DATE)+" "+FinTime)+60*2*Period()) && !FindR0  && MinAggres>0) {
    for(k = MathRound(MaxSL/20); k < MaxSL/2; k++) {
      r0=k;
      isChannel=false;
         
      a=0.0; b=0.0; c=0.0;
      sumx=0.0; sumy=0.0;
      sumxy=0.0; sumx2=0.0;
      h=0.0; l=0.0;
         
      //ñ÷èòàåì êàíàë ëèíåéíîé ðåãðåññèè îò i+Nlin äî i 
      for(j=2; j<Nlin+2; j++) {
         sumy+=Open[j];
         sumxy+=Open[j]*j;
         sumx+=j;
         sumx2+=j*j;
      }
      c=sumx2*Nlin-sumx*sumx;
      if(c==0.0) {
         Alert("Error in linear regression!"," Nlin=",Nlin);
         return(-1);
      }
      a=(sumxy*Nlin-sumx*sumy)/c;
      b=(sumy-sumx*a)/Nlin;
      
      //îïðåäåëÿåì ãðàíèöû êàíàëà
      for(j=2;j<Nlin+2;j++) {
        double LR=a*j+b;
        if (useClose) {
          if(Close[j]-LR > h) h = Close[j]-LR;
          if(LR - Close[j]> l) l = LR - Close[j];
        } else {
          if(High[j]-LR > h) h = High[j]-LR;
          if(LR - Low[j]> l) l = LR - Low[j];
        }           
      }  
      range = MathMax(l,h);
      if(range0==0) Comment("Range=",range/Point);
      
      //ïðîâåðêà øèðèíû êàíàëà
      if (range<r0*Point) {
         //åñòü êàíàë, ñîõðàíÿåì
         isChannel=true;
         a0=a;
         b0=b;
         range0=range;
         FindR0=true;
         for (j=2; j<Nlin+2; j++) {
            B3[j]=a0*j+b0+range0;
            B4[j]=a0*j+b0-range0;
            B0[j]=a0*j+b0+range0;
            B1[j]=a0*j+b0-range0;
         }
      }
      if(isChannel) break;                                            // Âûõîä èç for äëÿ r0
    }
  } 
  if(isChannel){
    loopbeg=(Time[0]-StrToTime(TimeToStr(Time[0], TIME_DATE)+" "+FinTime))/60/Period();
    for(i = loopbeg; i >= 0; i--) {
       //ó íàñ óæå åñòü êàíàë, æä¸ì, ïîêà åãî ïðîáü¸ò
       double up0=a0*(i-loopbeg+2)+b0+range0;
       double dn0=a0*(i-loopbeg+2)+b0-range0;
       B3[i]=up0;
       B4[i]=dn0;
          
       if(up0>0 && iTime(NULL,0,i)<StrToTime(TimeToStr(iTime(NULL,0,i), TIME_DATE)+" "+FinTime))
         continue;
       //ïðîâåðÿåì ïðîáèòèå
       //ââåðõ
       if (Open[i]>up0) {
          Up[i]=NormalizeDouble(Open[i],Digits);
          Stop[i]=NormalizeDouble(Open[i]-(up0-dn0),Digits);
          Target[i]=NormalizeDouble(Open[i]+(up0-dn0)*t0,Digits);
          isChannel=false;
          if(range0>0) Comment(StTime,"-",FinTime," TP=",(Target[i]-Up[i])/Point,", SL=",(Up[i]-Stop[i])/Point);
          break;
       }
       //âíèç
       if (Open[i]<dn0) {
          Dn[i]=NormalizeDouble(Open[i],Digits);
          Stop[i]=NormalizeDouble(Open[i]+(up0-dn0),Digits);
          Target[i]=NormalizeDouble(Open[i]-(up0-dn0)*t0,Digits);
          isChannel=false;
          if(range0>0) Comment(StTime,"-",FinTime," TP=",(Dn[i]-Target[i])/Point,", SL=",(Stop[i]-Dn[i])/Point);
          break;
       }
       //continue;
    }
  }
  return(0);
}
//+------------------------------------------------------------------+
void Fun_New_Bar()                              // Ô-èÿ îáíàðóæåíèÿ íîâîãî áàðà
  {                                             
   New_Bar=false;                               // Íîâîãî áàðà íåò
   if(New_Time!=Time[0])                        // Ñðàâíèâàåì âðåìÿ
     {
      New_Time=Time[0];                         // Òåïåðü âðåìÿ òàêîå
      New_Bar=true;                             // Ïîéìàëñÿ íîâûé áàð  
     }
  }



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