Navigation´╝ÜHome > Content >

Sentiment_zone_oscillator_histo_mtf_nmc.mq4

Time: 2018-04-26 | Download file:Sentiment_zone_oscillator_histo_mtf_nmc.mq4

//------------------------------------------------------------------
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1  Green
#property indicator_color2  Red
#property indicator_color3  Silver
#property indicator_color4  LimeGreen
#property indicator_color5  PaleVioletRed
#property indicator_color6  Goldenrod
#property indicator_style4  STYLE_DOT
#property indicator_style5  STYLE_DOT
#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  2
#property indicator_width6  2

//
//
//
//
//

extern string TimeFrame       = "Current time frame";
extern int    SZOPeriod       = 14;
extern int    SZOLongPeriod   = 30;
extern int    SZOPrice        = PRICE_CLOSE;
extern double SZOPercent      = 95;
extern int    AveragePeriod   = 14;
extern int    AverageType     = MODE_EMA;

//
//
//
//
//

double szoUp[];
double szoDn[];
double szo[];
double upz[];
double dnz[];
double avg[];
double prices[];
double trend[];

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

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   IndicatorBuffers(8);
   SetIndexBuffer(0,szoUp); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,szoDn); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,szo);
   SetIndexBuffer(3,upz);
   SetIndexBuffer(4,dnz);
   SetIndexBuffer(5,avg);
   SetIndexBuffer(6,prices);
   SetIndexBuffer(7,trend);

      //
      //
      //
      //
      //
      
         indicatorFileName = WindowExpertName();
         calculateValue    = TimeFrame=="calculateValue"; if (calculateValue) { return(0); }
         returnBars        = TimeFrame=="returnBars";     if (returnBars)     { return(0); }
         timeFrame         = stringToTimeFrame(TimeFrame);
      
      //
      //
      //
      //
      //
               
      IndicatorShortName(timeFrameToString(timeFrame)+" Sentiment zone oscillator ("+SZOPeriod+","+SZOLongPeriod+")");
   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 (returnBars) { upz[0] = MathMin(limit+1,Bars-1); return(0); }

   //
   //
   //
   //
   //

   if (calculateValue || timeFrame == Period())
   {
      for(int i=limit; i>=0; i--)
      {
         prices[i] = iMA(NULL,0,1,0,MODE_SMA,SZOPrice,i);
         double useValue = 0;
            if (prices[i]>prices[i+1]) useValue =  1;
            if (prices[i]0) trend[i] = 1; 
            if (szo[i]<0) trend[i] =-1;
            if (trend[i] == 1) szoUp[i] = szo[i]; 
            if (trend[i] ==-1) szoDn[i] = szo[i];
            
            //
            //
            //
            //
            //
            
            double hi = szo[ArrayMaximum(szo,SZOLongPeriod,i)];
            double lo = szo[ArrayMinimum(szo,SZOLongPeriod,i)];
            double rn = hi-lo;
            upz[i] = lo+rn*SZOPercent/100.0;
            dnz[i] = hi-rn*SZOPercent/100.0;
      }       
      for(i=limit; i>=0; i--) avg[i] = iMAOnArray(szo,0,AveragePeriod,0,AverageType,i);
      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]);
         szoUp[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",SZOPeriod,SZOLongPeriod,SZOPrice,SZOPercent,AveragePeriod,AverageType,0,y);
         szoDn[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",SZOPeriod,SZOLongPeriod,SZOPrice,SZOPercent,AveragePeriod,AverageType,1,y);
         szo[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",SZOPeriod,SZOLongPeriod,SZOPrice,SZOPercent,AveragePeriod,AverageType,2,y);
         upz[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",SZOPeriod,SZOLongPeriod,SZOPrice,SZOPercent,AveragePeriod,AverageType,3,y);
         dnz[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",SZOPeriod,SZOLongPeriod,SZOPrice,SZOPercent,AveragePeriod,AverageType,4,y);
         avg[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",SZOPeriod,SZOLongPeriod,SZOPrice,SZOPercent,AveragePeriod,AverageType,5,y);
   }
   return(0);
         
}


//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

double workTema[][3];
#define _ema1 0
#define _ema2 1
#define _ema3 2

double iTema(double price, double period, int r, int instanceNo=0)
{
   if (ArrayRange(workTema,0)!= Bars) ArrayResize(workTema,Bars); instanceNo*=3; r = Bars-r-1;

   //
   //
   //
   //
   //
      
   double alpha = 2.0 / (1.0+period);
          workTema[r][_ema1+instanceNo] = workTema[r-1][_ema1+instanceNo]+alpha*(price                        -workTema[r-1][_ema1+instanceNo]);
          workTema[r][_ema2+instanceNo] = workTema[r-1][_ema2+instanceNo]+alpha*(workTema[r][_ema1+instanceNo]-workTema[r-1][_ema2+instanceNo]);
          workTema[r][_ema3+instanceNo] = workTema[r-1][_ema3+instanceNo]+alpha*(workTema[r][_ema2+instanceNo]-workTema[r-1][_ema3+instanceNo]);
   return(workTema[r][_ema3+instanceNo]+3.0*(workTema[r][_ema1+instanceNo]-workTema[r][_ema2+instanceNo]));
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

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);
}

Recommend