iLRegressionBands

Price Data Components
Series array that contains close prices for each bar
Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
iLRegressionBands
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Green
#property indicator_color2 Green
#property indicator_color3 Green

//----------------------------------
extern int iPeriod=14;
extern double StdFrrors=2;
//----------------------------------
double ci[],uch[],dch[];
double L_Value, M_Dev;
//================================================
int init(){
   
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ci);
   
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,uch);
   
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,dch);   
   
   return(0);}
//================================================

int start(){
   int limit;
	int counted_bars=IndicatorCounted(); // îïðåäåëèì êîëè÷åñòâî ïðîñ÷èòàíûõ áàðîâ ó èíäèêàòîðà
	  if(counted_bars>0){
	     counted_bars--;
	  }	
	limit=Bars-counted_bars;
	if(counted_bars==0) limit-=iPeriod;
	for(int i=0;i<limit;i++){
	  LR_Close(i,i+iPeriod);
	  ci[i]=L_Value;
	  uch[i]=L_Value+StdFrrors*M_Dev;
	  dch[i]=L_Value-StdFrrors*M_Dev;	  
	}
 return(0);
}
//+------------------------------------------------------------------+
void LR_Close(int i0,int i1){
  int m_pos[2];
  m_pos[0]=i0;
  m_pos[1]=i1; 
  double m_value[2];  
   int n=m_pos[1]-m_pos[0]+1;
//---- calculate price values
   double value=Close[m_pos[0]];
   double a,b,c;
   double sumy=value;
   double sumx=0.0;
   double sumxy=0.0;
   double sumx2=0.0;
   for(int i=1; i<n; i++)
     {
      //Print("i=",i);
      value=Close[m_pos[0]+i];
      sumy+=value;
      sumxy+=value*i;
      sumx+=i;
      sumx2+=i*i;
      
     }    
   c=sumx2*n-sumx*sumx;
   if(c==0.0) return;
   b=(sumxy*n-sumx*sumy)/c;
   a=(sumy-sumx*b)/n;
   m_value[0]=a;
   m_value[1]=a+b*n;

//---- maximal deviation
   double maxdev=0;
   double deviation=0;
   double dvalue=a;
   for(i=0; i<n; i++)
     {
      value=Close[m_pos[0]+i];
      dvalue+=b;
      deviation=MathAbs(value-dvalue);
      if(maxdev<=deviation) maxdev=deviation;
     }
   L_Value=m_value[0];
   M_Dev=maxdev;
  }


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