//+------------------------------------------------------------------+
//| GraphicDemo.mq4 |
//| Copyright 2021, MetaQuotes Software Corp. |
//| R. Poster |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
//------------------------------------------------------------------
// ResourceSave function does NOT work in Strategy Tester!
// Workaround is to save x,y hist data in Terminal/Common/Files folder
// Data can be saved in Tester mode and then read in Terminal mode
// First, Run in Tester to create x,y chart data
// Then remove EA from chart to force Deint to run in Termnal mode
// Chart file is saved in Terminal mode using ResourceSave and is
// saved as .BMP file in MQL5/Files folder
//------------------------------------------------------------------
#include <Graphics\Graphic.mqh>
CGraphic ggraphic;
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnTick()
{
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
int jj;
double x[],y1[],y2[],y3[],y4[];
//
string Leg1,Leg2,Leg3,Leg4,Title,ChartName,Xaxis,Yaxis;
int NumPlots,NumPts;
uint colr =::ColorToARGB(clrBlack);
uint align =TA_LEFT;
//
NumPlots = 2;
Leg1 = " Curve 1";
Leg2 = " Curve 2";
Leg3 = " Curve 3";
Leg4 = " Curve 4";
Xaxis = "Month";
Yaxis = "Average Profit";
ChartName = "GraphTest";
Title =" Average Profit Vs Month ";
//
// ---- setup x,y hist test data in Tester Mode ----
if(MQLInfoInteger(MQL_TESTER))
{
NumPts = 10;
ArrayResize(x,NumPts);
ArrayResize(y1,NumPts);
ArrayResize(y2,NumPts);
ArrayResize(y3,NumPts);
ArrayResize(y4,NumPts);
for(jj=0;jj<NumPts;jj++)
{
x[jj] = 1.*jj +0.1;
y1[jj] = 1.*(jj+1)+0.1;
y2[jj] = 1.*(jj+3)+0.1;
y3[jj] = 1.*(jj+4);
y4[jj] = 1.*(jj+5);
}
// Save data file in Tester to Common/Files folder
StoreData(ChartName,NumPts,x,y1,y2,y3,y4);
}//--------------------------------------------------------------
//----------- Terminal --------------------------
// plot and load file in Terminal Mode (Expert is removed))
if(!MQLInfoInteger(MQL_TESTER)) // plot from end of tester run
{
// Load NumPts, x, y data
LoadData(ChartName,NumPts,x,y1,y2,y3,y4); // return NumPts,x,y1,..
//
PlotInit(ChartName);
SetLabels(Title,Xaxis,Yaxis);
Print(" ** Width, Ht ",ggraphic.Width()," ",ggraphic.Height());//***
// 0 = points only, 1= lines only, 2=points and lines
MakePlot(NumPlots,x,y1,y2,y3,y4,Leg1,Leg2,Leg3,Leg4,0,2,1,1);
// print rext in left upper corner
ggraphic.FontSet("Arial",14) ;
ggraphic.TextAdd(55,40, " ***** Description of This Plot *****",colr,align );
// print line under text
ggraphic.LineAdd(55,55,220,55,colr,0);
//
StorePlot(ChartName); // create bmp file in Files folder
}
}//------------------------------------------------------------------
//
//+----------------------------------------------------------------+
//| Plotting functions using graphic.mqh and canvas.mqh |
//| Plot up to 4 curves |
//| Limitation: one line title |
//+----------------------------------------------------------------+
void PlotInit(string ChName)
{
ggraphic.BackgroundColor(clrWhite);
ggraphic.Create(0,ChName,0,30,30,780,380);
return;
}
//-----------------------------------------------------------
void SetLabels(string Title,string XAxis,string YAxis)
{
ggraphic.BackgroundMainSize(18); // Font size for the chart title
ggraphic.BackgroundMain(Title); // Chart Title
ggraphic.XAxis().Name(XAxis);
ggraphic.XAxis().NameSize(14);
ggraphic.YAxis().Name(YAxis);
ggraphic.YAxis().NameSize(14);
ggraphic.YAxis().ValuesWidth(15);
return;
}
//------------------------------------------------------------
void MakePlot(int NCv, double &x[],
double &y1[],double &y2[],double &y3[],double &y4[],
string Leg1,string Leg2,string Leg3,string Leg4,
ENUM_CURVE_TYPE ct1,ENUM_CURVE_TYPE ct2,
ENUM_CURVE_TYPE ct3,ENUM_CURVE_TYPE ct4)
//+---------------------------------------------------------------------+
//enum ENUM_CURVE_TYPE
// {
// CURVE_POINTS,
// CURVE_LINES,
// CURVE_POINTS_AND_LINES,
// CURVE_STEPS,
// CURVE_HISTOGRAM,
// CURVE_CUSTOM,
// CURVE_NONE
// };
//--- type for the various point shapes that are available
//enum ENUM_POINT_TYPE
// {
// POINT_CIRCLE,
// POINT_SQUARE,
// POINT_DIAMOND,
// POINT_TRIANGLE,
// POINT_TRIANGLE_DOWN,
// POINT_X_CROSS,
// POINT_PLUS,
// POINT_STAR,
// POINT_HORIZONTAL_DASH,
// POINT_VERTICAL_DASH
// };
// enum ENUM_LINE_STYLE
// STYLE_SOLID
// STYLE_DASH
// STYLE_DOT
// STYLE_DASHDOT
// STYLE_DASHDOTDOT
//+--------------------------------------------------------------------+
{
CCurve *curve1=ggraphic.CurveAdd(x,y1,ct1);
curve1.Name(Leg1);
// fixed curve attributes
curve1.LinesSmooth(true);
curve1.PointsType(POINT_CIRCLE);
curve1.PointsFill(true);
curve1.LinesStyle(STYLE_SOLID);
// curve1.PointsSize(20);
// curve1.PointsColor(ColorToARGB(clrBlue,255));
//
if(NCv>1)
{
CCurve *curve2=ggraphic.CurveAdd(x,y2,ct2);
curve2.Name(Leg2);
// fixed curve attributes
curve2.LinesSmooth(true);
curve2.LinesStyle(STYLE_DASH);
curve2.PointsType(POINT_SQUARE);
curve2.PointsFill(true);
// curve2.PointsSize(20);
// curve2.PointsColor(ColorToARGB(clrRed,255));
}
if(NCv>2)
{
CCurve *curve3=ggraphic.CurveAdd(x,y3,ct3);
curve3.Name(Leg3);
curve3.LinesSmooth(true);
curve3.PointsType(POINT_SQUARE);
curve3.PointsFill(false);
}
if(NCv>3)
{
CCurve *curve4=ggraphic.CurveAdd(x,y4,ct4);
curve4.Name(Leg4);
curve4.LinesSmooth(false);
curve4.PointsType(POINT_DIAMOND);
curve4.PointsFill(true);
}
ggraphic.CurvePlotAll();
// graphic.CurvePlot(0); // plot curve 0
return;
}
//-------------------------------------------------------------
void StorePlot(string ChName)
{
ggraphic.Update();
if(ResourceSave("::"+ChName,ChName+".bmp"))
Print(" File ",ChName,".bmp saved");
else
Print(" File ",ChName,".bmp NOT saved! error=",GetLastError());
ggraphic.Destroy();
return;
}
//---------------------------------------------------
void StoreData(string ChartName,int NumPoints,double &x[],double &y1[],
double &y2[],double &y3[],double &y4[])
{
// call from Tester Mode
// returns no data
int jj;
string Filename;
struct Bufr
{
int NPoints;
double x;
double y1;
double y2;
double y3;
double y4;
};
Bufr B1[];
ArrayResize(B1,NumPoints);
for(jj=0;jj<NumPoints;jj++)
{
B1[jj].NPoints = NumPoints;
B1[jj].x = x[jj];
B1[jj].y1 = y1[jj];
B1[jj].y2 = y2[jj];
B1[jj].y3 = y3[jj];
B1[jj].y4 = y4[jj];
}
// Save file in Tester
Filename = ChartName +".dat";
if(!FileSave(Filename,B1,FILE_COMMON))
PrintFormat("FileSave() failed, error=%d",GetLastError());
else
Print(" File ",Filename," Saved to Common/Files ");
return;
}
//-------------------------------------------------------------------
void LoadData(string ChartName,int &NumPoints,double &x[],double &y1[],
double &y2[],double &y3[],double &y4[])
{
// called in Terminal mode
int jj;
string Filename;
struct Bufr
{
int NPoints;
double x;
double y1;
double y2;
double y3;
double y4;
};
//
Bufr B1[];
ArrayResize(B1,200); // maximum number of points
Filename = ChartName +".dat";
long count=FileLoad(Filename,B1,FILE_COMMON);
if(count!=-1)
{
Print(" Tester: Read Saved File Data: ",Filename);
}
NumPoints = B1[0].NPoints;
// resize x,y arrays
ArrayResize(x,NumPoints);
ArrayResize(y1,NumPoints);
ArrayResize(y2,NumPoints);
ArrayResize(y3,NumPoints);
ArrayResize(y4,NumPoints);
for(jj=0;jj<NumPoints;jj++)
{
x[jj] = B1[jj].x;
y1[jj] = B1[jj].y1;
y2[jj] = B1[jj].y2;
y3[jj] = B1[jj].y3;
y4[jj] = B1[jj].y4;
}
return;
}
//-------------------------------------------------------------
Comments