//+------------------------------------------------------------------+
//|                                           Damiani_volatmeter.mq4 |
//|                         Copyright © 2006, Luis Guilherme Damiani |
//|                                      http://www.damianifx.com.br |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Luis Guilherme Damiani"
#property link      "http://www.damianifx.com.br"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Silver
#property indicator_color2 Red
#property indicator_color3 LimeGreen
#property indicator_width2 2 
#property indicator_width3 2 
//---- input parameters
extern int                Viscosity          = 13;
extern int                Sedimentation      = 50;
extern double             Threshold_level    = 1.3;
extern bool               lag_supressor      = true;
extern ENUM_APPLIED_PRICE DeviationsPrice    = PRICE_TYPICAL;
extern ENUM_MA_METHOD     DeviationsMaMethod = MODE_LWMA;
extern string             UniqueID           = "SignalToNoizeLabel";
extern color              LabelColor         = clrDeepSkyBlue;
extern ENUM_BASE_CORNER   Corner             = CORNER_RIGHT_LOWER;
double    lag_s_K=0.5;
//---- buffers
double thresholdBuffer[];
double vol_m[];
double vol_t[];
double ind_c[];
double state[];

//------------------------------------------------------------------
//
//------------------------------------------------------------------
int init()
  {
//---- indicators
   IndicatorBuffers(5);
   SetIndexBuffer(0,thresholdBuffer); SetIndexStyle(1,DRAW_SECTION);
   SetIndexBuffer(1,vol_m);
   SetIndexBuffer(2,vol_t);
   SetIndexBuffer(3,ind_c);
   SetIndexBuffer(4,state);
   return(0);
}
int deinit() { ObjectDelete(UniqueID); 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);
         string mess="";
         
   for(int i=limit;i>=0;i--)
   {
      
      double sa  = iATR(NULL,0,Viscosity,i);
      double s1  = ind_c[i+1];
      double s3  = ind_c[i+3];
      double atr = iATR(NULL,0,Sedimentation,i);
      double vol = 0;
      if (atr!=0)
         if(lag_supressor)
               vol= sa/atr+lag_s_K*(s1-s3);   
         else  vol= sa/atr;   
      
      double dev        = iStdDev(NULL,0,Sedimentation,0,DeviationsMaMethod,DeviationsPrice,i);
      double anti_thres = iStdDev(NULL,0,Viscosity    ,0,DeviationsMaMethod,DeviationsPrice,i);
         if (dev!=0) anti_thres = anti_thres/dev;
      double t=Threshold_level-anti_thres;
     
      if (vol>t)
            { vol_t[i]=vol; vol_m[i]=vol;         state[i] =  1; mess="DAMIANI Signal/Noise: TRADE  /  ATR= "       +DoubleToStr(sa,Digits)+"    values:";}
      else  { vol_t[i]=vol; vol_m[i]=EMPTY_VALUE; state[i] = -1; mess="DAMIANI Signal/Noise: DO NOT trade  /  ATR= "+DoubleToStr(sa,Digits)+"    values:";}   
      ind_c[i] = vol;
         if (i == 0 && state[i] == -1) vol_m[i] = (t+vol)/2.0;
      thresholdBuffer[i] =  t;   
   }
   IndicatorShortName(mess);
   ObjectCreate(UniqueID,OBJ_LABEL,0,0,0);
      ObjectSetText(UniqueID,mess,10,"Arial",LabelColor);
      ObjectSet(UniqueID,OBJPROP_CORNER,Corner);
      ObjectSet(UniqueID,OBJPROP_XDISTANCE,10);
      ObjectSet(UniqueID,OBJPROP_YDISTANCE,10);
 
   
   return(0);
}