FractalChannel_v5

Author: Copyright � 2006-07, TrendLaboratory
FractalChannel_v5
Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
FractalChannel_v5
//+------------------------------------------------------------------+
//|                                            FractalChannel_v5.mq4 |
//|                             Copyright © 2006-07, TrendLaboratory |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                   E-mail: igorad2003@yahoo.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006-07, TrendLaboratory"
#property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 LightBlue
#property indicator_color2 Tomato
#property indicator_color3 Yellow
#property indicator_width1 1
#property indicator_width2 1
#property indicator_style3 2
//---- input parameters
extern int     FractalSize = 1; //Fractal Size in bars (ex. 1-3 bars,2-5 bars,3-7 bars)
extern int     PriceMode   = 0; //Apply to : 0-High/Low,1-Open/Close 
extern double  Margins     = 0; //Narrowing ration (ex.0.3)
extern int     Shift       = 0; //Displace in bars (ex. 1 or -1) 
extern int     Mode        = 0; //Channel Mode: 0-default,1-similar to Fractal Chaos Bands  
extern int     DisplayText = 0; //0-off,1-on
extern color   TextColor   = White;
extern double  TextSize    = 8;
extern double  TextDelta   = 0; //Text deviation in pips 

//---- buffers
double UpBuffer[];
double DnBuffer[];
double MdBuffer[];
double smin[];
double smax[];

double HiArray[],LoArray[];
int    Length;
string short_name, ob_name1, ob_name2;
bool   firstTime = true;
double prevUp, prevDn;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- indicator line
   IndicatorBuffers(5);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(0,UpBuffer);
   SetIndexBuffer(1,DnBuffer);
   SetIndexBuffer(2,MdBuffer);
   SetIndexBuffer(3,smin);
   SetIndexBuffer(4,smax);
//---- name for DataWindow and indicator subwindow label
   short_name="Fractal Channel("+FractalSize+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,"Up Band");
   SetIndexLabel(1,"Down Band");
   SetIndexLabel(2,"Middle");
   
   
   SetIndexShift(0,Shift);
   SetIndexShift(1,Shift);
   SetIndexShift(2,Shift);
//----
   Length = 2*FractalSize+1; 
   SetIndexDrawBegin(0,Length);
   SetIndexDrawBegin(1,Length);
   SetIndexDrawBegin(2,Length);
//----
   ArrayResize(HiArray,Length);
   ArrayResize(LoArray,Length);
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjDel(ob_name1);
   ObjDel(ob_name2);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| FractalChannel_v5                                                |
//+------------------------------------------------------------------+
int start()
{
   int  	   shift,k,counted_bars=IndicatorCounted();
   double   v1,v2;
			
   if ( counted_bars > 0 )  int limit=Bars-counted_bars-1;
   if ( counted_bars < 0 )  return(0);
   if ( counted_bars ==0 )  limit=Bars-Length; 
     
	if ( counted_bars < 1 )
      for(int i=1;i<Length;i++)
      { 
      UpBuffer[Bars-i] = EMPTY_VALUE;    
      DnBuffer[Bars-i] = EMPTY_VALUE;
      MdBuffer[Bars-i] = EMPTY_VALUE;
      }
   		
	for(shift=limit;shift>=0;shift--) 
   {	
      for(int j = 0; j < Length; j++)
	   { 
         if(PriceMode == 0)
         {HiArray[j] = High[shift+j]; LoArray[j] = Low[shift+j];}
	      else
	      if(PriceMode == 1)
	      {HiArray[j] = MathMax(Close[shift+j],Open[shift+j]); LoArray[j] = MathMin(Close[shift+j],Open[shift+j]);}
	   }
	   
	v1 = Fractals(HiArray,0,FractalSize);
	v2 = Fractals(LoArray,1,FractalSize);
	
	smax[shift]=smax[shift+1];			
	if ( v1>0 ) smax[shift]=v1; 
	if (Mode == 0)
	if (HiArray[0]>smax[shift]) smax[shift]=HiArray[0];
	
	smin[shift]=smin[shift+1];
	if ( v2>0 ) smin[shift]=v2; 
	if (Mode == 0)
	if (LoArray[0]<smin[shift]) smin[shift]=LoArray[0];
	
	if (shift==Bars-Length) {smin[shift]=LoArray[0];smax[shift]=HiArray[0];}
	
	UpBuffer[shift]=smax[shift]-(smax[shift]-smin[shift])*Margins;
	DnBuffer[shift]=smin[shift]+(smax[shift]-smin[shift])*Margins;
	MdBuffer[shift]=(UpBuffer[shift]+DnBuffer[shift])/2;
	}

   if (DisplayText==1 && (firstTime || NewBar() || UpBuffer[0] != prevUp || DnBuffer[0] != prevDn))
   {
   ob_name1 = short_name+" Up";
   ob_name2 = short_name+" Dn";
   ObjDel(ob_name1);  
   ObjDel(ob_name2);
   
   int win = 0;
   ObjectCreate(ob_name1,OBJ_TEXT,win,iTime(NULL,0,0)+(Time[0]-Time[1])*3,UpBuffer[0]+TextDelta*Point);
   ObjectCreate(ob_name2,OBJ_TEXT,win,iTime(NULL,0,0)+(Time[0]-Time[1])*3,DnBuffer[0]+TextDelta*Point);
   
   ObjectSetText(ob_name1,DoubleToStr(UpBuffer[0],Digits),TextSize,"Arial",TextColor);
   ObjectSetText(ob_name2,DoubleToStr(DnBuffer[0],Digits),TextSize,"Arial",TextColor);
   prevUp = UpBuffer[0];
   prevDn = DnBuffer[0];
   }
return(0);
}

double Fractals(double price[],int type,int size)
{
   int k=1;
	double v1, cond[100];
	
	while (k<=size) 
	{
      if (type==0) bool condition = price[size+k]<=price[size] && price[size-k]<price[size];
      else condition = price[size+k]>=price[size] && price[size-k]>price[size];
      
      if (condition) 
      {
      if (type==0) cond[k]=price[size]; else cond[k]=price[size];
         if(k==1)
	      {v1=cond[k];k++;} 
	      else
	      if(cond[k-1]==cond[k]){v1=cond[k];k++;}else {v1=0; break;}
	   }     
      else {v1=0; break;}
	}
return(v1);	
}  

//+------------------------------------------------------------------+
bool NewBar()
{
   static datetime dt = 0;
   
   if (Time[0] != dt) {dt = Time[0]; return(true);}
   return(false);
}

void ObjDel(string name)
{
   int _GetLastError;
   int obtotal = ObjectsTotal();
   
   for ( int i = 0; i < obtotal; i ++ )
   {
      if( !ObjectDelete(name) )
      {
      _GetLastError = GetLastError();
      //Print( "ObjectDelete Error #", _GetLastError );
      }
   }
}

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