Simple Pattern Finder

Author: Copyright © 2017, Vladimir V. Tkach
Price Data Components
Series array that contains the highest prices of each barSeries array that contains the lowest prices of each barSeries array that contains open prices of each barSeries array that contains close prices for each barSeries array that contains open time of each bar
0 Views
0 Downloads
0 Favorites
Simple Pattern Finder
ÿþ//+------------------------------------------------------------------+

//|                                Copyright 2017, Vladimir V. Tkach |

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

#property version "1.0"

#property copyright "Copyright © 2017, Vladimir V. Tkach"

#property description "This utilit finds in history similar price movement,"

#property description "plots farther movement with the color by its force (similarity)."

#property link      "https://www.mql5.com/ru/users/net/news"

#property strict



extern int

barA=10,                   //Seek patterns for bars

bar0=0,                    //...start from this bar 

barD=10,                   //Draw predicted bars

barH=2000,                 //Use History (bars)

color_force=100;          //Pattern force by bars color (%)



input ENUM_TIMEFRAMES timeframe=60;     //Pattern Timeframe



double

delta[100],

price[100];



int 

bar,

patterns,

progress;



string

str1,

str2,

str3;



color Clr;

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

//|Initialisation function                                           |

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

int OnInit()

  {

   bar=(int)iTime(Symbol(),timeframe,1);   

   

   //create colored markers

   for(int i=30; i<100; i+=10)

      {

         ObjectCreate(IntegerToString(i),OBJ_LABEL,0,0,0);

         ObjectSet(IntegerToString(i),OBJPROP_XDISTANCE,10);

         ObjectSet(IntegerToString(i),OBJPROP_YDISTANCE,55+(100-i));

         

         Clr=Purple;                  

         if(i==40) Clr=Blue;

         if(i==50) Clr=Aqua;

         if(i==60) Clr=Green;

         if(i==70) Clr=Yellow;

         if(i==80) Clr=Orange;

         if(i==90) Clr=Red;

                  

         ObjectSet(IntegerToString(i),OBJPROP_COLOR,Clr);

         ObjectSet(IntegerToString(i),OBJPROP_CORNER,0);

         ObjectSetText(IntegerToString(i),">"+IntegerToString(i)+"%");

         ObjectSet(IntegerToString(i),OBJPROP_FONTSIZE,8);

      }

                      

   return(INIT_SUCCEEDED);

  }

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

//|Deinitialisation function                                         |

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

void OnDeinit(const int reason)

  {

   //delete all graphics

   DeleteOldLines();

   ObjectDelete("90");

   ObjectDelete("80");

   ObjectDelete("70");

   ObjectDelete("60");

   ObjectDelete("50");

   ObjectDelete("40");

   ObjectDelete("30");   

   Comment("");

   return;

  }

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

//|Custom comment function                                           |

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

void Comment2()

  {

     Comment(

      str1,"\n",

      str2,"\n",

      str3,"\n"

      );

      

   return;

  }  

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

//|                                                                  |

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

void OnTick()

  {

  

   if(bar!=(int)iTime(Symbol(),timeframe,0))

      { 

         bar=(int)iTime(Symbol(),timeframe,0);

         DeleteOldLines();

         patterns=0;

         progress=0;

         if(color_force!=0) {str3=""; FindPattern(); str1="Searching for patterns: 100% done"; Comment2();}

         else (str3="NO CRITERIA!");

      }



   return;

  }

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

//|                                                                  |

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

