Pattern Matching from 7Bit

Post Reply
User avatar
babalu4u
Trader
Posts: 173
Joined: Fri Apr 13, 2012 6:52 pm
Location: Slovenia - EU

Pattern Matching from 7Bit

Post by babalu4u »

7Bit start this open surce project on forexfactory but there was no interest but i think this project MUST GO ON and the right place is this forum...
http://www.forexfactory.com/showthread.php?t=217486
From 7Bit:

Let f be a continuous function:

y=f(x); x,y ∈ ℝ; 0 ≤ x ≤ 100

whose function values, when plotted, would define the shape of a price pattern in such a way that f(0) would correspond to the close price of the current bar and f(100) to the beginning of the pattern in the past.

After having defined such a function I loop through a certain range of widths (say from 10 to 200 or even more) and for each such width I take this amount of recent bars, determine their highest high and lowest low and scale my pattern (x and f(x)) into this rectangle of market prices. For each such set of bars I calculate the error (the sum of all differences between each bar's close price and the ideal pattern value at this bar's position and finally divided by the area of the complete rectangle to make different sizes comparable) and if the best match has an error below a certain threshold I plot the pattern into the chart and open a trade.

In other words: On every bar open I test many differently sized rectangles of recent prices and try to fit the pattern into each of them until I find one that fits well enough.

This is how the error for a range of recent bars (determined by width) is calculated (negative width means pattern is mirrored at the x axis):

Code: Select all

/**
* calculate the error.
* width is the number of bars before the last_bar.
* sum up all errors between price and the theoretical
* pattern shape. It will determine needed scale and offset
* for the pattern function to fit into the rectangle 
* automatically
*/
double error(int width, int last_bar=0){
   int i;
   double price, pattern, error;
   double y_scale, y_0;
   int sign = 1;
   
   scalePatternY(width, last_bar, y_scale, y_0);
   
   if (width<0){
      width = -width;
      sign = -1;
   }
   
   for (i=0; i<=width; i++){
      price = Close[last_bar + i];
      pattern = y_0 + sign * y_scale * pattern(100.0 * i / width);
      error += MathAbs(price - pattern);
   }
   
   return(error / (width * rangeHeight(width, last_bar)));
}
the function scalePatternY() that is called here determines optimal y_0 and y_scale values by simply looking at the price range and the co-domain of our pattern function and doing some simple math to find the needed scale and offset for our pattern.

Has anybody tried this approach before?

It is a bit cumbersome to create the pattern functions and the one in the example image is only a quick test to debug the basic functionality, a rough fantasy pattern out of the top of my head, but already quite promising.

If somebody here is willing to help me find and test better pattern shapes then he can find the complete source code attached to this posting.

This two files go into the experts folder:

patternmatcher.mq4
patterndesigner.mq4

This three files go into the experts/include folder:

patternmatcher.common.mqh
patternmatcher.definitions.mqh
common_functions.mqh

Patternmatcher is the EA used for trading, Patterndesigner is an EA only used during pattern development for visualizing (plotting) the pattern function into a chart.

The source is licensed under GNU GPL V3, this means: don't remove the license or any copyright notices, derivative works MUST ALWAYS include full source code and copyright notices along with the SAME license, selling closed source EAs derived from or containing (parts of) this code is not allowed. Commercial violations of other people's copyrights are considered a crime in most countries of this world!

To quickly get you started with pattern coding here is how my pattern functions look like. Basically i just split the x range into pieces (with if-constructs) and for each piece I calculate the y-value with a simple line equation. Of course you could also use all sorts of fancy math instead of the simple ab() line function to get other shapes.

The pattern function is defined in patternmatcher.definitions.mqh which will be used by both EAs. Whenever you change this file you must recompile the EAs, not the include files.

