To create a working trade algorithms, it is essential to perform the evaluation of gathered forex tick data. One of the way to do that is to transform the data into range bar format
The simplest way to do that would be to check the bar patterns in definite range and look for statistical discrepancies in them which we will be able to abuse: for example, if we have 3 range bars that go up consecutively, the fourth bar also goes up in 75% of the cases.
The minimal range bar representation in C++ would be the following: store only open and close prices.
struct RangeBar
{
double open;
double close;
};
Then, for the simplicity of it, you can also define the structure to store the pattern results (we will get to it a bit later):
struct PatternStats
{
int ups;
int downs;
};
In my project, for the sake of simplicity, I have made a class (which inherits some stuff from the other parent class) that gathers pattern statistics, but I will only describe the algorithm here:
First step, initialization:
/*
* First, we decide how many bars will be analyzed for the patterns (bar amount).
* My standard options are anywhere from 3 to 7 bars.
* Then, we initialize the stats array for each possible
* pattern and set the current statistics to zero
* 1 << bar_amount is effectively the same as 2^bar_amount,
* so for 2 bars it will be 4 (up-up, up-down, down-up, down-down),
* 3 bars it will be 8, for 4 it will be 16, etc.
* Each pattern will be a set of zeroes and ones,
* 1 stands for ascending, and 0 - for descending bar
* So, for example, to change the pattern up-up stats,
* we will be accessing the stats[3] (00000011)
*/
stats = new PatternStats[1 << bar_amount];
for (int i = 0; i < pattern_amount; i++)
{
stats[i].downs = 0;
stats[i].ups = 0;
}
After that, you just go through all range bars you have and process every bar_amount of them:
/* go through all bars, but no further than
/* total_bar_amount - bar_amount (this amount you will go forward) - 1 (result)*/
for (int bar = 0; bar < total_bar_amount - bar_amount - 1; bar++)
{
/* go through bar_amount bars to distinguish the current pattern */
int pattern = 0;
for (int i = 0; i < bar_param_amount; i++)
{
//1 - raising bar, 0 - dropping bar
/* (if open is lower than close, we have an ascending bar) - true
* which is evaluated as 1, or false, which is evaluated as zero
* then we move it by i bits and get the pattern bit
* so if the first bar was zero, and the second bar was 1,
* we get 00000010
*/
pattern += (bars[bar + i].open < bars[bar + i].close) << (i);
}
/*
* after the pattern is formed, we check if the next bar is ascending
* or descending and increase the statistics accordingly
*/
if (bars[bar + bar_param_amount].close > bars[bar + bar_param_amount].open)
{
stats[pattern].ups++;
}
else
{
stats[pattern].downs++;
}
}
That is all. After you get the statistics, just see if there are any patterns that go up significantly more than down (or vice versa). Of course, it probably won’t be enough to make a successful EA, but it’s a start that can give some clarity.