def connect(self, userID, password, brokerID, address): """初始化连接""" self.userID = userID # 账号 self.password = password # 密码 self.brokerID = brokerID # 经纪商代码 self.address = address # 服务器地址 # 如果尚未建立服务器连接,则进行连接 if not self.connectionStatus: # 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径 path = getTempPath(self.gatewayName + '_') self.createFtdcTraderApi(path) # 订阅主题 self.subscribePrivateTopic(0) self.subscribePublicTopic(0) # self.subscribeUserTopic(0) # 注册服务器地址 self.registerFront(self.address) # 初始化连接,成功会调用onFrontConnected self.init() # 若已经连接但尚未登录,则进行登录 else: if not self.loginStatus: self.login()
def connect(self, userID, password, brokerID, address, productInfo, authCode): """初始化连接""" self.userID = userID # 账号 self.password = password # 密码 self.brokerID = brokerID # 经纪商代码 self.address = address # 服务器地址 self.productInfo = productInfo self.authCode = authCode # 如果尚未建立服务器连接,则进行连接 if not self.connectionStatus: # 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径 path = getTempPath(self.gatewayName + '_') self.createFtdcQueryApi(path) # 注册服务器地址 self.registerFront(self.address) # 初始化连接,成功会调用onFrontConnected self.init() # 若已经连接但尚未登录,则进行登录 else: if not self.loginStatus: self.login()
def addFileHandler(self): """添加文件输出""" if not self.fileHandler: filename = 'vt_' + datetime.now().strftime('%Y%m%d') + '.log' filepath = getTempPath(filename) self.fileHandler = logging.FileHandler(filepath) self.fileHandler.setLevel(self.level) self.fileHandler.setFormatter(self.formatter) self.logger.addHandler(self.fileHandler)
def addFileHandler(self, filename=''): """添加文件输出""" if not self.fileHandler: if not filename: filename = 'vt_' + datetime.now().strftime('%Y%m%d') + '.log' filepath = getTempPath(filename) self.fileHandler = logging.FileHandler(filepath, mode='w', encoding='utf-8') self.fileHandler.setLevel(self.level) self.fileHandler.setFormatter(self.formatter) self.logger.addHandler(self.fileHandler)
class DataEngine(object): """数据引擎""" contractFileName = 'ContractData.vt' contractFilePath = getTempPath(contractFileName) FINISHED_STATUS = [STATUS_ALLTRADED, STATUS_REJECTED, STATUS_CANCELLED] #---------------------------------------------------------------------- def __init__(self, eventEngine): """Constructor""" self.eventEngine = eventEngine # 保存合约详细信息的字典 self.contractDict = {} # 保存委托数据的字典 self.orderDict = {} # 保存活动委托数据的字典(即可撤销) self.workingOrderDict = {} # 持仓细节相关 self.detailDict = {} # vtSymbol:PositionDetail self.tdPenaltyList = globalSetting['tdPenalty'] # 平今手续费惩罚的产品代码列表 # 读取保存在硬盘的合约数据 self.loadContracts() # 注册事件监听 self.registerEvent() #---------------------------------------------------------------------- def registerEvent(self): """注册事件监听""" self.eventEngine.register(EVENT_CONTRACT, self.processContractEvent) self.eventEngine.register(EVENT_ORDER, self.processOrderEvent) self.eventEngine.register(EVENT_TRADE, self.processTradeEvent) self.eventEngine.register(EVENT_POSITION, self.processPositionEvent) #---------------------------------------------------------------------- def processContractEvent(self, event): """处理合约事件""" contract = event.dict_['data'] self.contractDict[contract.vtSymbol] = contract self.contractDict[contract.symbol] = contract # 使用常规代码(不包括交易所)可能导致重复 #---------------------------------------------------------------------- def processOrderEvent(self, event): """处理委托事件""" order = event.dict_['data'] self.orderDict[order.vtOrderID] = order # 如果订单的状态是全部成交或者撤销,则需要从workingOrderDict中移除 if order.status in self.FINISHED_STATUS and order.vtOrderID in self.workingOrderDict: del self.workingOrderDict[order.vtOrderID] # 否则则更新字典中的数据 else: self.workingOrderDict[order.vtOrderID] = order # 更新到持仓细节中 detail = self.getPositionDetail(order.vtSymbol) detail.updateOrder(order) #---------------------------------------------------------------------- def processTradeEvent(self, event): """处理成交事件""" trade = event.dict_['data'] # 更新到持仓细节中 detail = self.getPositionDetail(trade.vtSymbol) detail.updateTrade(trade) #---------------------------------------------------------------------- def processPositionEvent(self, event): """处理持仓事件""" pos = event.dict_['data'] # 更新到持仓细节中 detail = self.getPositionDetail(pos.vtSymbol) detail.updatePosition(pos) #---------------------------------------------------------------------- def getContract(self, vtSymbol): """查询合约对象""" try: return self.contractDict[vtSymbol] except KeyError: return None #---------------------------------------------------------------------- def getAllContracts(self): """查询所有合约对象(返回列表)""" return self.contractDict.values() #---------------------------------------------------------------------- def saveContracts(self): """保存所有合约对象到硬盘""" f = shelve.open(self.contractFilePath) f['data'] = self.contractDict f.close() #---------------------------------------------------------------------- def loadContracts(self): """从硬盘读取合约对象""" f = shelve.open(self.contractFilePath) if 'data' in f: d = f['data'] for key, value in d.items(): self.contractDict[key] = value f.close() #---------------------------------------------------------------------- def getOrder(self, vtOrderID): """查询委托""" try: return self.orderDict[vtOrderID] except KeyError: return None #---------------------------------------------------------------------- def getAllWorkingOrders(self): """查询所有活动委托(返回列表)""" return self.workingOrderDict.values() #---------------------------------------------------------------------- def getPositionDetail(self, vtSymbol): """查询持仓细节""" if vtSymbol in self.detailDict: detail = self.detailDict[vtSymbol] else: detail = PositionDetail(vtSymbol) self.detailDict[vtSymbol] = detail # 设置持仓细节的委托转换模式 contract = self.getContract(vtSymbol) if contract: detail.exchange = contract.exchange # 上期所合约 if contract.exchange == EXCHANGE_SHFE: detail.mode = detail.MODE_SHFE # 检查是否有平今惩罚 for productID in self.tdPenaltyList: if str(productID) in contract.symbol: detail.mode = detail.MODE_TDPENALTY return detail #---------------------------------------------------------------------- def updateOrderReq(self, req, vtOrderID): """委托请求更新""" vtSymbol = req.vtSymbol detail = self.getPositionDetail(vtSymbol) detail.updateOrderReq(req, vtOrderID) #---------------------------------------------------------------------- def convertOrderReq(self, req): """根据规则转换委托请求""" detail = self.detailDict.get(req.vtSymbol, None) if not detail: return [req] else: return detail.convertOrderReq(req)
class StAlgoEngine(object): """价差算法交易引擎""" algoFileName = 'SpreadTradingAlgo.vt' algoFilePath = getTempPath(algoFileName) #---------------------------------------------------------------------- def __init__(self, dataEngine, mainEngine, eventEngine): """Constructor""" self.dataEngine = dataEngine self.mainEngine = mainEngine self.eventEngine = eventEngine self.algoDict = {} # spreadName:algo self.vtSymbolAlgoDict = {} # vtSymbol:algo self.registerEvent() #---------------------------------------------------------------------- def registerEvent(self): """注册事件监听""" self.eventEngine.register(EVENT_SPREADTRADING_TICK, self.processSpreadTickEvent) self.eventEngine.register(EVENT_SPREADTRADING_POS, self.processSpreadPosEvent) self.eventEngine.register(EVENT_TRADE, self.processTradeEvent) self.eventEngine.register(EVENT_ORDER, self.processOrderEvent) self.eventEngine.register(EVENT_TIMER, self.processTimerEvent) #---------------------------------------------------------------------- def processSpreadTickEvent(self, event): """处理价差行情事件""" spread = event.dict_['data'] algo = self.algoDict.get(spread.name, None) if algo: algo.updateSpreadTick(spread) #---------------------------------------------------------------------- def processSpreadPosEvent(self, event): """处理价差持仓事件""" spread = event.dict_['data'] algo = self.algoDict.get(spread.name, None) if algo: algo.updateSpreadPos(spread) #---------------------------------------------------------------------- def processTradeEvent(self, event): """处理成交事件""" trade = event.dict_['data'] algo = self.vtSymbolAlgoDict.get(trade.vtSymbol, None) if algo: algo.updateTrade(trade) #---------------------------------------------------------------------- def processOrderEvent(self, event): """处理委托事件""" order = event.dict_['data'] algo = self.vtSymbolAlgoDict.get(order.vtSymbol, None) if algo: algo.updateOrder(order) #---------------------------------------------------------------------- def processTimerEvent(self, event): """""" for algo in self.algoDict.values(): algo.updateTimer() #---------------------------------------------------------------------- def sendOrder(self, vtSymbol, direction, offset, price, volume, payup=0): """发单""" contract = self.mainEngine.getContract(vtSymbol) if not contract: return '' req = VtOrderReq() req.symbol = contract.symbol req.exchange = contract.exchange req.vtSymbol = contract.vtSymbol req.direction = direction req.offset = offset req.volume = int(volume) req.priceType = PRICETYPE_LIMITPRICE if direction == DIRECTION_LONG: req.price = price + payup * contract.priceTick else: req.price = price - payup * contract.priceTick vtOrderID = self.mainEngine.sendOrder(req, contract.gatewayName) return vtOrderID #---------------------------------------------------------------------- def cancelOrder(self, vtOrderID): """撤单""" order = self.mainEngine.getOrder(vtOrderID) if not order: return req = VtCancelOrderReq() req.symbol = order.symbol req.exchange = order.exchange req.frontID = order.frontID req.sessionID = order.sessionID req.orderID = order.orderID self.mainEngine.cancelOrder(req, order.gatewayName) #---------------------------------------------------------------------- def buy(self, vtSymbol, price, volume, payup=0): """买入""" vtOrderID = self.sendOrder(vtSymbol, DIRECTION_LONG, OFFSET_OPEN, price, volume, payup) l = [] if vtOrderID: l.append(vtOrderID) return l #---------------------------------------------------------------------- def sell(self, vtSymbol, price, volume, payup=0): """卖出""" vtOrderID = self.sendOrder(vtSymbol, DIRECTION_SHORT, OFFSET_CLOSE, price, volume, payup) l = [] if vtOrderID: l.append(vtOrderID) return l #---------------------------------------------------------------------- def short(self, vtSymbol, price, volume, payup=0): """卖空""" vtOrderID = self.sendOrder(vtSymbol, DIRECTION_SHORT, OFFSET_OPEN, price, volume, payup) l = [] if vtOrderID: l.append(vtOrderID) return l #---------------------------------------------------------------------- def cover(self, vtSymbol, price, volume, payup=0): """平空""" vtOrderID = self.sendOrder(vtSymbol, DIRECTION_LONG, OFFSET_CLOSE, price, volume, payup) l = [] if vtOrderID: l.append(vtOrderID) return l #---------------------------------------------------------------------- def putAlgoEvent(self, algo): """发出算法状态更新事件""" event = Event(EVENT_SPREADTRADING_ALGO + algo.name) self.eventEngine.put(event) #---------------------------------------------------------------------- def writeLog(self, content): """输出日志""" log = VtLogData() log.logContent = content event = Event(EVENT_SPREADTRADING_ALGOLOG) event.dict_['data'] = log self.eventEngine.put(event) #---------------------------------------------------------------------- def saveSetting(self): """保存算法配置""" setting = {} for algo in self.algoDict.values(): setting[algo.spreadName] = algo.getAlgoParams() f = shelve.open(self.algoFilePath) f['setting'] = setting f.close() #---------------------------------------------------------------------- def loadSetting(self): """加载算法配置""" # 创建算法对象 l = self.dataEngine.getAllSpreads() for spread in l: algo = SniperAlgo(self, spread) self.algoDict[spread.name] = algo # 保存腿代码和算法对象的映射 for leg in spread.allLegs: self.vtSymbolAlgoDict[leg.vtSymbol] = algo # 加载配置 f = shelve.open(self.algoFilePath) setting = f.get('setting', None) f.close() if not setting: return for algo in self.algoDict.values(): if algo.spreadName in setting: d = setting[algo.spreadName] algo.setAlgoParams(d) #---------------------------------------------------------------------- def stopAll(self): """停止全部算法""" for algo in self.algoDict.values(): algo.stop() #---------------------------------------------------------------------- def startAlgo(self, spreadName): """启动算法""" algo = self.algoDict[spreadName] algoActive = algo.start() return algoActive #---------------------------------------------------------------------- def stopAlgo(self, spreadName): """停止算法""" algo = self.algoDict[spreadName] algoActive = algo.stop() return algoActive #---------------------------------------------------------------------- def getAllAlgoParams(self): """获取所有算法的参数""" return [algo.getAlgoParams() for algo in self.algoDict.values()] #---------------------------------------------------------------------- def setAlgoBuyPrice(self, spreadName, buyPrice): """设置算法买开价格""" algo = self.algoDict[spreadName] algo.setBuyPrice(buyPrice) #---------------------------------------------------------------------- def setAlgoSellPrice(self, spreadName, sellPrice): """设置算法卖平价格""" algo = self.algoDict[spreadName] algo.setSellPrice(sellPrice) #---------------------------------------------------------------------- def setAlgoShortPrice(self, spreadName, shortPrice): """设置算法卖开价格""" algo = self.algoDict[spreadName] algo.setShortPrice(shortPrice) #---------------------------------------------------------------------- def setAlgoCoverPrice(self, spreadName, coverPrice): """设置算法买平价格""" algo = self.algoDict[spreadName] algo.setCoverPrice(coverPrice) #---------------------------------------------------------------------- def setAlgoMode(self, spreadName, mode): """设置算法工作模式""" algo = self.algoDict[spreadName] algo.setMode(mode) #---------------------------------------------------------------------- def setAlgoMaxOrderSize(self, spreadName, maxOrderSize): """设置算法单笔委托限制""" algo = self.algoDict[spreadName] algo.setMaxOrderSize(maxOrderSize) #---------------------------------------------------------------------- def setAlgoMaxPosSize(self, spreadName, maxPosSize): """设置算法持仓限制""" algo = self.algoDict[spreadName] algo.setMaxPosSize(maxPosSize)
class DataEngine(object): """数据引擎""" contractFileName = 'ContractData.vt' contractFilePath = getTempPath(contractFileName) FINISHED_STATUS = [STATUS_ALLTRADED, STATUS_REJECTED, STATUS_CANCELLED] #---------------------------------------------------------------------- def __init__(self, eventEngine): """Constructor""" self.eventEngine = eventEngine # 保存数据的字典和列表 self.tickDict = {} self.contractDict = {} self.orderDict = {} self.workingOrderDict = {} # 可撤销委托 self.tradeDict = {} self.accountDict = {} self.positionDict = {} self.logList = [] self.errorList = [] # 持仓细节相关 self.detailDict = {} # vtSymbol:PositionDetail self.tdPenaltyList = globalSetting['tdPenalty'] # 平今手续费惩罚的产品代码列表 # 读取保存在硬盘的合约数据 self.loadContracts() # 注册事件监听 self.registerEvent() #---------------------------------------------------------------------- def registerEvent(self): """注册事件监听""" self.eventEngine.register(EVENT_TICK, self.processTickEvent) self.eventEngine.register(EVENT_CONTRACT, self.processContractEvent) self.eventEngine.register(EVENT_ORDER, self.processOrderEvent) self.eventEngine.register(EVENT_TRADE, self.processTradeEvent) self.eventEngine.register(EVENT_POSITION, self.processPositionEvent) self.eventEngine.register(EVENT_ACCOUNT, self.processAccountEvent) self.eventEngine.register(EVENT_LOG, self.processLogEvent) self.eventEngine.register(EVENT_ERROR, self.processErrorEvent) #---------------------------------------------------------------------- def processTickEvent(self, event): """处理成交事件""" tick = event.dict_['data'] self.tickDict[tick.vtSymbol] = tick #---------------------------------------------------------------------- def processContractEvent(self, event): """处理合约事件""" contract = event.dict_['data'] self.contractDict[contract.vtSymbol] = contract self.contractDict[contract.symbol] = contract # 使用常规代码(不包括交易所)可能导致重复 #---------------------------------------------------------------------- def processOrderEvent(self, event): """处理委托事件""" order = event.dict_['data'] self.orderDict[order.vtOrderID] = order # 如果订单的状态是全部成交或者撤销,则需要从workingOrderDict中移除 if order.status in self.FINISHED_STATUS: if order.vtOrderID in self.workingOrderDict: del self.workingOrderDict[order.vtOrderID] # 否则则更新字典中的数据 else: self.workingOrderDict[order.vtOrderID] = order # 更新到持仓细节中 detail = self.getPositionDetail(order.vtSymbol) detail.updateOrder(order) #---------------------------------------------------------------------- def processTradeEvent(self, event): """处理成交事件""" trade = event.dict_['data'] self.tradeDict[trade.vtTradeID] = trade # 更新到持仓细节中 detail = self.getPositionDetail(trade.vtSymbol) detail.updateTrade(trade) #---------------------------------------------------------------------- def processPositionEvent(self, event): """处理持仓事件""" pos = event.dict_['data'] self.positionDict[pos.vtPositionName] = pos # 更新到持仓细节中 detail = self.getPositionDetail(pos.vtSymbol) detail.updatePosition(pos) #---------------------------------------------------------------------- def processAccountEvent(self, event): """处理账户事件""" account = event.dict_['data'] self.accountDict[account.vtAccountID] = account #---------------------------------------------------------------------- def processLogEvent(self, event): """处理日志事件""" log = event.dict_['data'] self.logList.append(log) #---------------------------------------------------------------------- def processErrorEvent(self, event): """处理错误事件""" error = event.dict_['data'] self.errorList.append(error) #---------------------------------------------------------------------- def getTick(self, vtSymbol): """查询行情对象""" try: return self.tickDict[vtSymbol] except KeyError: return None #---------------------------------------------------------------------- def getContract(self, vtSymbol): """查询合约对象""" try: return self.contractDict[vtSymbol] except KeyError: return None #---------------------------------------------------------------------- def getAllContracts(self): """查询所有合约对象(返回列表)""" return self.contractDict.values() #---------------------------------------------------------------------- def saveContracts(self): """保存所有合约对象到硬盘""" f = shelve.open(self.contractFilePath) f['data'] = self.contractDict f.close() #---------------------------------------------------------------------- def loadContracts(self): """从硬盘读取合约对象""" f = shelve.open(self.contractFilePath) if 'data' in f: d = f['data'] for key, value in d.items(): self.contractDict[key] = value f.close() #---------------------------------------------------------------------- def getOrder(self, vtOrderID): """查询委托""" try: return self.orderDict[vtOrderID] except KeyError: return None #---------------------------------------------------------------------- def getAllWorkingOrders(self): """查询所有活动委托(返回列表)""" return self.workingOrderDict.values() #---------------------------------------------------------------------- def getAllOrders(self): """获取所有委托""" return self.orderDict.values() #---------------------------------------------------------------------- def getAllTrades(self): """获取所有成交""" return self.tradeDict.values() #---------------------------------------------------------------------- def getAllPositions(self): """获取所有持仓""" return self.positionDict.values() #---------------------------------------------------------------------- def getAllAccounts(self): """获取所有资金""" return self.accountDict.values() #---------------------------------------------------------------------- def getPositionDetail(self, vtSymbol): """查询持仓细节""" if vtSymbol in self.detailDict: detail = self.detailDict[vtSymbol] else: contract = self.getContract(vtSymbol) detail = PositionDetail(vtSymbol, contract) self.detailDict[vtSymbol] = detail # 设置持仓细节的委托转换模式 contract = self.getContract(vtSymbol) if contract: detail.exchange = contract.exchange # 上期所合约 if contract.exchange == EXCHANGE_SHFE: detail.mode = detail.MODE_SHFE # 检查是否有平今惩罚 for productID in self.tdPenaltyList: if str(productID) in contract.symbol: detail.mode = detail.MODE_TDPENALTY return detail #---------------------------------------------------------------------- def getAllPositionDetails(self): """查询所有本地持仓缓存细节""" return self.detailDict.values() #---------------------------------------------------------------------- def updateOrderReq(self, req, vtOrderID): """委托请求更新""" vtSymbol = req.vtSymbol detail = self.getPositionDetail(vtSymbol) detail.updateOrderReq(req, vtOrderID) #---------------------------------------------------------------------- def convertOrderReq(self, req): """根据规则转换委托请求""" detail = self.detailDict.get(req.vtSymbol, None) if not detail: return [req] else: return detail.convertOrderReq(req) #---------------------------------------------------------------------- def getLog(self): """获取日志""" return self.logList #---------------------------------------------------------------------- def getError(self): """获取错误""" return self.errorList
class OmEngine(object): """期权主引擎""" impvFileName = 'PricingImpv.vt' impvFilePath = getTempPath(impvFileName) #---------------------------------------------------------------------- def __init__(self, mainEngine, eventEngine): """Constructor""" self.mainEngine = mainEngine self.eventEngine = eventEngine self.portfolio = None self.optionContractDict = {} # symbol:contract self.strategyEngine = OmStrategyEngine(self, eventEngine) self.registerEvent() #---------------------------------------------------------------------- def registerEvent(self): """注册事件监听""" self.eventEngine.register(EVENT_CONTRACT, self.processContractEvent) #---------------------------------------------------------------------- def processTickEvent(self, event): """行情事件""" tick = event.dict_['data'] self.portfolio.newTick(tick) #---------------------------------------------------------------------- def processTradeEvent(self, event): """成交事件""" trade = event.dict_['data'] self.portfolio.newTrade(trade) #---------------------------------------------------------------------- def processContractEvent(self, event): """合约事件""" contract = event.dict_['data'] if contract.symbol and contract.productClass == PRODUCT_OPTION: self.optionContractDict[contract.symbol] = contract #---------------------------------------------------------------------- def subscribeEvent(self, symbol): """订阅对应合约的事件""" contract = self.mainEngine.getContract(symbol) if not contract: self.writeLog(u'行情订阅失败,找不到合约:%s' % symbol) return vtSymbol = contract.vtSymbol # 订阅行情 req = VtSubscribeReq() req.symbol = contract.symbol req.exchange = contract.exchange self.mainEngine.subscribe(req, contract.gatewayName) # 订阅事件 self.eventEngine.register(EVENT_TICK + vtSymbol, self.processTickEvent) self.eventEngine.register(EVENT_TRADE + vtSymbol, self.processTradeEvent) #---------------------------------------------------------------------- def initEngine(self, fileName): """初始化引擎""" if self.portfolio: return False f = open(fileName) setting = json.load(f) f.close() # 读取定价模型 model = MODEL_DICT.get(setting['model'], None) if not model: self.writeLog(u'找不到定价模型%s' % setting['model']) return # 创建标的对象 underlyingDict = OrderedDict() for underlyingSymbol in setting['underlying']: contract = self.mainEngine.getContract(underlyingSymbol) if not contract: self.writeLog(u'找不到标的物合约%s' % underlyingSymbol) continue detail = self.mainEngine.getPositionDetail(contract.vtSymbol) underlying = OmUnderlying(contract, detail) underlyingDict[underlyingSymbol] = underlying # 创建期权链对象并初始化 chainList = [] for d in setting['chain']: chainSymbol = d['chainSymbol'] r = d['r'] # 锁定标的对象 underlying = underlyingDict.get(d['underlyingSymbol'], None) if not underlying: self.writeLog(u'%s期权链的标的合约%s尚未创建,请检查配置文件' % (chainSymbol, underlyingSymbol)) continue # 创建期权对象并初始化 callDict = {} putDict = {} for symbol, contract in self.optionContractDict.items(): if contract.underlyingSymbol == d['chainSymbol']: detail = self.mainEngine.getPositionDetail( contract.vtSymbol) option = OmOption(contract, detail, underlying, model, r) if contract.optionType is OPTION_CALL: callDict[option.k] = option else: putDict[option.k] = option # 期权排序 strikeList = sorted(callDict.keys()) callList = [callDict[k] for k in strikeList] putList = [putDict[k] for k in strikeList] # 创建期权链 chain = OmChain(chainSymbol, callList, putList) chainList.append(chain) # 添加标的映射关系 underlying.addChain(chain) # 创建持仓组合对象并初始化 self.portfolio = OmPortfolio(setting['name'], model, underlyingDict.values(), chainList) # 载入波动率配置 self.loadImpvSetting() # 订阅行情和事件 for underlying in underlyingDict.values(): self.subscribeEvent(underlying.vtSymbol) for chain in chainList: for option in chain.optionDict.values(): self.subscribeEvent(option.vtSymbol) # 载入成功返回 return True #---------------------------------------------------------------------- def loadImpvSetting(self): """载入波动率配置""" f = shelve.open(self.impvFilePath) for chain in self.portfolio.chainDict.values(): for option in chain.optionDict.values(): option.pricingImpv = f.get(option.symbol, 0) f.close() #---------------------------------------------------------------------- def saveImpvSetting(self): """保存波动率配置""" if not self.portfolio: return f = shelve.open(self.impvFilePath) for chain in self.portfolio.chainDict.values(): for option in chain.optionDict.values(): f[option.symbol] = option.pricingImpv f.close() #---------------------------------------------------------------------- def stop(self): """关闭函数""" self.saveImpvSetting() #---------------------------------------------------------------------- def writeLog(self, content): """发出日志 """ log = VtLogData() log.logContent = content event = Event(EVENT_OM_LOG) event.dict_['data'] = log 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))