Donch_Pattern_finder_v2

0 Views
0 Downloads
0 Favorites
Donch_Pattern_finder_v2
ÿþ#property strict



#property indicator_chart_window

#property indicator_buffers 2

#property  indicator_color1  clrBlue

#property  indicator_color2  clrYellow

#property  indicator_width1  3

#property  indicator_width2  3



//--- input parameters



extern bool               reset_on_oposite = true; //Reset on oposite signal

extern bool               reverse_on_oposite = false; //Reverse on oposite signal (only if not reset)

extern int                Valid=300; //Valid, bars (only if not reset)

extern int                iPeriod = 60; //Period

extern int                Slowing  = 15; //Slowing

extern int                Width    = 10; //Zone width, points

extern double             Filter_Factor    = 0.5; //Filter factor

extern color ZONE_Buy_Color =clrSkyBlue; //Zone BUY color

extern color ZONE_Sell_Color=clrOrange; //Zone SELL color

 bool  draw_dots=false; //Draw dots (triggers) 

extern int                LIMIT   = 5000; //Limit, bars (0 - all hystory)



double donch_up[],donch_dn[],buy[],sell[];

datetime LastTime;

string obj_pref="SR_Donch_";

int trend;



struct SR_ZONE

{

 datetime first_bar; //time, when zone starts

 double   price;     //price

};

SR_ZONE Buy_Zone[];                //here we store information about Buy zones

SR_ZONE Sell_Zone[];               //here we store information about Sell zones

int BuyZonesTotal,SellZonesTotal;  //Number of zones of each type



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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   if(Slowing<2) Slowing=2;

   if(iPeriod < 2) iPeriod=2;

   if(LIMIT<10 || LIMIT>Bars-iPeriod-Slowing-iPeriod-2) LIMIT=Bars-iPeriod-Slowing-iPeriod-2;

   LastTime=0;

  ArrayResize(Buy_Zone ,MathMax(10,Bars));

  ArrayResize(Sell_Zone,MathMax(10,Bars));

  BuyZonesTotal=SellZonesTotal=0;

  trend=0;

   

   IndicatorBuffers(4);



   SetIndexBuffer(0,buy);   

   SetIndexStyle (0,(draw_dots?DRAW_ARROW:DRAW_NONE));// Line style

   SetIndexArrow (0,159);

   

   SetIndexBuffer(1,sell);   

   SetIndexStyle (1,(draw_dots?DRAW_ARROW:DRAW_NONE));// Line style

   SetIndexArrow (1,159);

   

   SetIndexBuffer(2,donch_up); 

   SetIndexBuffer(3,donch_dn);



   return(INIT_SUCCEEDED);

  }

  

int deinit()

{

  if(!IsTesting()) ObjectsDeleteAll(0,obj_pref);

  return(0);

}



