//+------------------------------------------------------------------+
//|                                               MTF Stoch hist.mq4 |
//+------------------------------------------------------------------+

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_width1 3
#property indicator_color2 Red
#property indicator_width2 3
#property indicator_level1 30
#property indicator_level2 0
#property indicator_level3 -30
#property indicator_levelcolor Gray
#property indicator_maximum 60
#property indicator_minimum -60

extern int      TimeFrame    = 60;
extern int      KPeriod      = 14;
extern int      DPeriod      = 3;
extern int      Slowing      = 3;
extern int      MAMethod     = 0;
extern int      PriceField   = 0;

double ExtMapBuffer1[];
double ExtMapBuffer2[];

//+------------------------------------------------------------------+
int init()  {
//+------------------------------------------------------------------+
  SetIndexBuffer(0,ExtMapBuffer1);
  SetIndexStyle(0,DRAW_HISTOGRAM);
 
  SetIndexBuffer(1,ExtMapBuffer2);
  SetIndexStyle(1,DRAW_HISTOGRAM);

  switch(TimeFrame)   {
     case 1 : string TimeFrameStr="Period_M1"; break;
     case 5 : TimeFrameStr="Period_M5"; break;
     case 15 : TimeFrameStr="Period_M15"; break;
     case 30 : TimeFrameStr="Period_M30"; break;
     case 60 : TimeFrameStr="Period_H1"; break;
     case 240 : TimeFrameStr="Period_H4"; break;
     case 1440 : TimeFrameStr="Period_D1"; break;
     case 10080 : TimeFrameStr="Period_W1"; break;
     case 43200 : TimeFrameStr="Period_MN1"; break;
     default : TimeFrameStr="Current Timeframe";
  } 
  IndicatorShortName(" MTF Stochastic "+TimeFrameStr);  
  return(0);
}

//+------------------------------------------------------------------+
int deinit()  {
//+------------------------------------------------------------------+
  Comment("");
  return(0);
}   
 
//+------------------------------------------------------------------+
int start()  {
//+------------------------------------------------------------------+
  datetime TimeArray[];
  int    i,shift,limit,y=0,counted_bars=IndicatorCounted();
  double A0,A1;
   
// Plot defined timeframe on to current timeframe   
  ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame); 
  
  limit=Bars-counted_bars+TimeFrame/Period();

  for (i=0,y=0; i<limit; i++)   {
    if (Time[i] < TimeArray[y]) y++;

    ExtMapBuffer1[i] = 0; 
    ExtMapBuffer2[i] = 0;
/***********************************************************   
  Add your main indicator loop below.  You can reference an existing
  indicator with its iName  or iCustom.
  Rule 1:  Add extern inputs above for all neccesary values   
  Rule 2:  Use 'TimeFrame' for the indicator timeframe
  Rule 3:  Use 'y' for the indicator's shift value
**********************************************************/  
    A1 = iStochastic(NULL,TimeFrame,KPeriod,DPeriod,Slowing,MAMethod,PriceField,0,y+1)-50; 
    A0 = iStochastic(NULL,TimeFrame,KPeriod,DPeriod,Slowing,MAMethod,PriceField,0,y)-50;

    if (A0 >= 0) ExtMapBuffer1[i] = A0;
    if (A0 <  0) ExtMapBuffer2[i] = A0;
  }  
  return(0);
}
//+------------------------------------------------------------------+