Pattern Matching from 7Bit

Pattern Matching from 7Bit

Postby babalu4u » Thu May 09, 2013 7:41 am

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]
 Please Login or Register to see this content
<


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]
 Please Login or Register to see this content
<

A simple V-shape pattern may look like this:
Code: [Select all]
 Please Login or Register to see this content
<
You do not have the required permissions to view the files attached to this post.
User avatar
babalu4u
 
Posts: 169
Joined: Fri Apr 13, 2012 6:52 pm
Location: Slovenia - EU

Re: Pattern Matching from 7Bit

Postby babalu4u » Thu May 09, 2013 7:45 am

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.
User avatar
babalu4u
 
Posts: 169
Joined: Fri Apr 13, 2012 6:52 pm
Location: Slovenia - EU

Re: Pattern Matching from 7Bit

Postby babalu4u » Thu May 09, 2013 7:47 am

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

Re: Pattern Matching from 7Bit

Postby babalu4u » Thu May 09, 2013 7:49 am

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

Re: Pattern Matching from 7Bit

Postby babalu4u » Thu May 09, 2013 8:06 am

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
 
Posts: 169
Joined: Fri Apr 13, 2012 6:52 pm
Location: Slovenia - EU

Re: Pattern Matching from 7Bit

Postby babalu4u » Thu May 09, 2013 8:13 am

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.
User avatar
babalu4u
 
Posts: 169
Joined: Fri Apr 13, 2012 6:52 pm
Location: Slovenia - EU

Re: Pattern Matching from 7Bit

Postby jcl » Sat May 25, 2013 1:03 pm

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.
jcl
 
Posts: 82
Joined: Wed Oct 31, 2012 8:04 am
Location: Frankfurt / Germany


Return to Automated trading systems

Who is online

Users browsing this forum: Bing [Bot] and 2 guests