//+------------------------------------------------------------------+
//|                                                           rsi ma |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "www.forex-station.com"
#property link      "www.forex-station.com"

#property indicator_separate_window
#property indicator_buffers    2
#property indicator_color1     clrBlue
#property indicator_color2     clrOrange
#property indicator_width1     2
#property indicator_minimum    0
#property indicator_maximum    100
#property indicator_level1     70
#property indicator_level2     50
#property indicator_level3     30

//
//
//
//
//

extern string TimeFrame           = "Current time frame";
enum enPrices
{
   pr_close,      // Close
   pr_open,       // Open
   pr_high,       // High
   pr_low,        // Low
   pr_median,     // Median
   pr_typical,    // Typical
   pr_weighted,   // Weighted
   pr_average,    // Average (high+low+open+close)/4
   pr_medianb,    // Average median body (open+close)/2
   pr_tbiased,    // Trend biased price
   pr_tbiased2,   // Trend biased (extreme) price
   pr_haclose,    // Heiken ashi close
   pr_haopen ,    // Heiken ashi open
   pr_hahigh,     // Heiken ashi high
   pr_halow,      // Heiken ashi low
   pr_hamedian,   // Heiken ashi median
   pr_hatypical,  // Heiken ashi typical
   pr_haweighted, // Heiken ashi weighted
   pr_haaverage,  // Heiken ashi average
   pr_hamedianb,  // Heiken ashi median body
   pr_hatbiased,  // Heiken ashi trend biased price
   pr_hatbiased2, // Heiken ashi trend biased (extreme) price
   pr_habclose,   // Heiken ashi (better formula) close
   pr_habopen ,   // Heiken ashi (better formula) open
   pr_habhigh,    // Heiken ashi (better formula) high
   pr_hablow,     // Heiken ashi (better formula) low
   pr_habmedian,  // Heiken ashi (better formula) median
   pr_habtypical, // Heiken ashi (better formula) typical
   pr_habweighted,// Heiken ashi (better formula) weighted
   pr_habaverage, // Heiken ashi (better formula) average
   pr_habmedianb, // Heiken ashi (better formula) median body
   pr_habtbiased, // Heiken ashi (better formula) trend biased price
   pr_habtbiased2 // Heiken ashi (better formula) trend biased (extreme) price
};
extern int    RsiPeriod            = 5;
input enPrices RSIPrice            = pr_haclose;        // RSI price to use
extern int    MaPeriod             = 3;
enum enMaTypes
{
   
   ma_ema,     // Exponential moving average
   ma_emaf,    // Fast exponential moving average - FEMA
   ma_lwma,    // Linear weighted MA
   ma_sma,     // Simple moving average
   ma_smma,    // Smoothed MA
   ma_zldema,  // Zero lag dema
   ma_zltema   // Zero lag tema
};
extern enMaTypes  MaType           = ma_ema;
extern bool   alertsOn             = false;
extern bool   alertsOnCurrent      = true;
extern bool   alertsMessage        = true;
extern bool   alertsSound          = false;
extern bool   alertsNotify         = false;
extern bool   alertsEmail          = false;
extern bool   ShowArrows           = true;
extern string arrowsIdentifier     = "rsima Arrows1";
extern double arrowsUpperGap       = 1.0;
extern double arrowsLowerGap       = 1.0;
extern color  arrowsUpColor        = clrLimeGreen;
extern color  arrowsDnColor        = clrRed;
extern int    arrowsUpCode         = 241;
extern int    arrowsDnCode         = 242;
extern bool   Interpolate          = true;

//
//
//
//
//

double rsi[];
double rsiMa[];
double trend[];

//
//
//
//
//

string indicatorFileName;
int    timeFrame;
bool   returnBars;
bool   calculateValue;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
  IndicatorBuffers(3);
    SetIndexBuffer(0,rsi);
    SetIndexBuffer(1,rsiMa);
    SetIndexBuffer(2,trend);
    
    //
    //
    //
    //
    //
      
      indicatorFileName = WindowExpertName();
      calculateValue    = (TimeFrame=="calculateValue"); if (calculateValue) return(0);
      returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
      timeFrame         = stringToTimeFrame(TimeFrame);
      
    //
    //
    //
    //
    //
      
    IndicatorShortName(timeFrameToString(timeFrame)+"  RSI  "+getAvgName(MaType)+" ("+RsiPeriod+","+MaPeriod+")");
 
   return(0);
}

