//------------------------------------------------------------------
#property copyright "lambic & dr.house7 & mladen"
#property link      "www.forex-tsd.com"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 7
#property indicator_color1  DimGray  
#property indicator_color2  DarkGray
#property indicator_color3  Red
#property indicator_color4  LimeGreen
#property indicator_color5  Red
#property indicator_color6  LimeGreen
#property indicator_color7  Red
#property indicator_width1  2
#property indicator_width2  2
#property indicator_width6  3
#property indicator_width7  3
#property indicator_level1  0

//
//
//
//
//

extern string TimeFrame       = "Current time frame";
extern int    price           = 1;
extern int    MaMethod        = MODE_EMA;
extern int    SignalPeriod    = 9;
extern int    SignalMethod    = MODE_EMA;
extern double AccStep         = 0.01;
extern double AccLimit        = 0.1;

extern bool   alertsOn        = true;
extern bool   alertsOnCurrent = false;
extern bool   alertsMessage   = true;
extern bool   alertsSound     = false;
extern bool   alertsEmail     = false;
extern bool   alertsNotify    = false;

double coef = 0.05;

//
//
//
//
//

double OBVh[];
double OBV[];
double signal[];
double sarUp[];
double sarDn[];
double saraUp[];
double saraDn[];

string indicatorFileName;
int    timeFrame;
bool   returnBars;
bool   calculateValue;

//------------------------------------------------------------------
//
//------------------------------------------------------------------
// 
//
//
//
//

int init()
{
   SetIndexBuffer(0,OBVh);
   SetIndexBuffer(1,OBV); 
   SetIndexBuffer(2,signal); 
   SetIndexBuffer(3,sarUp);  SetIndexStyle(3,DRAW_ARROW); SetIndexArrow(3,159);
   SetIndexBuffer(4,sarDn);  SetIndexStyle(4,DRAW_ARROW); SetIndexArrow(4,159);
   SetIndexBuffer(5,saraUp); SetIndexStyle(5,DRAW_ARROW); SetIndexArrow(5,159);
   SetIndexBuffer(6,saraDn); SetIndexStyle(6,DRAW_ARROW); SetIndexArrow(6,159);

        indicatorFileName = WindowExpertName();
        calculateValue    = (TimeFrame=="calculateValue"); if (calculateValue) return(0);
        returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
        timeFrame         = stringToTimeFrame(TimeFrame);

   IndicatorShortName(timeFrameToString(timeFrame)+" Parabolic OBV("+price+")");
return(0);
}

//
//
//
//
//

