diff --git a/config.ini b/config.ini index aec6001..40b0856 100644 --- a/config.ini +++ b/config.ini @@ -1,7 +1,7 @@ [config] miniQMTPath=D:\\Programs\\DTQMT\\userdata_mini ; miniQMTPath=D:\\Programs\\DTQMT_MN\\userdata_mini ; 测试账号 -grid_price=1.665,1.660,1.655,1.650,1.645,1.640,1.635,1.630,1.625,1.620,1.615 +grid_price=1.665,1.660,1.655,1.650,1.645,1.640,1.635,1.630,1.625,1.620,1.615,1.610 ; grid_price=11.0,10.0,9.0,8.0,7.0,6.0,5.0,4.0,3.0,2.0,1.0 grid_volume = 200 account_no = '99082560' diff --git a/core/sfgrid_strategy.py b/core/sfgrid_strategy.py index 631d988..c125a63 100644 --- a/core/sfgrid_strategy.py +++ b/core/sfgrid_strategy.py @@ -61,111 +61,119 @@ class SFGridStrategy: return bool(self.tradeTarget.enabled) def onDataUpdate(self, data): - if not self.isEnabled(): - return + print(f'|- 标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 市价更新-START') self.dataUpdateLock.acquire() - index = self.tradeTarget.grid_index - price = sfgrid_constants.grid_price[int(index)] # pyright: ignore[reportArgumentType] - lowPrice = sfgrid_constants.grid_price[int(index) + 1] # pyright: ignore[reportArgumentType] - highPrice = sfgrid_constants.grid_price[int(index) - 1] # pyright: ignore[reportArgumentType] + try: + index = self.tradeTarget.grid_index + price = sfgrid_constants.grid_price[int(index)] # pyright: ignore[reportArgumentType] + lowPrice = sfgrid_constants.grid_price[int(index) + 1] if len(sfgrid_constants.grid_price)>int(index) + 1 else -1.0 # pyright: ignore[reportArgumentType] + highPrice = sfgrid_constants.grid_price[int(index) - 1] # pyright: ignore[reportArgumentType] - lastPrice = float("{:.3f}".format(data[self.tradeTarget.stock_code]['lastPrice'])) - print(f"标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 市价: {lastPrice}, 网格序号: {index}, 网格价格: {price}, 计划多单价: {lowPrice}, 计划空单价: {highPrice}") + lastPrice = float("{:.3f}".format(data[self.tradeTarget.stock_code]['lastPrice'])) + print(f"|- 标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 市价信息-价格: {lastPrice}, 网格序号: {index}, 网格价格: {price}, 计划多单价: {lowPrice}, 计划空单价: {highPrice}") - if lastPrice <= lowPrice: # 下下方多单 - orders = queryPendingOrder(str(self.tradeTarget.stock_code), self.getName(), self.xt_trader, self.account) - if len([order for order in orders if order.order_type == xtconstant.STOCK_BUY and order.price == lowPrice]) > 0: - # 已存在未交易的多单 - print(f' |- 已存在未交易的多单,不重复下单') - else: - print(f' |- 下网格多单') - self.tradeTarget.current_buy_order_no = self.xt_trader.order_stock_async( - self.account, - str(self.tradeTarget.stock_code), - xtconstant.STOCK_BUY, - sfgrid_constants.grid_volume, - xtconstant.FIX_PRICE, - lowPrice, - self.getName(), # strategy_name - self.tradeTarget.stock_code # remark # type: ignore - ) - self.tradeTarget.grid_index = int(index) + 1 # type: ignore - self.tradeTarget.current_buy_price = float(lowPrice) # type: ignore - print(f' |- 下网格多单号 {self.tradeTarget.current_buy_order_no}, 网格基准价 {price}, 下单价 {lowPrice}, 下单量 {sfgrid_constants.grid_volume}') - elif lastPrice == highPrice: # 下上方空单 - orders = queryPendingOrder(str(self.tradeTarget.stock_code), self.getName(), self.xt_trader, self.account) - if len([order for order in orders if order.order_type == xtconstant.STOCK_SELL and order.price == highPrice]) > 0: - # 已存在未交易的空单 - print(f' |- 已存在未交易的空单,不重复下单') - else: - print(f' |- 下网格空单') - self.tradeTarget.current_sell_order_no = self.xt_trader.order_stock_async( + if lastPrice <= lowPrice: # 下下方多单 + orders = queryPendingOrder(str(self.tradeTarget.stock_code), self.getName(), self.xt_trader, self.account) + if len([order for order in orders if order.order_type == xtconstant.STOCK_BUY and order.price == lowPrice]) > 0: + # 已存在未交易的多单 + print(f' |- 已存在未交易的多单,不重复下单') + else: + print(f' |- 下网格多单') + self.tradeTarget.current_buy_order_no = self.xt_trader.order_stock_async( self.account, str(self.tradeTarget.stock_code), - xtconstant.STOCK_SELL, + xtconstant.STOCK_BUY, sfgrid_constants.grid_volume, xtconstant.FIX_PRICE, - highPrice, - self.getName(), - self.tradeTarget.stock_code) # type: ignore - self.tradeTarget.grid_index = int(index) - 1 # type: ignore - self.tradeTarget.current_sell_price = float(highPrice) # type: ignore - print(f' |- 下网格空单号 {self.tradeTarget.current_sell_order_no}, 网格基准价 {price}, 下单价 {highPrice}, 下单量 {sfgrid_constants.grid_volume}') - self.tradeTarget.save() - self.dataUpdateLock.release() + lowPrice, + self.getName(), # strategy_name + self.tradeTarget.stock_code # remark # type: ignore + ) + self.tradeTarget.current_buy_price = float(lowPrice) # type: ignore + print(f' |- 下网格多单号 {self.tradeTarget.current_buy_order_no}, 网格基准价 {price}, 下单价 {lowPrice}, 下单量 {sfgrid_constants.grid_volume}') + elif lastPrice == highPrice: # 下上方空单 + orders = queryPendingOrder(str(self.tradeTarget.stock_code), self.getName(), self.xt_trader, self.account) + if len([order for order in orders if order.order_type == xtconstant.STOCK_SELL and order.price == highPrice]) > 0: + # 已存在未交易的空单 + print(f' |- 已存在未交易的空单,不重复下单') + else: + print(f' |- 下网格空单') + self.tradeTarget.current_sell_order_no = self.xt_trader.order_stock_async( + self.account, + str(self.tradeTarget.stock_code), + xtconstant.STOCK_SELL, + sfgrid_constants.grid_volume, + xtconstant.FIX_PRICE, + highPrice, + self.getName(), + self.tradeTarget.stock_code) # type: ignore + self.tradeTarget.current_sell_price = float(highPrice) # type: ignore + print(f' |- 下网格空单号 {self.tradeTarget.current_sell_order_no}, 网格基准价 {price}, 下单价 {highPrice}, 下单量 {sfgrid_constants.grid_volume}') + self.tradeTarget.save() + finally: + print('onDataUpdate release lock') + self.dataUpdateLock.release() + print(f'|- 标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 市价更新-END') def onAsyncOrderResponse(self, order:XtOrderResponse): + print(f' |- 委托下单回调') self.dataUpdateLock.acquire() - stockCode = order.order_remark - orderSeq = order.seq - if (self.tradeTarget.status == 1): # 正常交易阶段订单下单成功 - if self.tradeTarget.current_buy_order_no == order.seq: - self.tradeTarget.current_buy_order_no = order.order_id - elif self.tradeTarget.current_sell_order_no == order.seq: - self.tradeTarget.current_sell_order_no = order.order_id - else: - print(f' |- 委托回调: 委托单 {order.order_id} {stockCode} {orderSeq} 不在策略监控范围内') - return - rc = self.tradeTarget.save() + try: + stockCode = order.order_remark + orderSeq = order.seq + if (self.tradeTarget.status == 1): # 正常交易阶段订单下单成功 + if self.tradeTarget.current_buy_order_no == order.seq: + self.tradeTarget.current_buy_order_no = order.order_id + elif self.tradeTarget.current_sell_order_no == order.seq: + self.tradeTarget.current_sell_order_no = order.order_id + else: + print(f' |- 委托回调: 委托单 {order.order_id} {stockCode} {orderSeq} 不在策略监控范围内') + rc = self.tradeTarget.save() + finally: + print('onAsyncOrderResponse release lock') + self.dataUpdateLock.release() print(f' |- 委托下单成功[{rc}]: 委托单 {order.order_id} {stockCode} {orderSeq} ') - self.dataUpdateLock.release() def onOrderTrade(self, trade:XtTrade): + print(f' |- 委托成交通知') self.dataUpdateLock.acquire() - if int(self.tradeTarget.status) == 0 and trade.order_id == self.initBuyOrderId : # type: ignore - # 此时为建仓成交 - self.tradeTarget.current_position = int(self.tradeTarget.current_position) + trade.traded_volume # 当前持仓数,账户原有持仓不在策略范围内 # type: ignore - self.tradeTarget.last_trade_price = float(trade.traded_price) # type: ignore - self.tradeTarget.grid_index = 1 # type: ignore - self.tradeTarget.status = 1 # type: ignore - self.tradeTarget.save() - print(f"|- 标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 建初始仓订单ID: {self.initBuyOrderId}已成交 ") - print(f' 成交价: {trade.traded_price} 成交量: {trade.traded_volume}') - print(f' 当前持仓: {self.tradeTarget.current_position}') - print(f' 网格坐标: {self.tradeTarget.grid_index}') - elif trade.order_id == self.tradeTarget.current_sell_order_no and int(self.tradeTarget.status) == 1: # type: ignore - # 上涨一格:此时空单成交 - self.tradeTarget.current_position = int(self.tradeTarget.current_position) - trade.traded_volume # type: ignore - self.tradeTarget.last_trade_price = float(trade.traded_price) # type: ignore - self.tradeTarget.grid_index = int(self.tradeTarget.grid_index) - 1 # type: ignore - self.tradeTarget.save() - print(f"|- 标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 上涨 卖单已成交 订单ID: {self.tradeTarget.current_sell_order_no} Price: {sfgrid_constants.grid_price[int(self.tradeTarget.grid_index)]} Volume: {sfgrid_constants.grid_volume} 手续费: {trade.commission}\n") # type: ignore - print(f' 成交价: {trade.traded_price} 成交量: {trade.traded_volume}') - print(f' 当前持仓: {self.tradeTarget.current_position}') - print(f' 网格坐标: {self.tradeTarget.grid_index}') - elif trade.order_id == self.tradeTarget.current_buy_order_no and int(self.tradeTarget.status) == 1: # type: ignore - # 下跌一格:此时多单成交 - self.tradeTarget.current_position = int(self.tradeTarget.current_position) + trade.traded_volume # type: ignore - self.tradeTarget.last_trade_price = float(trade.traded_price) # type: ignore - self.tradeTarget.grid_index = int(self.tradeTarget.grid_index) + 1 # type: ignore - self.tradeTarget.save() - print(f"|- 标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 下跌 买单已成交 订单ID: {self.tradeTarget.current_buy_order_no} Price: {trade.traded_price} Volume: {sfgrid_constants.grid_volume} 手续费: {trade.commission}") - print(f' 成交价: {trade.traded_price} 成交量: {trade.traded_volume}') - print(f' 当前持仓: {self.tradeTarget.current_position}') - print(f' 网格坐标: {self.tradeTarget.grid_index}') - else: - # 打印订单信息和订单状态 - print(f'|- 非策略内部订单,或订单状态不满足监控条件 {trade.order_id} {trade.stock_code}-{trade.instrument_name} {trade.commission}') - - self.dataUpdateLock.release() + try: + if int(self.tradeTarget.status) == 0 and trade.order_id == self.initBuyOrderId : # type: ignore + # 此时为建仓成交 + self.tradeTarget.current_position = int(self.tradeTarget.current_position) + trade.traded_volume # 当前持仓数,账户原有持仓不在策略范围内 # type: ignore + self.tradeTarget.last_trade_price = float(trade.traded_price) # type: ignore + self.tradeTarget.grid_index = 1 # type: ignore + self.tradeTarget.status = 1 # type: ignore + self.tradeTarget.save() + print(f"|- 标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 建初始仓订单ID: {self.initBuyOrderId}已成交 ") + print(f' 成交价: {trade.traded_price} 成交量: {trade.traded_volume}') + print(f' 当前持仓: {self.tradeTarget.current_position}') + print(f' 网格坐标: {self.tradeTarget.grid_index}') + elif trade.order_id == self.tradeTarget.current_sell_order_no and int(self.tradeTarget.status) == 1: # type: ignore + # 上涨一格:此时空单成交 + self.tradeTarget.current_position = int(self.tradeTarget.current_position) - trade.traded_volume # type: ignore + self.tradeTarget.last_trade_price = float(trade.traded_price) # type: ignore + self.tradeTarget.grid_index = int(self.tradeTarget.grid_index) - 1 # type: ignore + self.tradeTarget.save() + print(f"|- 标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 上涨 卖单已成交 订单ID: {self.tradeTarget.current_sell_order_no} Price: {sfgrid_constants.grid_price[int(self.tradeTarget.grid_index)]} Volume: {sfgrid_constants.grid_volume} 手续费: {trade.commission}\n") # type: ignore + print(f' 成交价: {trade.traded_price} 成交量: {trade.traded_volume}') + print(f' 当前持仓: {self.tradeTarget.current_position}') + print(f' 网格坐标: {self.tradeTarget.grid_index}') + elif trade.order_id == self.tradeTarget.current_buy_order_no and int(self.tradeTarget.status) == 1: # type: ignore + # 下跌一格:此时多单成交 + self.tradeTarget.current_position = int(self.tradeTarget.current_position) + trade.traded_volume # type: ignore + self.tradeTarget.last_trade_price = float(trade.traded_price) # type: ignore + self.tradeTarget.grid_index = int(self.tradeTarget.grid_index) + 1 # type: ignore + self.tradeTarget.save() + print(f"|- 标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 下跌 买单已成交 订单ID: {self.tradeTarget.current_buy_order_no} Price: {trade.traded_price} Volume: {sfgrid_constants.grid_volume} 手续费: {trade.commission}") + print(f' 成交价: {trade.traded_price} 成交量: {trade.traded_volume}') + print(f' 当前持仓: {self.tradeTarget.current_position}') + print(f' 网格坐标: {self.tradeTarget.grid_index}') + else: + # 打印订单信息和订单状态 + print(f'|- 非策略内部订单,或订单状态不满足监控条件 {trade.order_id} {trade.stock_code}-{trade.instrument_name} {trade.commission}') + finally: + print('onOrderTrade release lock') + self.dataUpdateLock.release() + print(f' |- 委托成交处理完成 {trade.stock_code}') diff --git a/example.db b/example.db index 5c2e124..4b95f6d 100644 Binary files a/example.db and b/example.db differ