int deinit() 
{  
   deleteArrows(); 
   
return(0); 
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,r,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { rsi[0] = limit+1; return(0); }

   //
   //
   //
   //
   //

   if (calculateValue || timeFrame==Period())
   {     
         for(i = limit, r=Bars-i-1; i >= 0; i--,r++) 
         {
            rsi[i]   = iRsi(getPrice(RSIPrice,Open,Close,High,Low,i,Bars),RsiPeriod,i);
            rsiMa[i] = iCustomMa(MaType,rsi[i],MaPeriod,i,Bars); 
            trend[i] = trend[i+1]; 
	         if (rsi[i] > rsiMa[i]) trend[i]= 1; 
	         if (rsi[i] < rsiMa[i]) trend[i]=-1; 
	         
	         //
	         //
	         //
	         //
	         //
	         
	         if (ShowArrows)
            {
              deleteArrow(Time[i]);
              if (trend[i] != trend[i+1])
              {
                if (trend[i] == 1)  drawArrow(i,arrowsUpColor,arrowsUpCode,false);
                if (trend[i] ==-1)  drawArrow(i,arrowsDnColor,arrowsDnCode, true);
              }
            }
        }    
        manageAlerts();
       return(0);
       }
          
       //
       //
       //
       //
       //
  
       limit = MathMax(limit,MathMin(Bars,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
       for (i=limit;i>=0; i--)
       {
          int y = iBarShift(NULL,timeFrame,Time[i]);
              rsi[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RsiPeriod,RSIPrice,MaPeriod,MaType,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,0,y);
              rsiMa[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RsiPeriod,RSIPrice,MaPeriod,MaType,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,1,y);
              
              if (timeFrame <= Period() || y==iBarShift(NULL,timeFrame,Time[i-1])) continue;
               if (!Interpolate) continue;

            //
            //
            //

            datetime time = iTime(NULL,timeFrame,y);
               for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;	
               double factor = 1.0 / n;
               for(int k = 1; k < n; k++)
               {
                  rsi[i+k]   = k*factor*rsi[i+n]   + (1.0-k*factor)*rsi[i];
                  rsiMa[i+k] = k*factor*rsiMa[i+n] + (1.0-k*factor)*rsiMa[i];
               }
               
       }
return(0);
}

//------------------------------------------------------------------
//                                                                  
//------------------------------------------------------------------

string getAvgName(int method)
{
      switch(method)
      {
         case ma_ema:    return("EMA");
         case ma_lwma:   return("LWMA");
         case ma_sma:    return("SMA");
         case ma_smma:   return("SMMA");
         case ma_emaf:   return("FEMA");
         case ma_zldema: return("Zero lag DEMA");
         case ma_zltema: return("Zero lag TEMA");
      }
return("");      
}

//------------------------------------------------------------------
//                                                                  
//------------------------------------------------------------------

#define _maInstances 1
#define _maWorkBufferx1 1*_maInstances
#define _maWorkBufferx2 2*_maInstances
#define _maWorkBufferx3 3*_maInstances
#define _maWorkBufferx4 4*_maInstances
#define _maWorkBufferx6 6*_maInstances

double iCustomMa(int mode, double price, double length, int r, int bars, int instanceNo=0)
{
   r = bars-r-1;
   switch (mode)
   {
      case ma_ema   : return(iEma(price,length,r,bars,instanceNo));
      case ma_emaf  : return(iEmaf(price,length,r,bars,instanceNo));
      case ma_lwma  : return(iLwma(price,(int)ceil(length),r,bars,instanceNo));
      case ma_sma   : return(iSma(price,(int)ceil(length),r,bars,instanceNo));
      case ma_smma  : return(iSmma(price,(int)ceil(length),r,bars,instanceNo));
      case ma_zldema: return(iZlDema(price,length,r,bars,instanceNo));
      case ma_zltema: return(iZlTema(price,length,r,bars,instanceNo));
      default       : return(price);
   }
}

//
//
//
//
//

double workSma[][_maWorkBufferx1];
double iSma(double price, int period, int r, int _bars, int instanceNo=0)
{
   if (ArrayRange(workSma,0)!= _bars) ArrayResize(workSma,_bars);

   workSma[r][instanceNo+0] = price;
   double avg = price; int k=1;  for(; k<period && (r-k)>=0; k++) avg += workSma[r-k][instanceNo+0];  
   return(avg/(double)k);
}

//
//
//
//
//

double workEma[][_maWorkBufferx1];
double iEma(double price, double period, int r, int _bars, int instanceNo=0)
{
   if (ArrayRange(workEma,0)!= _bars) ArrayResize(workEma,_bars);

   workEma[r][instanceNo] = price;
   if (r>0 && period>1)
          workEma[r][instanceNo] = workEma[r-1][instanceNo]+(2.0/(1.0+period))*(price-workEma[r-1][instanceNo]);
   return(workEma[r][instanceNo]);
}

//
//
//
//
//

double workSmma[][_maWorkBufferx1];
double iSmma(double price, double period, int r, int _bars, int instanceNo=0)
{
   if (ArrayRange(workSmma,0)!= _bars) ArrayResize(workSmma,_bars);

   workSmma[r][instanceNo] = price;
   if (r>1 && period>1)
          workSmma[r][instanceNo] = workSmma[r-1][instanceNo]+(price-workSmma[r-1][instanceNo])/period;
   return(workSmma[r][instanceNo]);
}

//
//
//
//
//

double workLwma[][_maWorkBufferx1];
double iLwma(double price, double period, int r, int _bars, int instanceNo=0)
{
   if (ArrayRange(workLwma,0)!= _bars) ArrayResize(workLwma,_bars);
   
   workLwma[r][instanceNo] = price; if (period<=1) return(price);
      double sumw = period;
      double sum  = period*price;

      for(int k=1; k<period && (r-k)>=0; k++)
      {
         double weight = period-k;
                sumw  += weight;
                sum   += weight*workLwma[r-k][instanceNo];  
      }             
      return(sum/sumw);
}

//
//
//

double workEmaf[][_maWorkBufferx1];
double iEmaf(double price, double period, int r, int bars, int instanceNo=0)
{
   if (period<=1) return(price);
   if (ArrayRange(workEmaf,0)!= bars) ArrayResize(workEmaf,bars);

   //
   //
   //
      
   workEmaf[r][instanceNo] = price;
   if (r>0 && period>1)
          workEmaf[r][instanceNo] = workEmaf[r-1][instanceNo]+(2.0/(2.0+(period-1.0)/2.0))*(price-workEmaf[r-1][instanceNo]);
   return(workEmaf[r][instanceNo]);
}

//
//
//
//
//

double workZlDema[][_maWorkBufferx4];
#define _zdema11 0
#define _zdema21 1
#define _zdema12 2
#define _zdema22 3

double iZlDema(double price, double period, int r, int bars, int instanceNo=0)
{
   if (ArrayRange(workZlDema,0)!= bars) ArrayResize(workZlDema,bars); instanceNo*=4;

   //
   //
   //
   //
   //

   workZlDema[r][_zdema11+instanceNo] = price;
   workZlDema[r][_zdema21+instanceNo] = price;
   workZlDema[r][_zdema12+instanceNo] = price;
   workZlDema[r][_zdema22+instanceNo] = price;
   if (r>0 && period>1)
   {
      double alpha = 2.0 / (1.0+period);
       workZlDema[r][_zdema11+instanceNo] = workZlDema[r-1][_zdema11+instanceNo]+alpha*(price                             -workZlDema[r-1][_zdema11+instanceNo]);
       workZlDema[r][_zdema21+instanceNo] = workZlDema[r-1][_zdema21+instanceNo]+alpha*(workZlDema[r][_zdema11+instanceNo]-workZlDema[r-1][_zdema21+instanceNo]);
       double dema1 = 2.0*workZlDema[r][_zdema11+instanceNo]-workZlDema[r][_zdema21+instanceNo];

       workZlDema[r][_zdema12+instanceNo] = workZlDema[r-1][_zdema12+instanceNo]+alpha*(dema1                             -workZlDema[r-1][_zdema12+instanceNo]);
       workZlDema[r][_zdema22+instanceNo] = workZlDema[r-1][_zdema22+instanceNo]+alpha*(workZlDema[r][_zdema12+instanceNo]-workZlDema[r-1][_zdema22+instanceNo]);
       double dema2 = 2.0*workZlDema[r][_zdema12+instanceNo]-workZlDema[r][_zdema22+instanceNo];
       return(2.0*dema1-dema2);
   }
   return(price);
}

//
//
//
//
//

double workZlTema[][_maWorkBufferx6];
#define _ztema11 0
#define _ztema21 1
#define _ztema31 2
#define _ztema12 3
#define _ztema22 4
#define _ztema32 5

double iZlTema(double price, double period, int r, int bars, int instanceNo=0)
{
   if (ArrayRange(workZlTema,0)!= bars) ArrayResize(workZlTema,bars); instanceNo*=6;

   //
   //
   //
   //
   //
      
   workZlTema[r][_ztema11+instanceNo] = price;
   workZlTema[r][_ztema21+instanceNo] = price;
   workZlTema[r][_ztema31+instanceNo] = price;
   workZlTema[r][_ztema12+instanceNo] = price;
   workZlTema[r][_ztema22+instanceNo] = price;
   workZlTema[r][_ztema32+instanceNo] = price;
   if (r>0 && period>1)
   {
      double alpha = 2.0 / (1.0+period);
       workZlTema[r][_ztema11+instanceNo] = workZlTema[r-1][_ztema11+instanceNo]+alpha*(price                             -workZlTema[r-1][_ztema11+instanceNo]);
       workZlTema[r][_ztema21+instanceNo] = workZlTema[r-1][_ztema21+instanceNo]+alpha*(workZlTema[r][_ztema11+instanceNo]-workZlTema[r-1][_ztema21+instanceNo]);
       workZlTema[r][_ztema31+instanceNo] = workZlTema[r-1][_ztema31+instanceNo]+alpha*(workZlTema[r][_ztema21+instanceNo]-workZlTema[r-1][_ztema31+instanceNo]); 
       double tema1 = workZlTema[r][_ztema31+instanceNo]+3.0*(workZlTema[r][_ztema11+instanceNo]-workZlTema[r][_ztema21+instanceNo]);

       workZlTema[r][_ztema12+instanceNo] = workZlTema[r-1][_ztema12+instanceNo]+alpha*(tema1                             -workZlTema[r-1][_ztema12+instanceNo]);
       workZlTema[r][_ztema22+instanceNo] = workZlTema[r-1][_ztema22+instanceNo]+alpha*(workZlTema[r][_ztema12+instanceNo]-workZlTema[r-1][_ztema22+instanceNo]);
       workZlTema[r][_ztema32+instanceNo] = workZlTema[r-1][_ztema32+instanceNo]+alpha*(workZlTema[r][_ztema22+instanceNo]-workZlTema[r-1][_ztema32+instanceNo]); 
       double tema2 = workZlTema[r][_ztema32+instanceNo]+3.0*(workZlTema[r][_ztema12+instanceNo]-workZlTema[r][_ztema22+instanceNo]);
       return(2.0*tema1-tema2);
   }
   return(price);
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

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);
}

//
//
//
//
//

void drawArrow(int i,color theColor,int theCode,bool up)
{
   string name = arrowsIdentifier+":"+Time[i];
   double gap  = iATR(NULL,0,20,i);   
   
      //
      //
      //
      //
      //
      
      ObjectCreate(name,OBJ_ARROW,0,Time[i],0);
         ObjectSet(name,OBJPROP_ARROWCODE,theCode);
         ObjectSet(name,OBJPROP_COLOR,theColor);
         if (up)
               ObjectSet(name,OBJPROP_PRICE1,High[i] + arrowsUpperGap * gap);
         else  ObjectSet(name,OBJPROP_PRICE1,Low[i]  - arrowsLowerGap * gap);
}

//
//
//
//
//

void deleteArrows()
{
   string lookFor       = arrowsIdentifier+":";
   int    lookForLength = StringLen(lookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i);
         if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
   }
}

