Author: Copyright 2020, Dodonov Vitaly (mql_5)
2 Views
0 Downloads
0 Favorites
MACD_lrma
//+------------------------------------------------------------------+
//|                                                    MACD_lrma.mq4 |
//|                           Copyright 2020, Dodonov Vitaly (mql_5) |
//|                                           https://www.ellizii.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, Dodonov Vitaly (mql_5)"
#property link      "https://www.ellizii.ru"
#property version   "1.10"
#property strict
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots   4
//--- plot Label1
#property indicator_label1  "Signal"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot Label2
#property indicator_label2  "MACD"
#property indicator_type2   DRAW_HISTOGRAM
#property indicator_color2  clrSilver
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2

#property indicator_label3  "MACD_DOWN"
#property indicator_type3   DRAW_HISTOGRAM
#property indicator_color3  clrOrangeRed
#property indicator_style3  STYLE_SOLID
#property indicator_width3  2

#property indicator_label4  "MACD_UP"
#property indicator_type4   DRAW_HISTOGRAM
#property indicator_color4  clrPaleGreen
#property indicator_style4  STYLE_SOLID
#property indicator_width4  2

//--- indicator parameters
input int InpFastEMA=12;   // Fast EMA Period
input int InpSlowEMA=26;   // Slow EMA Period
input int InpSignalSMA=9;  // Signal SMA Period
input bool EnableColors = true; // Enable colors

//--- indicator buffers
double         SignalBuffer[];
double         MACDBuffer1[];
double         MACDBuffer2[];
double         MACDBuffer3[];
double         hma_fast[];
double         hma_slow[];
//--- right input parameters flag
bool      ExtParameters=false;
int       wsm,fast,slow,signal;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- Indicator digits
   IndicatorSetInteger(INDICATOR_DIGITS,(int)SymbolInfoInteger(Symbol(),SYMBOL_DIGITS)+1);
//--- drawing settings
   SetIndexDrawBegin(1,InpSignalSMA);
//--- indicator buffers mapping
   SetIndexBuffer(0,SignalBuffer);
   SetIndexBuffer(1,MACDBuffer1);
   SetIndexBuffer(2,MACDBuffer2);
   SetIndexBuffer(3,MACDBuffer3);
       
//--- name for DataWindow and indicator subwindow label
   IndicatorSetString(INDICATOR_SHORTNAME,"MACD lrma("+IntegerToString(InpFastEMA)+","+IntegerToString(InpSlowEMA)+","+IntegerToString(InpSignalSMA)+")");
//--- check for input parameters
   fast = InpFastEMA;
   slow = InpSlowEMA;
   signal = InpSignalSMA;

   if(fast<=1)
      fast=2;
   if(slow<=1)
      slow=2;
   if(signal<=1)
      signal=2;

//--
   ArraySetAsSeries(hma_fast,true);
   ArraySetAsSeries(hma_slow,true);

