| | |
| | | # 交易管理器 |
| | | import time |
| | | |
| | | import dask |
| | | |
| | | from db import mysql_data, redis_manager |
| | | from trade import trade_data_manager, l2_trade_util |
| | | from trade.trade_gui import THSBuyWinManagerNew, THSGuiTrade |
| | | import time as t |
| | | from l2 import l2_data_manager |
| | | from l2 import l2_data_manager, l2_data_log |
| | | |
| | | from log import * |
| | | |
| | |
| | | |
| | | # 开始交易 |
| | | def start_buy(code, capture_timestamp, last_data, last_data_index): |
| | | # 是否禁止交易 |
| | | if l2_trade_util.is_in_forbidden_trade_codes(code): |
| | | raise Exception("禁止交易") |
| | | trade_state = get_trade_state(code) |
| | | if trade_state != TRADE_STATE_NOT_TRADE and trade_state != TRADE_STATE_BUY_CANCEL_SUCCESS and trade_state != TRADE_STATE_BUY_CANCEL_ING: |
| | | raise Exception("代码处于不可交易状态") |
| | | money = get_available_money() |
| | | if money is None: |
| | | raise Exception("未获取到账户可用资金") |
| | | price = gpcode_manager.get_limit_up_price(code) |
| | | if price is None: |
| | | raise Exception("尚未获取到涨停价") |
| | | # 买一手的资金是否足够 |
| | | if price * 100 > money: |
| | | raise Exception("账户可用资金不足") |
| | | @dask.delayed |
| | | def is_forbidden(code): |
| | | if l2_trade_util.is_in_forbidden_trade_codes(code): |
| | | return Exception("禁止交易") |
| | | return None, None |
| | | |
| | | @dask.delayed |
| | | def is_state_right(code): |
| | | trade_state = get_trade_state(code) |
| | | if trade_state != TRADE_STATE_NOT_TRADE and trade_state != TRADE_STATE_BUY_CANCEL_SUCCESS and trade_state != TRADE_STATE_BUY_CANCEL_ING: |
| | | return Exception("代码处于不可交易状态"), trade_state |
| | | return None, trade_state |
| | | |
| | | @dask.delayed |
| | | def is_money_enough(code): |
| | | money = get_available_money() |
| | | if money is None: |
| | | return Exception("未获取到账户可用资金"), None |
| | | price = gpcode_manager.get_limit_up_price(code) |
| | | if price is None: |
| | | return Exception("尚未获取到涨停价"), None |
| | | # 买一手的资金是否足够 |
| | | if price * 100 > money: |
| | | return Exception("账户可用资金不足"), price |
| | | return None, price |
| | | |
| | | @dask.delayed |
| | | def can_trade(*args): |
| | | for arg in args: |
| | | if arg[0] is not None: |
| | | return arg[0], None, None |
| | | return None, args[1][1], args[2][1] |
| | | |
| | | _start_time = tool.get_now_timestamp() |
| | | |
| | | f1 = is_forbidden(code) |
| | | f2 = is_state_right(code) |
| | | f3 = is_money_enough(code) |
| | | dask_result = can_trade(f1, f2, f3) |
| | | ex, trade_state, price = dask_result.compute() |
| | | if ex is not None: |
| | | raise ex |
| | | |
| | | # 并行改造 |
| | | # # 是否禁止交易 |
| | | # if l2_trade_util.is_in_forbidden_trade_codes(code): |
| | | # raise Exception("禁止交易") |
| | | # trade_state = get_trade_state(code) |
| | | # if trade_state != TRADE_STATE_NOT_TRADE and trade_state != TRADE_STATE_BUY_CANCEL_SUCCESS and trade_state != TRADE_STATE_BUY_CANCEL_ING: |
| | | # raise Exception("代码处于不可交易状态") |
| | | # money = get_available_money() |
| | | # if money is None: |
| | | # raise Exception("未获取到账户可用资金") |
| | | # price = gpcode_manager.get_limit_up_price(code) |
| | | # if price is None: |
| | | # raise Exception("尚未获取到涨停价") |
| | | # # 买一手的资金是否足够 |
| | | # if price * 100 > money: |
| | | # raise Exception("账户可用资金不足") |
| | | |
| | | print("开始买入") |
| | | logger_trade.info("{}开始买入".format(code)) |
| | | set_trade_state(code, TRADE_STATE_BUY_PLACE_ORDER) |
| | | _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, "买入判断时间", force=True) |
| | | __buy(code, price, trade_state, capture_timestamp, last_data, last_data_index) |
| | | l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, "异步买入时间", force=True) |
| | | |
| | | |
| | | # 中断买入 |
| | |
| | | break |
| | | except Exception as e: |
| | | logger_trade.error("{}再次撤单异常:{}".format(code, str(e))) |
| | | time.sleep(0.1+0.05*i) |
| | | time.sleep(0.1 + 0.05 * i) |
| | | pass |
| | | |
| | | |
| | | # 取消委托成功 |
| | | def __cancel_success(code): |
| | | trade_data_manager.TradeBuyDataManager.remove_buy_position_info(code) |