Navigation:Home > Content >

Auto_TL_04_fibo.mq4

Time: 2013-04-26 | Download file:Auto_TL_04_fibo.mq4

//+------------------------------------------------------------------+
//|Auto TL 04.mq4 
//|Copyright © 2008, Todd Geiger aka [email protected] ([email protected])
//|http://www.metaquotes.net
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Todd Geiger aka [email protected] ([email protected])"
#property link      "http://www.metaquotes.net"

#property indicator_chart_window

extern int     period=1440;  //time period from which the initial high/lows are taken from
extern int     Sensitivity=2;//see note below *
extern color   Color=Magenta;

/*Sensitivity is the number of period bars from the current bar to the 2nd point of the last 
  trendline that triggers a new trendline creation.  If the trendlines are too close, increase
  the value.  Too far, then decrease the value.*/

int s;
double   L1,L2,H1,H2;
double   df1,df2,uf1,uf2;
datetime LT1,LT2,HT1,HT2;
datetime dft1,dft2,uft1,uft2;
string hn,ln,dfn,ufn;

int init()  {  
   dfn="start";
   ufn="start";
   s=Sensitivity;
return(0);  }

int deinit(){ 
//delete all indicator created TL's
   for(int i=ObjectsTotal();i>=0;i--)  {
      if(StringSubstr(ObjectName(i),0,5)=="Lo TL" ||
         StringSubstr(ObjectName(i),0,5)=="Hi TL" ||
         StringSubstr(ObjectName(i),0,5)=="Bear " ||
         StringSubstr(ObjectName(i),0,5)=="Bull ")  {
         ObjectDelete(ObjectName(i));  }  }
return(0);  }

int start() {
//.....trendline breach handling.....
//.....get most recent TL names
if(ObjectsTotal()>0) {
   string lt,ht;
   ln="Lo TL 1970.01.01 00:00";
   hn="Hi TL 1970.01.01 00:00";
   for(int i=ObjectsTotal();i>=0;i--)  {
      if(StringSubstr(ObjectName(i),0,5)=="Lo TL") {
         lt=ObjectName(i);
         if(ObjectGet(lt,2)>StrToTime(StringSubstr(ln,6,16)))   { 
            ln=lt;
            LT1=ObjectGet(ln,0);
            L1=ObjectGet(ln,1);
            LT2=ObjectGet(ln,2);
            L2=ObjectGet(ln,3);
            for(int iii=1; iiiStrToTime(StringSubstr(hn,6,16)))   {
            hn=ht;
            HT1=ObjectGet(hn,0);
            H1=ObjectGet(hn,1);
            HT2=ObjectGet(hn,2);
            H2=ObjectGet(hn,3);
            for(int ii=1; iiObjectGetValueByShift(hn,ii))   {
                  ObjectDelete(hn); hn="Hi TL 1970.01.01 00:00";  }  }  }  }  }  }

   if(L2H1 || HT1==HT2 || H2==0 || H1==0)   {
      ObjectDelete(hn);
      H1=0; H2=0; HT1=0; HT2=0; hn="Hi TL 1970.01.01 00:00";   }
//if no TL's, make some...
   if(ObjectFind(hn)<0  || iBarShift(Symbol(),period,HT2)>s) { HTL(); }
   if(ObjectFind(ln)<0  || iBarShift(Symbol(),period,LT2)>s) { LTL(); }
FIB();
return(0);}

