def saveContracts(self): """保存所有合约对象到硬盘""" contractFilePath = WORKING_DIR + 'temp/contracts' data = { key: value.__dict__ for key, value in self.contractDict.items() } save_json(data, contractFilePath)
def saveSetting(self): """保存策略配置""" l = [] for strategy in self.strategyDict.values(): setting = {} for param in strategy.paramList: setting[param] = strategy.__getattribute__(param) l.append(setting) save_json(l, self.settingfilePath)
def saveSyncData(self, strategy): """保存策略的持仓情况到本地""" flt = {'name': strategy.name, 'vtSymbol': strategy.vtSymbol} d = copy(flt) for key in strategy.syncList: d[key] = strategy.__getattribute__(key) syncFile = WORKING_DIR + 'setting/syncdata/' + strategy.name + '-' + strategy.vtSymbol save_json(d, syncFile) # self.mainEngine.dbUpdate(POSITION_DB_NAME, strategy.className, # d, flt, True) content = '策略{name}持仓同步数据保存成功,当前持仓{pos}'.format(name=strategy.name, pos=strategy.pos) self.writeCtaLog(content)
def insertInstrument(self, event): """插入合约对象""" data = event.dict_['data'] last = event.dict_['last'] self.list_instrument.append(data) # 创建合约信息实例 contract = CtaContractData() contract.vtSymbol = contract.symbol = data['InstrumentID'] contract.name = data['InstrumentName'] contract.exchange = data['ExchangeID'] # 合约数值 contract.size = data['VolumeMultiple'] contract.priceTick = data['PriceTick'] contract.strikePrice = data['StrikePrice'] contract.underlyingSymbol = data['UnderlyingInstrID'] # 期权类型 if data['OptionsType'] == '1': contract.optionType = OPTION_CALL elif data['OptionsType'] == '2': contract.optionType = OPTION_PUT # 推送合约信息 self.onContract(contract) if last: # 最后一条数据 # 将查询完成的合约信息保存到本地文件 event = Event(type_=EVENT_LOG) log = '合约信息查询完成' event.dict_['log'] = log self.ee.put(event) # 保存合约信息到本地 self.saveContracts() # 字典格式的合约数据也保存一份 instrumentFile = WORKING_DIR + 'temp/InstrumentID.json' save_json(self.list_instrument, instrumentFile) # 推送日志 event = Event(type_=EVENT_LOG) log = '合约信息已经保存到本地' event.dict_['log'] = log self.ee.put(event)
def insertMarketData(self, event): """插入合约截面数据""" data = event.dict_['data'] last = event.dict_['last'] self.list_marketdata.append(data) if last: # 更新交易日 self.md.TradingDay = data['TradingDay'] # 将查询完成的合约截面数据保存到本地文件 event = Event(type_=EVENT_LOG) log = '合约截面数据查询完成' event.dict_['log'] = log self.ee.put(event) # 字典格式的合约截面数据也保存一份 makertdataFile = WORKING_DIR + 'temp/Marketdata.json' save_json(self.list_marketdata, makertdataFile) # 推送日志 event = Event(type_=EVENT_LOG) log = '合约截面数据已经保存到本地' event.dict_['log'] = log self.ee.put(event) if not self.ctaActive: self.runStrategy() self.ctaActive = True