| | |
| | | from strategy import data_cache, account_management |
| | | import data_server |
| | | from log_module.log import logger_debug, logger_common |
| | | from strategy.trade_setting import TradeSetting |
| | | |
| | | from trade import huaxin_trade_api, huaxin_trade_data_update, middle_api_protocol |
| | | from utils import huaxin_util, tool |
| | |
| | | |
| | | # 下单买入函数(按金额,以限价买)【按金额买 基础版】 |
| | | def buy_order_by_value(symbol, buy_order_value, sec_name, current_price): |
| | | # 自动买 开关监听方法 |
| | | if not TradeSetting().get_auto_buy(): |
| | | # 暂停自动买 |
| | | logger.info(f"在交易方法函数处 关闭了 自动买") |
| | | return |
| | | if current_price < 3 or current_price > 30: |
| | | # 当前单价超出预设限制 |
| | | logger.info(f"当前标的个股{sec_name}单价超出预设限制!预设值3 < current_price < 30,当前最新价{current_price}") |
| | | return |
| | | price = round(float(current_price), 2) |
| | | volume = (int(buy_order_value / price) // 100) * 100 |
| | | if volume < 100: |
| | | volume = 100 |
| | | # 调用笼子价计算工具计算下单价格 |
| | | order_price = tool.get_buy_max_price(current_price) |
| | | order_price = round(tool.get_buy_max_price(current_price), 2) |
| | | buy_order = huaxin_trade_api.order(1, symbol[-6:], volume, order_price, blocking=True) |
| | | logger.info(f"current_price===={current_price} order_price===={order_price}") |
| | | logger.info(f"buy_order===={buy_order}") |
| | |
| | | # 调用资金查询函数 查看资金变化 |
| | | account_management.finance_management() |
| | | logger.info(f"更新的资金数据data_cache.account_finance_dict=={data_cache.account_finance_dict}") |
| | | if symbol[-6:] in data_cache.account_positions_dict: |
| | | logger.info(f"该股已经持仓==》{sec_name}") |
| | | pass |
| | | |
| | | # 因为上面的更新持仓数据函数会计算 今日新增持仓数量,所以如果再手动新增持仓数据会重复计算【考虑到持仓函数有可能会有延迟,这也可能是同时运行一段时间没有出现BUG的原因,先暂时保留以下这段代码,只是注释】 |
| | | # # 买票后添加 持仓代码集合 |
| | | # data_cache.position_symbols_set.add(symbol) |
| | | # # 买票后添加 今日新增持仓代码集合 |
| | | # data_cache.addition_position_symbols_set.add(symbol) |
| | | # logger.info(f"当前持仓数量:::{len(data_cache.position_symbols_set)}") |
| | | # logger.info(f"今日新增持仓数量:::{len(data_cache.addition_position_symbols_set)}") |
| | | # todo 当前为测试阶段的冗余打印 |
| | | # 检测持仓信息中有无下单个股且有该个股的当前持仓,只有当前持仓数量不为0时,才认为交易成功 |
| | | for i in data_cache.account_positions_dict: |
| | | # print(i) |
| | | if i['securityID'] == symbol[-6:]: |
| | | # print(i['currentPosition']) |
| | | if i['currentPosition'] == 0: |
| | | logger.info(f"【{i['securityName']}】交易失败~") |
| | | else: |
| | | # 买票后添加 持仓代码集合 |
| | | data_cache.position_symbols_set.add(symbol) |
| | | logger.info(f"【{i['securityName']}】交易成功!") |
| | | |
| | | |
| | | # 下单买入函数(按可用资金的一定比例,在涨停价买)【按金额买 高级版】 |
| | |
| | | |
| | | # 下单卖出函数(按持仓数量,在限价卖)【按量卖 基础版】 |
| | | def sell_order_by_volume(symbol, volume, sec_name, current_price): |
| | | # 自动卖开关监听方法 |
| | | if not TradeSetting().get_auto_sell(): |
| | | # 暂停自动卖 |
| | | logger.info(f"在交易方法函数处 关闭了 自动卖") |
| | | return |
| | | # price = round(float(price), 2) |
| | | # 调用笼子价计算工具计算下单价格 |
| | | order_price = tool.get_buy_min_price(current_price) |
| | |
| | | :param index: 持仓对象列表中的个股对应序列号 |
| | | :return: 尝试返回的订单数据 |
| | | """ |
| | | |
| | | logger.info(f"当前个股持仓手数【当前函数被调用时传进来的同步数据data_cache中的持仓数据】==={position_volume_yesterday}") |
| | | # sell_order_volume = int(position_volume_yesterday * part_of_volume) |
| | | sell_order_volume = round(position_volume_yesterday * part_of_volume / 100) * 100 |