//+------------------------------------------------------------------+ //| Linear Regression Channel.mq4 | //| Copyright © 2008, Trading Automatics Ltd | //| http://www.tradingautomatics.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2008, Trading Automatics Ltd" #property link "http://www.tradingautomatics.com" #property indicator_chart_window //---- indicator parameters extern int ChannelId = 1; extern int Length = 100; extern color LR_Color = Red; extern color SD_Color = Red; extern int PriceVal = PRICE_MEDIAN; extern double StdDev = 2; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- ObjectCreate("Reg_line" + ChannelId, OBJ_TREND, 0, 0,0, 0,0); ObjectSet("Reg_line" + ChannelId, OBJPROP_COLOR, LR_Color); ObjectSet("Reg_line" + ChannelId, OBJPROP_STYLE, STYLE_SOLID); ObjectSet("Reg_line" + ChannelId, OBJPROP_WIDTH, 1); ObjectCreate("Reg_upper" + ChannelId, OBJ_TREND, 0, 0,0, 0,0); ObjectSet("Reg_upper" + ChannelId, OBJPROP_COLOR, SD_Color); ObjectSet("Reg_upper" + ChannelId, OBJPROP_STYLE, STYLE_SOLID); ObjectSet("Reg_upper" + ChannelId, OBJPROP_WIDTH, 1); ObjectCreate("Reg_lower" + ChannelId, OBJ_TREND, 0, 0,0, 0,0); ObjectSet("Reg_lower" + ChannelId, OBJPROP_COLOR, SD_Color); ObjectSet("Reg_lower" + ChannelId, OBJPROP_STYLE, STYLE_SOLID); ObjectSet("Reg_lower" + ChannelId, OBJPROP_WIDTH, 1); //---- return(0); } //+------------------------------------------------------------------+ //| Custor indicator deinitialization function | //+------------------------------------------------------------------+ void deinit() { ObjectDelete("Reg_line" + ChannelId); ObjectDelete("Reg_upper" + ChannelId); ObjectDelete("Reg_lower" + ChannelId); } //+------------------------------------------------------------------+ //| Regression Line | //+------------------------------------------------------------------+ int start() { double y1,y2, price; double a1, a2, a3, b1, a, b; double stddiv, tmp_div; double x_n_up, x_1_up, x_n_down, x_1_down; int shift, n; if(Bars <= Length) return(0); a1 = 0; a2 = 0; a3 = 0; b1 = 0; a = 0; b = 0; y1 = 0; y2 = 0; tmp_div = 0; n = Length; for(shift = Length; shift > 0; shift--) { price = iMA(Symbol(), 0, 1, 0, MODE_SMA, PriceVal, shift); a1 = a1 + shift*price; a2 = a2 + shift; a3 = a3 + price; b1 = b1 + shift*shift; } b = (n*a1 - a2*a3)/(n*b1 - a2*a2); a = (a3 - b*a2)/n; y1 = a + b*n; y2 = a + b; ObjectSet("Reg_line" + ChannelId, OBJPROP_TIME1, Time[Length]); ObjectSet("Reg_line" + ChannelId, OBJPROP_TIME2, Time[0]); ObjectSet("Reg_line" + ChannelId, OBJPROP_PRICE1, y1); ObjectSet("Reg_line" + ChannelId, OBJPROP_PRICE2, y2); for(shift = Length; shift > 0; shift--) { price = iMA(Symbol(), 0, 1, 0, MODE_SMA, PriceVal, shift); tmp_div = tmp_div + (price - (a + b*shift))*(price - (a + b*shift)); } stddiv = MathSqrt(tmp_div/n); x_n_up = y1 + StdDev*stddiv; x_1_up = y2 + StdDev*stddiv; x_n_down = y1 - StdDev*stddiv; x_1_down = y2 - StdDev*stddiv; ObjectSet("Reg_upper" + ChannelId, OBJPROP_TIME1, Time[Length]); ObjectSet("Reg_upper" + ChannelId, OBJPROP_TIME2, Time[0]); ObjectSet("Reg_upper" + ChannelId, OBJPROP_PRICE1, x_n_up); ObjectSet("Reg_upper" + ChannelId, OBJPROP_PRICE2, x_1_up); ObjectSet("Reg_lower" + ChannelId, OBJPROP_TIME1, Time[Length]); ObjectSet("Reg_lower" + ChannelId, OBJPROP_TIME2, Time[0]); ObjectSet("Reg_lower" + ChannelId, OBJPROP_PRICE1, x_n_down); ObjectSet("Reg_lower" + ChannelId, OBJPROP_PRICE2, x_1_down); return(0); } //+------------------------------------------------------------------+