Download MetaTrader 5

Three-Dimensional Graphs - a Professional Tool of Market Analyzing

13 September 2007, 12:27
Antoniuk Oleg
3
1 468

Introduction

All traders operate with constantly changing prices and quotes. Everyone searches correspondences and regularities. We analyze volumes, values of different indicators and many other parameters. I don't think it necessary to persuade you of MQL4 benefits in terms of market analysis. You simply write a script and make conclusions based on results. Usually analysis is conducted upon charts or graphs that reflect a certain correspondences of two variables. All this is fine, of course, but sometimes we need to find the correspondences of three variables and visualize them. For this purpose 3D graphs and their analysis are used all over the world. Such approach saves your time and money.

In this article we will write a simple library for the construction of 3D graphs and their further viewing in Microsoft Excel. We will use standard MQL4 options to prepare and export data into *.csv file


What Is the File Format *.csv?

It is a text file, which stores the succession of values, separated by a preset separator, in our case it is a semicolon - ";". For drawing a table we simply need to write several code strings. The advantage of this format is that it is easily recognized and interpreted in the form of a table by Microsoft Excel. And in this program we can easily create a 3D graph based on the acquired data and analyze it.


Library generateCsv

For quick and easy data entering we will create a library. So, create a new library and insert the code below, save it under the name generateCsv.mq4 in the folder "...MetaTrader\experts\libraries". After that compile the library.

//+------------------------------------------------------------------+
//|                                                  generateCsv.mq4 |
//|         Copyright © 2006, Antonio Banderass. All rights reserved |
//|                                               banderassa@ukr.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Antonio Banderass. All rights reserved"
#property link      "banderassa@ukr.net"
#property library
 
//+------------------------------------------------------------------+
//| PrepareString                                                    |
//+------------------------------------------------------------------+
 
string PrepareString(string s)
  {
   bool exit = false;
   int index = 0;
   string str = s;
   
   while(!exit)
     {
       index = StringFind(str, ".", index);
      
       if(index > -1)
           str = StringSetChar(str, index, ',');
       else
           exit = true;
     }
   return(str);
  }
 
 
//+------------------------------------------------------------------+
//| GenerateCsv                                                      |
//+------------------------------------------------------------------+
int GenerateCsv(string fileName, int arraySizeX, int arraySizeY,
                double arrayIndexX[], double arrayIndexY[], double arrayZ[][])
  {
   int handle = FileOpen(fileName, FILE_CSV|FILE_WRITE, ' '), x, y;
   string str;
   
   if(handle1)
     {
       Print("Error:", GetLastError());
       return(handle);
     }
   else
     {
       str = ";";
       for(x = 0; x < arraySizeX; x++)
         {
           str = str + arrayIndexX[x];
           str = str + ";";         
         }
       FileWrite(handle,PrepareString(str));
      
       for(y = 0; y < arraySizeY; y++)
         {
           str = "";   
           str = str + arrayIndexY[y] + ";";
         
           for(x = 0; x < arraySizeX; x++)
             {
               str = str + arrayZ[x,y];
               str = str + ";";         
             }
           FileWrite(handle, PrepareString(str));
         }
     }
      
   FileClose(handle);   
   return(handle);
  }

Now create a new header file for the access to the library functions, named generateCsv. mqh, insert the below code and save in the folder "...MetaTrader\experts\include".

//+------------------------------------------------------------------+
//|                                                  generateCsv.mqh |
//|         Copyright © 2006, Antonio Banderass. All rights reserved |
//|                                               banderassa@ukr.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Antonio Banderass. All rights reserved"
#property link "banderassa@ukr.net"
 
#import "generateCsv.ex4"
 
string PrepareString(string s);
int GenerateCsv(string fileName, int arraySizeX, int arraySizeY,
double arrayIndexX[], double arrayIndexY[],double arrayZ[][]);

