Creating fractals in MQ L5 using the Iterated Function Systems (IF S)
This is a powerful addition to your MetaTrader 5 toolkit designed to optimize market analysis and performance. This Expert Advisor serves as automated trading software. It is utilized to monitor financial markets and execute trades based on predefined algorithmic rules, enabling precise position management without the need for constant manual oversight.
How to Setup and Use Creating fractals in MQ L5 using the Iterated Function Systems (IF S)
1. Installation: Open the "File" menu, select "Open Data Folder," navigate to MQL/Experts, paste your file, and restart the terminal.
2. Activation: Drag the EA from the Navigator onto a chart, ensure "Allow live trading" is checked in the Common tab, and verify the AutoTrading button is green.
3. Optimization: Right-click your chart, choose "Expert List," click "Properties" to adjust inputs, and save your preferred setup as a set file for future use.
4. Maintenance: Regularly check the "Experts" tab in the terminal window to monitor trade logs and potential execution errors.
Frequently Asked Questions
Q: Why is my EA not opening trades? A: Check the "AutoTrading" button, ensure "Allow live trading" is enabled, and verify your broker allows automated trading on your account type.
Q: Can I run multiple EAs on one chart? A: No, each chart can only host one active EA; however, you can open multiple charts for different currency pairs to run several EAs.
Q: What does the "smiley face" icon mean? A: A smiley face in the top-right corner of the chart indicates the EA is successfully running; a frowny face means it is disabled.
Description & Settings
Introduction
There are many programs, allowing the creation of self-similar sets, defined by (IFS). See, for example, , or . Thanks to the speed of language and possibility of working with graphic objects, these beautiful sets can be studied in client terminal.
The library, developed by (Integer) provides new graphic opportunities and greatly simplifies the creation of graphic images. This library was with special prize by .
In this publication we will consider the examples of working with library. In addition, we will cover the algorithms of creation of fractal sets using the Iterated Function Systems.
1. Affine Transformation of Plane
The affine transformation of plane is a mapping . Generally, the affine 2-D transform can be defined with some matrix and , vector. The point with coordinates (x,y) tranforms to some other point using the linear transformation:
The transformation must be non-singular, the . The affine transform changes the size times.
The affine transforms does not change the structure of geometric objects (the lines transformed to lines), the AT allows to describe simple "deformation" of the objects, such as rotation, scaling and translation.
Example of affine plane transforms:
1)
Rotation of plane
on angle:
2) "
Scaling" of a plane
with and coefficients (X and Y axes):
3)
Translation
of plane by vector:
The is the key (see results).
If and have coordinates and and is a metric (for example, Euclid metric: ). The affine transformation called if , where .
Here is an example of affine transformation:
The result is:
2. Similarity Transforms
The fractals constructed the following way: some (simple) geometric object (section, triangle, square) divided into N pieces and M of them used for further "construction" of the the set (if N=M, we will get the integer dimension of the resulting set). Further this process repeated again and again for each of the pieces.
Classic fractals:Sections:
Triadic Koch Curve, N=3, M=4;
Cantor Dust, N=3, M=2;
Triangle:
Sierpinski Gasket, N=4, M=3;
Squares:
Sierpinski Carpet, N=9, M=8;
Vichek fractal, N=9, M=5.
and so on.
The fractals have self-similar structure, some of them can defined by several similarity transformations. The structure of affine transform depends on the way of the fractal construction.
As you will see further, it's very simple, and the only problem we have to solve is to decribe only the first iteration of fractal construction and find the corresponding set of affine tranforms.
Suppose we have some set. According to the fractal creation algorithm we need to reduce it, rotate and "put on a certain place". The problem is to describe this process using affine transformations, i.e. we need to find the matrix and vector.
It's easy to prove that it's enough to take 3 points of the initial set (nontrivial) and tranform them into 3 corresponding points of the "reduced" set. This transformation will lead to 6 linear equations, allowing us to find the a,b,c,d,e,f as solution.
Let's show it. Suppose triangle transformed to triangle.
By solving the system of linear equations we will able to get the a,b,c,d,e and f coefficients:
Example:Sierpinski Gasket:
The coordinates of the points are:
A (0,0)
B (0,1)
C (1,1)
D(0,1/2)
E (1/2,1)
F(1/2,1/2)
We have 3 tranformations:
ABC -> ADF
ABC -> DBE
ABC -> FEC
The system of linear equations looks as follows:
The solutions are: , ,
We have found the coefficients of three affine transforms. Furher we will use them for creation of self-similar sets.
3. Creating Fractals Using the Iterated Function Systems
The is a set of affine contractions where - is the "weights". Each of IFS functions is defined by 7 numbers: , whereweights is used when iteration process as a probability of n-th tranformation. It's better to define their values, proportional to contraction: .
Let's consider the algorithm of fractal construction using the Iterated Function System (see also ).
The first we need is to take some initial point with coordinates . The next, we choose randomly some of the contractions and plot the point . And again, let's choose randomly one of the contractions and plot . Finally we will have the as a set of points.
The choice of contraction depend on its "probability".
If we repeat the process (for example, to ~30000 points) and plot the resulting set, we will see its structure despite the random process.
Here is an example of Sierpinski Gasket:
Figure 1. The Sierpinski Gasket, generated with IFS coefficients calculated in chapter 2
The code:
If we set scale to 1350, increase the number of iterations to 15000000, and change the shift the initial point:
we will able to see the zoomed region of the set. One can see (Fig. 2), that it has a self-similar structure:
Figure 2. Zoomed region of Sierpinski Gasket
Let's consider the famous , proposed by . It's more complex.
Figure 3. Barnsley's Fern
The code is similar, but in this case we have 4 IFS contractions with different weights.
It's remarkable that such complex structure can be defined by only 28 numbers.
If we increase the scale to 150, and set iterations to 1250000 we will see the zoomed fragment:
Figure 4. A fragment of Barnsley's Fern
As you see the algorithm is universal, it allows you to generate different fractal sets.
The next example is Sierpinski Carpet, defined by following IFS coefficients:
Figure 5. Sierpinski Carpet
In chapter 2 we have considered the algorithm of calculation of coefficients of IFS contractions.
Let's consider how to create the "fractal words". In Russian, the "Fractals" word looks like:
Figure 6. "Fractals" word in Russian
To find the IFS coefficients, we need to solve the corresponding linear systems. The solutions are:
As a result, we will get the following image:
Figure 7. Self-similar word
The full source code can be found in ifs_fractals.mq5.
If we zoom the set, we see the self-similar structure:
Figure 8. Zoomed region of the set
The self-similar sets, based on IFS, can be constructed using the .
We have covered the topic of creating fractal sets using the Iterated Function Systems. Thanks to the library, the process is very simple. Now it's time to create a class and add some features to make the images better.
You may notice that correct construction of sets driven by probabilites. The difference in probabilities means that set has irregular structure (see weights of Barnsley Fern IFS). This fact can be used for creation of beautiful images. We need to set the color, proportional to the frequency of the point in some neighbourhood.
It can be done using the virtual screen (just an array), if the pixel color will depend on the previous values. Finally, the virtual screen will be rendered into the bmp using the Palette. The bmp image itself can be drawn as a background picture of the chart.
Here is the code of Expert Advisor, based on CIFS class:
The result is:
Figure 9. Barnsley's fern image, created with CIFS class
Figure 10. The zoomed region of Barnsley's Fern
Figure 11. The zoomed region of Barnsley's Fern
Figure 12. The zoomed region of Barnsley's Fern
Do It Yourself
1. There is a lot of IFS fractals in , for example:
Plot these sets. How to find the initial similarity transforms using the IFS coefficients?
2. Create your own fractals sets and calculate their coefficients (chapter 2).
3. Try to play with palette colors (uchar Palette array), extend the palette and add gradient colors.
4. What about the fractal (Hausdorf-Bezikovitch) dimension of Barnsley's Fern? Is there a formula for calculation of fractal dimension using the IFS coefficients*.
5. Add zoom of a certain region, using the information about the mouse click coordinates in :
Conclusion
We have considered the algorithm of creation of self-similar sets using the Iterated Function System.
The use of library greatly simplifies the work with graphic images. Besides the DrawDot(x,y,color) method we have used, the cIntBMP class contains many other useful methods. But it's another story.