//+------------------------------------------------------------------+ //| i-Regresion channel.mq4 | //| UnadaJapon | //| None | //+------------------------------------------------------------------+ #property copyright "UnadaJapon" #property link "None" #property version "1.00" #property strict #property indicator_chart_window #property indicator_buffers 5 #property indicator_plots 5 //--- plot fx #property indicator_label1 "i-Reg" #property indicator_type1 DRAW_LINE #property indicator_color1 clrLightGray #property indicator_style1 STYLE_DASHDOT #property indicator_width1 1 //--- plot sqh #property indicator_label2 "High Reg" #property indicator_type2 DRAW_LINE #property indicator_color2 clrGold #property indicator_style2 STYLE_SOLID #property indicator_width2 1 //--- plot sql #property indicator_label3 "Low Reg" #property indicator_type3 DRAW_LINE #property indicator_color3 clrGold #property indicator_style3 STYLE_SOLID #property indicator_width3 1 //--- plot sqh #property indicator_label4 "High Reg 2" #property indicator_type4 DRAW_LINE #property indicator_color4 clrLime #property indicator_style4 STYLE_SOLID #property indicator_width4 1 //--- plot sql #property indicator_label5 "Low Reg 2" #property indicator_type5 DRAW_LINE #property indicator_color5 clrLime #property indicator_style5 STYLE_SOLID #property indicator_width5 1 //--- input parameters input int degree = 3; input double kstd = 2.0; input double kstd2 = 2.6; input int num_of_bars = 250; input int shift = 0; //--- indicator buffers double fxBuffer[]; double sqhBuffer[]; double sqlBuffer[]; double sqh2Buffer[]; double sql2Buffer[]; double ai[10, 10], b[10], x[10], sx[20]; double sum, qq, mm, tt, sq, sq2; int ip, p, n, f, ii, jj, kk, ll, nn, i0 = 0; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0, fxBuffer); SetIndexBuffer(1, sqhBuffer); SetIndexBuffer(2, sqlBuffer); SetIndexBuffer(3, sqh2Buffer); SetIndexBuffer(4, sql2Buffer); SetIndexEmptyValue(0, 0.0); SetIndexEmptyValue(1, 0.0); SetIndexEmptyValue(2, 0.0); SetIndexEmptyValue(3, 0.0); SetIndexEmptyValue(4, 0.0); SetIndexShift(0, shift); SetIndexShift(1, shift); SetIndexShift(2, shift); SetIndexShift(3, shift); SetIndexShift(4, shift); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, 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[]) { //--- if(rates_total < num_of_bars) return(-1); int mi; ip = num_of_bars; p = ip; sx[1] = p + 1; nn = degree + 1; SetIndexDrawBegin(0, rates_total - p - 1); SetIndexDrawBegin(1, rates_total - p - 1); SetIndexDrawBegin(2, rates_total - p - 1); SetIndexDrawBegin(3, rates_total - p - 1); SetIndexDrawBegin(4, rates_total - p - 1); for(mi = 1; mi <= nn * 2 - 2; mi++) { sum = 0; for(n = i0; n <= i0 + p; n++) sum += MathPow(n, mi); sx[mi+1] = sum; } for(mi = 1; mi <= nn; mi++) { sum = 0.00000; for(n = i0; n <= i0 + p; n++) { if(mi == 1) sum += Close[n]; else sum += Close[n] * MathPow(n, mi - 1); } b[mi] = sum; } for(jj = 1; jj <= nn; jj++) { for(ii = 1; ii <= nn; ii++) { kk = ii + jj - 1; ai[ii, jj] = sx[kk]; } } for(kk = 1; kk <= nn - 1; kk++) { ll = 0; mm = 0; for(ii = kk; ii <= nn; ii++) { if(MathAbs(ai[ii, kk]) > mm) { mm = MathAbs(ai[ii, kk]); ll = ii; } } if(ll == 0) return(0); if(ll != kk) { for(jj = 1; jj <= nn; jj++) { tt = ai[kk, jj]; ai[kk, jj] = ai[ll, jj]; ai[ll, jj] = tt; } tt = b[kk]; b[kk] = b[ll]; b[ll] = tt; } for(ii = kk + 1;ii <= nn;ii++) { qq = ai[ii, kk] / ai[kk, kk]; for(jj = 1;jj <= nn;jj++) { if(jj == kk) ai[ii, jj] = 0; else ai[ii, jj] = ai[ii, jj] - qq * ai[kk, jj]; } b[ii] = b[ii] - qq * b[kk]; } } x[nn] = b[nn]/ai[nn, nn]; for(ii = nn - 1;ii >= 1;ii--) { tt = 0; for(jj = 1;jj <= nn - ii;jj++) { tt = tt + ai[ii, ii + jj] * x[ii + jj]; x[ii] = (1 / ai[ii, ii]) * (b[ii] - tt); } } for(n = i0;n<=i0+p;n++) { sum = 0; for(kk = 1; kk <= degree; kk++) sum+=x[kk+1] * MathPow(n, kk); fxBuffer[n] = x[1] + sum; } sq = 0.0; for(n = i0; n <= i0 + p; n++) sq += MathPow(Close[n] - fxBuffer[n], 2); sq = MathSqrt(sq / (p + 1)) * kstd; sq2 = 0.0; for(n = i0; n <= i0 + p; n++) sq2 += MathPow(Close[n] - fxBuffer[n], 2); sq2 = MathSqrt(sq2 / (p + 1)) * kstd2; for(n = i0; n <= i0 + p; n++) { sqhBuffer[n] = fxBuffer[n] + sq; sqlBuffer[n] = fxBuffer[n] - sq; sqh2Buffer[n] = fxBuffer[n] + sq2; sql2Buffer[n] = fxBuffer[n] - sq2; } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+