This simple library consists of two functions. Let us view the meaning of each function. The first function PrepareString is intended for substituting points in a string by commas. The fact is, when writing data into a file using the standard function FileWrite the integer and the fractional parts are separated by a point. And this is unacceptable for us, because Microsoft Excel recognizes decimals only if the parts are separated by a comma. That is why when placing data into the file *.csv we call this function in the body of the function GenerateCsv. The only one parameter of the function is a string, where we need to replace points by commas. Return the value - a new string without points. Please note, that you do not need to call this function, it is done by the second function automatically, when needed.

The function GenerateCsv is used for writing data in *.csv format into a file. Here are its parameters:

int GenerateCsv(string fileName, int arraySizeX, int arraySizeY,
                double arrayIndexX[], double arrayIndexY[], double arrayZ[][]);
  • string fileName - name of a file for writing, do not forget to indicate the format *.csv;
  • int arraySizeX - width of the array with data;
  • int arraySizeY - height of the array with data;
  • double arrayIndexX[] - array of the first variable values;
  • double arrayIndexY[] - array of the second variable values;
  • double arrayZ[][] - two-dimensional array of the third variable values;

In order to understand the purpose of each parameter, look at the picture:


So all you need for writing this data is three value arrays for each variable. Now let us see how it works.


Using the Library generateCsv

Create a new script named 3dGraphicOut and insert the below code in it.

//+------------------------------------------------------------------+
//|                                                 3dGraphicOut.mq4 |
//|         Copyright © 2006, Antonio Banderass. All rights reserved |
//|                                               banderassa@ukr.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Antonio Banderass. All rights reserved"
#property link      "banderassa@ukr.net"
 
#include <generateCsv.mqh>
 
#define ARRAY_SIZE_X 16
#define ARRAY_SIZE_Y 16
 
//+------------------------------------------------------------------+
//| start                                                            |
//+------------------------------------------------------------------+
int start()
  {
  
   int x,y;
   
   double arrayIndexX[ARRAY_SIZE_X];
   double arrayIndexY[ARRAY_SIZE_Y];
   double arrayZ[ARRAY_SIZE_X,ARRAY_SIZE_Y];
   
   
   for(x=0;x<ARRAY_SIZE_X;x++)
      arrayIndexX[x]=x/10.0; 
      
   for(y=0;y<ARRAY_SIZE_Y;y++)
      arrayIndexY[y]=y/10.0;
   
   for(x=0;x<ARRAY_SIZE_X;x++)
      for(y=0;y<ARRAY_SIZE_Y;y++)
         arrayZ[x,y]=MathSin(arrayIndexX[x]+arrayIndexY[y]);
 
   GenerateCsv("test.csv",ARRAY_SIZE_X,ARRAY_SIZE_Y,arrayIndexX,arrayIndexY,arrayZ); 
   return(0);
  }

This is a standard template, from which every 3D graph should start. Let us view each line separately:

#include <generateCsv.mqh>

Start the library generateCsv.

#define ARRAY_SIZE_X 16
#define ARRAY_SIZE_Y 16

Here we define the dimensions of the table: ARRAY_SIZE_X - width of the table, ARRAY_SIZE_Y - height.

int x,y;

These are two array indexes. As they are used many times, I placed them outside the cycle body.

double arrayIndexX[ARRAY_SIZE_X];

This is an array of values of the first variable. Suppose you analyze the influence of two indicators upon profit. In this case the values of the first indicator will be located in this array. Please note that you should start with some start value, for example from zero, and then increase it with a certain step, for example 0. 5.

double arrayIndexY[ARRAY_SIZE_Y];

The values of the second indicator are placed in this array. Also choose the appropriate start value and step.

double arrayZ[ARRAY_SIZE_X,ARRAY_SIZE_Y];

And this one should contain the profit value, depending on the indicator values. Once again look at the above picture to understand the values.

Pay attention, that the array type is double. This makes them universal. If you use integers (int) as the elements of the arrays, it is also ok, because MQL4 has an implicit type transformation.

for(x = 0; xARRAY_SIZE_X; x++)
  {
    // arrayIndexX[x]= ... ;
  }

