//+------------------------------------------------------------------+
//|                                       smTMMS Oscillator_vX
//+------------------------------------------------------------------+
#property copyright "Copyright 30.06.2019, SwingMan"
#property strict
#property indicator_separate_window

#property indicator_buffers 6

#property indicator_color1 clrSienna
#property indicator_color2 clrLime
#property indicator_color3 clrDodgerBlue
#property indicator_color4 clrGreen
#property indicator_color5 clrRed
#property indicator_color6 clrDarkGray

#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 2
#property indicator_width4 3
#property indicator_width5 3
#property indicator_width6 3


//#property indicator_level1 80
//#property indicator_level2 50
//#property indicator_level3 20
#property indicator_level1 30
#property indicator_level2 0
#property indicator_level3 -30
#property indicator_levelcolor clrSilver
#property indicator_levelstyle STYLE_DOT
#property indicator_levelwidth 1

//#property indicator_maximum 100
//#property indicator_minimum 0
#property indicator_maximum 50
#property indicator_minimum -50

//+------------------------------------------------------------------+
sinput string           sRSI="RSI"; //=================================     
input int      RSI_Period=14;
input ENUM_APPLIED_PRICE      RSI_AppliedPrice=PRICE_CLOSE;
//
sinput string           sStochastic="STOCHASTIC"; //=================================    
input int            Stochastic_1_Period_K  = 8;
input int            Stochastic_1_Period_D  = 3;
input int            Stochastic_1_Slowing   = 3;
input ENUM_STO_PRICE Stochastic_1_PriceField = STO_LOWHIGH;
input ENUM_MA_METHOD Stochastic_1_MAmethod   = MODE_SMMA;
input int            Stochastic_2_Period_K  = 14;
input int            Stochastic_2_Period_D  = 3;
input int            Stochastic_2_Slowing   = 3;
input ENUM_STO_PRICE Stochastic_2_PriceField = STO_LOWHIGH;
input ENUM_MA_METHOD Stochastic_2_MAmethod   = MODE_SMMA;
//+------------------------------------------------------------------+

//---- buffers
double bufRSI[],bufStoch1[],bufStoch2[];
double bufHistUP[],bufHistDN[],bufHistNO[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   int iBuff=-1;
   iBuff++; SetIndexBuffer(iBuff,bufRSI);     SetIndexStyle(iBuff,DRAW_LINE);       SetIndexLabel(iBuff,"RSI");
   iBuff++; SetIndexBuffer(iBuff,bufStoch1);  SetIndexStyle(iBuff,DRAW_LINE);       SetIndexLabel(iBuff,"Stochastic ("+Stochastic_1_Period_K+")");
   iBuff++; SetIndexBuffer(iBuff,bufStoch2);  SetIndexStyle(iBuff,DRAW_LINE);       SetIndexLabel(iBuff,"Stochastic ("+Stochastic_2_Period_K+")");
   iBuff++; SetIndexBuffer(iBuff,bufHistUP);  SetIndexStyle(iBuff,DRAW_HISTOGRAM);  SetIndexLabel(iBuff,NULL);
   iBuff++; SetIndexBuffer(iBuff,bufHistDN);  SetIndexStyle(iBuff,DRAW_HISTOGRAM);  SetIndexLabel(iBuff,NULL);
   iBuff++; SetIndexBuffer(iBuff,bufHistNO);  SetIndexStyle(iBuff,DRAW_HISTOGRAM);  SetIndexLabel(iBuff,NULL);

//---
   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    i,pos;
   double threshold=50,limit=0;

//---
   if(rates_total<=10)
      return(0);
////--- counting from 0 to rates_total
//   ArraySetAsSeries(bufRSI,false);
//   ArraySetAsSeries(bufStoch1,false);
//   ArraySetAsSeries(bufStoch2,false);
//   ArraySetAsSeries(bufHistUP,false);
//   ArraySetAsSeries(bufHistDN,false);
//   ArraySetAsSeries(bufHistNO,false);
//
////--- preliminary calculation
//   if(prev_calculated>1)
//      pos=prev_calculated-1;
//   else
   pos=0;

//--- main loop of calculations
   for(i=pos; i<rates_total; i++)
     {
      //--- oscillators
      bufRSI[i]=iRSI(Symbol(),Period(),RSI_Period,RSI_AppliedPrice,i);
      bufStoch1[i]=iStochastic(Symbol(),Period(),
                               Stochastic_1_Period_K,Stochastic_1_Period_D,Stochastic_1_Slowing,
                               Stochastic_1_MAmethod,Stochastic_1_PriceField,MODE_MAIN,i);
      bufStoch2[i]=iStochastic(Symbol(),Period(),
                               Stochastic_2_Period_K,Stochastic_2_Period_D,Stochastic_2_Slowing,
                               Stochastic_2_MAmethod,Stochastic_2_PriceField,MODE_MAIN,i);
      bufRSI[i]=bufRSI[i]-threshold;
      bufStoch1[i]=bufStoch1[i]-threshold;
      bufStoch2[i]=bufStoch2[i]-threshold;
                               
      //--- histograms
      bufHistUP[i]=EMPTY_VALUE;
      bufHistDN[i]=EMPTY_VALUE;
      bufHistNO[i]=EMPTY_VALUE;
      if(bufRSI[i]>limit && bufStoch1[i]>limit && bufStoch2[i]>limit)
         bufHistUP[i]=bufStoch2[i];
      else
      if(bufRSI[i]<limit && bufStoch1[i]<limit && bufStoch2[i]<limit)
         bufHistDN[i]=bufStoch2[i];
      else
         bufHistNO[i]=bufStoch2[i];
     }

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
