• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 11:58am
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 11:58am
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

Account Matching or Prop Trading 3 replies

Pattern Matching Concept EA 61 replies

Two matching highs trend breakout? 3 replies

Matching metatrader times to alerts 0 replies

Oanda's NOT a matching engine...? 4 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
  • 20
Attachments: pattern matching
Exit Attachments

pattern matching

  • Last Post
  •  
  • Page 1 234 5
  • Page 1 234 5
  •  
  • Post #1
  • Quote
  • First Post: Edited Feb 24, 2010 7:11am Jan 25, 2010 11:59am | Edited Feb 24, 2010 7:11am
  •  7bit
  • Joined Mar 2009 | Status: Member | 1,231 Posts
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):
Inserted Code
/**
* 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?

Attached Image (click to enlarge)
Click to Enlarge

Name: pattern_match_m15_gPuJVu.png
Size: 69 KB


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:
Inserted Code
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:
Inserted Code
double pattern(double x){
   if (x < 50) return(ab(0,0,50,-100,x));
               return(ab(50,-100,100,0,x));   
}
Anybody interested in designing such patterns?

Latest version of the code can be found in posting 67 you will need FPC/Lazarus to compile the dll. The files attached below are very early prototypes and only here for historic reasons, they implement the same concept without dll in pure mql4 and thus run very slow, i dont recommend to use them.
Attached Files
File Type: mq4 patternmatcher.mq4   39 KB | 1,090 downloads | Uploaded Jan 27, 2010 12:15pm
File Type: mq4 patterndesigner.mq4   4 KB | 878 downloads | Uploaded Jan 27, 2010 12:16pm
File Type: mqh patternmatcher.common.mqh   6 KB | 788 downloads | Uploaded Jan 27, 2010 12:16pm
File Type: mqh patternmatcher.definitions.mqh   3 KB | 754 downloads | Uploaded Jan 26, 2010 5:43pm
File Type: mqh common_functions.mqh   25 KB | 759 downloads | Uploaded Jan 26, 2010 5:43pm
  • Post #2
  • Quote
  • Jan 25, 2010 2:05pm Jan 25, 2010 2:05pm
  •  delinquent
  • | Joined Dec 2009 | Status: Member | 15 Posts
pm sent..
time for some fun
 
 
  • Post #3
  • Quote
  • Jan 25, 2010 6:15pm Jan 25, 2010 6:15pm
  •  7bit
  • Joined Mar 2009 | Status: Member | 1,231 Posts
I am just working on a tool to make pattern design a lot easier: When the EA is in design mode you can move around the patterns with the mouse on the chart to see how they match, make changes to the pattern function and simply click recompile to immediately see the effect.

Attached Image (click to enlarge)
Click to Enlarge

Name: pattern_designer.png
Size: 42 KB


I will release this whole thing under GNU GPL3. Currently making the last cleanups and some source code comments and other final polishing so it will soon be ready for the initial release. I still hope there will be some more resonance, so we can develop pattern functions together that actually work and provide a strong edge.
 
 
  • Post #4
  • Quote
  • Jan 25, 2010 8:14pm Jan 25, 2010 8:14pm
  •  luxinterior
  • Joined Nov 2006 | Status: MT4 EA Coder Since 2006 | 300 Posts
Looks very interesting 7bit. Be interesting to see the results. Appreciate your efforts!

Lux
MT4 EA, Indicator and Alert Coder Since 2006
 
 
  • Post #5
  • Quote
  • Jan 25, 2010 11:47pm Jan 25, 2010 11:47pm
  •  sloof
  • | Joined Apr 2009 | Status: Birejji (ビレッジ) ►FOREX◄ | 141 Posts
very cool! something like this would work very nicely with a project i'm working on.

Suppose you found a general pattern in the market... essentially being able to predict volatility what would you do with that knowledge?
 
 
  • Post #6
  • Quote
  • Jan 26, 2010 9:54am Jan 26, 2010 9:54am
  •  7bit
  • Joined Mar 2009 | Status: Member | 1,231 Posts
Quoting sloof
Disliked
essentially being able to predict volatility
Ignored
I have never understood how i could profit from volatility alone. To some extend i can "predict" that volatility will increase sometime in the near future when i see a narrowing consolidation or the sun is about to rise in Europe but i still need a direction to trade, the only thing i could imagine would be opening a random trade with small SL and hoping that in such a condition the win ratio would be bigger (near 50%) than the RR.
 
 
  • Post #7
  • Quote
  • Jan 26, 2010 10:20am Jan 26, 2010 10:20am
  •  Graff
  • | Joined May 2009 | Status: Member | 35 Posts
Hi, 7bit!
Nice work. Have you tryed neural networks? Can you share all sources?
 
 
  • Post #8
  • Quote
  • Jan 26, 2010 11:48am Jan 26, 2010 11:48am
  •  forexsaint
  • Joined Jun 2009 | Status: <-That's how u gonna b, in the END! | 1,508 Posts
Hi
so you are still on this....i remember, few months back we had few conversations on this. I am glad..you are successful in embedding python and stuff.
I am available for any help i can ..just let me know
(though i am not a full programmer)
Subscribed.
Regards
100 Fold Challenge->Interested? ->https://www.forexfactory.com/thread/32152
 
 
  • Post #9
  • Quote
  • Jan 26, 2010 11:51am Jan 26, 2010 11:51am
  •  forexsaint
  • Joined Jun 2009 | Status: <-That's how u gonna b, in the END! | 1,508 Posts
Quoting 7bit
Disliked
I have never understood how i could profit from volatility alone. To some extend i can "predict" that volatility will increase sometime in the near future when i see a narrowing consolidation or the sun is about to rise in Europe but i still need a direction to trade
Ignored
There is a way ! Its like "Show me the Volatility, i will give you the money".
Pm sent.
100 Fold Challenge->Interested? ->https://www.forexfactory.com/thread/32152
 
 
  • Post #10
  • Quote
  • Jan 26, 2010 12:56pm Jan 26, 2010 12:56pm
  •  7bit
  • Joined Mar 2009 | Status: Member | 1,231 Posts
I have released the source code. It is attached to post #1 in this thread along with some instructions. License is GNU GPL V3.

Now i want to see pattern functions with an edge. I released my initial work for the very egoistic motivation of letting you continue and improve it and you being forced by GPL to share your results with all of us, including me.
 
 
  • Post #11
  • Quote
  • Jan 26, 2010 5:04pm Jan 26, 2010 5:04pm
  •  Adal
  • Joined Mar 2009 | Status: Member | 770 Posts
Quoting 7bit
Disliked
I have never understood how i could profit from volatility alone. To some extend i can "predict" that volatility will increase sometime in the near future when i see a narrowing consolidation or the sun is about to rise in Europe but i still need a direction to trade, the only thing i could imagine would be opening a random trade with small SL and hoping that in such a condition the win ratio would be bigger (near 50%) than the RR.
Ignored
Here's how: http://en.wikipedia.org/wiki/Straddle
 
 
  • Post #12
  • Quote
  • Jan 26, 2010 5:48pm Jan 26, 2010 5:48pm
  •  sloof
  • | Joined Apr 2009 | Status: Birejji (ビレッジ) ►FOREX◄ | 141 Posts
Quoting 7bit
Disliked
I have released the source code. It is attached to post #1 in this thread along with some instructions. License is GNU GPL V3.

Now i want to see pattern functions with an edge. I released my initial work for the very egoistic motivation of letting you continue and improve it and you being forced by GPL to share your results with all of us, including me.
Ignored
thanks! I'll play around with it and report any significant findings
 
 
  • Post #13
  • Quote
  • Jan 26, 2010 6:18pm Jan 26, 2010 6:18pm
  •  7bit
  • Joined Mar 2009 | Status: Member | 1,231 Posts
I have updated the files a few minutes ago. I have now split it up into two EAs and the patterns itself are contained in a completely separate file (patternmatcher.definitions.mqh), so the patterns you may have found won't be overwritten by an updated version of the EA.

.mq4 files go into experts, .mqh files go into experts/include

The Patterndesigner works as follows: attach the EA to a chart. You should see 4 vertical lines and two copies of the pattern plotted between them, one for long and one for short. Move the lines around to move the pattern. The orange number is the error value calculated while matching the pattern to the price, the red and green lines are stop and target levels for this pattern.
In the top left corner it shows the height of the pattern. The pattern height must be near or exactly 100 or your pattern wont be able to be fitted to the height of the price range. (I will remove this restriction in later versions because I already have a better algorithm for scaling and fitting the pattern in mind)

Now make your changes to the pattern function in patternmatcher.definitions.mqh (this is the reason why you are actually using this EA: writing pattern functions) save the file and recompile patterndesigner.mq4 (important: recompile the EA not the include file) Your changes should now become visible in the chart immediately after moving one of the lines a bit.

After you have designed a nice pattern and determined some inital values for stop_factor and target_factor and the max_error threshold just recompile the Patternmatcher EA too and do a few backtests.
 
 
  • Post #14
  • Quote
  • Edited at 10:56pm Jan 26, 2010 10:17pm | Edited at 10:56pm
  •  sloof
  • | Joined Apr 2009 | Status: Birejji (ビレッジ) ►FOREX◄ | 141 Posts
I'm testing it out now and it looks awesome! I could definitely find some awesome patterns. I just have a few Qs

1) Error value - I'm assuming the higher the value the greater the error?

2) How can we generate and save a different pattern that is already set(that zig zag type pattern)?
{
EDIT - after playing around some more it looks like we just have to edit the patternmatcher.definitions file and recompile the EAs?

Is there documentation for this function (the one used to define patterns)? ab(0,0,0,0,x)
}

3) This is probably asking for too much, but would it be possible to make a tutorial video?
 
 
  • Post #15
  • Quote
  • Edited Jan 27, 2010 12:09am Jan 26, 2010 11:45pm | Edited Jan 27, 2010 12:09am
  •  7bit
  • Joined Mar 2009 | Status: Member | 1,231 Posts
Quoting sloof
Disliked
I'm testing it out now and it looks awesome! I could definitely find some awesome patterns. I just have a few Qs

1) Error value - I'm assuming the higher the value the greater the error?

2) How can we generate and save a different pattern that is already set(that zig zag type pattern)?
{
EDIT - after playing around some more it looks like we just have to edit the patternmatcher.definitions file and recompile the EAs?

Is there documentation for this function (the one used to define patterns)? ab(0,0,0,0,x)
}

3) This is probably asking for too...
Ignored
1: the smaller the better the match. There was a small bug in patternmatcher.common.mqh, i just uploaded the fixed version, the old one showed wrong error values in the patterndesigner (didn't affect trading).

2: the definitions file is the correct place for the patterns. just make a new function for every pattern and in the pattern() function just call the one you want to use.

the ab() function is just a line equation:

ab(0,0,50,50,x) would define a line between the points (0,0) and (50,50) return the y values that lie on this line for every x. x=0 would return 0, x=50 would return 50, x=25 would return 25. I use it for the line segments of the zigzags. the if construct calls another line function for different ranges of x. The second point (bx,by) is always the first point (ax,ay) in the next line segment, in the next if branch.

here is another interesting and very simple pattern I'm just experimenting with on the eurusd M1 chart. Use it with stop and target around 0.5 and a relatively small max_error around 0.07:
Inserted Code
double patternCatchTopsAndBottoms(double x){
   // another experimantal pattern, 
   // could need some improvement.
   if (x < 40) return(ab(0,0, 40,35,x));
   if (x < 65) return(ab(40,35, 65,15,x));
               return(ab(65,15, 100,90,x));   
}
It defines a zigzag line (right to left)
from (0,0) to (40,35)
then from (40,35) to (65,15)
then from (65,15) to (100,90)

(always backwards on the chart,
current bar (right) is 0 left side of pattern is 100)

Attached Image (click to enlarge)
Click to Enlarge

Name: topsbottoms.png
Size: 64 KB


3: Video tutorial: Maybe sometime, but since i know myself and my lazyness this could very well mean "not within the next 10 years".
 
 
  • Post #16
  • Quote
  • Jan 27, 2010 12:35am Jan 27, 2010 12:35am
  •  sloof
  • | Joined Apr 2009 | Status: Birejji (ビレッジ) ►FOREX◄ | 141 Posts
Thanks a bunch! Makes sense now =) now I need to figure out how to work these trade settings lol

I've got this pattern and from the live test your EA finds the patterns, but i don't think I've got the settings right because trades aren't placed at the best spots... i'll play around some more, but here's the pattern.

Inserted Code
   if (x < 55) return(ab(0,0, 55,-100,x));
               return(ab(55,-100, 100,-60,x));

it's very simple and it's from GBP/USD 5MIN

I must say... watching this work in visual mode is beautiful! Nicely done!
 
 
  • Post #17
  • Quote
  • Jan 27, 2010 2:05am Jan 27, 2010 2:05am
  •  sloof
  • | Joined Apr 2009 | Status: Birejji (ビレッジ) ►FOREX◄ | 141 Posts
ok so I think i've figured everything out so I should be able to find some decent patterns now

Inserted Code
double hs(double x){   
   // this was one of the first pattern i tried,
   // it is the one visible in the first screenshot in the FF thread
   if (x < 30) return(ab(0,0, 30,-20,x));
   if (x < 60) return(ab(30,-20, 60,0,x));
   if (x < 80) return(ab(60,0, 80,75,x));
   if (x <= 100) return(ab(80,75, 100,45,x));
}

I was aiming for a h&s type configuration
 
 
  • Post #18
  • Quote
  • Jan 27, 2010 8:07am Jan 27, 2010 8:07am
  •  7bit
  • Joined Mar 2009 | Status: Member | 1,231 Posts
I'm currently thinking about renaming the EA to patterntrader and writing a third EA (patternscanner) that would permanently scan a list of symbols with a wide scan range that covers all timeframes from M1 up to D1 and alert on each pattern that it finds.
 
 
  • Post #19
  • Quote
  • Jan 27, 2010 9:01am Jan 27, 2010 9:01am
  •  forexsaint
  • Joined Jun 2009 | Status: <-That's how u gonna b, in the END! | 1,508 Posts
Quoting 7bit
Disliked
I'm currently thinking about renaming the EA to patterntrader and writing a third EA (patternscanner) that would permanently scan a list of symbols with a wide scan range that covers all timeframes from M1 up to D1 and alert on each pattern that it finds.
Ignored
I just cant wait to test this...having some problem at my home..darn!
That will be uber-cool 7bit..."pattern scanner" and than a "pop up"
A software called amibroker has similar pattern scanner ability to churn out stocks with fed entries.

@sloof..nice..good going.
100 Fold Challenge->Interested? ->https://www.forexfactory.com/thread/32152
 
 
  • Post #20
  • Quote
  • Jan 27, 2010 2:00pm Jan 27, 2010 2:00pm
  •  7bit
  • Joined Mar 2009 | Status: Member | 1,231 Posts
I just uploaded new versions of

patternmatcher.mq4
patterndesigner.mq4
patternmatcher.common.mqh

to the first posting in this thread.

I changed the algorithm how the pattern is scaled into the rectangle of prices and at the same time removed all restrictions to the co-domain of the function or the value of f(0). Your function values may now reach from ±<whatever> to ±<whatever_else>, it will always be correctly scaled to the prices. This should make pattern design a lot easier. The only convention that is still in place (and won't ever change) is that x values need to range from 0 to 100.
 
 
  • Platform Tech
  • /
  • pattern matching
  • Reply to Thread
    • Page 1 234 5
    • Page 1 234 5
0 traders viewing now
  • More
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2022