# 简单低波动率指数

S&P 500 Low Volatility Index

HS300

## 实现细节

``````import numpy as np
import pandas as pd
start = '2012-01-01'                       # 回测起始时间
end = '2015-05-01'                         # 回测结束时间
benchmark = 'HS300'                        # 策略参考标准
universe = set_universe('HS300')  # 证券池，回测支持股票和基金
capital_base = 10000000                      # 起始资金
refresh_rate = 100                           # 调仓频率，即每 refresh_rate 个交易日执行一次 handle_data() 函数

cal = Calendar('China.SSE')

def initialize(account):                   # 初始化虚拟账户状态
pass

def handle_data(account):                  # 每个交易日的买入卖出指令
volatility_res = {}
cal_today = Date.fromDateTime(account.current_date)

for stk in universe:
try:
data = DataAPI.EquRetudGet(ticker=stk[:6], beginDate=Date.toDateTime(start_day).strftime('%Y%m%d'), endDate=Date.toDateTime(yesterday).strftime('%Y%m%d'), field=['ticker',"dailyReturnNoReinv"])
revenue = data['dailyReturnNoReinv']
volatility_res[stk] = np.std(revenue)
except:
universe.remove(stk)

res = pd.Series(volatility_res).order()[:50]
temp = np.ones(50)
res = np.divide(temp, res)
weight_sum = res.values.sum()
order_list = dict(res/weight_sum)

for stk in account.valid_secpos:
order_to(stk, 0)

for s, weight in order_list.iteritems():
if account.referencePrice[s] == 0:
continue
order(s, capital_base*weight/account.referencePrice[s])
``````

``````print "Benchmark Volatility : ", perf['benchmark_volatility']
print "Index Volatility : ", perf['volatility']

Benchmark Volatility :  0.213927304422
Index Volatility :  0.156413355501
``````