//
//
//
//
//

void deleteArrow(datetime time)
{
   string lookFor = arrowsIdentifier+":"+time; ObjectDelete(lookFor);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

void manageAlerts()
{
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1;
      if (trend[whichBar] != trend[whichBar+1])
      {
            if (trend[whichBar] ==  1) doAlert(whichBar," crossed signal line up");
            if (trend[whichBar] == -1) doAlert(whichBar," crossed signal line 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 =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," - ",timeFrameToString(timeFrame)+" rsi ma ",doWhat);
          if (alertsMessage) Alert(message);
          if (alertsNotify)  SendNotification(message);
          if (alertsEmail)   SendMail(StringConcatenate(Symbol()," rsi ma "),message);
          if (alertsSound)   PlaySound("alert2.wav");
   }
}

//------------------------------------------------------------------
//                                                                  
//------------------------------------------------------------------

double workRsi[][3];
#define _price  0
#define _change 1
#define _changa 2

double iRsi(double price, double period, int i, int instanceNo=0)
{
   if (ArrayRange(workRsi,0)!=Bars) ArrayResize(workRsi,Bars);
      int z = instanceNo*3; 
      int r = Bars-i-1;
   
   //
   //
   //
   //
   //
   
   workRsi[r][z+_price] = price;
         double alpha = 1.0/period; 
         if (r<period)
            {
               int k; double sum = 0; for (k=0; k<period && (r-k-1)>=0; k++) sum += MathAbs(workRsi[r-k][z+_price]-workRsi[r-k-1][z+_price]);
                  workRsi[r][z+_change] = (workRsi[r][z+_price]-workRsi[0][z+_price])/MathMax(k,1);
                  workRsi[r][z+_changa] =                                         sum/MathMax(k,1);
            }
         else
            {
               double change = workRsi[r][z+_price]-workRsi[r-1][z+_price];
                               workRsi[r][z+_change] = workRsi[r-1][z+_change] + alpha*(        change  - workRsi[r-1][z+_change]);
                               workRsi[r][z+_changa] = workRsi[r-1][z+_changa] + alpha*(MathAbs(change) - workRsi[r-1][z+_changa]);
            }
         if (workRsi[r][z+_changa] != 0)
               return(50.0*(workRsi[r][z+_change]/workRsi[r][z+_changa]+1));
         else  return(50.0);
   
}

//
//
//

#define _prHABF(_prtype) (_prtype>=pr_habclose && _prtype<=pr_habtbiased2)
#define _priceInstances     1
#define _priceInstancesSize 4
double workHa[][_priceInstances*_priceInstancesSize];
double getPrice(int tprice, const double& open[], const double& close[], const double& high[], const double& low[], int i, int bars, int instanceNo=0)
{
  if (tprice>=pr_haclose)
   {
      if (ArrayRange(workHa,0)!= bars) ArrayResize(workHa,bars); instanceNo*=_priceInstancesSize; int r = bars-i-1;
         
         //
         //
         //
         //
         //
         
         double haOpen  = (r>0) ? (workHa[r-1][instanceNo+2] + workHa[r-1][instanceNo+3])/2.0 : (open[i]+close[i])/2;;
         double haClose = (open[i]+high[i]+low[i]+close[i]) / 4.0;
         if (_prHABF(tprice))
               if (high[i]!=low[i])
                     haClose = (open[i]+close[i])/2.0+(((close[i]-open[i])/(high[i]-low[i]))*fabs((close[i]-open[i])/2.0));
               else  haClose = (open[i]+close[i])/2.0; 
         double haHigh  = fmax(high[i], fmax(haOpen,haClose));
         double haLow   = fmin(low[i] , fmin(haOpen,haClose));

         //
         //
         //
         //
         //
         
         if(haOpen<haClose) { workHa[r][instanceNo+0] = haLow;  workHa[r][instanceNo+1] = haHigh; } 
         else               { workHa[r][instanceNo+0] = haHigh; workHa[r][instanceNo+1] = haLow;  } 
                              workHa[r][instanceNo+2] = haOpen;
                              workHa[r][instanceNo+3] = haClose;
         //
         //
         //
         //
         //
         
         switch (tprice)
         {
            case pr_haclose:
            case pr_habclose:    return(haClose);
            case pr_haopen:   
            case pr_habopen:     return(haOpen);
            case pr_hahigh: 
            case pr_habhigh:     return(haHigh);
            case pr_halow:    
            case pr_hablow:      return(haLow);
            case pr_hamedian:
            case pr_habmedian:   return((haHigh+haLow)/2.0);
            case pr_hamedianb:
            case pr_habmedianb:  return((haOpen+haClose)/2.0);
            case pr_hatypical:
            case pr_habtypical:  return((haHigh+haLow+haClose)/3.0);
            case pr_haweighted:
            case pr_habweighted: return((haHigh+haLow+haClose+haClose)/4.0);
            case pr_haaverage:  
            case pr_habaverage:  return((haHigh+haLow+haClose+haOpen)/4.0);
            case pr_hatbiased:
            case pr_habtbiased:
               if (haClose>haOpen)
                     return((haHigh+haClose)/2.0);
               else  return((haLow+haClose)/2.0);        
            case pr_hatbiased2:
            case pr_habtbiased2:
               if (haClose>haOpen)  return(haHigh);
               if (haClose<haOpen)  return(haLow);
                                    return(haClose);        
         }
   }
   
   //
   //
   //
   //
   //
   
   switch (tprice)
   {
      case pr_close:     return(close[i]);
      case pr_open:      return(open[i]);
      case pr_high:      return(high[i]);
      case pr_low:       return(low[i]);
      case pr_median:    return((high[i]+low[i])/2.0);
      case pr_medianb:   return((open[i]+close[i])/2.0);
      case pr_typical:   return((high[i]+low[i]+close[i])/3.0);
      case pr_weighted:  return((high[i]+low[i]+close[i]+close[i])/4.0);
      case pr_average:   return((high[i]+low[i]+close[i]+open[i])/4.0);
      case pr_tbiased:   
               if (close[i]>open[i])
                     return((high[i]+close[i])/2.0);
               else  return((low[i]+close[i])/2.0);        
      case pr_tbiased2:   
               if (close[i]>open[i]) return(high[i]);
               if (close[i]<open[i]) return(low[i]);
                                     return(close[i]);        
   }
   return(0);
}