int start()
{
   int i,limit,counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { OBVh[0] = limit+1; return(0); }

   //
   //
   //
   //
   //

   if (calculateValue || timeFrame==Period())
   {
      for(i=limit; i>=0; i--) 
      {
         double sarClose;
         double sarOpen;
         double sarPosition;
         double sarChange;
         OBV[i] = iOBV(NULL,0,price,i);
         iParabolic(OBV[i],OBV[i],AccStep,AccLimit,sarClose,sarOpen,sarPosition,sarChange,i);
         OBVh[i]   = OBV[i];
         sarUp[i]  = EMPTY_VALUE;
         sarDn[i]  = EMPTY_VALUE;
         saraUp[i] = EMPTY_VALUE;
         saraDn[i] = EMPTY_VALUE;
      
         if (sarPosition==1)
               sarUp[i] = sarClose;
         else  sarDn[i] = sarClose;
         if (sarChange!=0)
            if (sarPosition==1)
                  saraUp[i] = sarClose;
            else  saraDn[i] = sarClose;
      }   
      for(i=limit; i>=0; i--) signal[i] = iMAOnArray(OBV,0,SignalPeriod,0,SignalMethod,i);
      manageAlerts();
      return(0);
   }      
   
   //
   //
   //
   //
   //

   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   for (i=limit;i>=0; i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
      int x = iBarShift(NULL,timeFrame,Time[i+1]);
         OBVh[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",price,MaMethod,SignalPeriod,SignalMethod,AccStep,AccLimit,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,0,y);
         OBV[i]    = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",price,MaMethod,SignalPeriod,SignalMethod,AccStep,AccLimit,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,1,y);
         signal[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",price,MaMethod,SignalPeriod,SignalMethod,AccStep,AccLimit,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,2,y);
         sarUp[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",price,MaMethod,SignalPeriod,SignalMethod,AccStep,AccLimit,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,3,y);
         sarDn[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",price,MaMethod,SignalPeriod,SignalMethod,AccStep,AccLimit,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,4,y);
         saraUp[i] = EMPTY_VALUE;
         saraDn[i] = EMPTY_VALUE;
         if (x!=y)
         {
            saraUp[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",price,MaMethod,SignalPeriod,SignalMethod,AccStep,AccLimit,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,5,y);
            saraDn[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",price,MaMethod,SignalPeriod,SignalMethod,AccStep,AccLimit,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,6,y);
         }            
   }
   return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

double work[][7];
#define _high     0
#define _low      1
#define _ohigh    2
#define _olow     3
#define _open     4
#define _position 5
#define _af       6


void iParabolic(double high, double low, double step, double limit, double& pClose, double& pOpen, double& pPosition, double& pChange, int i)
{
   if (ArrayRange(work,0)!=Bars) ArrayResize(work,Bars); i = Bars-i-1;
   
   //
   //
   //
   //
   //

      pChange = 0;
         work[i][_ohigh]    = high;
         work[i][_olow]     = low;
            if (i<1)
               {
                  work[i][_high]     = high;
                  work[i][_low]      = low;
                  work[i][_open]     = high;
                  work[i][_position] = -1;
                  return;
               }
         work[i][_open]     = work[i-1][_open];
         work[i][_af]       = work[i-1][_af];
         work[i][_position] = work[i-1][_position];
         work[i][_high]     = MathMax(work[i-1][_high],high);
         work[i][_low]      = MathMin(work[i-1][_low] ,low );

   //
   //
   //
   //
   //
   
   if (work[i][_position] == 1)
      if (low<=work[i][_open])
         {
            work[i][_position] = -1;
               pChange = -1;
               pClose  = work[i][_high];
                         work[i][_high] = high;
                         work[i][_low]  = low;
                         work[i][_af]   = step;
                         work[i][_open] = pClose + work[i][_af]*(work[i][_low]-pClose);
                            if (work[i][_open]<work[i  ][_ohigh]) work[i][_open] = work[i  ][_ohigh];
                            if (work[i][_open]<work[i-1][_ohigh]) work[i][_open] = work[i-1][_ohigh];
         }
      else
         {
               pClose = work[i][_open];
                    if (work[i][_high]>work[i-1][_high] && work[i][_af]<limit) work[i][_af] = MathMin(work[i][_af]+step,limit);
                        work[i][_open] = pClose + work[i][_af]*(work[i][_high]-pClose);
                            if (work[i][_open]>work[i  ][_olow]) work[i][_open] = work[i  ][_olow];
                            if (work[i][_open]>work[i-1][_olow]) work[i][_open] = work[i-1][_olow];
         }
   else
      if (high>=work[i][_open])
         {
            work[i][_position] = 1;
               pChange = 1;
               pClose  = work[i][_low];
                         work[i][_high] = high;
                         work[i][_low]  = low;
                         work[i][_af]   = step;
                         work[i][_open] = pClose + work[i][_af]*(work[i][_high]-pClose);
                            if (work[i][_open]>work[i  ][_olow]) work[i][_open] = work[i  ][_olow];
                            if (work[i][_open]>work[i-1][_olow]) work[i][_open] = work[i-1][_olow];
         }
      else
         {
               pClose = work[i][_open];
               if (work[i][_low]<work[i-1][_low] && work[i][_af]<limit) work[i][_af] = MathMin(work[i][_af]+step,limit);
                   work[i][_open] = pClose + work[i][_af]*(work[i][_low]-pClose);
                            if (work[i][_open]<work[i  ][_ohigh]) work[i][_open] = work[i  ][_ohigh];
                            if (work[i][_open]<work[i-1][_ohigh]) work[i][_open] = work[i-1][_ohigh];
         }

   //
   //
   //
   //
   //
   
   pOpen     = work[i][_open];
   pPosition = work[i][_position];
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

void manageAlerts()
{
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1;
      if (saraUp[whichBar] != EMPTY_VALUE || saraDn[whichBar] != EMPTY_VALUE)
      {
         if (saraUp[whichBar] !=  EMPTY_VALUE) doAlert(whichBar,"up");
         if (saraDn[whichBar] !=  EMPTY_VALUE) doAlert(whichBar,"down");
      }
   }
}

//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
   if (previousAlert != doWhat || previousTime != Time[forBar]) {
       previousAlert  = doWhat;
       previousTime   = Time[forBar];

       //
       //
       //
       //
       //

       message = _Symbol+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" parabolic Sar OBV trend changed to "+doWhat;
          if (alertsMessage) Alert(message);
          if (alertsEmail)   SendMail(_Symbol+"parabolic sar OBV",message);
          if (alertsNotify)  SendNotification(message);
          if (alertsSound)   PlaySound("alert2.wav");
   }
}

//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
//
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   tfs = stringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}
string stringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int tchar = StringGetChar(s, length);
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                     s = StringSetChar(s, length, tchar - 32);
         else if(tchar > -33 && tchar < 0)
                     s = StringSetChar(s, length, tchar + 224);
   }
return(s);
}