| | |
| | | |
| | | # 撤卖单 |
| | | import time |
| | | |
| | |
| | | |
| | | # 开始撤单 |
| | | def start_sell(code, volume, price_type, limit_up_price, limit_down_price, current_price, blocking=False, |
| | | request_id=None,order_ref=None): |
| | | request_id=None, order_ref=None): |
| | | price = sell_util.get_sell_price(price_type, limit_up_price, limit_down_price, current_price) |
| | | if not price: |
| | | raise Exception("价格获取出错") |
| | |
| | | order_ref = huaxin_util.create_order_ref() |
| | | result = huaxin_trade_api.order(2, code, volume, price, order_ref=order_ref, |
| | | blocking=blocking, request_id=request_id) |
| | | return result |
| | | return result |
| | |
| | | import time |
| | | import concurrent.futures |
| | | |
| | | from code_atrribute import history_k_data_util |
| | | from log_module import async_log_util |
| | | from log_module.log import hx_logger_trade_debug, hx_logger_trade_loop, hx_logger_trade_callback, \ |
| | | logger_system |
| | |
| | | from trade.huaxin_trade_order_processor import CancelOrderManager, HuaxinOrderEntity, TradeResultProcessor |
| | | from trade.huaxin_trade_record_manager import TradeOrderIdManager |
| | | |
| | | from utils import socket_util, tool, huaxin_util |
| | | from utils import socket_util, tool, huaxin_util, cb_data_util |
| | | |
| | | __response_thread_pool = concurrent.futures.ThreadPoolExecutor(max_workers=15) |
| | | __save_data_queue = queue.Queue() |
| | |
| | | if huaxin_util.is_can_cancel(order_.orderStatus): |
| | | cancel_order(2, code, order_.orderSysID) |
| | | |
| | | def __sell_cb_code(code, volume): |
| | | # 获取现价 |
| | | results = history_k_data_util.HistoryKDatasUtils.get_gp_current_info([code]) |
| | | if results: |
| | | current_price = results[0]["price"] |
| | | price = round(tool.get_buy_min_price(current_price, True), 3) |
| | | results = order(2, code, volume, price) |
| | | async_log_util.info(hx_logger_trade_debug, f"卖出可转债结果:{results}") |
| | | # TODO 判断Ref是否被卖掉,如果没有卖掉,要继续卖 |
| | | |
| | | def __process_order(data): |
| | | code = data["securityID"] |
| | | accountID = data["accountID"] |
| | |
| | | direction = data.get("direction") |
| | | limitPrice = data.get("limitPrice") |
| | | volume = data.get("volume") |
| | | sinfo = data.get("sinfo") |
| | | |
| | | order = HuaxinOrderEntity(code, orderStatus, orderRef, accountID, orderSysID, |
| | | insertTime=insertTime, acceptTime=acceptTime, |
| | |
| | | if huaxin_util.is_deal(order.orderStatus): |
| | | # 如果成交了需要刷新委托列表 |
| | | huaxin_trade_data_update.add_delegate_list("卖成交") |
| | | else: |
| | | # 买入 |
| | | if tool.is_cb_code(code) and sinfo in cb_data_util.need_sell_sinfos and str( |
| | | orderStatus) == huaxin_util.TORA_TSTP_OST_AllTraded: |
| | | # 可转债买入 |
| | | # 卖出可转债 |
| | | __sell_cb_code(code, volume) |
| | | # TradeResultProcessor.process_buy_order(order) |
| | | need_watch_cancel = TradeResultProcessor.process_sell_order(order) |
| | | if need_watch_cancel: |
| | |
| | | def get_delegate_list(can_cancel=True, blocking=True, timeout=TIMEOUT): |
| | | request_id = __request(ClientSocketManager.CLIENT_TYPE_DELEGATE_LIST, |
| | | {"type": ClientSocketManager.CLIENT_TYPE_DELEGATE_LIST, |
| | | "can_cancel": 1 if can_cancel else 0}, blocking=blocking, is_pipe=is_pipe_channel_normal(), is_trade=True) |
| | | "can_cancel": 1 if can_cancel else 0}, blocking=blocking, is_pipe=is_pipe_channel_normal(), |
| | | is_trade=True) |
| | | |
| | | return __read_response(request_id, blocking, timeout=timeout) |
| | | |
| | |
| | | from trade import huaxin_trade_data_update, huaxin_sell_util, huaxin_trade_api |
| | | from trade.huaxin_trade_record_manager import PositionManager |
| | | from trade.sell_rule_manager import TradeRuleManager, SellRule |
| | | from utils import outside_api_command_manager, middle_api_protocol, tool, huaxin_util, socket_util |
| | | from utils import outside_api_command_manager, middle_api_protocol, tool, huaxin_util, socket_util, cb_data_util |
| | | from utils.outside_api_command_manager import ActionCallback |
| | | import concurrent.futures |
| | | |
| | |
| | | codes = L1DataProcessor.get_latest_update_codes() |
| | | result = {"code": 0, "data": list(codes)} |
| | | self.send_response(result, client_id, request_id) |
| | | elif ctype == "buy_cb_for_commission": |
| | | # 用可转债填交易费漏洞 |
| | | code = data["code"] |
| | | volume = data["volume"] |
| | | current_price = None |
| | | results = history_k_data_util.HistoryKDatasUtils.get_gp_current_info([code]) |
| | | if results: |
| | | current_price = results[0]["price"] |
| | | order_ref = huaxin_util.create_order_ref() |
| | | try: |
| | | if not current_price: |
| | | raise Exception("尚未获取到现价") |
| | | price = round(tool.get_buy_max_price(current_price), 3) |
| | | result = huaxin_trade_api.order(1, code, volume, price, blocking=True) |
| | | if result: |
| | | if result["code"] == 0: |
| | | sinfo = result["data"]["sinfo"] |
| | | # 需要立即卖的数据 |
| | | cb_data_util.need_sell_sinfos.add(sinfo) |
| | | # 记录当前的sinfo |
| | | async_log_util.info(logger_trade, f"API买结果: {result}") |
| | | self.send_response(result, client_id, request_id) |
| | | except Exception as e: |
| | | if str(e).find("超时") >= 0: |
| | | self.send_response({"code": 0, "data": {"orderRef": order_ref}}, client_id, request_id) |
| | | else: |
| | | raise e |
| | | |
| | | |
| | | class L1DataProcessor: |
New file |
| | |
| | | # 可转债需要卖的sinfo集合 |
| | | need_sell_sinfos = set() |
| | |
| | | |
| | | |
| | | # 获取买入价格笼子的最低价 |
| | | def get_buy_min_price(price): |
| | | def get_buy_min_price(price, is_cb=False): |
| | | price1 = price * (1 - 0.02) |
| | | price1 = math.ceil(price1 * 100) / 100 |
| | | if is_cb: |
| | | price1 = math.ceil(price1 * 1000) / 1000 |
| | | else: |
| | | price1 = math.ceil(price1 * 100) / 100 |
| | | price2 = price - 0.1 |
| | | return max(price1, price2) |
| | | |
| | | |
| | | def get_buy_max_price(price, is_cb=False): |
| | | price1 = price * (1 + 0.02) |
| | | if is_cb: |
| | | price1 = math.ceil(price1 * 1000) / 1000 |
| | | else: |
| | | price1 = math.ceil(price1 * 100) / 100 |
| | | price2 = price + 0.1 |
| | | return max(price1, price2) |
| | | |
| | | |
| | |
| | | |
| | | if __name__ == "__main__": |
| | | printlog(trade_time_sub("13:00:01", |
| | | "11:29:55")) |
| | | "11:29:55")) |
| | | |
| | | |
| | | class CodeDataCacheUtil: |
| | |
| | | return 0.9 |
| | | else: |
| | | return 0.8 |
| | | |
| | | |
| | | def get_thread_id(): |
| | | try: |
| | |
| | | return get_market_type(code) == MARKET_TYPE_SZSE |
| | | |
| | | |
| | | def is_cb_code(code): |
| | | """ |
| | | 是否是可转债 |
| | | @param code: |
| | | @return: |
| | | """ |
| | | return code.find("11")==0 or code.find("12")==0 |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | pass |
| | | print(get_buy_max_price(100.123, is_cb=True)) |