股票——通达信:

代码:

{############通达信自带的标准偏差函数不好用,需要自己根据算法来算############}

TYPICAL_PRICE:=(H+L+C)/3;
METHOD:=EMA(TYPICAL_PRICE,N);
APPLY_TO:=TYPICAL_PRICE;

MYSTDDEV:=SQRT(SUM(POW(APPLY_TO-METHOD,2),N)/N);
MAXB:=HHV(MYSTDDEV,M);
MINB:=LLV(MYSTDDEV,M);
VIX:100*(MYSTDDEV-MINB)/(MAXB-MINB);
10,DOTLINE,COLORGREEN;
50,DOTLINE,COLORYELLOW;
90,DOTLINE,COLORMAGENTA;

样图:

波动率指标_局部变量

期货——盘立方:

代码:

Params    
  //参数定义
  Integer N(14);
  Integer M(50);
GlobalVars  
  //全局变量定义
Vars    
  //局部变量定义
Begin   
  //策略执行区
  Numeric TYPICAL_PRICE = (H+L+C)/3;
  Numeric METHOD = EMA(TYPICAL_PRICE,N);
  Numeric APPLY_TO = TYPICAL_PRICE;
  
  Numeric MYSTDDEV = Sqrt(Summation(Power(APPLY_TO-METHOD,2),N)/N);
  Numeric MAXB = Highest(MYSTDDEV,M);
  Numeric MINB = Lowest(MYSTDDEV,M);
  PlotNumeric("VIX",100*(MYSTDDEV-MINB)/(MAXB-MINB));
  
  PlotHorzLine(10,Green,2);
  //PlotHorzLine(50,Yellow);
  PlotHorzLine(90,Magenta);
End

样图:

波动率指标_局部变量_02

外汇——MT4:

代码:

#property copyright "yee"
#property link  "https://blog.51cto.com/noican"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1  PaleVioletRed
#property indicator_width1  2

//
//
//
//
//

extern int StdDevLen           = 14;
extern int StdDevPrice         = PRICE_TYPICAL;
extern int StdDevMaMode        = MODE_EMA;
extern int NormalizationPeriod = 50;

//
//
//
//
//

double width[];
double devs[];

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

int init()
{
   IndicatorBuffers(2);
   SetIndexBuffer(0, width);
   SetIndexBuffer(1, devs);
      IndicatorShortName(" BB width normalized ("+StdDevLen+")");
   return(0);
}
int deinit() { 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);

   //
   //
   //
   //
   //

   for(i = limit; i >= 0 ; i--)
   {
      devs[i]  = iStdDev(NULL,0,StdDevLen,0,StdDevMaMode,StdDevPrice,i);
            double max = devs[ArrayMaximum(devs,NormalizationPeriod,i)];
            double min = devs[ArrayMinimum(devs,NormalizationPeriod,i)];
            if (max!=min)            
                  width[i] = 100.0*(devs[i] - min) / (max-min);
            else  width[i] = 0;
   }
   return(0);
}

样例:

波动率指标_局部变量_03