美文网首页数据科学与Python投资理财财经·投资·理财
如何统计投资品种波动率(python)?

如何统计投资品种波动率(python)?

作者: 阿甘数量化 | 来源:发表于2016-07-09 16:17 被阅读1318次

    目前正在网H股ETF,进行了接近一年了还没出清,虽然已盈利,但相对于50ETF来说现在想它还真不是好品种,不考虑强周期、通胀、流动性等因素,仅就网格的最大影响因素波动性来讲,它就比不上50ETF。以前不会统计,现在学了点Python,自己写了小程序计算了下,先上结果:

    volatility(510050vs510900).png
      图中蓝色为50ETF,可以看出,过去三年来它的日波动性明显强于H股ETF。
      下面就以50ETF和H股ETF为例,统计这两个投资品种的日波动率,其他品种、其他周期类似处理即可。附上代码:
    # -- coding: utf-8 --
    """
    Created on Sat Jul 09 12:03:03 2016
    @author: forestgumpgg(雪球ID、聚宽ID),欢迎关注,共同讨论量化分析之道
    比较上证50ETF(510050)和H股ETF(510900)的日波动率
    日波动率=sqrt[(最高值-收盘值)^2 + (最低值- 收盘值) ^2]
    
    """
    import pandas as pd
    import matplotlib.pyplot as plt
    import tushare as ts
    import math
    
    ts.set_token('********') #此次隐去了自己用的token
    
    start_date = '2013-07-01'
    end_date = '2016-06-30'
    
    info510050 = ts.get_hist_data('510050', start= start_date, end=end_date)
    info510050 = info510050.sort_index(axis = 'index', ascending = True)
    info510050['day_volatility'] = (pow(info510050['high'] -info510050['close'] , 2) +
                            pow(info510050['low'] -info510050['close'] , 2) )
    for i in range(0,len(info510050.index)):
        info510050['day_volatility'][i] = math.sqrt(info510050['day_volatility'][i])
        
        
    info510900 = ts.get_hist_data('510900', start=start_date, end=end_date)
    info510900 = info510900.sort_index(axis = 'index', ascending = True)
    info510900['day_volatility'] = (pow(info510900['high'] -info510900['close'] , 2) +
                            pow(info510900['low'] -info510900['close'] , 2) )
    for i in range(0,len(info510900.index)):
        info510900['day_volatility'][i] = math.sqrt(info510900['day_volatility'][i])    
                            
    plt_title = 'volatility(50ETF vs H_ETF)'  
    plt_figsize = (16, 9)  #unit is inch                      
    plt.figure()
    info510050['day_volatility'].plot(figsize = plt_figsize, title = plt_title, grid = True, legend =True)                          
    info510900['day_volatility'].plot(figsize = plt_figsize, title = plt_title, grid = True, legend =True) 
    plt.legend(['50ETF','H_ETF'])
    
    plt.savefig("volatility(510050vs510900).png")

    相关文章

      网友评论

        本文标题:如何统计投资品种波动率(python)?

        本文链接:https://www.haomeiwen.com/subject/hhorjttx.html