//+------------------------------------------------------------------+ //| np_graphics.mqh | //| Copyright 2025, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, MetaQuotes Ltd." #property link "https://www.mql5.com" #include #include "np.mqh" //--- namespace np { //+------------------------------------------------------------------+ //| plot a matrix | //+------------------------------------------------------------------+ template CGraphic* plotMatrix(matrix&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 CGraphic* plotMatrices(matrix&x,matrix&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 void plotmatrices(matrix&x,matrix&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 CGraphic* plotMatrices(matrix&x,matrix&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 void plotmatrices(matrix&x,matrix&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 CGraphic* plotXY(vector&x,vector&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 void plotxy(vector&x,vector&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 CGraphic* scatterplot(vector&x,vector&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 void scatter_plot(vector&x,vector&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 CGraphic* histogram_plot(vector&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) 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 //+------------------------------------------------------------------+