Индикаторы: BB MACD

 

BB MACD:

Классический индикатор MACD, заключенный в торговые полосы.

Author: Collector

 

Индикатор достаточно интересный, только есть небольшой недостаток, загружает комп на все 100%, т.е. любое переключение между периодами приводит к дикой загрузке проца, сурово.

 
lsv:

Индикатор достаточно интересный, только есть небольшой недостаток, загружает комп на все 100%, т.е. любое переключение между периодами приводит к дикой загрузке проца, сурово.

Перепутаны параметры функции - iStdDevOnArray.
Правильно было бы так:
sDev = iStdDevOnArray(bbMacd, 0, Length, 0, MODE_EMA, i);

Но, всё равно iStdDevOnArray, почему-то очень медленно работает.
Если посчитать в ручную, не тормозит.

//+------------------------------------------------------------------+
//|                                             Custom BB_MACD_2.mq4 |
//|                                     Copyright © 2005, adoleh2000 |
//|                                       https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
 
#property  copyright "Copyright © 2005, adoleh2000"
#property  link      "https://www.metaquotes.net/"
//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 4
#property  indicator_color1  Lime    //bbMacd up
#property  indicator_color2  Magenta //bbMacd up
#property  indicator_color3  Blue    //Upperband
#property  indicator_color4  Red     //Lowerband
//---- indicator parameters
extern int FastLen = 12;
extern int SlowLen = 26;
extern int Length = 10;
extern double StDv = 2.5;
//---- indicator buffers
double ExtMapBuffer1[];  // bbMacd
double ExtMapBuffer2[];  // bbMacd
double ExtMapBuffer3[];  // Upperband Line
double ExtMapBuffer4[];  // Lowerband Line
//---- buffers
double bbMacd[];
double avg[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 6 additional buffers are used for counting.
   IndicatorBuffers(6);   
//---- drawing settings     
   SetIndexBuffer(0, ExtMapBuffer1); // bbMacd line
   SetIndexStyle(0, DRAW_ARROW);
   SetIndexArrow(0, 108);
   IndicatorDigits(Digits + 1);
//----
   SetIndexBuffer(1, ExtMapBuffer2); // bbMacd line
   SetIndexStyle(1, DRAW_ARROW);
   SetIndexArrow(1, 108);
   IndicatorDigits(Digits + 1);
//----   
   SetIndexBuffer(2, ExtMapBuffer3); // Upperband line
   SetIndexStyle(2, DRAW_LINE, STYLE_SOLID, 1);
   IndicatorDigits(Digits + 1);
//----   
   SetIndexBuffer(3, ExtMapBuffer4); // Lowerband line
   SetIndexStyle(3, DRAW_LINE, STYLE_SOLID, 1);
   IndicatorDigits(Digits + 1);
//----
   SetIndexBuffer(4, bbMacd);
   SetIndexBuffer(5, avg);    
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("BB MACD(" + FastLen + "," + SlowLen + "," + Length+")");
   SetIndexLabel(0, "bbMacd");
   SetIndexLabel(1, "Upperband");
   SetIndexLabel(2, "Lowerband");  
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom BB_MACD                                                   |
//+------------------------------------------------------------------+
int start()
  {
   int limit,i,k;
   int counted_bars = IndicatorCounted();
   double deviation;
   double sum,oldval,newres;
//---- check for possible errors
   if(counted_bars < 0) 
       return(-1);
//---- last counted bar will be recounted
   if(counted_bars > 0) 
       counted_bars--;
   limit = Bars - counted_bars;
//----
   for(i = 0; i < limit; i++)
       bbMacd[i] = iMA(NULL, 0, FastLen, 0, MODE_EMA, PRICE_CLOSE, i) - 
                   iMA(NULL, 0, SlowLen, 0, MODE_EMA, PRICE_CLOSE, i);
//----
   for(i = 0; i < limit; i++)
       avg[i] = iMAOnArray(bbMacd, 0, Length, 0, MODE_EMA, i);
//---- Bands
   for(i = 0; i < limit; i++)
     {
      sum=0.0;
      k=i+Length-1;
      oldval=avg[i];
      while(k>=i)
        {
         newres=bbMacd[k]-oldval;
         sum+=newres*newres;
         k--;
        }
      deviation=StDv*MathSqrt(sum/Length);
      ExtMapBuffer3[i]=oldval+deviation;  // Upperband Line
      ExtMapBuffer4[i]=oldval-deviation;  // Lowerband Line
     }
     
//----
   for(i = 0; i < limit; i++)
     {
       ExtMapBuffer1[i]=bbMacd[i];     // Uptrend bbMacd
       ExtMapBuffer2[i]=bbMacd[i];     // downtrend bbMacd
       if(bbMacd[i] > bbMacd[i+1]) ExtMapBuffer2[i] = EMPTY_VALUE;
       if(bbMacd[i] < bbMacd[i+1]) ExtMapBuffer1[i] = EMPTY_VALUE;
     }
     
//---- done
   return(0);
  }
//+------------------------------------------------------------------+