int start()

  {

  int counted_bars=IndicatorCounted(),

      limit=(counted_bars==0)?LIMIT:(Bars-counted_bars);



   if(limit>LIMIT) limit=LIMIT;

   if(limit<3) limit=3;

   

   double hst,lst;



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

    {

      donch_up[i]  = High[iHighest(NULL, 0, MODE_HIGH, iPeriod, i)];

      donch_dn[i]  = Low [iLowest (NULL, 0, MODE_LOW , iPeriod, i)];

      

      int up_min=ArrayMinimum(donch_up,Slowing,i),

          dn_max=ArrayMaximum(donch_dn,Slowing,i);

      

      hst=donch_up[i]-donch_up[up_min];

      lst=donch_dn[i]-donch_dn[dn_max];



      buy [i]=(hst>0 &&  hst>(donch_up[up_min]-donch_dn[up_min])*Filter_Factor)?donch_up[up_min]:EMPTY_VALUE;

      sell[i]=(lst<0 && -lst>(donch_up[dn_max]-donch_dn[dn_max])*Filter_Factor)?donch_dn[dn_max]:EMPTY_VALUE;

    }



  Text(1,"No signal. Trend: "+(trend==+1?"BUY":"SELL"),(trend==+1?ZONE_Buy_Color:ZONE_Sell_Color));

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

   if(Close[0]>=Buy_Zone[i].price-Width*Point && 

      Close[0]<=Buy_Zone[i].price+Width*Point)   

    Text(1,"Bid in BUY zone "+IntegerToString(i+1),ZONE_Buy_Color);



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

   if(Close[0]>=Sell_Zone[i].price-Width*Point && 

      Close[0]<=Sell_Zone[i].price+Width*Point)   

    Text(1,"Bid in SELL zone "+IntegerToString(i+1),ZONE_Sell_Color);



   //once per bar

   if(LastTime == iTime(NULL,0,0)) return(0);

      LastTime  = iTime(NULL,0,0);

 

   for (int i=limit+1; i>0 && !IsStopped(); i--)

    {

     if((buy [i]!=EMPTY_VALUE && buy [i+1]==EMPTY_VALUE) ||

        (buy [i]!=EMPTY_VALUE && buy [i-1]==EMPTY_VALUE))

        {

         bool found=false;

         for(int j=0;j<BuyZonesTotal; j++) if(CompareToll(Buy_Zone[j].price,buy [i],Width*Point)) {found=true;break;}

         if(found) continue;

         Buy_Zone[BuyZonesTotal].price    =buy [i];

         Buy_Zone[BuyZonesTotal].first_bar=Time[i];

         BuyZonesTotal++;

         trend=+1;

        }

     if((sell[i]!=EMPTY_VALUE && sell[i+1]==EMPTY_VALUE) ||

        (sell[i]!=EMPTY_VALUE && sell[i-1]==EMPTY_VALUE))

        {

         bool found=false;

         for(int j=0;j<SellZonesTotal; j++) if(CompareToll(Sell_Zone[j].price,sell[i],Width*Point)) {found=true;break;}

         if(found) continue;

         Sell_Zone[SellZonesTotal].price    =sell[i];

         Sell_Zone[SellZonesTotal].first_bar=Time[i];

         SellZonesTotal++;

         trend=-1;

        }

     

     Clean_up(i);

     for(int j=0;j<BuyZonesTotal; j++) Draw_BUY_ZONE (j,i);

     for(int j=0;j<SellZonesTotal;j++) Draw_SELL_ZONE(j,i);     

    }

  

//  Text(3,"Buy Zones total: "+IntegerToString(BuyZonesTotal));

//  Text(4,"Sell Zones total: "+IntegerToString(SellZonesTotal));

//  for(int j=0;j<BuyZonesTotal;  j++) Text(j+6,"Buy Zone "+IntegerToString(j+1)+": "+/*TimeToString(Buy_Zone[j].first_bar,TIME_MINUTES)+" - "+*/DoubleToString(Buy_Zone[j].price,Digits),ZONE_Buy_Color);

//  for(int j=0;j<SellZonesTotal; j++) Text(j+7+BuyZonesTotal,"Sell Zone "+/*IntegerToString(j+1)+": "+TimeToString(Sell_Zone[j].first_bar,TIME_MINUTES)+" - "+*/DoubleToString(Sell_Zone[j].price,Digits),ZONE_Sell_Color);





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

   return(0);

  }

  

void Draw_BUY_ZONE(int i, int curr_bar=0)

{

   int start_bar=iBarShift(NULL,0,Buy_Zone[i].first_bar);

   string name=obj_pref+"Buy_"+TimeToString(Buy_Zone[i].first_bar);

   if(ObjectCreate  (0,name,OBJ_RECTANGLE,0,0,0,0,0))

    {

      ObjectSetInteger(0,name,OBJPROP_COLOR,ZONE_Buy_Color);

      ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);

      ObjectSetInteger(0,name,OBJPROP_HIDDEN,true);

    }

   ObjectSetInteger(0,name,OBJPROP_TIME1,Buy_Zone[i].first_bar);

   ObjectSetInteger(0,name,OBJPROP_TIME2,Time[curr_bar]);

   ObjectSetDouble (0,name,OBJPROP_PRICE1,Buy_Zone[i].price-Width*Point/2);

   ObjectSetDouble (0,name,OBJPROP_PRICE2,Buy_Zone[i].price+Width*Point/2);

}