In this block we fill in the first variable array with values. Do not forget, they should be increased with a certain step.

for(y = 0; y < ARRAY_SIZE_Y; y++)
   {
      // arrayIndexY[y]= ... ;
   }

The same is for the second variable.

for(x = 0; x < ARRAY_SIZE_X; x++)
   {
     for(y = 0; y < ARRAY_SIZE_Y; y++)
       {
         // arrayZ[x,y]= ... ;
       }
   }

And here is the most interesting part of the template: we fill in the two-dimensional array with values depending on two other variables.

GenerateCsv("test.csv",ARRAY_SIZE_X,ARRAY_SIZE_Y,arrayIndexX,arrayIndexY,arrayZ);

And finally we write the data into the file for further analysis in Microsoft Excel. Now save this template for further usage.


A Simple Example

Let us view a very simple example of using the library. I intentionally did not write the example, connected with trading, to make the code maximally simple and available.

//+------------------------------------------------------------------+
//|                                                 3dGraphicOut.mq4 |
//|         Copyright © 2006, Antonio Banderass. All rights reserved |
//|                                               banderassa@ukr.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Antonio Banderass. All rights reserved"
#property link      "banderassa@ukr.net"
 
#include <generateCsv.mqh>
 
#define ARRAY_SIZE_X 16
#define ARRAY_SIZE_Y 16
 
//+------------------------------------------------------------------+
//| start                                                            |
//+------------------------------------------------------------------+
int start()
  {
   int x,y;
 
   double arrayIndexX[ARRAY_SIZE_X];
   double arrayIndexY[ARRAY_SIZE_Y];
   double arrayZ[ARRAY_SIZE_X,ARRAY_SIZE_Y];
 
 
   for(x=0;x<ARRAY_SIZE_X;x++)
      arrayIndexX[x]=x/10.0;
 
   for(y=0;y<ARRAY_SIZE_Y;y++)
      arrayIndexY[y]=y/10.0;
 
   for(x=0;x<ARRAY_SIZE_X;x++)
      for(y=0;y<ARRAY_SIZE_Y;y++)
         arrayZ[x,y]=MathSin(arrayIndexX[x]+arrayIndexY[y]);
 
   GenerateCsv("test.csv",ARRAY_SIZE_X,ARRAY_SIZE_Y,arrayIndexX,arrayIndexY,arrayZ);
 
   return(0);
  }

Save the text script, compile it and start in the terminal. Now open the folder "...MetaTraider\experts\files\", a new file "test.csv" appeared there. Open it using Microsoft Excel. The file will be transformed into a two-dimensional table, as shown below:


Now select all cells (CTRL+A) and enter Insert -> Chart (ALT+I,H):


Then choose Chart Type -> Surface, Chart sub-type -> 3D-Surface and click Finish:


After that a 3D graph will appear:


Conclusion

Now available for you is a convenient tool for a quick data export from the trading terminal MetaTrader into the program Microsoft Excel for a quick construction of 3D graphs and their further analysis. Now you can analyze Forex correspondences and regularities between three variables simultaneously. Try to analyze 3D histograms of profit dependence on such parameters as TakeProfit and StopLoss or see, how a chart of volumes' dependence on the current weekday and hour looks like. I think you will find a lot of interesting and useful correspondences.

Attached files |
3dGraphicOut.mq4 (1.43 KB)
generateCsv.mq4 (2.24 KB)
generateCsv.mqh (0.77 KB)
Last comments | Go to discussion (3)
MQL4 Comments
MQL4 Comments | 3 Jan 2008 at 02:59
I am trying to make this 3D chart for x=time, y=price, z=volume.
Sorry for simple questions I'm very new to coding.
//+------------------------------------------------------------------+
//|                                                  generateCsv.mq4 |
//|         Copyright © 2006, Antonio Banderass. All rights reserved |
//|                                               banderassa@ukr.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Antonio Banderass. All rights reserved"
#property link      "banderassa@ukr.net"
#property library
//+------------------------------------------------------------------+
//| PrepareString                                                    |
//+------------------------------------------------------------------+
string PrepareString(string s)
  {
   bool exit = false;
   int index = 0;
   string str = s;
//----
   while(!exit)
     {
       index = StringFind(str, ".", index);
       if(index > -1)
           str = StringSetChar(str, index, ',');
       else
           exit = true;
     }
   return(str);
  }