If you attach the Patterndesigner EA to an empty chart you can quickly view changes you made to the pattern function directly on the chart and drag the pattern around with the mouse to see how it looks compared to actual price. After you modified the function in patternmatcher.definitions.mqh just recompile patterndesigner.mq4 (don't compile the include files), move the lines around and see your changes directly in the chart.

The example from the above screenshot is defined by a function that looks like this:

Code: Select all

double ab(double ax, double ay, double bx, double by, double x){
   return (ay + (by-ay)*(x-ax)/(bx-ax));
}

double pattern(double x){
   
   x *= 0.7; // change the x range
   
   if (x < 8) return(ab(0,0,8,8-50,x));
   if (x < 13) return(ab(8,8-50,13,-13-50,x));
   if (x < 21) return(ab(13,-13-50,21,21-50,x));
   if (x < 34) return(ab(21,21-50,34,-34-50,x));
   if (x < 55) return(ab(34,-34-50,55,55-50,x));
               return(ab(55,55-50,70,-45-50,x));   
}
A simple V-shape pattern may look like this:

Code: Select all

double pattern(double x){
   if (x < 50) return(ab(0,0,50,-100,x));
               return(ab(50,-100,100,0,x));   
}
You do not have the required permissions to view the files attached to this post.
User avatar
babalu4u
Trader
Posts: 173
Joined: Fri Apr 13, 2012 6:52 pm
Location: Slovenia - EU

Re: Pattern Matching from 7Bit

Post by babalu4u »

The current versions of the dll and the EA. Both have been rewritten to get rid of all globals variables in the dll, so it can be used on more than one pair at the same time and the code has been cleaned up, made understandable and commented as good as i can.

The patttern function is now renamed to pf() and is found in a separate unit. It is still the same old dummy function from last time, don't expect it to be profitable.

the zip file contains the dll source code, open the .lpi file with Lazarus and compile as usual. Then move the pattern.dll to experts/libraries/

patterntrader.mq4 is the EA, move it into experts/
pattern.mqh is an include file and belongs into experts/include/
common_functions.mqh also belongs to experts/include/

Lazarus Free Pascal:
http://www.freepascal.org/download.var
You do not have the required permissions to view the files attached to this post.
Last edited by babalu4u on Thu May 09, 2013 8:39 am, edited 2 times in total.
User avatar
babalu4u
Trader
Posts: 173
Joined: Fri Apr 13, 2012 6:52 pm
Location: Slovenia - EU

Re: Pattern Matching from 7Bit

Post by babalu4u »

Patternmatcher..
You do not have the required permissions to view the files attached to this post.
User avatar
babalu4u
Trader
Posts: 173
Joined: Fri Apr 13, 2012 6:52 pm
Location: Slovenia - EU

Re: Pattern Matching from 7Bit

Post by babalu4u »

pattern designer...
You do not have the required permissions to view the files attached to this post.
User avatar
babalu4u
Trader
Posts: 173
Joined: Fri Apr 13, 2012 6:52 pm
Location: Slovenia - EU

Re: Pattern Matching from 7Bit

Post by babalu4u »

and the last version with DLL - very fast
You do not have the required permissions to view the files attached to this post.
User avatar
babalu4u
Trader
Posts: 173
Joined: Fri Apr 13, 2012 6:52 pm
Location: Slovenia - EU

Re: Pattern Matching from 7Bit

Post by babalu4u »

And interactive pattern design tool it is Python because the dynamic compiling and evaluation at runtime is much easier with a dynamic script language.

The pattern shape to the left will update while you are typing to the right.

copy&paste your findings into a text editor since it has no load and save functions. After you are finished with the function just port it from python to pascal to use it in the dll. Since this must not be done every day and the translation from python to pascal is ralatively easy i have decided that this solution is good enough.
You do not have the required permissions to view the files attached to this post.
jcl
Trader
Posts: 82
Joined: Wed Oct 31, 2012 8:04 am
Location: Frankfurt / Germany

Re: Pattern Matching from 7Bit

Post by jcl »

Have you tried the Frechet algorithm? I've found that it works quite well for pattern matching. Your approach, as far as I understand it, has the disadvantage that it won't detect patterns that are internally distorted, f.i. a three peaks where the middle peak is slightly shifted towards the first or the last one.
Post Reply

Return to “Automated trading systems”