diff --git a/vnpy/trader/app/optionMaster/omBase.py b/vnpy/trader/app/optionMaster/omBase.py index 116b652b..d2fb2aec 100644 --- a/vnpy/trader/app/optionMaster/omBase.py +++ b/vnpy/trader/app/optionMaster/omBase.py @@ -4,6 +4,7 @@ from __future__ import division from copy import copy from collections import OrderedDict +from math import log1p from vnpy.trader.vtConstant import * from vnpy.trader.vtObject import VtTickData @@ -271,7 +272,12 @@ class OmOption(OmInstrument): def setUnderlying(self, underlying): """设置标的物对象""" self.underlying = underlying - + + #---------------------------------------------------------------------- + def setR(self, r): + """设置折现率""" + self.r = r + ######################################################################## class OmChain(object): @@ -375,6 +381,34 @@ class OmChain(object): self.posGamma = self.posGamma - oldPosGamma + option.posGamma self.posTheta = self.posTheta - oldPosTheta + option.posTheta self.posVega = self.posVega - oldPosVega + option.posVega + + #---------------------------------------------------------------------- + def adjustR(self): + """调整折现率(r)""" + l = [] + callList = self.callDict.values() + putList = self.putDict.values() + + # 通过计算期权链所有PCP的平价利率 + for n, call in enumerate(callList): + put = putList[n] + + # 如果有任意中间价为0,则忽略该PCP + if (not call.underlying.midPrice or + not put.midPrice or + not call.midPrice or + not call.k, + not call.t): + continue + + temp = (call.underlying.midPrice + put.midPrice - call.midPrice) / call.k + r = log1p(temp-1) / (-call.t) + l.append(r) + + # 求平均值来计算拟合折现率 + self.r = sum(l)/len(l) + for option in self.optionDict.values(): + option.setR(r) ######################################################################## @@ -469,4 +503,11 @@ class OmPortfolio(object): elif symbol in self.underlyingDict: underlying = self.underlyingDict[symbol] underlying.newTrade(trade) - self.calculatePosGreeks() \ No newline at end of file + self.calculatePosGreeks() + + #---------------------------------------------------------------------- + def adjustR(self): + """调整折现率""" + for chain in self.chainDict.values(): + chain.adjustR() + \ No newline at end of file diff --git a/vnpy/trader/app/optionMaster/omEngine.py b/vnpy/trader/app/optionMaster/omEngine.py index 3e2eba03..008997a6 100644 --- a/vnpy/trader/app/optionMaster/omEngine.py +++ b/vnpy/trader/app/optionMaster/omEngine.py @@ -223,7 +223,16 @@ class OmEngine(object): event = Event(EVENT_OM_LOG) event.dict_['data'] = log - self.eventEngine.put(event) + self.eventEngine.put(event) + + #---------------------------------------------------------------------- + def adjustR(self): + """调整折现率""" + if self.portfolio: + self.portfolio.adjustR() + + for chain in self.portfolio.chainDict.values(): + self.writeLog(u'期权链%s的折现率r拟合为%.3f' %(chain.symbol, chain.r)) ######################################################################## diff --git a/vnpy/trader/app/optionMaster/uiOmWidget.py b/vnpy/trader/app/optionMaster/uiOmWidget.py index f8764a58..b857bacb 100644 --- a/vnpy/trader/app/optionMaster/uiOmWidget.py +++ b/vnpy/trader/app/optionMaster/uiOmWidget.py @@ -80,6 +80,10 @@ class OmManager(QtWidgets.QWidget): self.buttonStrategyManager.clicked.connect(self.openStrategyManager) self.buttonStrategyManager.setDisabled(True) + self.buttonAdjustR = QtWidgets.QPushButton(u'拟合利率') + self.buttonAdjustR.clicked.connect(self.omEngine.adjustR) + self.buttonAdjustR.setDisabled(True) + self.logMonitor = QtWidgets.QTextEdit() self.logMonitor.setReadOnly(True) @@ -92,6 +96,7 @@ class OmManager(QtWidgets.QWidget): hbox.addWidget(self.buttonVolatilityManager) hbox.addWidget(self.buttonAnalysisManager) hbox.addWidget(self.buttonStrategyManager) + hbox.addWidget(self.buttonAdjustR) hbox.addStretch() hbox2 = QtWidgets.QHBoxLayout() @@ -130,6 +135,7 @@ class OmManager(QtWidgets.QWidget): self.buttonVolatilityManager.setEnabled(True) self.buttonAnalysisManager.setEnabled(True) self.buttonStrategyManager.setEnabled(True) + self.buttonAdjustR.setEnabled(True) #---------------------------------------------------------------------- def writeLog(self, content, time=''):