""" tqz_logging.py """
import logging
import os
from vnpy.trader.tqz_extern.tools.file_path_operator.file_path_operator import TQZFilePathOperator
class TQZLogging:
__strategy_bug_fold = TQZFilePathOperator.current_file_father_path(file=__file__) + f'/strategy_bug_log'
__formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(name)s - %(message)s')
@classmethod
def write_bug_log(cls, vt_symbol: str, message: str):
if os.path.exists(cls.__strategy_bug_fold) is False:
os.makedirs(cls.__strategy_bug_fold)
symbol = vt_symbol.split('.')[0]
log_path = cls.__strategy_bug_fold + f'/{symbol}.log'
logger = logging.getLogger(vt_symbol)
logger.setLevel(level=logging.DEBUG)
handler = logging.FileHandler(log_path) # filename
handler.setLevel(logging.INFO)
handler.setFormatter(cls.__formatter)
logger.addHandler(handler)
logger.info(message) # message
logger.removeHandler(handler)
if __name__ == '__main__':
# TQZLogging.write_bug_log(vt_symbol="rb2201.SHFE", message="this is a test.")
pass
"""engine.py"""
def process_account_event(self, event: Event):
# strategy data
current_strategy_data = TQZPositionJsonOperator.tqz_get_single_jsonfile_format_data(jsonfile=self.data_filename)
current_strategy_data_format = TQZPositionJsonOperator.tqz_get_sum_position_format_data_with_jsonfileContentList(current_strategy_data)
# strategy data change
if current_strategy_data_format != self.last_strategy_data_format:
self.__compare_strategy_position(last_strategy_data_format=self.last_strategy_data_format, current_strategy_data_format=current_strategy_data_format)
self.last_strategy_data_format = current_strategy_data_format
def __compare_strategy_position(self, last_strategy_data_format, current_strategy_data_format):
for current_vt_symbol in current_strategy_data_format.keys():
message_list = []
if current_vt_symbol in last_strategy_data_format.keys(): # last_strategy_data have current_vt_symbol
if current_strategy_data_format[current_vt_symbol][TQZPositionKeyType.BUY_POSITION_KEY.value] is not last_strategy_data_format[current_vt_symbol][TQZPositionKeyType.BUY_POSITION_KEY.value]:
message = f'[strategy position is not equal][vt_symbol: {str(current_vt_symbol)}, pre buy position: {str(last_strategy_data_format[current_vt_symbol][TQZPositionKeyType.BUY_POSITION_KEY.value])}, current buy position: {str(current_strategy_data_format[current_vt_symbol][TQZPositionKeyType.BUY_POSITION_KEY.value])}]'
message_list.append(message)
if current_strategy_data_format[current_vt_symbol][TQZPositionKeyType.SELL_POSITION_KEY.value] is not last_strategy_data_format[current_vt_symbol][TQZPositionKeyType.SELL_POSITION_KEY.value]:
message = f'[strategy position is not equal][vt_symbol: {str(current_vt_symbol)}, pre sell position: {str(last_strategy_data_format[current_vt_symbol][TQZPositionKeyType.SELL_POSITION_KEY.value])}, current sell position: {str(current_strategy_data_format[current_vt_symbol][TQZPositionKeyType.SELL_POSITION_KEY.value])}]'
message_list.append(message)
else: # last_strategy_data have not current_vt_symbol
if current_strategy_data_format[current_vt_symbol][TQZPositionKeyType.BUY_POSITION_KEY.value] is not 0:
message = f'[strategy position is not equal][vt_symbol: {str(current_vt_symbol)}, pre buy position: {str(0)}, current buy position: {str(current_strategy_data_format[current_vt_symbol][TQZPositionKeyType.BUY_POSITION_KEY.value])}]'
message_list.append(message)
if current_strategy_data_format[current_vt_symbol][TQZPositionKeyType.SELL_POSITION_KEY.value] is not 0:
message = f'[strategy position is not equal][vt_symbol: {str(current_vt_symbol)}, pre sell position: {str(0)}, current sell position: {str(current_strategy_data_format[current_vt_symbol][TQZPositionKeyType.SELL_POSITION_KEY.value])}]'
message_list.append(message)
if self.log_start is True:
[self.write_bug_log(vt_symbol=current_vt_symbol, message=message) for message in message_list]
def process_position_event(self, event: Event):
""" """
position = event.data
pre_positions = copy(self.positions)
self.positions[position.vt_positionid] = position
self.offset_converter.update_position(position)
if pre_positions != self.positions: # real position have change.
self.__compare_real_position(pre_positions=pre_positions, current_positions=self.positions)
def __compare_real_position(self, pre_positions, current_positions):
message_dictionary = {}
for vt_symbol_direction, position_data in current_positions.items(): # scan current real positions
message: str = ''
if vt_symbol_direction in pre_positions.keys(): # pre_positions data have this vt_symbol_direction
if position_data.direction in [Direction.LONG]: # judge position_data direction
if position_data.volume is not pre_positions[vt_symbol_direction].volume:
message = f'[real position is not equal][vt_symbol: {str(position_data.vt_symbol)}, pre buy position: {str(pre_positions[vt_symbol_direction].volume)}, current buy position: {str(position_data.volume)}]'
elif position_data.direction in [Direction.SHORT]:
if position_data.volume is not pre_positions[vt_symbol_direction].volume:
message = f'[real position is not equal][vt_symbol: {str(position_data.vt_symbol)}, pre sell position: {str(pre_positions[vt_symbol_direction].volume)}, current sell position: {str(position_data.volume)}]'
else: # pre_positions data have not vt_symbol_direction
if position_data.direction in [Direction.LONG]: # judge position_data direction
if position_data.volume is not 0:
message = f'[real position is not equal][vt_symbol: {str(position_data.vt_symbol)}, pre buy position: {str(0)}, current buy position: {str(position_data.volume)}]'
elif position_data.direction in [Direction.SHORT]:
if position_data.volume is not 0:
message = f'[real position is not equal][vt_symbol: {str(position_data.vt_symbol)}, pre sell position: {str(0)}, current sell position: {str(position_data.volume)}]'
if '' != message:
message_dictionary[position_data.vt_symbol] = message
if self.log_start is True: # write log of bug or not
[self.write_bug_log(vt_symbol=vt_symbol, message=message) for vt_symbol, message in message_dictionary.items()]