From a18b2a2b0d2607c598eb89d03589df66346f8d8b Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Tue, 13 Feb 2018 14:40:58 +0800 Subject: [PATCH] =?UTF-8?q?[Add]=E6=96=B0=E5=A2=9EOptionMaster=E6=9C=9F?= =?UTF-8?q?=E6=9D=83=E6=8A=98=E7=8E=B0=E7=8E=87=E8=87=AA=E5=8A=A8=E6=8B=9F?= =?UTF-8?q?=E5=90=88=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/trader/app/optionMaster/omBase.py | 45 +++++++++++++++++++++- vnpy/trader/app/optionMaster/omEngine.py | 11 +++++- vnpy/trader/app/optionMaster/uiOmWidget.py | 6 +++ 3 files changed, 59 insertions(+), 3 deletions(-) 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=''):