//---
   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[])
  {
   
//---
   ArrayResize(hma_fast,rates_total);
   ArrayResize(hma_slow,rates_total);
   
//---
   int i,limit;
//---
   if(rates_total<=signal)
      return(0);
//--- last counted bar will be recounted
   limit=rates_total-prev_calculated-MathMax((double)fast,(double)slow);
   if(prev_calculated>0)
      limit=1;
   hmaOnArray(fast,1,hma_fast,prev_calculated);
   hmaOnArray(slow,1,hma_slow,prev_calculated);
   
   //ArraySetAsSeries(hma_fast,false);
 //  ArraySetAsSeries(hma_slow,false);
//--- macd counted in the 1-st buffer
   for(i=limit-1; i>=0; i--)
     {
    
      MACDBuffer1[i]=(hma_fast[i]-hma_slow[i]);
     if(EnableColors){
      if(MACDBuffer1[i]>MACDBuffer1[i+1])MACDBuffer3[i]=MACDBuffer1[i];
      else MACDBuffer3[i]=0.0;
      if(MACDBuffer1[i]<MACDBuffer1[i+1])MACDBuffer2[i]=MACDBuffer1[i];
      else MACDBuffer2[i]=0.0;
     }
     }
//--- signal line counted in the 2-nd buffer
   ExponentialMAOnBuffer(rates_total,prev_calculated,0,signal,MACDBuffer1,SignalBuffer);
//--- return value of prev_calculated for next call
   return(rates_total);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void hmaOnArray(int hma_period,int hma_method,double &out[],int prev_calculate)
  {
   int counted_bars=prev_calculate;
   if(counted_bars < 0)
      return;
//----
   int x=0;
   int p=(int)MathSqrt((double)hma_period);
   int e=iBars(Symbol(),Period()) - counted_bars + hma_period + 1;
//----
   double vect[], trend[];
//----
   if(e > iBars(Symbol(),Period()))
      e=iBars(Symbol(),Period());
//----
   ArrayResize(vect, e);
   ArraySetAsSeries(vect, true);
   ArrayResize(trend, e);
   ArraySetAsSeries(trend, true);
//----
   double close[];
   ArrayCopySeries(close,MODE_CLOSE,Symbol(),Period());

   for(x=0; x < e-hma_period; x++)
      //----
      out[x]=LRMA(x,hma_period,close);
   return;
  }

//+------------------------------------------------------------------+
//|             Linear regression                                    |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double LRMA(const int pos,const int period,const double  &price[])
  {
   double Res=0;
   double tmpS=0,tmpW=0,wsum=0;
   int ps=0;
   int i=-1;
   bool set = ArrayGetAsSeries(price);
   if(set)
     {
      if((pos+period)>ArraySize(price))
         return(0);
      for(i=0; i<period; i++)
        {
         tmpS+=price[pos+i];
         tmpW+=price[pos+i]*(period-i);
         wsum+=(period-i);
        }
      tmpS/=period;
      tmpW/=wsum;
      Res=2.0*tmpW-1.0*tmpS;
     }
   else
     {
      if((pos-period)<0)
         return(0);
      for(i=0; i<period; i++)
        {
         tmpS+=price[pos-i];
         tmpW+=price[pos-i]*(period-i);
         wsum+=(period-i);
        }
      tmpS/=period;
      tmpW/=wsum;
      Res=2.0*tmpW-1.0*tmpS;
     }
   return(Res);
  }

//+------------------------------------------------------------------+
//|  Exponential moving average on price array                       |
//+------------------------------------------------------------------+
int ExponentialMAOnBuffer(const int rates_total,const int prev_calculated,const int begin,
                          const int period,const double& price[],double& buffer[])
  {
   int    i,limit;
//--- check for data
   if(period<=1 || rates_total-begin<period)
      return(0);
   double dSmoothFactor=2.0/(1.0+period);
//--- save as_series flags
   bool as_series_price=ArrayGetAsSeries(price);
   bool as_series_buffer=ArrayGetAsSeries(buffer);
   if(as_series_price)
      ArraySetAsSeries(price,false);
   if(as_series_buffer)
      ArraySetAsSeries(buffer,false);
//--- first calculation or number of bars was changed
   if(prev_calculated==0)
     {
      limit=period+begin;
      //--- set empty value for first bars
      for(i=0; i<begin; i++)
         buffer[i]=0.0;
      //--- calculate first visible value
      buffer[begin]=price[begin];
      for(i=begin+1; i<limit; i++)
         buffer[i]=price[i]*dSmoothFactor+buffer[i-1]*(1.0-dSmoothFactor);
     }
   else
      limit=prev_calculated-1;
//--- main loop
   for(i=limit; i<rates_total; i++)
      buffer[i]=price[i]*dSmoothFactor+buffer[i-1]*(1.0-dSmoothFactor);
//--- restore as_series flags
   if(as_series_price)
      ArraySetAsSeries(price,true);
   if(as_series_buffer)
      ArraySetAsSeries(buffer,true);
//---
   return(rates_total);
  }
//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---