//+------------------------------------------------------------------+ //| CalcShowRegression_script.mq5 | //| Copyright 2016, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2016, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property script_show_inputs //--- Include files #include #include //--- input parameters input int bars=500; // Number of bars input ENUM_TIMEFRAMES timeframe=PERIOD_M5; // Time-frame input string symbol1="WIN$"; // Leg A input string symbol2="WDO$"; // Leg B input int show_time=10; // Duration of chart display in seconds //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- Close prices of two financial instruments double closes1[],closes2[]; //--- Copy Close prices of the first instrument int copied=CopyClose(symbol1,timeframe,1,bars,closes1); if(copied!=bars) { PrintFormat("Error! %s copied just %d bars out of %d",symbol1,copied,bars); return; } //--- Copy Close prices of the second instrument copied=CopyClose(symbol2,timeframe,1,bars,closes2); if(copied!=bars) { PrintFormat("Error! %s recieved just %d bars out of %d",symbol2,copied,bars); return; } //--- CLinearRegression regression(closes2,closes1,symbol2,symbol1); regression.Plot(); if(IS_DEBUG_MODE) { MessageBox("The script is running in debug mode, go to MetaEditor to view"); DebugBreak(); } else { int seconds=show_time; while(seconds>=0) { Comment("Chart will be deleted after ",seconds," seconds"); Sleep(1000); seconds--; } Comment(""); } } //+------------------------------------------------------------------+ //| Class to draw a regression line on two arrays | //+------------------------------------------------------------------+ class CLinearRegression { private: double m_x[]; // Data of the first array double m_y[]; // Data of the second array double m_a; // coefficient A in regression Y=A*X+B double m_b; // coefficient B in regression Y=A*X+B double m_x1; double m_y1; double m_x2; double m_y2; CGraphic m_graphic; // Class for drawing the chart string m_xname; // The name for the first array data string m_yname; // The name for the second array data public: CLinearRegression(const double &x[],const double &y[], const string xname,const string yname); ~CLinearRegression(){m_graphic.Destroy();}; void Plot(void); }; //+------------------------------------------------------------------+ //| Constructor | //+------------------------------------------------------------------+ CLinearRegression::CLinearRegression(const double &x[],const double &y[], const string xname,const string yname) { int size=ArraySize(x); if(ArraySize(x)!=ArraySize(y) || size<=0) return; m_xname=xname; m_yname=yname; ArraySetAsSeries(m_x,true); ArraySetAsSeries(m_y,true); //--- ArrayCopy(m_x,x); ArrayCopy(m_y,y); m_x1=m_x[0]; m_x2=m_x[0]; CMatrixDouble xy(size,2); xy[0].Set(0, m_x[0]); xy[0].Set(1, m_y[1]); for(int i=1; im_x[i]) m_x1=m_x[i]; if(m_x2