//+------------------------------------------------------------------+
//|                               Indicator: ++MaWiloObvTools++1.mq4 |
//|                                                 Created by Betyx |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Created by Betyx"
#property link      "betyx2@gmail.com"
#property version   "1.02"
#property description "Genial Tools"

#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 4

#property indicator_type1 DRAW_LINE
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
#property indicator_color1 0xFF00E6
#property indicator_label1 "Wilo"

#property indicator_type2 DRAW_LINE
#property indicator_style2 STYLE_SOLID
#property indicator_width2 5
#property indicator_color2 0x00FF40
#property indicator_label2 "Wilo"

#property indicator_type3 DRAW_LINE
#property indicator_style3 STYLE_SOLID
#property indicator_width3 5
#property indicator_color3 0x2B00FF
#property indicator_label3 "Wilo"

#property indicator_type4 DRAW_LINE
#property indicator_style4 STYLE_SOLID
#property indicator_width4 1
#property indicator_color4 0x00FFEA
#property indicator_label4 "Quickwilo"

//--- indicator buffers
double Buffer1[];
double Buffer2[];
double Buffer3[];
double Buffer4[];

extern int PowerMA = 14;
extern int Power_Shift = 0;
extern int PowerWilo = 14;
extern int MAobv = 42;
extern int Powerma = 3;
extern int powerma_Shift = 0;
extern int powerwilo = 7;
double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | ++MaWiloObvTools++1 @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(4);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, EMPTY_VALUE);
   SetIndexBuffer(2, Buffer3);
   SetIndexEmptyValue(2, EMPTY_VALUE);
   SetIndexBuffer(3, Buffer4);
   SetIndexEmptyValue(3, EMPTY_VALUE);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   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[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   ArraySetAsSeries(Buffer3, true);
   ArraySetAsSeries(Buffer4, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, EMPTY_VALUE);
      ArrayInitialize(Buffer2, EMPTY_VALUE);
      ArrayInitialize(Buffer3, EMPTY_VALUE);
      ArrayInitialize(Buffer4, EMPTY_VALUE);
     }
   else
      limit++;
   
   int j;
   
   double IndicatorData1[];
   int IndicatorData1_period[2];
   IndicatorData1_period[0] = PowerMA;
   IndicatorData1_period[1] = Powerma;
   int IndicatorData1_count = 2*IndicatorData1_period[ArrayMaximum(IndicatorData1_period)];
   if(IndicatorData1_count < MathMax(limit, 50)) IndicatorData1_count = MathMax(limit, 50);
   ArrayResize(IndicatorData1, IndicatorData1_count);
   ArraySetAsSeries(IndicatorData1, true);
   for(j = IndicatorData1_count-1; j >= 0; j--)
     {
      IndicatorData1[j] = iWPR(NULL, PERIOD_CURRENT, PowerWilo, j); //IndicatorData1 = William's Percent Range
     }
   
   double IndicatorData2[];
   int IndicatorData2_count = 2*MAobv;
   if(IndicatorData2_count < MathMax(limit, 50)) IndicatorData2_count = MathMax(limit, 50);
   ArrayResize(IndicatorData2, IndicatorData2_count);
   ArraySetAsSeries(IndicatorData2, true);
   for(j = IndicatorData2_count-1; j >= 0; j--)
     {
      if(iBarShift(Symbol(), PERIOD_H4, Time[j]) < 0) continue;
      IndicatorData2[j] = iOBV(NULL, PERIOD_H4, PRICE_CLOSE, iBarShift(Symbol(), PERIOD_H4, Time[j])); //IndicatorData2 = On Balance Volume
     }
   
   double IndicatorData3[];
   int IndicatorData3_count = 2*Powerma;
   if(IndicatorData3_count < MathMax(limit, 50)) IndicatorData3_count = MathMax(limit, 50);
   ArrayResize(IndicatorData3, IndicatorData3_count);
   ArraySetAsSeries(IndicatorData3, true);
   for(j = IndicatorData3_count-1; j >= 0; j--)
     {
      IndicatorData3[j] = iWPR(NULL, PERIOD_CURRENT, powerwilo, j); //IndicatorData3 = William's Percent Range
     }
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      
      int barshift_H4 = iBarShift(Symbol(), PERIOD_H4, Time[i]);
      if(barshift_H4 < 0) continue;
      
      //Indicator Buffer 1
      if(iMAOnArray(IndicatorData1, 0, PowerMA, Power_Shift, MODE_SMMA, i) != 0 //Moving Average is not equal to fixed value
      )
        {
         Buffer1[i] = iMAOnArray(IndicatorData1, 0, PowerMA, Power_Shift, MODE_SMMA, i); //Set indicator value at Moving Average
        }
      else
        {
         Buffer1[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 2
      if(iOBV(NULL, PERIOD_H4, PRICE_CLOSE, barshift_H4) > iMAOnArray(IndicatorData2, 0, MAobv, 0, MODE_SMA, i) //On Balance Volume > Moving Average
      )
        {
         Buffer2[i] = iMAOnArray(IndicatorData1, 0, PowerMA, Power_Shift, MODE_SMMA, i); //Set indicator value at Moving Average
        }
      else
        {
         Buffer2[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 3
      if(iOBV(NULL, PERIOD_H4, PRICE_CLOSE, barshift_H4) < iMAOnArray(IndicatorData2, 0, MAobv, 0, MODE_SMA, i) //On Balance Volume < Moving Average
      )
        {
         Buffer3[i] = iMAOnArray(IndicatorData1, 0, PowerMA, Power_Shift, MODE_SMMA, i); //Set indicator value at Moving Average
        }
      else
        {
         Buffer3[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 4
      if(iMAOnArray(IndicatorData3, 0, Powerma, powerma_Shift, MODE_SMA, i) != 0 //Moving Average is not equal to fixed value
      )
        {
         Buffer4[i] = iMAOnArray(IndicatorData1, 0, Powerma, powerma_Shift, MODE_SMA, i); //Set indicator value at Moving Average
        }
      else
        {
         Buffer4[i] = EMPTY_VALUE;
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+