"""
|
交易记录日志工具
|
"""
|
|
# 下单信息
|
import json
|
|
from log_module import async_log_util
|
from log_module.log import logger_trade_record
|
|
TYPE_PLACE_ORDER = "place_order" # 下单
|
TYPE_REAL_PLACE_ORDER_POSITION = "real_place_order_position" # 真实下单位置
|
TYPE_CANCEL_WATCH_INDEXES = "cancel_watch_indexes" # 撤单监听位置
|
TYPE_FORBIDDEN_BUY = "forbidden_buy" # 禁止买入
|
TYPE_CANT_PLACE_ORDER = "can_not_place_order" # 不能下单
|
TYPE_CANCEL = "cancel" # 撤单
|
|
|
class PlaceOrderInfo(object):
|
def __init__(self, buy_single_index=None, buy_exec_index=None, m_val=None, safe_count=None, big_num_indexes=None,
|
kpl_blocks=None, kpl_match_blocks=None, mode=None, mode_desc=None, sell_info=None):
|
self.buy_single_index = buy_single_index
|
self.buy_exec_index = buy_exec_index
|
self.m_val = m_val
|
self.safe_count = safe_count
|
self.big_num_indexes = big_num_indexes
|
self.kpl_blocks = kpl_blocks
|
self.kpl_match_blocks = kpl_match_blocks
|
self.mode = mode
|
self.mode_desc = mode_desc
|
self.sell_info = sell_info
|
|
def set_buy_index(self, buy_single_index, buy_exec_index):
|
self.buy_single_index = buy_single_index
|
self.buy_exec_index = buy_exec_index
|
|
def set_sell_info(self, sell_info):
|
self.sell_info = sell_info
|
|
def set_trade_factor(self, m_val, safe_count, big_num_indexes):
|
self.m_val = m_val
|
self.safe_count = safe_count
|
self.big_num_indexes = big_num_indexes
|
|
def set_kpl_blocks(self, kpl_blocks):
|
self.kpl_blocks = kpl_blocks
|
|
def set_kpl_match_blocks(self, kpl_blocks):
|
self.kpl_match_blocks = kpl_blocks
|
|
def to_json_str(self):
|
return json.dumps(vars(self))
|
|
def to_dict(self):
|
return vars(self)
|
|
@classmethod
|
def to_object(cls, json_str: str):
|
d = json.loads(json_str)
|
return PlaceOrderInfo(**d)
|
|
|
# 撤单揽括信息
|
class CancelWatchIndexesInfo(object):
|
CANCEL_TYPE_H = "h_cancel"
|
CANCEL_TYPE_S = "s_cancel"
|
CANCEL_TYPE_L_UP = "l_cancel_up"
|
CANCEL_TYPE_L_DOWN = "l_cancel_down"
|
CANCEL_TYPE_D = "d_cancel"
|
|
def __init__(self, cancel_type, buy_single_index, watch_indexes: list):
|
self.cancel_type = cancel_type
|
self.watch_indexes = watch_indexes
|
self.buy_single_index = buy_single_index
|
|
def to_json_str(self):
|
return json.dumps(vars(self))
|
|
def to_dict(self):
|
return vars(self)
|
|
@classmethod
|
def to_object(cls, json_str: str):
|
d = json.loads(json_str)
|
return CancelWatchIndexesInfo(**d)
|
|
|
# 添加日志
|
def __add_log(type, code, data):
|
try:
|
fdata = {"code": code, "type": type, "data": data}
|
async_log_util.info(logger_trade_record, json.dumps(fdata))
|
except:
|
pass
|
|
|
# 添加下单日志
|
def add_place_order_log(code, info: PlaceOrderInfo):
|
__add_log(TYPE_PLACE_ORDER, code, info.to_dict())
|
|
|
# 加入禁止买入
|
def add_forbidden_buy_log(code, msg):
|
__add_log(TYPE_FORBIDDEN_BUY, code, {"msg": msg})
|
|
|
__latest_cant_place_order_log_msg_dict = {}
|
|
|
# 不能买
|
def add_cant_place_order_log(code, msg):
|
if __latest_cant_place_order_log_msg_dict.get(code) == msg:
|
return
|
__latest_cant_place_order_log_msg_dict[code] = msg
|
__add_log(TYPE_CANT_PLACE_ORDER, code, {"msg": msg})
|
|
|
# 真实下单位置
|
def add_real_place_order_position_log(code, index, buy_single_index):
|
__add_log(TYPE_REAL_PLACE_ORDER_POSITION, code, {"index": index, "buy_single_index": buy_single_index})
|
|
|
# 撤单策略监听位置
|
def add_cancel_watch_indexes_log(code, info: CancelWatchIndexesInfo):
|
__add_log(TYPE_CANCEL_WATCH_INDEXES, code, info.to_dict())
|
|
|
# 添加撤单信息
|
def add_cancel_msg_log(code, real_place_order_index, msg):
|
__add_log(TYPE_CANCEL, code, {"msg": msg, "real_place_order_index": real_place_order_index})
|
|
|
if __name__ == "__main__":
|
pass
|