//+------------------------------------------------------------------+ //| LinearRegressionChannel.mq5 | //| Copyright © 2008, dimicr | //| http://www.signalsbg.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2008, dimicr" #property link "http://www.signalsbg.com" #property description "LinearRegression Channel" //---- indicator version number #property version "1.00" //---- drawing the indicator in the main window #property indicator_chart_window //---- number of indicator buffers 3 #property indicator_buffers 3 //---- only 5 graphical plots are used #property indicator_plots 3 //+-----------------------------------+ //| Indicator drawing parameters | //+-----------------------------------+ //---- drawing the indicator as a line #property indicator_type1 DRAW_LINE //---- blue-violet color is used as the color of the indicator line #property indicator_color1 BlueViolet //---- the indicator line is a dash-dotted curve #property indicator_style1 STYLE_DASHDOTDOT //---- indicator line width is equal to 1 #property indicator_width1 1 //---- displaying the indicator label #property indicator_label1 "LinearRegression Line" //+--------------------------------------------+ //| Levels indicator drawing parameters | //+--------------------------------------------+ //---- drawing the levels as lines #property indicator_type2 DRAW_LINE #property indicator_type3 DRAW_LINE //---- selection of Bollinger Bands colors #property indicator_color2 Lime #property indicator_color3 Red //---- levels are dott-dash curves #property indicator_style2 STYLE_DASHDOTDOT #property indicator_style3 STYLE_DASHDOTDOT //---- Bollinger Bands width is equal to 1 #property indicator_width2 1 #property indicator_width3 1 //---- display levels labels #property indicator_label2 "LinearRegression Resistance" #property indicator_label3 "LinearRegression Support" //+-----------------------------------+ //| enumeration declaration | //+-----------------------------------+ enum ENUM_USER_PRICE { MODE_CLOSE, //Используем close MODE_HIGH_LOW //Используем high и low }; //+-----------------------------------+ //| INDICATOR INPUT PARAMETERS | //+-----------------------------------+ input ENUM_USER_PRICE UserPrice=MODE_CLOSE; //price mode input uint barsToCount=50; //Number of bars for calculation //+-----------------------------------+ //---- declaration of a dynamic array that further // will be used as an indicator buffer double LineBuffer[]; //---- declaration of dynamic arrays that will further be // used as indicator buffers double ResBuffer[],SupBuffer[]; //---- Declaration of integer variables of data starting point int min_rates_total; //+------------------------------------------------------------------+ //| LinearRegression indicator initialization function | //+------------------------------------------------------------------+ void OnInit() { //---- Initialization of variables of the start of data calculation min_rates_total=int(barsToCount); //---- set dynamic array as an indicator buffer SetIndexBuffer(0,LineBuffer,INDICATOR_DATA); //---- performing the shift of beginning of indicator drawing PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total); //---- setting the indicator values that won't be visible on a chart PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0); //---- indexing elements in the buffer as time series ArraySetAsSeries(LineBuffer,true); //---- setting dynamic arrays as indicator buffers SetIndexBuffer(1,ResBuffer,INDICATOR_DATA); SetIndexBuffer(2,SupBuffer,INDICATOR_DATA); //---- set the position, from which the Bollinger Bands drawing starts PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,min_rates_total); PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,min_rates_total); //---- restriction to draw empty values for the indicator PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0); PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0); //---- indexing elements in the buffer as time series ArraySetAsSeries(ResBuffer,true); ArraySetAsSeries(SupBuffer,true); //---- initializations of variable for indicator short name string shortname; StringConcatenate(shortname,"LinearRegression(",EnumToString(UserPrice),", ",barsToCount,")"); //--- creation of the name to be displayed in a separate sub-window and in a pop up help IndicatorSetString(INDICATOR_SHORTNAME,shortname); //--- determining the accuracy of displaying the indicator values IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1); //---- end of initialization } //+------------------------------------------------------------------+ //| LinearRegression iteration function | //+------------------------------------------------------------------+ int OnCalculate( const int rates_total, // amount of history in bars at the current tick const int prev_calculated,// amount of history in bars at the previous tick const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[] ) { //---- checking the number of bars to be enough for calculation if(rates_totalrates_total || prev_calculated<=0)// checking for the first start of the indicator calculation { limit=rates_total-min_rates_total-1; } else limit=rates_total-prev_calculated; //---- erase already unnecessary data for(bar=limit+min_rates_total; bar>min_rates_total && !IsStopped(); bar--) { LineBuffer[bar]=0.0; SupBuffer[bar]=0.0; ResBuffer[bar]=0.0; } for(bar=0; barl) res=h; else res=l; for(bar=0; bar