Now we have Market Experts:118 Market Indicators:77 Source code:15125 ,E-mail:[email protected]

Volatility_quality_nrp_-_alerts_&_arrows.mq4

Indicators Views
//+------------------------------------------------------------------+
//|                                           Volatility quality.mq4 |
//+------------------------------------------------------------------+
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1  DeepSkyBlue
#property indicator_color2  PaleVioletRed
#property indicator_color3  PaleVioletRed
#property indicator_width1  2
#property indicator_width2  2

//
//
//
//
//

extern string TimeFrame          = "Current time frame";
extern int    Length             = 5;
extern int    Method             = 3;
extern int    Smoothing          = 1;
extern int    Filter             = 5;
extern bool   Interpolate        = true;
extern bool   arrowsVisible      = false;
extern string arrowsIdentifier   = "VQnrpArrows";
extern color  arrowsUpColor      = DeepSkyBlue;
extern color  arrowsDnColor      = Red;
extern bool   alertsOn           = false;
extern bool   alertsOnCurrent    = true;
extern bool   alertsMessage      = true;
extern bool   alertsSound        = false;
extern bool   alertsEmail        = false;

//
//
//
//
//

double UpBuffer[];
double DnBuffera[];
double DnBufferb[];
double SumVQ[];
double trend[];

//
//
//
//
//

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

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

int init()
{
   IndicatorBuffers(5);
      SetIndexBuffer(0,UpBuffer);  SetIndexDrawBegin(0,MathMax(Length,Smoothing));
      SetIndexBuffer(1,DnBuffera); SetIndexDrawBegin(1,MathMax(Length,Smoothing));
      SetIndexBuffer(2,DnBufferb); SetIndexDrawBegin(2,MathMax(Length,Smoothing));
      SetIndexBuffer(3,SumVQ);     SetIndexEmptyValue(3,0.0);
      SetIndexBuffer(4,trend);
      
      Filter    = MathMax(Filter,0);      
      Smoothing = MathMax(Smoothing,1);      

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

   IndicatorShortName(timeFrameToString(timeFrame)+" volatility quality {"+Length+","+Method+","+Smoothing+","+Filter+"|");
   return(0);
}

//
//
//
//
//

int deinit()
{
   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);
   }
   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) { UpBuffer[0] = limit+1; return(0); }

   //
   //
   //
   //
   //

   if (calculateValue || timeFrame==Period())
   {
      if (trend[limit]==-1) ClearPoint(limit,DnBuffera,DnBufferb);
      for (i=limit; i>= 0; i--)
      {
         double MH  = iMA(NULL,0,Length,0,Method,PRICE_HIGH, i);
         double ML  = iMA(NULL,0,Length,0,Method,PRICE_LOW,  i);
         double MO  = iMA(NULL,0,Length,0,Method,PRICE_OPEN, i);
         double MC  = iMA(NULL,0,Length,0,Method,PRICE_CLOSE,i);
         double MC1 = iMA(NULL,0,Length,0,Method,PRICE_CLOSE,i+Smoothing);
      
         //
         //
         //
         //
         //
      
         if (MH != ML)
               double VQ = MathAbs(((MC-MC1) / MathMax(MH-ML,MathMax(MH-MC1,MC1-ML)) + (MC-MO)/(MH-ML)) * 0.5) * ((MC-MC1 + (MC-MO)) * 0.5);
         else         VQ = 0.00;
         SumVQ[i] = SumVQ[i+1] + VQ;

         if (MathAbs(SumVQ[i]-SumVQ[i+1]) < Filter*Point) SumVQ[i]=SumVQ[i+1];

         //
         //
         //
         //
         //

         UpBuffer[i]  = SumVQ[i];
         DnBuffera[i] = EMPTY_VALUE;
         DnBufferb[i] = EMPTY_VALUE;
         trend[i]     = trend[i+1];
            if (SumVQ[i] > SumVQ[i+1]) trend[i] =  1;
            if (SumVQ[i] < SumVQ[i+1]) trend[i] = -1;
            if (trend[i] == -1) PlotPoint(i,DnBuffera,DnBufferb,UpBuffer);
               manageArrow(i);
      }
      manageAlerts();
      return(0);
   }      

   //
   //
   //
   //
   //
   
   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   if (trend[limit]==-1) ClearPoint(limit,DnBuffera,DnBufferb);
   for(i=limit; i>=0; i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         UpBuffer[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Length,Method,Smoothing,Filter,0,y);
         trend[i]     = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Length,Method,Smoothing,Filter,4,y);
         DnBuffera[i] = EMPTY_VALUE;
         DnBufferb[i] = EMPTY_VALUE;
         manageArrow(i);
            
         //
         //
         //
         //
         //
      
         if (!Interpolate || y==iBarShift(NULL,timeFrame,Time[i-1])) 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++)
               UpBuffer[i+k]  = UpBuffer[i]  + (UpBuffer[i+n] -UpBuffer[i])*k/n;
   }
   for(i=limit; i>=0; i--) if (trend[i]==-1) PlotPoint(i,DnBuffera,DnBufferb,UpBuffer);
   
   //
   //
   //
   //
   //
   
   manageAlerts();
   return(0);
   
}

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

void manageArrow(int i)
{
   if (!calculateValue && arrowsVisible )
   {
         deleteArrow(Time[i]);
         if (trend[i]!=trend[i+1])
         {
            if (trend[i] == 1) drawArrow(i,arrowsUpColor,241,false);
            if (trend[i] ==-1) drawArrow(i,arrowsDnColor,242,true);
         }
   }
}               

//
//
//
//
//

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]+gap);
         else  ObjectSet(name,OBJPROP_PRICE1,Low[i] -gap);
}

//
//
//
//
//

void deleteArrow(datetime time)
{
   string lookFor = arrowsIdentifier+":"+time; ObjectDelete(lookFor);
}


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

void manageAlerts()
{
   if (!calculateValue && alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1; whichBar = iBarShift(NULL,0,iTime(NULL,timeFrame,whichBar));
      if (trend[whichBar] != trend[whichBar+1])
      {
         if (trend[whichBar] == 1) doAlert(whichBar,"up");
         if (trend[whichBar] ==-1) 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 =  StringConcatenate(Symbol()," ",timeFrameToString(timeFrame)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," VQ trend changed to  ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"VQ nrp "),message);
             if (alertsSound)   PlaySound("alert2.wav");
      }
}

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

void ClearPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

//
//
//
//
//

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (first[i+1] == EMPTY_VALUE)
      {
      if (first[i+2] == EMPTY_VALUE) {
          first[i]    = from[i];
          first[i+1]  = from[i+1];
          second[i]   = EMPTY_VALUE;
         }
      else {
          second[i]   = from[i];
          second[i+1] = from[i+1];
          first[i]    = EMPTY_VALUE;
         }
      }
   else
      {
         first[i]   = from[i];
         second[i]  = EMPTY_VALUE;
      }
}

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

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