//+------------------------------------------------------------------+
//|                                                         OSMA.mq4 |
//| 10 ноября 2008г.                                    Yuriy Tokman |
//| ICQ#:481-971-287                           yuriytokman@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Yuriy Tokman"
#property link      "yuriytokman@gmail.com"

#property indicator_separate_window

#include <hanover --- function header.mqh>

#property indicator_buffers 3

#property indicator_color1 LimeGreen
#property indicator_color2 Crimson
#property indicator_level1 50
#property indicator_levelcolor Black

extern int      period_RSI           = 9;
extern int      applied_price_RSI    = 0;
extern int      period_MA            = 3;
extern int      ma_method            = 3;
extern bool     Sound                = false; 

extern string   FontName             = "Arial";
extern color    FontColorBullish     = LimeGreen;
extern color    FontColorBearish     = Crimson;
extern int      FontSize             = 14;
extern int      TextCorner           = 3;
extern int      TextHpos             = 10;
extern int      TextVpos             = 10;
extern string   TextMask             = "'RSI = 'T-5.2";

double Buf0[], Buf1[], Buf2[];

string     IndiName = "RSI";

//+------------------------------------------------------------------+
int init()  {
//+------------------------------------------------------------------+
  IndicatorShortName(IndiName);

  SetIndexBuffer(0,Buf0);
  SetIndexBuffer(1,Buf1);
  SetIndexBuffer(2,Buf2);
//  SetIndexLabel(0, "");
//  SetIndexLabel(1, "");
//  SetIndexLabel(2, "");
  SetIndexStyle(0,DRAW_LINE,0,2);
  SetIndexStyle(1,DRAW_LINE,0,2);
  SetIndexStyle(2,DRAW_NONE);
  return(0);
}

//+------------------------------------------------------------------+
int deinit()  {
//+------------------------------------------------------------------+
  del_obj();
  return(0);
}

//+------------------------------------------------------------------+
void del_obj()  {
//+------------------------------------------------------------------+
  int k=0;
  while (k<ObjectsTotal())   {
    string objname = ObjectName(k);
    if (StringSubstr(objname,0,StringLen(IndiName)) == IndiName)  
      ObjectDelete(objname);
    else
      k++;
  }    
  return(0);
}

//+------------------------------------------------------------------+
int start()  {
//+------------------------------------------------------------------+
  int limit;
  int counted_bars=IndicatorCounted();
  if(counted_bars>0) counted_bars--;
  limit=Bars-counted_bars;
  for (int i=0; i<limit; i++)   {
    Buf2[i]=iRSI(NULL,0,period_RSI,applied_price_RSI,i);
  }
    
  for (i=0; i<limit; i++)   {
    double r = iMAOnArray(Buf2,0,period_MA,0,ma_method,i) ;
    double r1 = iMAOnArray(Buf2,0,period_MA,0,ma_method,i+1) ;
    if (r<53)  Buf1[i]=r ;
    if (r>47)  Buf0[i]=r ;
    if (Sound == true)     {
      if(r>=50 && r1<=50) {Alert("Crossing to Bullish\n\" Indicators to order e-mail: yuriytokman@gmail.com \"\nRSI= ", r);}
      if(r<=50 && r1>=50) {Alert("Crossing to Bearish\n\" Indicators to order ISQ#:481-971-287 \"\nRSI= ", r);}
    }
  }
  r = iMAOnArray(Buf2,0,period_MA,0,ma_method,0) ;
//  IndicatorShortName(IndiName + "Smoothed RSI value = "+ DoubleToStr(r,4)+"                  ");
  color FontColor = FontColorBearish;
  if (r>=50) 
    FontColor = FontColorBullish;
  PlotLabel (IndiName+"1", true, 0, TextCorner, TextHpos, TextVpos, NumberToStr(r,TextMask), FontColor, FontSize, FontName, 0, false, 0);      // Plot text label
  return(0);
}
//+------------------------------------------------------------------+
#include <hanover --- extensible functions.mqh>