<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 基于 VIX 指數的擇時策略 > 來源:https://uqer.io/community/share/55b6152ff9f06c91fa18c5c9 波動率VIX指數是跟蹤市場波動性的指數,一般通過標的期權的隱含波動率計算得來,以芝加哥期權交易所的VIX指數為例,如標的期權的隱含波動率越高,則VIX指數相應越高,一般而言,該指數反映出投資者愿意付出多少成本去對沖投資風險。業內認為,當VIX越高時,表示市場參與者預期后市波動程度會更加激烈,同時也反映其不安的心理狀態;相反,VIX越低時,則反映市場參與者預期后市波動程度會趨于緩和的心態。因此,VIX又被稱為投資人恐慌指標(The Investor Fear Gauge)。 中國波指是由上證所發布,用于衡量上證50ETF未來30日的預期波動。該指數是根據方差互換的原理,結合50ETF期權的實際運作特點,并通過對上證所交易的50ETF期權價格的計算編制而得。網址為: http://www.sse.com.cn/assortment/derivatives/options/volatility/ 本文中,基于優礦平臺,自己嘗試計算了日間的中國波指,并將其用在了華夏上證50的擇時買賣上,以驗證VIX指數對未來的預測性 由于上證所未發布其iVIX計算方法,所以此處的計算基于CBOE發布的方法,具體參見: http://www.cboe.com/micro/vix/part2.aspx ## 策略思路 + 當VIX指數快速上升時,表示市場恐慌情緒蔓延,產生賣出信號 + 當VIX指數快速下降時,恐慌情緒有所舒緩,產生買入信號 + 賣出買入信號均用來買賣華夏上證50ETF基金 注:國內唯一一只期權上證50ETF期權,跟蹤標的為華夏上證50ETF(510050)基金 ## 1. 計算歷史VIX指數 ```py from matplotlib import pylab import numpy as np import pandas as pd import DataAPI import seaborn as sns sns.set_style('white') ``` ```py from CAL.PyCAL import * from pandas import Series, DataFrame, concat import pandas as pd import numpy as np import seaborn as sns sns.set_style('white') from matplotlib import pylab import time import math def getHistDayOptions(var, date): # 使用DataAPI.OptGet,拿到已退市和上市的所有期權的基本信息; # 同時使用DataAPI.MktOptdGet,拿到歷史上某一天的期權成交信息; # 返回歷史上指定日期交易的所有期權信息,包括: # optID varSecID contractType strikePrice expDate tradeDate closePrice # 以optID為index。 vixDateStr = date.toISO().replace('-', '') optionsMkt = DataAPI.MktOptdGet(tradeDate = vixDateStr, field = [u"optID", "tradeDate", "closePrice"], pandas = "1") optionsMkt = optionsMkt.set_index(u"optID") optionsMkt.closePrice.name = u"price" optionsID = map(str, optionsMkt.index.values.tolist()) fieldNeeded = ["optID", u"varSecID", u'contractType', u'strikePrice', u'expDate'] optionsInfo = DataAPI.OptGet(optID=optionsID, contractStatus = [u"DE", u"L"], field=fieldNeeded, pandas="1") optionsInfo = optionsInfo.set_index(u"optID") options = concat([optionsInfo, optionsMkt], axis=1, join='inner').sort_index() return options[options.varSecID==var] def getNearNextOptExpDate(options, vixDate): # 找到options中的當月和次月期權到期日; # 用這兩個期權隱含的未來波動率來插值計算未來30隱含波動率,是為市場恐慌指數VIX; # 如果options中的最近到期期權離到期日僅剩1天以內,則拋棄這一期權,改 # 選擇次月期權和次月期權之后第一個到期的期權來計算。 # 返回的near和next就是用來計算VIX的兩個期權的到期日 optionsExpDate = Series(options.expDate.values.ravel()).unique().tolist() near = min(optionsExpDate) optionsExpDate.remove(near) if Date.parseISO(near) - vixDate < 1: near = min(optionsExpDate) optionsExpDate.remove(near) next = min(optionsExpDate) return near, next def getStrikeMinCallMinusPutClosePrice(options): # options 中包括計算某日VIX的call和put兩種期權, # 對每個行權價,計算相應的call和put的價格差的絕對值, # 返回這一價格差的絕對值最小的那個行權價, # 并返回該行權價對應的call和put期權價格的差 call = options[options.contractType==u"CO"].set_index(u"strikePrice").sort_index() put = options[options.contractType==u"PO"].set_index(u"strikePrice").sort_index() callMinusPut = call.closePrice - put.closePrice strike = abs(callMinusPut).idxmin() priceDiff = callMinusPut[strike] return strike, priceDiff def calSigmaSquare(options, FF, R, T): # 計算某個到期日期權對于VIX的貢獻sigma; # 輸入為期權數據options,FF為forward index price, # R為無風險利率, T為期權剩余到期時間 callAll = options[options.contractType==u"CO"].set_index(u"strikePrice").sort_index() putAll = options[options.contractType==u"PO"].set_index(u"strikePrice").sort_index() callAll['deltaK'] = 0.05 putAll['deltaK'] = 0.05 # Interval between strike prices index = callAll.index if len(index) < 3: callAll['deltaK'] = index[-1] - index[0] else: for i in range(1,len(index)-1): callAll['deltaK'].ix[index[i]] = (index[i+1]-index[i-1])/2.0 callAll['deltaK'].ix[index[0]] = index[1]-index[0] callAll['deltaK'].ix[index[-1]] = index[-1] - index[-2] index = putAll.index if len(index) < 3: putAll['deltaK'] = index[-1] - index[0] else: for i in range(1,len(index)-1): putAll['deltaK'].ix[index[i]] = (index[i+1]-index[i-1])/2.0 putAll['deltaK'].ix[index[0]] = index[1]-index[0] putAll['deltaK'].ix[index[-1]] = index[-1] - index[-2] call = callAll[callAll.index > FF] put = putAll[putAll.index < FF] FF_idx = FF if not put.empty: FF_idx = put.index[-1] put['closePrice'].iloc[-1] = (putAll.ix[FF_idx].closePrice + callAll.ix[FF_idx].closePrice)/2.0 callComponent = call.closePrice*call.deltaK/call.index/call.index putComponent = put.closePrice*put.deltaK/put.index/put.index sigma = (sum(callComponent)+sum(putComponent))*np.exp(T*R)*2/T sigma = sigma - (FF/FF_idx - 1)**2/T return sigma def calDayVIX(optionVarSecID, vixDate): # 利用CBOE的計算方法,計算歷史某一日的未來30日期權波動率指數VIX # The risk-free interest rates R_near = 0.06 R_next = 0.06 # 拿取所需期權信息 options = getHistDayOptions(optionVarSecID, vixDate) termNearNext = getNearNextOptExpDate(options, vixDate) optionsNearTerm = options[options.expDate == termNearNext[0]] optionsNextTerm = options[options.expDate == termNearNext[1]] # time to expiration T_near = (Date.parseISO(termNearNext[0]) - vixDate)/365.0 T_next = (Date.parseISO(termNearNext[1]) - vixDate)/365.0 # the forward index prices nearPriceDiff = getStrikeMinCallMinusPutClosePrice(optionsNearTerm) nextPriceDiff = getStrikeMinCallMinusPutClosePrice(optionsNextTerm) near_F = nearPriceDiff[0] + np.exp(T_near*R_near)*nearPriceDiff[1] next_F = nextPriceDiff[0] + np.exp(T_next*R_next)*nextPriceDiff[1] # 計算不同到期日期權對于VIX的貢獻 near_sigma = calSigmaSquare(optionsNearTerm, near_F, R_near, T_near) next_sigma = calSigmaSquare(optionsNextTerm, next_F, R_next, T_next) # 利用兩個不同到期日的期權對VIX的貢獻sig1和sig2, # 已經相應的期權剩余到期時間T1和T2; # 差值得到并返回VIX指數(%) w = (T_next - 30.0/365.0)/(T_next - T_near) vix = T_near*w*near_sigma + T_next*(1 - w)*next_sigma return 100*np.sqrt(vix*365.0/30.0) def getHistVIX(beginDate, endDate): # 計算歷史一段時間內的VIX指數并返回 optionVarSecID = u"510050.XSHG" cal = Calendar('China.SSE') dates = cal.bizDatesList(beginDate, endDate) dates = map(Date.toDateTime, dates) histVIX = pd.DataFrame(0.0, index=dates, columns=['VIX']) histVIX.index.name = 'date' for date in histVIX.index: histVIX['VIX'][date] = calDayVIX(optionVarSecID, Date.fromDateTime(date)) return histVIX def getDayVIX(date): optionVarSecID = u"510050.XSHG" return calDayVIX(optionVarSecID, date) ``` ## 2. VIX指數與華夏上證50ETF基金的走勢對比 ```py secID = '510050.XSHG' begin = Date(2015, 2, 9) end = Date(2015, 7, 23) # 歷史VIX histVIX = getHistVIX(begin, end) # 華夏上證50ETF etf = DataAPI.MktFunddGet(secID, beginDate=begin.toISO().replace('-', ''), endDate=end.toISO().replace('-', ''), field=['tradeDate', 'closePrice']) etf['tradeDate'] = pd.to_datetime(etf['tradeDate']) etf = etf.set_index('tradeDate') ``` ```py font.set_size(12) pylab.figure(figsize = (16,8)) ax1 = histVIX.plot(x=histVIX.index, y='VIX', style='r') ax1.set_xlabel(u'日期', fontproperties=font) ax1.set_ylabel(u'VIX(%)', fontproperties=font) ax2 = ax1.twinx() ax2.plot(etf.index,etf.closePrice) ax2.set_ylabel(u'ETF Price', fontproperties=font) <matplotlib.text.Text at 0x5a66390> ``` ![](https://box.kancloud.cn/2016-07-30_579cbdb42fdd0.png) 關于VIX,比較成熟的美國市場中,標普500指數和相應的VIX之間呈負相關性。具體可以參照CBOE的數據:http://www.cboe.com/micro/vix/part3.aspx 這可以理解為: + 當VIX越高時,表示市場參與者預期后市波動程度會更加激烈,所以謹慎持倉,甚至逐漸減倉; + 相反,VIX越低時,市場參與者預期后市波動程度會趨于緩和,開始放心投資股市。 上圖中的中國市場VIX指數與華夏上證50ETF走勢對比中,我們不難發現以下幾點: + 上證50ETF期權于2月9日上市,之后一個月VIX穩定在低位運行,同時市場也表現出穩定的態勢 + 3月下旬到5月初一段時間,VIX指數顯著上升,表示市場認為后期震蕩會加劇,但這種恐慌淹沒在牛市大潮中 + 5月到6月VIX高位運行,但似乎沒有引起市場的足夠重視 + 6月中的股市大跌開始后,VIX指數快速上升到接近60 + 7月時候,市場認可國家救市決心,VIX開始從高位迅速下降,股指也日趨穩定 可以看出,VIX指數在和股指的并駕齊驅中總是慢人一步,沒法充分表現出股指在六月極高位時候市場的不安;實際上,國內期權市場建立不足半年,期權流動性并不夠大,導致基于期權市場的VIX指數對于中國股市的預測并不如成熟市場一樣流暢 ## 3. 基于VIX指數的擇時策略示例 ```py start = datetime(2015, 2, 9) # 回測起始時間 end = datetime(2015, 7, 26) # 回測結束時間 benchmark = '510050.XSHG' # 策略參考標準 universe = ['510050.XSHG'] # 股票池 capital_base = 100000 # 起始資金 commission = Commission(0.0,0.0) window_short = 1 window_long = 5 longest_history = 1 SD = 0.08 histVIX['short_window'] = pd.rolling_mean(histVIX['VIX'], window=window_short) histVIX['long_window'] = pd.rolling_mean(histVIX['VIX'], window=window_long) def initialize(account): # 初始化虛擬賬戶狀態 account.fund = universe[0] def handle_data(account): # 每個交易日的買入賣出指令 hist = account.get_history(longest_history) fund = account.fund # 獲取回測當日的前一天日期 dt = Date.fromDateTime(account.current_date) cal = Calendar('China.IB') lastTDay = cal.advanceDate(dt,'-1B',BizDayConvention.Preceding) #計算出前一個交易日期 last_day_str = lastTDay.strftime("%Y-%m-%d") # 計算買入賣出信號 try: short_mean = histVIX['short_window'].loc[last_day_str] # 計算短均線值 long_mean = histVIX['long_window'].loc[last_day_str] # 計算長均線值 long_flag = True if (short_mean - long_mean) < -SD * long_mean else False short_flag = True if (short_mean - long_mean) > SD * long_mean else False except: long_flag = False short_flag = False if long_flag: if account.position.secpos.get(fund, 0) == 0: # 空倉時全倉買入,買入股數為100的整數倍 approximationAmount = int(account.cash / hist[fund]['closePrice'][-1]/100.0) * 100 order(fund, approximationAmount) elif short_flag: # 賣出時,全倉清空 if account.position.secpos.get(fund, 0) >= 0: order_to(fund, 0) ``` ![](https://box.kancloud.cn/2016-07-30_579cbdb44a569.jpg) 可以看出: + 基于VIX指數高位時空倉、低位時進場的策略,可以比較有效地避開股指大跌的風險 + 但由于國內期權市場流動性不足,VIX指數并不能有效反應市場的情緒,導致我們也錯過了很多牛市的蛋糕
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看