"""
|
华鑫交易
|
"""
|
import json
|
import time
|
|
import constant
|
from db.redis_manager import RedisManager, RedisUtils
|
from log_module.log import logger_juejin_trade, hx_logger_trade_debug
|
from trade.huaxin import huaxin_trade_api
|
from utils import tool, huaxin_util
|
from l2 import huaxin
|
|
__context_dict = {}
|
|
|
# 交易订单号管理
|
class TradeOrderIdManager:
|
__redisManager = RedisManager(2)
|
|
@classmethod
|
def __get_redis(cls):
|
return cls.__redisManager.getRedis()
|
|
# 添加订单ID
|
@classmethod
|
def add_order_id(cls, code, account_id, order_id):
|
RedisUtils.sadd( cls.__get_redis(), f"huaxin_order_id-{code}", json.dumps((account_id, order_id)))
|
RedisUtils.expire(cls.__get_redis(), f"huaxin_order_id-{code}", tool.get_expire())
|
|
# 删除订单ID
|
@classmethod
|
def remove_order_id(cls, code, account_id, order_id):
|
RedisUtils.srem(cls.__get_redis(), f"huaxin_order_id-{code}", json.dumps((account_id, order_id)))
|
|
# 查询所有的订单号
|
@classmethod
|
def list_order_ids(cls, code):
|
return RedisUtils.smembers(cls.__get_redis(), f"huaxin_order_id-{code}")
|
|
|
def init(context):
|
__context_dict["init"] = context
|
print("掘金交易初始化成功")
|
|
|
# 可用金额
|
def get_account_left_money():
|
if "init" in __context_dict:
|
dict_ = __context_dict["init"].account().cash
|
return dict_["available"]
|
return None
|
|
|
# 通过量下单,返回(代码,账号ID,订单号)
|
def order_volume(code, price, count, last_data_index):
|
price = round(float(price), 2)
|
if code.find("00") != 0 and code.find("60") != 0:
|
raise Exception("只支持00开头与60开头的代码下单")
|
start_time = time.time()
|
# 保存下单信息
|
huaxin.huaxin_delegate_postion_manager.place_order(code, price, count, last_data_index)
|
if not constant.TRADE_ENABLE:
|
return
|
result = None
|
try:
|
result = huaxin_trade_api.order(1, code, count, price)
|
print("华鑫下单耗时", time.time() - start_time)
|
hx_logger_trade_debug.info(f"{code}:下单耗时{round(time.time() - start_time, 3)}s")
|
except Exception as e:
|
if str(e).find("超时") >= 0:
|
# 此处出现超时,需要通过读取委托列表来设置订单信息
|
hx_logger_trade_debug.error(f"{code}:下单结果反馈出错-{str(e)}")
|
else:
|
raise e
|
|
if result:
|
print("下单结果", result)
|
if result['code'] == 0:
|
result = result["data"]
|
if result["orderStatus"] == huaxin_util.TORA_TSTP_OST_Rejected:
|
logger_juejin_trade.info(f"{code}:下单失败:{result.get('statusMsg')}")
|
raise Exception(result.get('statusMsg'))
|
else:
|
TradeOrderIdManager.add_order_id(code, result["accountID"], result["orderSysID"])
|
logger_juejin_trade.info(f"{code}:下单成功 orderSysID:{result['orderSysID']}")
|
return result["securityId"], result["accountID"], result["orderSysID"]
|
else:
|
raise Exception(result['msg'])
|
else:
|
raise Exception("下单失败,无返回")
|
|
|
def order_success(code, accountId, orderSysID):
|
TradeOrderIdManager.add_order_id(code, accountId, orderSysID)
|
|
|
def cancel_order_success(code, accountId, orderSysID):
|
TradeOrderIdManager.remove_order_id(code, accountId, orderSysID)
|
|
|
# 撤单
|
def cancel_order(code):
|
orders_info = TradeOrderIdManager.list_order_ids(code)
|
orders = []
|
if orders_info:
|
for order in orders_info:
|
order_info = json.loads(order)
|
orders.append({'orderSysID': order_info[1], 'accountId': order_info[0]})
|
if orders:
|
logger_juejin_trade.info(f"{code}:开始执行撤单")
|
logger_juejin_trade.info(f"{code}:撤单成功,撤单数量:{len(orders)}")
|
for order in orders:
|
huaxin_trade_api.cancel_order(1, code, order["orderSysID"])
|
TradeOrderIdManager.remove_order_id(code, order["accountId"], order["orderSysID"])
|
|
|
if __name__ == "__main__":
|
pass
|