Navigation´╝ÜHome > Content >

Indi#33.mq4

Time: 2013-08-24 | Download file:Indi#33.mq4

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "sss"
#property link      "sss"

#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1  SteelBlue
#property indicator_color2  Gold
#property indicator_color3  SteelBlue
#property indicator_color4  DeepSkyBlue
#property indicator_color5  Red
#property indicator_width2  2
#property indicator_width4  2
#property indicator_levelcolor DimGray

//
//
//
//
//

extern string TimeFrame                = "Current time frame";
extern int    RsiPeriod                = 13;
extern int    RsiPrice                 = PRICE_CLOSE;
extern int    RsiPriceLinePeriod       = 2;
extern int    RsiPriceLineMAMode       = MODE_SMA;
extern int    RsiSignalLinePeriod      = 7;
extern int    RsiSignalLineMAMode      = MODE_SMA;
extern int    VolatilityBandPeriod     = 34;
extern int    VolatilityBandMAMode     = MODE_SMA;
extern double VolatilityBandMultiplier = 1.6185;
extern double LevelDown                = 32;
extern double LevelMiddle              = 50;
extern double LevelUp                  = 68;
extern bool   Interpolate              = true;

//
//
//
//
//

double rsi[];
double rsiPriceLine[];
double rsiSignalLine[];
double bandUp[];
double bandMiddle[];
double bandDown[];

//
//
//
//
//

string indicatorFileName;
bool   calculateValue;
bool   returnBars;
int    timeFrame;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init() 
{
   IndicatorBuffers(6);
   SetIndexBuffer(0,bandUp);
   SetIndexBuffer(1,bandMiddle);
   SetIndexBuffer(2,bandDown);
   SetIndexBuffer(3,rsiPriceLine);
   SetIndexBuffer(4,rsiSignalLine);
   SetIndexBuffer(5,rsi);

      //
      //
      //
      //
      //

      indicatorFileName = WindowExpertName();
      calculateValue    = (TimeFrame=="calculateValue"); if (calculateValue) return(0);
      returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
      timeFrame         = stringToTimeFrame(TimeFrame);

      //
      //
      //
      //
      //
      
   SetLevelValue(0,LevelUp);
   SetLevelValue(1,LevelMiddle);
   SetLevelValue(2,LevelDown);
   IndicatorShortName(timeFrameToString(timeFrame)+" Traders dynamic index");
   return (0);
}
  
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { bandUp[0] = limit+1; return(0); }

   //
   //
   //
   //
   //

   if (calculateValue || timeFrame == Period())
   {
      for(i=limit; i>=0; i--) rsi[i] = iRSI(NULL,0,RsiPeriod,RsiPrice,i);
      for(i=limit; i>=0; i--)
      {
         rsiPriceLine[i]  = iMAOnArray(rsi,0,RsiPriceLinePeriod,0,RsiPriceLineMAMode,i);
         rsiSignalLine[i] = iMAOnArray(rsi,0,RsiSignalLinePeriod,0,RsiSignalLineMAMode,i);
             double deviation = iStdDevOnArray(rsi,0,VolatilityBandPeriod,0,VolatilityBandMAMode,i);
             double average   = iMAOnArray(rsi,0,VolatilityBandPeriod,0,VolatilityBandMAMode,i);
                bandUp[i]     = average+VolatilityBandMultiplier*deviation;
                bandDown[i]   = average-VolatilityBandMultiplier*deviation;
                bandMiddle[i] = average;
      }
      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]);
         bandUp[i]        = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RsiPeriod,RsiPrice,RsiPriceLinePeriod,RsiPriceLineMAMode,RsiSignalLinePeriod,RsiSignalLineMAMode,VolatilityBandPeriod,VolatilityBandMAMode,VolatilityBandMultiplier,0,y);
         bandMiddle[i]    = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RsiPeriod,RsiPrice,RsiPriceLinePeriod,RsiPriceLineMAMode,RsiSignalLinePeriod,RsiSignalLineMAMode,VolatilityBandPeriod,VolatilityBandMAMode,VolatilityBandMultiplier,1,y);
         bandDown[i]      = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RsiPeriod,RsiPrice,RsiPriceLinePeriod,RsiPriceLineMAMode,RsiSignalLinePeriod,RsiSignalLineMAMode,VolatilityBandPeriod,VolatilityBandMAMode,VolatilityBandMultiplier,2,y);
         rsiPriceLine[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RsiPeriod,RsiPrice,RsiPriceLinePeriod,RsiPriceLineMAMode,RsiSignalLinePeriod,RsiSignalLineMAMode,VolatilityBandPeriod,VolatilityBandMAMode,VolatilityBandMultiplier,3,y);
         rsiSignalLine[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RsiPeriod,RsiPrice,RsiPriceLinePeriod,RsiPriceLineMAMode,RsiSignalLinePeriod,RsiSignalLineMAMode,VolatilityBandPeriod,VolatilityBandMAMode,VolatilityBandMultiplier,4,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;	
            for(int k = 1; k < n; k++)
            {
               bandUp[i+k]        = bandUp[i]        + (bandUp[i+n]        - bandUp[i]       )*k/n;
               bandMiddle[i+k]    = bandMiddle[i]    + (bandMiddle[i+n]    - bandMiddle[i]   )*k/n;
               bandDown[i+k]      = bandDown[i]      + (bandDown[i+n]      - bandDown[i]     )*k/n;
               rsiPriceLine[i+k]  = rsiPriceLine[i]  + (rsiPriceLine[i+n]  - rsiPriceLine[i] )*k/n;
               rsiSignalLine[i+k] = rsiSignalLine[i] + (rsiSignalLine[i+n] - rsiSignalLine[i])*k/n;
            }               
   }

   //
   //
   //
   //
   //
   return(0);
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

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 char111 = StringGetChar(s, length);
         if((char111 > 96 && char111 < 123) || (char111 > 223 && char111 < 256))
                     s = StringSetChar(s, length, char111 - 32);
         else if(char111 > -33 && char111 < 0)
                     s = StringSetChar(s, length, char111 + 224);
   }
   return(s);
}

Recommend