//+------------------------------------------------------------------+
//| GenerateCsv                                                      |
//+------------------------------------------------------------------+
int GenerateCsv(string fileName, int arraySizeX, int arraySizeY, int arraySizeZ,
                double arrayIndexX[], double arrayIndexY[], double arrayIndexZ[])
  {
   int handle = FileOpen(fileName, FILE_CSV|FILE_WRITE, ' '), x, y, z;
   string str;
   if(handle < 1)
     {
       Print("Error:", GetLastError());
       return(handle);
     }
   else
     {
       str = ";";
       for(x = 0; x < arraySizeX; x++)
         {
           str = str + arrayIndexX[x];
           str = str + ";";         
         }
         }
       FileWrite(handle, PrepareString(str));
       
          for(y = 0; y < arraySizeY; y++)
           { 
           str = str + arrayIndexY[y];
           str = str + ";";
             }
           FileWrite(handle, PrepareString(str));
           
                for(z = 0; z < arraySizeZ; z++)
                 {
                str = str + arrayIndexZ[z];
                str = str + ";";         
               }
             FileWrite(handle, PrepareString(str));
       {
     }
   FileClose(handle);   
   return(handle);
  }
//+------------------------------------------------------------------+
JUAN MANUEL ALMODOVAR
JUAN MANUEL ALMODOVAR | 25 Sep 2008 at 17:29

Just a little bug.

At the line


arrayZ[x,y]=MathSin(arrayIndexX[x]+arrayIndexY[y]);


the code must be:

arrayZ[x,y]=MathSin(arrayIndexX[x]) + MathSin(arrayIndexY[y]);


if you see the results of the Z array there are greater than 1 and it's not possible in a Sinus, but in fact they approach to 2, the sum of the x and y sinus...

MQL4 Comments
MQL4 Comments | 30 Jan 2012 at 11:48

Hello,

I would like to ask for support.

When I drag&drop the script into a price chart no new file "test.csv" appeared there.

I've also corrected the line as specified in the comment by "moneymakers", had no compiling errors and seemingly everything is placed where it should be.

Kind regards,

serendippo

Step on New Rails: Custom Indicators in MQL5 Step on New Rails: Custom Indicators in MQL5

I will not list all of the new possibilities and features of the new terminal and language. They are numerous, and some novelties are worth the discussion in a separate article. Also there is no code here, written with object-oriented programming, it is a too serous topic to be simply mentioned in a context as additional advantages for developers. In this article we will consider the indicators, their structure, drawing, types and their programming details, as compared to MQL4. I hope that this article will be useful both for beginners and experienced developers, maybe some of them will find something new.

Here Comes the New MetaTrader 5 and MQL5 Here Comes the New MetaTrader 5 and MQL5

This is just a brief review of MetaTrader 5. I can't describe all the system's new features for such a short time period - the testing started on 2009.09.09. This is a symbolical date, and I am sure it will be a lucky number. A few days have passed since I got the beta version of the MetaTrader 5 terminal and MQL5. I haven't managed to try all its features, but I am already impressed.

False trigger protection for Trading Robot False trigger protection for Trading Robot

Profitability of trading systems is defined not only by logic and precision of analyzing the financial instrument dynamics, but also by the quality of the performance algorithm of this logic. False trigger is typical for low quality performance of the main logic of a trading robot. Ways of solving the specified problem are considered in this article.

Using text files for storing input parameters of Expert Advisors, indicators and scripts Using text files for storing input parameters of Expert Advisors, indicators and scripts

The article describes the application of text files for storing dynamic objects, arrays and other variables used as properties of Expert Advisors, indicators and scripts. The files serve as a convenient addition to the functionality of standard tools offered by MQL languages.