//------------------------------------------------------------------
#property copyright "www.forex-station.com" 
#property link      "www.forex-station.com" 
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1  LimeGreen
#property indicator_color2  PaleVioletRed
#property indicator_color3  DarkGray
#property indicator_width1  2
#property indicator_width2  2
#property indicator_minimum -1.1
#property indicator_maximum +1.1

//
//
//
//
//

extern ENUM_TIMEFRAMES    TimeFrame = PERIOD_CURRENT;
extern int                EmaFast   = 5;
extern int                EmaSlow   = 12;
extern ENUM_APPLIED_PRICE EmaPrice  = PRICE_CLOSE;
extern int                RsiPeriod = 14;
extern ENUM_APPLIED_PRICE RsiPrice  = PRICE_CLOSE;
extern ENUM_MA_METHOD     MaMode    = MODE_EMA;

double signup[];
double signdn[];
double trend[];
string indicatorFileName;
bool   returnBars;

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,signup); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,signdn); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,trend);
      indicatorFileName = WindowExpertName();
      returnBars        = TimeFrame==-99; 
      TimeFrame         = MathMax(TimeFrame,_Period);
      IndicatorShortName(timeFrameToString(TimeFrame)+" ema + rsi binary ("+EmaFast+","+EmaSlow+","+RsiPeriod+")");
   return(0);
}
int deinit() { return(0); }

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars < 0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);
         if (TimeFrame!=_Period) limit = (int)MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-98,0,0)*TimeFrame/Period()));

   //
   //
   //
   //
   //

   for(int i = limit; i >= 0; i--)
   {
      int y = iBarShift(NULL,TimeFrame,Time[i]);
      double macd = iMA(NULL,TimeFrame,EmaFast,0,MaMode,EmaPrice,y)-iMA(NULL,TimeFrame,EmaSlow,0,MaMode,EmaPrice,y);
      double rsi  = iRSI(NULL,TimeFrame,RsiPeriod,RsiPrice,y);
         signup[i] = EMPTY_VALUE;
         signdn[i] = EMPTY_VALUE;
         trend[i]  = trend[i+1];
            if (macd > 0 && rsi > 50) { trend[i]=  1; signup[i] =  1; }
            if (macd < 0 && rsi < 50) { trend[i]= -1; signdn[i] = -1; }
   }
   return(0);
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

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("");
}