[Add]新增OptionMaster期权折现率自动拟合功能

This commit is contained in:
vn.py 2018-02-13 14:40:58 +08:00
parent 925940996e
commit a18b2a2b0d
3 changed files with 59 additions and 3 deletions

View File

@ -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()
self.calculatePosGreeks()
#----------------------------------------------------------------------
def adjustR(self):
"""调整折现率"""
for chain in self.chainDict.values():
chain.adjustR()

View File

@ -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))
########################################################################

View File

@ -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=''):