//------------------------------------------------------------------
#property copyright "www.forex-station.com"
#property link      "www.forex-station.com"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1  clrYellow
#property indicator_color2  clrLimeGreen
#property indicator_color3  clrRed

#property indicator_width1  1
#property indicator_width2  2
#property indicator_width3  2

#property strict

//
//
//
//
//

extern string            ForSymbol             = "AUDUSD";                // Oposite Symbol to use 
input int                Stoperiod        = 14;  // Stochastoc period
input int                slowing              = 3; // Stochastoc slowing
input int                signal               = 3; // Stochastoc signal
input ENUM_MA_METHOD     signalMode           = MODE_EMA; // Stochastoc mode
input ENUM_STO_PRICE     price                = STO_CLOSECLOSE; // Stochastoc price


double ma[],maUa[],maUb[],valc[];


//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int OnInit()
{
   IndicatorBuffers(4);
   SetIndexBuffer(0,ma,   INDICATOR_DATA); SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(1,maUa, INDICATOR_DATA); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,maUb, INDICATOR_DATA); SetIndexStyle(2,DRAW_HISTOGRAM);
 
   SetIndexBuffer(3,valc);
  
   ForSymbol = (ForSymbol=="") ? _Symbol : ForSymbol; 
   
 //  IndicatorSetDouble(INDICATOR_MINIMUM,0);
 //  IndicatorSetDouble(INDICATOR_MAXIMUM,1);
   
   IndicatorShortName(" stochastic delta  ");
   
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason){   }

//
//
//

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,limit=fmin(rates_total-prev_calculated+1,rates_total-1); 
     
   for(i=limit;i>=0; i--)
   {  
      double _curr   = iStochastic(_Symbol,_Period,Stoperiod,signal,slowing,signalMode,price,MODE_MAIN,i);  
      double _oppo   = iStochastic(ForSymbol,_Period,Stoperiod,signal,slowing,signalMode,price,MODE_MAIN,i);  
      ma[i] = ForSymbol==_Symbol ? _curr : _curr - _oppo;  
   //   ma[i] = _curr ;  
      valc[i] = (i<rates_total-1) ? (ma[i]>0) ? 1 : (ma[i]<0) ? -1 : valc[i+1]  : 0; 
      maUa[i] = (valc[i] == 1) ? ma[i] : EMPTY_VALUE;
      maUb[i] = (valc[i] ==-1) ? ma[i] : EMPTY_VALUE; 
     
   }
return(rates_total);
}

//------------------------------------------------------------------
//                                                                  
//------------------------------------------------------------------

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}
 

