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)));
}
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));
}
Code: Select all
double pattern(double x){
if (x < 50) return(ab(0,0,50,-100,x));
return(ab(50,-100,100,0,x));
}