Untitled

 avatar
unknown
c_cpp
2 years ago
5.7 kB
10
Indexable
//+------------------------------------------------------------------+
//|                                                      ImpulseMACD |
//|                                                 Patryk Dabrowski |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Patryk Dabrowski"
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <MovingAverages.mqh>

//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 6
#property indicator_plots   3
#property indicator_type1   DRAW_LINE
#property indicator_type2   DRAW_LINE
#property indicator_type3   DRAW_HISTOGRAM
#property indicator_color1  Blue
#property indicator_color2  Orange
#property indicator_color3  Green
#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  1
#property indicator_label1  "ImpulseMACD"
#property indicator_label2  "Signal"
#property indicator_label3  "Histogram"

//---- indicator parameters
input int MasterMA=34;
input int SignalMA=9;

//---- indicator buffers
double MacdDivrBuffer[];
double SignalBuffer[];
double HistogramBuffer[];
double HiIndBuffer[];
double LoIndBuffer[];
double MasterIndBuffer[];

int HiIndHandler;
int LoIndHandler;
int MasterIndHandler;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//---- indicator buffers mapping
   SetIndexBuffer(0, MacdDivrBuffer, INDICATOR_DATA);
   SetIndexBuffer(1, SignalBuffer, INDICATOR_DATA);
   SetIndexBuffer(2, HistogramBuffer, INDICATOR_DATA);
   SetIndexBuffer(3, HiIndBuffer, INDICATOR_CALCULATIONS);
   SetIndexBuffer(4, LoIndBuffer, INDICATOR_CALCULATIONS);
   SetIndexBuffer(5, MasterIndBuffer, INDICATOR_CALCULATIONS);
//---- drawing settings

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,SignalMA-1);

//---- name for DataWindow and indicator subwindow label
   IndicatorSetString(INDICATOR_SHORTNAME, "Impulse MACD(" + IntegerToString(MasterMA) + "," + IntegerToString(SignalMA) + ")");

   HiIndHandler = iMA(NULL, 0, MasterMA, 0, MODE_SMMA, PRICE_HIGH);
   LoIndHandler = iMA(NULL, 0, MasterMA, 0, MODE_SMMA, PRICE_LOW);
   MasterIndHandler = iMA(NULL, 0, MasterMA, 0, MODE_LWMA, PRICE_WEIGHTED);


   Print("Impulse MACD(" + IntegerToString(MasterMA) + "," + IntegerToString(SignalMA) + ")");
//---- initialization done
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
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(prev_calculated<=0)
     {
      ArrayInitialize(MacdDivrBuffer,0);
      ArrayInitialize(SignalBuffer,0);
      ArrayInitialize(HistogramBuffer,0);
     }
  
   if(rates_total<SignalMA)
      return(0);
//--- not all data may be calculated
   int calculated=BarsCalculated(HiIndHandler);
   if(calculated<rates_total)
     {
      Print("Not all data of HiIndHandler is calculated (",calculated," bars). Error ",GetLastError());
      return(0);
     }
   calculated=BarsCalculated(LoIndHandler);
   if(calculated<rates_total)
     {
      Print("Not all data of LoIndHandler is calculated (",calculated," bars). Error ",GetLastError());
      return(0);
     }
   calculated=BarsCalculated(MasterIndHandler);
   if(calculated<rates_total)
     {
      Print("Not all data of MasterIndHandler is calculated (",calculated," bars). Error ",GetLastError());
      return(0);
     }
//--- we can copy not all data
   int to_copy;
   if(prev_calculated>rates_total || prev_calculated<0)
      to_copy=rates_total;
   else
     {
      to_copy=rates_total-prev_calculated;
      if(prev_calculated>0)
         to_copy++;
     }

//--- get data
   if(IsStopped()) // checking for stop flag
      return(0);
   if(CopyBuffer(HiIndHandler,0,0,to_copy,HiIndBuffer)<=0)
     {
      Print("Error ",GetLastError());
      return(0);
     }
   if(IsStopped()) // checking for stop flag
      return(0);
   if(CopyBuffer(LoIndHandler,0,0,to_copy,LoIndBuffer)<=0)
     {
      Print("Error ",GetLastError());
      return(0);
     }
   if(IsStopped()) // checking for stop flag
      return(0);
   if(CopyBuffer(MasterIndHandler,0,0,to_copy,MasterIndBuffer)<=0)
     {
      Print("Error ",GetLastError());
      return(0);
     }
//---
   int start;
   if(prev_calculated==0)
      start=0;
   else
      start=prev_calculated-1;



//Print(prev_calculated, " ", rates_total, " ", to_copy, " ", start);

//--- calculate MACD
   for(int i=start; i<rates_total && !IsStopped(); i++)
     {
      if(MasterIndBuffer[i] > HiIndBuffer[i])
         MacdDivrBuffer[i] = MasterIndBuffer[i] - HiIndBuffer[i];

      if(MasterIndBuffer[i] < LoIndBuffer[i])
         MacdDivrBuffer[i] = MasterIndBuffer[i] - LoIndBuffer[i];
     }
//--- calculate Signal
   SimpleMAOnBuffer(rates_total, prev_calculated, 0, SignalMA, MacdDivrBuffer, SignalBuffer);
//--- OnCalculate done. Return new prev_calculated.

for(int i=start; i<rates_total && !IsStopped(); i++)
  {
      HistogramBuffer[i] = MacdDivrBuffer[i] - SignalBuffer[i];
  }

   return(rates_total);
  }
//+------------------------------------------------------------------+
Editor is loading...