Article-22714-Volatility-Mo.../Include/slsqp_article/np_graphics.mqh

442 行
16 KiB
MQL5

2026-06-03 20:14:05 +02:00
//+------------------------------------------------------------------+
//| np_graphics.mqh |
//| Copyright 2025, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#include<Graphics\Graphic.mqh>
#include "np.mqh"
//---
namespace np
{
//+------------------------------------------------------------------+
//| plot a matrix |
//+------------------------------------------------------------------+
template<typename T>
CGraphic* plotMatrix(matrix<T>&in,string plot_name,string legend="col", string x_axis_label="x_axis",string y_axis_label = "y_axis", bool points_fill = true,long chart_id=0, int sub_win=0,int x1=30, int y1=40, int x2=550, int y2=310, bool chart_show=true, int linewidth=5, ENUM_CURVE_TYPE curve_type=CURVE_LINES)
{
CGraphic* graph = new CGraphic();
ChartRedraw(chart_id);
ChartSetInteger(chart_id, CHART_SHOW, chart_show);
if(!graph.Create(chart_id, plot_name, sub_win, x1, y1, x2, y2))
{
delete graph;
Print(__FUNCTION__," Failed to Create graphical object on the Main chart Err ", GetLastError());
return NULL;
}
int cols = (int)in.Cols(), rows = (int)in.Rows();
string colnames[];
int split = StringSplit(legend,StringGetCharacter(",",0),colnames);
if(split<cols)
Print(__FUNCTION__," WARNING: Invalid legend labels ");
T x[], y[];
ArrayResize(x, rows);
for(int i=0; i<rows; ++i)
x[i] = T(i+1);
CColorGenerator generator;
for(int i=0; i<cols; ++i)
{
if(!np::vecAsArray(in.Col(i), y))
{
Print(__FUNCTION__, " error ", GetLastError());
ObjectDelete(chart_id,plot_name);
delete graph;
return NULL;
}
CCurve* newcurve = graph.CurveAdd(x, y, generator.Next(),curve_type,split<cols?legend+string(i+1):colnames[i]);
newcurve.PointsFill(points_fill);
newcurve.LinesWidth(linewidth);
}
graph.XAxis().Name(x_axis_label);
graph.XAxis().NameSize(13);
graph.YAxis().Name(y_axis_label);
graph.YAxis().NameSize(13);
graph.FontSet("Lucida Console", 13);
graph.CurvePlotAll();
graph.Update();
return graph;
}
//+------------------------------------------------------------------+
//| plot a matrix |
//+------------------------------------------------------------------+
template<typename T>
CGraphic* plotMatrices(matrix<T>&x,matrix<T>&y,string plot_name, bool by_column = false, string x_axis_label="x_axis",string y_axis_label = "y_axis", string legend="col", ENUM_CURVE_TYPE curve_type = CURVE_LINES,int line_width = 5,long chart_id=0, int sub_win=0,int x1=30, int y1=40, int x2=550, int y2=310, bool chart_show=true)
{
if((by_column && x.Rows()!=y.Rows()) || (!by_column && x.Cols()!=y.Cols()))
{
Print(__FUNCTION__," invalid inputs ");
return NULL;
}
CGraphic* graph = new CGraphic();
ChartRedraw(chart_id);
ChartSetInteger(chart_id, CHART_SHOW, chart_show);
if(!graph.Create(chart_id, plot_name, sub_win, x1, y1, x2, y2))
{
delete graph;
Print(__FUNCTION__," Failed to Create graphical object on the Main chart Err ", GetLastError());
return NULL;
}
int cols = (int)y.Cols(), rows = (int)y.Rows();
string colnames[];
int split = StringSplit(legend,StringGetCharacter(",",0),colnames);
if((by_column && split<cols) || (!by_column && split<rows))
Print(__FUNCTION__," WARNING: Invalid legend labels ");
T xx[], yy[];
CColorGenerator generator;
int ncurves = (by_column)?cols:rows;
for(int i=0; i<ncurves; ++i)
{
if((by_column && (!np::vecAsArray(y.Col(i), yy) || !np::vecAsArray(x.Col(i), xx))) ||
(!by_column && (!np::vecAsArray(y.Row(i), yy) || !np::vecAsArray(x.Row(i), xx))))
{
Print(__FUNCTION__, " error ", GetLastError());
ObjectDelete(chart_id,plot_name);
delete graph;
return NULL;
}
CCurve* newcurve = graph.CurveAdd(xx, yy, generator.Next(),curve_type,((by_column && split<cols) || (!by_column && split<rows))?legend+string(i+1):colnames[i]);
newcurve.LinesWidth(line_width);
}
graph.XAxis().Name(x_axis_label);
graph.XAxis().NameSize(13);
graph.YAxis().Name(y_axis_label);
graph.YAxis().NameSize(13);
graph.FontSet("Lucida Console", 13);
graph.CurvePlotAll();
graph.Update();
return graph;
}
//+------------------------------------------------------------------+
//| plot matrices |
//+------------------------------------------------------------------+
template<typename T>
void plotmatrices(matrix<T>&x,matrix<T>&y,string plot_name, bool by_column = false, string x_axis_label="x_axis",string y_axis_label = "y_axis", string legend="col", ENUM_CURVE_TYPE curve_type = CURVE_LINES,int line_width = 5,long chart_id=0, int sub_win=0,int x1=30, int y1=40, int x2=550, int y2=310, bool chart_show=true,int duration = 20)
{
CGraphic *plot = plotMatrices(x,y,plot_name,by_column,x_axis_label,y_axis_label,legend,curve_type,line_width,chart_id,sub_win,x1,y1,x2,y2,chart_show);
if(plot!=NULL)
{
Sleep(duration*1000);
plot.Destroy();
delete plot;
ChartRedraw(chart_id);
return;
}
}
//+------------------------------------------------------------------+
//| plot a matrix |
//+------------------------------------------------------------------+
template<typename T>
CGraphic* plotMatrices(matrix<T>&x,matrix<T>&y,string plot_name, bool by_column = false, string x_axis_label="x_axis",string y_axis_label = "y_axis", string legend="col", bool fill_points = true,long chart_id=0, int sub_win=0,int x1=30, int y1=40, int x2=550, int y2=310, bool chart_show=true)
{
if((by_column && x.Rows()!=y.Rows()) || (!by_column && x.Cols()!=y.Cols()))
{
Print(__FUNCTION__," invalid inputs ");
return NULL;
}
CGraphic* graph = new CGraphic();
ChartRedraw(chart_id);
ChartSetInteger(chart_id, CHART_SHOW, chart_show);
if(!graph.Create(chart_id, plot_name, sub_win, x1, y1, x2, y2))
{
delete graph;
Print(__FUNCTION__," Failed to Create graphical object on the Main chart Err ", GetLastError());
return NULL;
}
int cols = (int)y.Cols(), rows = (int)y.Rows();
string colnames[];
int split = StringSplit(legend,StringGetCharacter(",",0),colnames);
if((by_column && split<cols) || (!by_column && split<rows))
Print(__FUNCTION__," WARNING: Invalid legend labels ");
T xx[], yy[];
CColorGenerator generator;
int ncurves = (by_column)?cols:rows;
for(int i=0; i<ncurves; ++i)
{
if((by_column && (!np::vecAsArray(y.Col(i), yy) || !np::vecAsArray(x.Col(i), xx))) ||
(!by_column && (!np::vecAsArray(y.Row(i), yy) || !np::vecAsArray(x.Row(i), xx))))
{
Print(__FUNCTION__, " error ", GetLastError());
ObjectDelete(chart_id,plot_name);
delete graph;
return NULL;
}
CCurve* newcurve = graph.CurveAdd(xx, yy, generator.Next(),CURVE_POINTS_AND_LINES,((by_column && split<cols) || (!by_column && split<rows))?legend+string(i+1):colnames[i]);
newcurve.PointsFill(fill_points);
newcurve.LinesWidth(ncurves-i);
}
graph.XAxis().Name(x_axis_label);
graph.XAxis().NameSize(13);
graph.YAxis().Name(y_axis_label);
graph.YAxis().NameSize(13);
graph.FontSet("Lucida Console", 13);
graph.CurvePlotAll();
graph.Update();
return graph;
}
//+------------------------------------------------------------------+
//| plot matrices |
//+------------------------------------------------------------------+
template<typename T>
void plotmatrices(matrix<T>&x,matrix<T>&y,string plot_name, bool by_column = false, string x_axis_label="x_axis",string y_axis_label = "y_axis", string legend="col", bool fill_points=true,long chart_id=0, int sub_win=0,int x1=30, int y1=40, int x2=550, int y2=310, bool chart_show=true,int duration = 20)
{
CGraphic *plot = plotMatrices(x,y,plot_name,by_column,x_axis_label,y_axis_label,legend,fill_points,chart_id,sub_win,x1,y1,x2,y2,chart_show);
if(plot!=NULL)
{
Sleep(duration*1000);
plot.Destroy();
delete plot;
ChartRedraw(chart_id);
return;
}
}
//+------------------------------------------------------------------+
//| plot a matrix |
//+------------------------------------------------------------------+
template<typename T>
CGraphic* plotXY(vector<T>&x,vector<T>&y,string plot_name, string x_axis_label="x_axis",string y_axis_label = "y_axis", bool points_fill = true,long chart_id=0, int sub_win=0,int x1=30, int y1=40, int x2=750, int y2=500, bool chart_show=true,int line_width = 3, ENUM_CURVE_TYPE curve_type=CURVE_POINTS_AND_LINES)
{
T xa[];
T ya[];
if(x.Size()!=y.Size() || !np::vecAsArray(x,xa) || !np::vecAsArray(y,ya))
{
Print(__FUNCTION__," Failed to Create graphical object on the Main chart Err ", GetLastError());
return NULL;
}
CGraphic* graph = new CGraphic();
ChartRedraw(chart_id);
ChartSetInteger(chart_id, CHART_SHOW, chart_show);
if(!graph.Create(chart_id, plot_name, sub_win, x1, y1, x2, y2))
{
delete graph;
Print(__FUNCTION__," Failed to Create graphical object on the Main chart Err ", GetLastError());
return NULL;
}
CColorGenerator generator;
CCurve* newcurve = graph.CurveAdd(xa, ya, generator.Next(),curve_type,plot_name);
newcurve.PointsFill(points_fill);
newcurve.LinesWidth(line_width);
graph.XAxis().Name(x_axis_label);
graph.XAxis().NameSize(13);
graph.YAxis().Name(y_axis_label);
graph.YAxis().NameSize(13);
graph.FontSet("Lucida Console", 13);
graph.CurvePlotAll();
graph.Update();
return graph;
}
//+------------------------------------------------------------------+
//| plot vectors |
//+------------------------------------------------------------------+
template<typename T>
void plotxy(vector<T>&x,vector<T>&y,string plot_name, string x_axis_label="x_axis",string y_axis_label = "y_axis", bool points_fill = true,long chart_id=0, int sub_win=0,int x1=30, int y1=40, int x2=750, int y2=500, bool chart_show=true,int line_width = 3, ENUM_CURVE_TYPE curve_type=CURVE_POINTS_AND_LINES,int duration = 20)
{
CGraphic *plot = plotXY(x,y,plot_name,x_axis_label,y_axis_label,points_fill,chart_id,sub_win,x1,y1,x2,y2,chart_show,line_width,curve_type);
if(plot!=NULL)
{
Sleep(duration*1000);
plot.Destroy();
delete plot;
ChartRedraw(chart_id);
return;
}
}
//+------------------------------------------------------------------+
//| plot a scatterplot |
//+------------------------------------------------------------------+
template<typename T>
CGraphic* scatterplot(vector<T>&x,vector<T>&y,string plot_name, string x_axis_label="x_axis",string y_axis_label = "y_axis", bool points_fill = true,long chart_id=0, int sub_win=0,int x1=30, int y1=40, int x2=550, int y2=310, bool chart_show=true)
{
T xa[];
T ya[];
if(x.Size()!=y.Size() || !np::vecAsArray(x,xa) || !np::vecAsArray(y,ya))
{
Print(__FUNCTION__," Failed to Create graphical object on the Main chart Err ", GetLastError());
return NULL;
}
CGraphic* graph = new CGraphic();
ChartRedraw(chart_id);
ChartSetInteger(chart_id, CHART_SHOW, chart_show);
if(!graph.Create(chart_id, plot_name, sub_win, x1, y1, x2, y2))
{
delete graph;
Print(__FUNCTION__," Failed to Create graphical object on the Main chart Err ", GetLastError());
return NULL;
}
CColorGenerator generator;
CCurve* newcurve = graph.CurveAdd(xa, ya, generator.Next(), CURVE_POINTS,plot_name);
newcurve.PointsFill(points_fill);
graph.XAxis().Name(x_axis_label);
graph.XAxis().NameSize(15);
graph.XAxis().ValuesSize(15);
graph.YAxis().Name(y_axis_label);
graph.YAxis().NameSize(15);
graph.YAxis().ValuesSize(15);
graph.FontSet("Lucida Console", 18);
graph.CurvePlotAll();
graph.Update();
return graph;
}
//+------------------------------------------------------------------+
//| plot a scatterplot |
//+------------------------------------------------------------------+
template<typename T>
void scatter_plot(vector<T>&x,vector<T>&y,string plot_name, string x_axis_label="x_axis",string y_axis_label = "y_axis", bool points_fill = true,long chart_id=0, int sub_win=0,int x1=30, int y1=40, int x2=550, int y2=310, bool chart_show=true,int duration=20)
{
CGraphic *plot = scatterplot(x,y,plot_name,x_axis_label,y_axis_label,points_fill,chart_id,sub_win,x1,y1,x2,y2,chart_show);
if(plot!=NULL)
{
Sleep(duration*1000);
plot.Destroy();
delete plot;
ChartRedraw(chart_id);
return;
}
}
//+------------------------------------------------------------------+
//| plot a scatterplot |
//+------------------------------------------------------------------+
template<typename T>
CGraphic* histogram_plot(vector<T>&x,ulong num_bins,string plot_name, string x_axis_label="x_axis",string y_axis_label = "y_axis", int histogram_width = 6,long chart_id=0, int sub_win=0,int x1=30, int y1=40, int x2=550, int y2=310, bool chart_show=true)
{
T xa[];
T ya[];
if(x.Size()<num_bins*10)
{
Print(__FUNCTION__," invalid parameter: num_bins. Invalid relative to size of dataset");
return NULL;
}
double range=x.Max()-x.Min();
double width=range/double(num_bins);
if(width==0)
{
Print(__FUNCTION__," width is zero ");
return NULL;
}
ArrayResize(xa,(int)num_bins);
ArrayResize(ya,(int)num_bins);
//--- define the interval centers
for(ulong i=0; i<num_bins; ++i)
{
xa[i]=x.Min()+(i+0.5)*width;
ya[i]=0.0;
}
//--- fill the frequencies of falling within the interval
for(ulong i=0; i<x.Size(); ++i)
{
ulong ind=ulong((x[i]-x.Min())/width);
if(ind>=num_bins)
ind=num_bins-1;
++ya[ind];
}
CGraphic* graph = new CGraphic();
ChartRedraw(chart_id);
ChartSetInteger(chart_id, CHART_SHOW, chart_show);
if(!graph.Create(chart_id, plot_name, sub_win, x1, y1, x2, y2))
{
delete graph;
Print(__FUNCTION__," Failed to Create graphical object on the Main chart Err ", GetLastError());
return NULL;
}
CColorGenerator generator;
CCurve* newcurve = graph.CurveAdd(xa, ya, generator.Next(), CURVE_HISTOGRAM,plot_name);
newcurve.HistogramWidth(histogram_width);
graph.XAxis().Name(x_axis_label);
graph.XAxis().NameSize(15);
graph.XAxis().ValuesSize(15);
graph.YAxis().Name(y_axis_label);
graph.YAxis().NameSize(15);
graph.YAxis().ValuesSize(15);
graph.FontSet("Lucida Console", 18);
graph.CurvePlotAll();
graph.Update();
return graph;
}
}
//+------------------------------------------------------------------+
//| defines |
//+------------------------------------------------------------------+
// #define MacrosHello "Hello, world!"
// #define MacrosYear 2010
//+------------------------------------------------------------------+
//| DLL imports |
//+------------------------------------------------------------------+
// #import "user32.dll"
// int SendMessageA(int hWnd,int Msg,int wParam,int lParam);
// #import "my_expert.dll"
// int ExpertRecalculate(int wParam,int lParam);
// #import
//+------------------------------------------------------------------+
//| EX5 imports |
//+------------------------------------------------------------------+
// #import "stdlib.ex5"
// string ErrorDescription(int error_code);
// #import
//+------------------------------------------------------------------+