//+------------------------------------------------------------------+
void FIB()  {
   if(LT1-1)  {  ObjectDelete(dfn);   }
      dfn="Bear Fib "+TimeToStr(LT1,TIME_DATE|TIME_MINUTES);
      dft1=Time[iHighest(Symbol(),0,MODE_HIGH,iBarShift(Symbol(),0,LT1),0)];
      df1=High[iHighest(Symbol(),0,MODE_HIGH,iBarShift(Symbol(),0,LT1),0)];
      dft2=Time[iLowest(Symbol(),0,MODE_LOW,iBarShift(Symbol(),0,dft1),0)];
      df2=Low[iLowest(Symbol(),0,MODE_LOW,iBarShift(Symbol(),0,dft1),0)];

      if(ObjectFind(ufn)>-1)  {  ObjectDelete(ufn);   }
      ufn="Bull Fib "+TimeToStr(HT1,TIME_DATE|TIME_MINUTES);
      uft2=Time[iHighest(Symbol(),0,MODE_HIGH,iBarShift(Symbol(),0,LT1),0)];
      uf2=High[iHighest(Symbol(),0,MODE_HIGH,iBarShift(Symbol(),0,LT1),0)];
      uft1=Time[iLowest(Symbol(),0,MODE_LOW,iBarShift(Symbol(),0,LT1)+5,0)];
      uf1=Low[iLowest(Symbol(),0,MODE_LOW,iBarShift(Symbol(),0,LT1)+5,0)];   }

   if(HT1-1)  {  ObjectDelete(ufn);   }
      ufn="Bull Fib "+TimeToStr(HT1,TIME_DATE|TIME_MINUTES);
      uft1=Time[iLowest(Symbol(),0,MODE_LOW,iBarShift(Symbol(),0,HT1),0)];
      uf1=Low[iLowest(Symbol(),0,MODE_LOW,iBarShift(Symbol(),0,HT1),0)];
      uft2=Time[iHighest(Symbol(),0,MODE_HIGH,iBarShift(Symbol(),0,uft1),0)];
      uf2=High[iHighest(Symbol(),0,MODE_HIGH,iBarShift(Symbol(),0,uft1),0)];

      if(ObjectFind(dfn)>-1)  {  ObjectDelete(dfn);   }
      dfn="Bear Fib "+TimeToStr(LT1,TIME_DATE|TIME_MINUTES);
      dft2=Time[iLowest(Symbol(),0,MODE_LOW,iBarShift(Symbol(),0,HT1),0)];
      df2=Low[iLowest(Symbol(),0,MODE_LOW,iBarShift(Symbol(),0,HT1),0)];
      dft1=Time[iHighest(Symbol(),0,MODE_HIGH,iBarShift(Symbol(),0,HT1)+5,0)];
      df1=High[iHighest(Symbol(),0,MODE_HIGH,iBarShift(Symbol(),0,HT1)+5,0)];   }

      ObjectCreate(dfn,10,0,dft1,df1,dft2,df2);
      ObjectSet(dfn,9,1);//set as background drawing
      ObjectSet(dfn,201,Red);
      ObjectSet(dfn,200,4);
      ObjectSet(dfn,210+0,0);
      ObjectSetFiboDescription(dfn,0,"0");
      ObjectSet(dfn,210+1,0.382);
      ObjectSetFiboDescription(dfn,1,"0.382");
      ObjectSet(dfn,210+2,0.618);
      ObjectSetFiboDescription(dfn,2,"0.618");
      ObjectSet(dfn,210+3,1.00);
      ObjectSetFiboDescription(dfn,3,"1.00");
      
      ObjectCreate(ufn,10,0,uft1,uf1,uft2,uf2);
      ObjectSet(ufn,9,1);//set as background drawing
      ObjectSet(ufn,201,Green);
      ObjectSet(ufn,200,4);
      ObjectSet(ufn,210+0,0);
      ObjectSetFiboDescription(ufn,0,"0");
      ObjectSet(ufn,210+1,0.382);
      ObjectSetFiboDescription(ufn,1,"0.382");
      ObjectSet(ufn,210+2,0.618);
      ObjectSetFiboDescription(ufn,2,"0.618");
      ObjectSet(ufn,210+3,1.00);
      ObjectSetFiboDescription(ufn,3,"1.00");   }//end FIB();


void HTL()  {
   if(HT1==0)   {
      HT1=iTime(Symbol(),period,iHighest(Symbol(),period,MODE_HIGH,iBars(Symbol(),period),1));
      H1=iHigh(Symbol(),period,iHighest(Symbol(),period,MODE_HIGH,iBars(Symbol(),period),1));
      HT2=iTime(Symbol(),period,iHighest(Symbol(),period,MODE_HIGH,iBars(Symbol(),period),1)-1);
      H2=iHigh(Symbol(),period,iHighest(Symbol(),period,MODE_HIGH,iBars(Symbol(),period),1)-1);
      if(H2==0) H2=H1-1*Point;
      hn="Hi TL "+TimeToStr(HT1,TIME_DATE|TIME_MINUTES);
      create.hi(); }
   if(HT1>0)   {
      HT1=HT2;
      H1=H2;
      HT2=Time[iBarShift(Symbol(),Period(),HT1)-1];
      H2=High[iBarShift(Symbol(),Period(),HT1)-1];
      hn="Hi TL "+TimeToStr(HT1,TIME_DATE|TIME_MINUTES);
      create.hi();}  }//end HTL();

void LTL()  {
   if(LT1==0)   {
      LT1=iTime(Symbol(),period,iLowest(Symbol(),period,MODE_LOW,iBars(Symbol(),period),1));
      L1=iLow(Symbol(),period,iLowest(Symbol(),period,MODE_LOW,iBars(Symbol(),period),1));
      LT2=iTime(Symbol(),period,iLowest(Symbol(),period,MODE_LOW,iBars(Symbol(),period),1)-1);
      L2=iLow(Symbol(),period,iLowest(Symbol(),period,MODE_LOW,iBars(Symbol(),period),1)-1);
      ln="Lo TL "+TimeToStr(LT1,TIME_DATE|TIME_MINUTES);
      create.low(); }
   if(LT1>0)   {
      LT1=LT2;
      L1=L2;
      LT2=Time[iBarShift(Symbol(),Period(),LT1)-1];
      L2=Low[iBarShift(Symbol(),Period(),LT1)-1];
      ln="Lo TL "+TimeToStr(LT1,TIME_DATE|TIME_MINUTES);
      create.low();}  }//end LTL();

void create.low() {
   ObjectCreate(ln,2,0,LT1,L1,LT2,L2);
   ObjectSet(ln,6,Color);//color
   ObjectSet(ln,7,0);//STYLE_DASH
   ObjectSet(ln,8,0);//width
   ObjectSet(ln,9,1);//set as background drawing
   ObjectSet(ln,10,1);//set as ray
   for(int ii=iBarShift(Symbol(),Period(),LT1); ii>0; ii--)  {
      if(Low[ii]0; ii--)  {
      if(High[ii]>ObjectGetValueByShift(hn,ii)) {
         HT2=Time[ii];
         H2=High[ii];
         ObjectMove(hn,1,HT2,H2);   }  }  }//end create.hi();

Recommend