Administrator
2023-08-25 0dd67c24540b3e638a2da4832a973cf904d48dbd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
华鑫交易
"""
import copy
import json
import time
 
import constant
from log_module import async_log_util
from log_module.log import logger_juejin_trade, hx_logger_trade_debug, logger_trade
from trade.huaxin import huaxin_trade_api
from trade.huaxin.huaxin_trade_record_manager import TradeOrderIdManager
from utils import tool, huaxin_util
from l2 import huaxin
 
__context_dict = {}
 
__TradeOrderIdManager = TradeOrderIdManager()
 
 
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
    blocking = False
    try:
        result = huaxin_trade_api.order(1, code, count, price, blocking=blocking)
        print("华鑫下单耗时", time.time() - start_time)
        async_log_util.info(hx_logger_trade_debug, f"{code}:下单耗时{round(time.time() - start_time, 3)}s")
    except Exception as e:
        if str(e).find("超时") >= 0:
            # 此处出现超时,需要通过读取委托列表来设置订单信息
            async_log_util.error(hx_logger_trade_debug, f"{code}:下单结果反馈出错-{str(e)}")
        else:
            raise e
 
    if result:
        print("下单结果", result)
        if blocking:
            if result['code'] == 0:
                result = result["data"]
                if result["orderStatus"] == huaxin_util.TORA_TSTP_OST_Rejected:
                    async_log_util.info(hx_logger_trade_debug, f"{code}:下单失败:{result.get('statusMsg')}")
                    raise Exception(result.get('statusMsg'))
                else:
                    __TradeOrderIdManager.add_order_id(code, result["accountID"], result["orderSysID"])
                    async_log_util.info(hx_logger_trade_debug, f"{code}:下单成功 orderSysID:{result['orderSysID']}")
                    return result["securityId"], result["accountID"], result["orderSysID"]
            else:
                raise Exception(result['msg'])
        else:
            local_order_id = result["local_order_id"]
            __TradeOrderIdManager.add_local_order_id(code, local_order_id)
            async_log_util.info(hx_logger_trade_debug, f"{code}:下单成功 localOrderId:{local_order_id}")
            return code, "local", local_order_id
    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_cache(code)
    orders_info = copy.deepcopy(orders_info)
    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:
            async_log_util.info(logger_trade, f"{code}:华鑫开始执行撤单")
            for order in orders:
                huaxin_trade_api.cancel_order(1, code, order["orderSysID"])
                __TradeOrderIdManager.remove_order_id(code, order["accountId"], order["orderSysID"])
            async_log_util.info(logger_trade, f"{code}:华鑫撤单结束,撤单数量:{len(orders)}")
 
        # 查询是否有本地订单号
    local_orders_info = __TradeOrderIdManager.list_local_order_ids_cache(code)
 
    if local_orders_info:
        local_orders_info = copy.deepcopy(local_orders_info)
        for order_id in local_orders_info:
            async_log_util.info(logger_trade, f"{code}:华鑫开始执行撤单")
            huaxin_trade_api.cancel_order(1, code, '', localOrderID=order_id)
            __TradeOrderIdManager.remove_local_order_id(code, order_id)
            async_log_util.info(logger_trade, f"{code}:华鑫执行撤单结束")
 
 
if __name__ == "__main__":
    pass