void FindPattern()

   {

      double 

      O=2.0, C=2.0, H=1.0, L=1.0, //weights for prices

      dev=O+C+H+L;

      

      //averaged price

      price[0]=(O*iOpen(Symbol(),timeframe,bar0)+C*iClose(Symbol(),timeframe,bar0)+H*iHigh(Symbol(),timeframe,bar0)+L*iLow(Symbol(),timeframe,bar0))/dev;

      

      for(int i=bar0+barA; i<barH-barA; i++)

         {

            progress=i+barA;

            str1="Searching for patterns: "+StringSubstr(DoubleToString(1.0*progress/(1.0*barH)*100.0),0,3)+"% done";

            Comment2();

            

            int cf=0;

            double rcf=0.0;

            

            //compare colors

            for(int e=0; e<barA; e++)

               {

                  if( (iOpen(Symbol(),timeframe,bar0+e)>iClose(Symbol(),timeframe,bar0+e) && iOpen(Symbol(),timeframe,i+e)>iClose(Symbol(),timeframe,i+e)) ||

                  (iOpen(Symbol(),timeframe,bar0+e)<iClose(Symbol(),timeframe,bar0+e) && iOpen(Symbol(),timeframe,i+e)<iClose(Symbol(),timeframe,i+e))) 

                     {

                        cf+=1; //colors are similar

                     }

               }

            

            rcf=1.0*cf/(1.0*barA)*100.0;  //rate of similarity in colors, %

                                  

            if(rcf>=color_force && patterns<100) 

               {

                  patterns+=1; 

                  str2="Found patterns: "+IntegerToString(patterns);

                  Comment2(); 

                  

                  //save further movement of a found pattern

                  for(int e2=0; e2<100; e2++)

                     {

                        if(e2>=barD || i-e2-1<1) {delta[e2]=0.0; continue;}

                        

                        delta[e2]=(O*iOpen(Symbol(),timeframe,i-e2-1)+C*iClose(Symbol(),timeframe,i-e2-1)+H*iHigh(Symbol(),timeframe,i-e2-1)+L*iLow(Symbol(),timeframe,i-e2-1)*L)/dev-

                        (O*iOpen(Symbol(),timeframe,i-e2)+C*iClose(Symbol(),timeframe,i-e2)+H*iHigh(Symbol(),timeframe,i-e2)+L*iLow(Symbol(),timeframe,i-e2))/dev;

                     }      

                                              

                  Clr=Purple;                  

                  if(rcf>40) Clr=Blue;

                  if(rcf>50) Clr=Aqua;

                  if(rcf>60) Clr=Green;

                  if(rcf>70) Clr=Yellow;

                  if(rcf>80) Clr=Orange;

                  if(rcf>90) Clr=Red;

                  

                  //plot further movement

                  PlotPattern(Clr);

               }               

         }

   

      return;

   }

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

//|                                                                  |

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

void PlotPattern(color Clr2)

   {

      string name;

      int gap=0, prevtime=0;

      

      for(int i=1; i<=barD; i++)

         {

            if(delta[i]==0.0) {break;}

            

            if(i==1) prevtime=(int)iTime(Symbol(),timeframe,bar0);

            price[i]=price[i-1]+delta[i-1];

          

            name="Pattern Line "+IntegerToString(ObjectsTotal());

            

            //make correction if weekends

            gap=0;

            if(TimeDayOfWeek(iTime(Symbol(),timeframe,bar0)+i*timeframe*60)==0) gap=1*24*60*60;

            if(TimeDayOfWeek(iTime(Symbol(),timeframe,bar0)+i*timeframe*60)==6) gap=2*24*60*60;

            

            //plot colored line

            ObjectCreate(name,OBJ_TREND,0,

            prevtime,

            price[i-1],

            iTime(Symbol(),timeframe,bar0)+i*timeframe*60+gap,

            price[i]);

            

            prevtime=(int)iTime(Symbol(),timeframe,bar0)+i*timeframe*60+gap;

            

            ObjectSet(name,OBJPROP_WIDTH,1);

            ObjectSet(name,OBJPROP_RAY_RIGHT,false); 

            ObjectSet(name,OBJPROP_COLOR,Clr2);

         }

   

      return;

   }

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

//|                                                                  |

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

void DeleteOldLines()

  {

   for(int i=ObjectsTotal()-1; i>-1; i--)

     {

      if(StringFind(ObjectName(i),"Pattern Line ",0)==-1) continue;

      ObjectDelete(ObjectName(i));

     }

   return;

  }

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

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