void Draw_SELL_ZONE(int i,int curr_bar=0)

{

   int start_bar=iBarShift(NULL,0,Sell_Zone[i].first_bar);

   string name=obj_pref+"Sell_"+TimeToString(Sell_Zone[i].first_bar);

   if(ObjectCreate  (0,name,OBJ_RECTANGLE,0,0,0,0,0))

    {

      ObjectSetInteger(0,name,OBJPROP_COLOR,ZONE_Sell_Color);

      ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);

      ObjectSetInteger(0,name,OBJPROP_HIDDEN,true);

    }

   ObjectSetInteger(0,name,OBJPROP_TIME1,Sell_Zone[i].first_bar);

   ObjectSetInteger(0,name,OBJPROP_TIME2,Time[curr_bar]);

   ObjectSetDouble (0,name,OBJPROP_PRICE1,Sell_Zone[i].price+Width*Point/2);

   ObjectSetDouble (0,name,OBJPROP_PRICE2,Sell_Zone[i].price-Width*Point/2);

}



void Clean_up(int curr_bar=0)

 {

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

   if((!reset_on_oposite && iBarShift(NULL,0,Buy_Zone[i].first_bar)-curr_bar>Valid) ||

       (reset_on_oposite && !reverse_on_oposite && trend==-1))

    {

     //if(i<BuyZonesTotal-1)

      for(int j=i;j<BuyZonesTotal-1;j++)

       {

        Buy_Zone[j].first_bar =Buy_Zone[j+1].first_bar;

        Buy_Zone[j].price     =Buy_Zone[j+1].price;

       }

     BuyZonesTotal--;

    }

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

   if((!reset_on_oposite && iBarShift(NULL,0,Sell_Zone[i].first_bar)-curr_bar>Valid) ||

       (reset_on_oposite && !reverse_on_oposite && trend==+1))

    {

     //if(i<SellZonesTotal-1)

      for(int j=i;j<SellZonesTotal-1;j++)

       {

        Sell_Zone[j].first_bar =Sell_Zone[j+1].first_bar;

        Sell_Zone[j].price     =Sell_Zone[j+1].price;

       }

     SellZonesTotal--;

    }



  if(!reverse_on_oposite || reset_on_oposite) return;

  

  if(trend==-1)

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

    {

     Sell_Zone[SellZonesTotal].first_bar =Time[curr_bar+1];

     Sell_Zone[SellZonesTotal].price     =Buy_Zone[i].price;

     SellZonesTotal++;

     for(int j=i;j<BuyZonesTotal-1;j++)

      {

       Buy_Zone[j].first_bar =Buy_Zone[j+1].first_bar;

       Buy_Zone[j].price     =Buy_Zone[j+1].price;

      }

     BuyZonesTotal--;

    }

    

  if(trend==+1)

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

    {

     Buy_Zone[BuyZonesTotal].first_bar =Time[curr_bar+1];

     Buy_Zone[BuyZonesTotal].price     =Sell_Zone[i].price;

     BuyZonesTotal++;

     for(int j=i;j<SellZonesTotal-1;j++)

      {

       Sell_Zone[j].first_bar =Sell_Zone[j+1].first_bar;

       Sell_Zone[j].price     =Sell_Zone[j+1].price;

      }

     SellZonesTotal--;

    }

 }

 

void Text(int i,string text="",color Color=clrLime)

{

   string name=obj_pref+"_T_"+IntegerToString(i);

   if(ObjectCreate(name,OBJ_LABEL,0,0,0))

    {

     ObjectSet(name,OBJPROP_SELECTABLE,false);

     ObjectSet(name,OBJPROP_HIDDEN,true);

     ObjectSet(name,OBJPROP_XDISTANCE,5);    

     ObjectSet(name,OBJPROP_CORNER,CORNER_LEFT_UPPER);

     ObjectSet(name,OBJPROP_YDISTANCE,14*i);

    }

   ObjectSetText(name,text,10,"Arial",Color);

}



bool CompareToll(double value1,double value2,double tollerance)

{

 return(value1+tollerance>=value2 && value1-tollerance<=value2);

}

Comments