遇到非大商所,没有ActionDay字段的data会报错
例如data:
{'TradingDay': '20220119', 'reserve1': 'SPD SA204&SA212', 'ExchangeID': '', 'reserve2': '', 'LastPrice': 1.7976931348623157e+308, 'PreSettlementPrice': 1.7976931348623157e+308, 'PreClosePrice': 0.0, 'PreOpenInterest': 0.0, 'OpenPrice': 0.0, 'HighestPrice': 0.0, 'LowestPrice': 0.0, 'Volume': 0, 'Turnover': 0.0, 'OpenInterest': 0.0, 'ClosePrice': 0.0, 'SettlementPrice': 1.7976931348623157e+308, 'UpperLimitPrice': 319.0, 'LowerLimitPrice': -513.0, 'PreDelta': 0.0, 'CurrDelta': 0.0, 'UpdateTime': '19:40:13', 'UpdateMillisec': 0, 'BidPrice1': 0.0, 'BidVolume1': 0, 'AskPrice1': 0.0, 'AskVolume1': 0, 'BidPrice2': 0.0, 'BidVolume2': 0, 'AskPrice2': 0.0, 'AskVolume2': 0, 'BidPrice3': 0.0, 'BidVolume3': 0, 'AskPrice3': 0.0, 'AskVolume3': 0, 'BidPrice4': 0.0, 'BidVolume4': 0, 'AskPrice4': 0.0, 'AskVolume4': 0, 'BidPrice5': 0.0, 'BidVolume5': 0, 'AskPrice5': 0.0, 'AskVolume5': 0, 'AveragePrice': 0.0, 'ActionDay': '', 'InstrumentID': 'SPD SA204&SA212', 'ExchangeInstID': ''}
初始为
# 对大商所的交易日字段取本地日期
if contract.exchange == Exchange.DCE:
date_str: str = self.current_date
else:
date_str: str = data["ActionDay"]
timestamp: str = f"{date_str} {data['UpdateTime']}.{int(data['UpdateMillisec']/100)}"
dt: datetime = datetime.strptime(timestamp, "%Y%m%d %H:%M:%S.%f")
修改为
# 对大商所的交易日字段取本地日期
if contract.exchange == Exchange.DCE:
date_str: str = self.current_date
else:
if data["ActionDay"] == '':
date_str: str = self.current_date
else:
date_str: str = data["ActionDay"]
timestamp: str = f"{date_str} {data['UpdateTime']}.{int(data['UpdateMillisec']/100)}"
dt: datetime = datetime.strptime(timestamp, "%Y%m%d %H:%M:%S.%f")