"""
|
华鑫LV2处理工具类
|
"""
|
|
|
# 处理逐笔委托
|
# item逐笔委托
|
# (data['SecurityID'], data['Price'], data['Volume'], data['Side'], data['OrderType'],
|
# data['OrderTime'],data['MainSeq'], data['SubSeq'], data['OrderNO'], data['OrderStatus'])
|
|
|
def convert_time(time_str, with_ms=False):
|
time_str = str(time_str)
|
if time_str.startswith("9"):
|
time_str = f"0{time_str}"
|
ms = "{:0<3}".format(time_str[6:])
|
time_ = f"{time_str[0:2]}:{time_str[2:4]}:{time_str[4:6]}"
|
if with_ms:
|
return f"{time_}.{ms}"
|
return time_
|
|
|
def __convert_order(item, limit_up_price):
|
time_str = f"{item[5]}"
|
if time_str.startswith("9"):
|
time_str = f"0{time_str}"
|
ms = "{:0<3}".format(time_str[6:])
|
time_ = f"{time_str[0:2]}:{time_str[2:4]}:{time_str[4:6]}"
|
price = item[1]
|
if price <= 0:
|
# 深证的买撤无价格数据,需要去查找价格数据,暂时设置为涨停价
|
price = limit_up_price
|
|
limitPrice = 1 if abs(limit_up_price - price) < 0.001 else 0
|
operateType = 0
|
if item[9] == 'D':
|
if item[3] == '1':
|
# 买撤
|
operateType = 1
|
else:
|
# 卖撤
|
operateType = 3
|
else:
|
if item[3] == '1':
|
# 买
|
operateType = 0
|
else:
|
# 卖
|
operateType = 2
|
return {"time": time_, "tms": ms, "price": price, "num": item[2] // 100, "limitPrice": limitPrice,
|
"operateType": operateType, "cancelTime": 0, "cancelTimeUnit": 0, "orderNo": item[8],
|
"mainSeq": item[6], "subSeq": item[7]}
|
|
|
def __format_l2_data(origin_datas, code, limit_up_price, filter_not_limit_up=True, filter_limit_up_sell=False):
|
"""
|
处理l2数据
|
@param origin_datas:原始数据
|
@param code:代码
|
@param limit_up_price:涨停价
|
@param filter_not_limit_up:是否过滤掉非涨停数据
|
@param filter_limit_up_sell:是否过滤涨停卖
|
@return:格式化过后的数据
|
"""
|
datas = []
|
dataIndexs = {}
|
same_time_num = {}
|
for i in range(0, len(origin_datas)):
|
item = origin_datas[i]
|
# 解析数据
|
time = item["time"]
|
if time in same_time_num:
|
same_time_num[time] = same_time_num[time] + 1
|
else:
|
same_time_num[time] = 1
|
|
price = item["price"]
|
num = item["num"]
|
limitPrice = item["limitPrice"]
|
# 涨停价
|
if limit_up_price is not None:
|
if abs(price - limit_up_price) < 0.001:
|
limitPrice = 1
|
else:
|
limitPrice = 0
|
item["limitPrice"] = "{}".format(limitPrice)
|
operateType = item["operateType"]
|
# 不需要非涨停买与买撤
|
if filter_not_limit_up and int(item["limitPrice"]) != 1 and (
|
int(operateType) == 0 or int(operateType) == 1) and num != 1:
|
continue
|
|
if filter_limit_up_sell and int(item["limitPrice"]) == 1 and int(operateType) == 2:
|
# 过滤涨停卖
|
continue
|
|
key = "{}-{}-{}".format(code, item["mainSeq"], item["subSeq"])
|
if key in dataIndexs:
|
# 数据重复次数+1
|
datas[dataIndexs[key]]['re'] = datas[dataIndexs[key]]['re'] + 1
|
else:
|
# 数据重复次数默认为1
|
datas.append({"key": key, "val": item, "re": 1})
|
dataIndexs.setdefault(key, len(datas) - 1)
|
return datas
|
|
|
def get_format_l2_datas(code, origin_datas, limit_up_price, start_index, filter_limit_up_sell=False):
|
"""
|
华鑫L2数据格式化
|
@param code:
|
@param origin_datas:
|
@param limit_up_price:涨停价
|
@param start_index:
|
@param filter_limit_up_sell:是否过滤涨停卖
|
@return:
|
"""
|
|
# 先转变数据格式
|
datas = [__convert_order(x, float(limit_up_price)) for x in origin_datas]
|
# 在9:25之前不过滤非涨停金额
|
# filter_not_limit_up = True
|
# if int(datas[0]["time"][:5].replace(":", "")) <= 925:
|
# filter_not_limit_up = False
|
# 不过滤非涨停金额
|
filter_not_limit_up = False
|
fdatas = __format_l2_data(datas, code, float(limit_up_price), filter_not_limit_up=filter_not_limit_up,
|
filter_limit_up_sell=filter_limit_up_sell)
|
for i in range(0, len(fdatas)):
|
fdatas[i]["index"] = start_index + i
|
return fdatas
|
|
|
# 获取大资金的金额
|
def get_big_money_val(limit_up_price):
|
if limit_up_price > 3.0:
|
return min(299 * 10000, round(limit_up_price * 7999 * 100))
|
else:
|
max_money = limit_up_price * 10000 * 100
|
return int(max_money * 0.95)
|
|
if __name__ == "__main__":
|
ds = ["('605167', 10.08, 68500, '1', '0', 9303108, 2, 439438, 436472, 'D', 1695864632451)",
|
"('603439', 17.97, 27800, '1', '0', 9304966, 6, 435127, 407524, 'D', 1695864649883)",
|
"('002369', 0.0, 100800, '1', '2', 93051880, 2011, 1431910, 1160638, 'D', 1695864651875)"
|
]
|
for d in ds:
|
d = eval(d)
|
print(__convert_order(d, 15.55))
|