This commit is contained in:
2025-11-04 15:01:08 +08:00
parent 2fdc57e2b8
commit 8f14c0dea2
3 changed files with 102 additions and 94 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
[config] [config]
miniQMTPath=D:\\Programs\\DTQMT\\userdata_mini miniQMTPath=D:\\Programs\\DTQMT\\userdata_mini
; miniQMTPath=D:\\Programs\\DTQMT_MN\\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_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 grid_volume = 200
account_no = '99082560' account_no = '99082560'
+101 -93
View File
@@ -61,111 +61,119 @@ class SFGridStrategy:
return bool(self.tradeTarget.enabled) return bool(self.tradeTarget.enabled)
def onDataUpdate(self, data): def onDataUpdate(self, data):
if not self.isEnabled(): print(f'|- 标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 市价更新-START')
return
self.dataUpdateLock.acquire() self.dataUpdateLock.acquire()
index = self.tradeTarget.grid_index try:
price = sfgrid_constants.grid_price[int(index)] # pyright: ignore[reportArgumentType] index = self.tradeTarget.grid_index
lowPrice = sfgrid_constants.grid_price[int(index) + 1] # pyright: ignore[reportArgumentType] price = sfgrid_constants.grid_price[int(index)] # pyright: ignore[reportArgumentType]
highPrice = sfgrid_constants.grid_price[int(index) - 1] # 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'])) 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}") print(f"|- 标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 市价信息-价格: {lastPrice}, 网格序号: {index}, 网格价格: {price}, 计划多单价: {lowPrice}, 计划空单价: {highPrice}")
if lastPrice <= lowPrice: # 下下方多单 if lastPrice <= lowPrice: # 下下方多单
orders = queryPendingOrder(str(self.tradeTarget.stock_code), self.getName(), self.xt_trader, self.account) 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: if len([order for order in orders if order.order_type == xtconstant.STOCK_BUY and order.price == lowPrice]) > 0:
# 已存在未交易的多单 # 已存在未交易的多单
print(f' |- 已存在未交易的多单,不重复下单') print(f' |- 已存在未交易的多单,不重复下单')
else: else:
print(f' |- 下网格多单') print(f' |- 下网格多单')
self.tradeTarget.current_buy_order_no = self.xt_trader.order_stock_async( 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(
self.account, self.account,
str(self.tradeTarget.stock_code), str(self.tradeTarget.stock_code),
xtconstant.STOCK_SELL, xtconstant.STOCK_BUY,
sfgrid_constants.grid_volume, sfgrid_constants.grid_volume,
xtconstant.FIX_PRICE, xtconstant.FIX_PRICE,
highPrice, lowPrice,
self.getName(), self.getName(), # strategy_name
self.tradeTarget.stock_code) # type: ignore self.tradeTarget.stock_code # remark # type: ignore
self.tradeTarget.grid_index = int(index) - 1 # type: ignore )
self.tradeTarget.current_sell_price = float(highPrice) # type: ignore self.tradeTarget.current_buy_price = float(lowPrice) # type: ignore
print(f' |- 下网格单号 {self.tradeTarget.current_sell_order_no}, 网格基准价 {price}, 下单价 {highPrice}, 下单量 {sfgrid_constants.grid_volume}') print(f' |- 下网格单号 {self.tradeTarget.current_buy_order_no}, 网格基准价 {price}, 下单价 {lowPrice}, 下单量 {sfgrid_constants.grid_volume}')
self.tradeTarget.save() elif lastPrice == highPrice: # 下上方空单
self.dataUpdateLock.release() 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): def onAsyncOrderResponse(self, order:XtOrderResponse):
print(f' |- 委托下单回调')
self.dataUpdateLock.acquire() self.dataUpdateLock.acquire()
stockCode = order.order_remark try:
orderSeq = order.seq stockCode = order.order_remark
if (self.tradeTarget.status == 1): # 正常交易阶段订单下单成功 orderSeq = order.seq
if self.tradeTarget.current_buy_order_no == order.seq: if (self.tradeTarget.status == 1): # 正常交易阶段订单下单成功
self.tradeTarget.current_buy_order_no = order.order_id if self.tradeTarget.current_buy_order_no == order.seq:
elif self.tradeTarget.current_sell_order_no == order.seq: self.tradeTarget.current_buy_order_no = order.order_id
self.tradeTarget.current_sell_order_no = order.order_id elif self.tradeTarget.current_sell_order_no == order.seq:
else: self.tradeTarget.current_sell_order_no = order.order_id
print(f' |- 委托回调: 委托单 {order.order_id} {stockCode} {orderSeq} 不在策略监控范围内') else:
return print(f' |- 委托回调: 委托单 {order.order_id} {stockCode} {orderSeq} 不在策略监控范围内')
rc = self.tradeTarget.save() rc = self.tradeTarget.save()
finally:
print('onAsyncOrderResponse release lock')
self.dataUpdateLock.release()
print(f' |- 委托下单成功[{rc}]: 委托单 {order.order_id} {stockCode} {orderSeq} ') print(f' |- 委托下单成功[{rc}]: 委托单 {order.order_id} {stockCode} {orderSeq} ')
self.dataUpdateLock.release()
def onOrderTrade(self, trade:XtTrade): def onOrderTrade(self, trade:XtTrade):
print(f' |- 委托成交通知')
self.dataUpdateLock.acquire() self.dataUpdateLock.acquire()
if int(self.tradeTarget.status) == 0 and trade.order_id == self.initBuyOrderId : # type: ignore 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.current_position = int(self.tradeTarget.current_position) + trade.traded_volume # 当前持仓数,账户原有持仓不在策略范围内 # type: ignore
self.tradeTarget.grid_index = 1 # type: ignore self.tradeTarget.last_trade_price = float(trade.traded_price) # type: ignore
self.tradeTarget.status = 1 # type: ignore self.tradeTarget.grid_index = 1 # type: ignore
self.tradeTarget.save() self.tradeTarget.status = 1 # type: ignore
print(f"|- 标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 建初始仓订单ID: {self.initBuyOrderId}已成交 ") self.tradeTarget.save()
print(f' 成交价: {trade.traded_price} 成交量: {trade.traded_volume}') print(f"|- 标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 建初始仓订单ID: {self.initBuyOrderId}已成交 ")
print(f' 当前持仓: {self.tradeTarget.current_position}') print(f' 成交价: {trade.traded_price} 成交量: {trade.traded_volume}')
print(f' 网格坐标: {self.tradeTarget.grid_index}') print(f' 当前持仓: {self.tradeTarget.current_position}')
elif trade.order_id == self.tradeTarget.current_sell_order_no and int(self.tradeTarget.status) == 1: # type: ignore 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.current_position = int(self.tradeTarget.current_position) - trade.traded_volume # type: ignore
self.tradeTarget.grid_index = int(self.tradeTarget.grid_index) - 1 # type: ignore self.tradeTarget.last_trade_price = float(trade.traded_price) # type: ignore
self.tradeTarget.save() self.tradeTarget.grid_index = int(self.tradeTarget.grid_index) - 1 # type: ignore
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 self.tradeTarget.save()
print(f' 成交价: {trade.traded_price} 成交量: {trade.traded_volume}') 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' 当前持仓: {self.tradeTarget.current_position}') print(f' 成交价: {trade.traded_price} 成交量: {trade.traded_volume}')
print(f' 网格坐标: {self.tradeTarget.grid_index}') print(f' 当前持仓: {self.tradeTarget.current_position}')
elif trade.order_id == self.tradeTarget.current_buy_order_no and int(self.tradeTarget.status) == 1: # type: ignore 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.current_position = int(self.tradeTarget.current_position) + trade.traded_volume # type: ignore
self.tradeTarget.grid_index = int(self.tradeTarget.grid_index) + 1 # type: ignore self.tradeTarget.last_trade_price = float(trade.traded_price) # type: ignore
self.tradeTarget.save() self.tradeTarget.grid_index = int(self.tradeTarget.grid_index) + 1 # type: ignore
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}") self.tradeTarget.save()
print(f' 成交价: {trade.traded_price} 成交量: {trade.traded_volume}') 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' 当前持仓: {self.tradeTarget.current_position}') print(f' 成交价: {trade.traded_price} 成交量: {trade.traded_volume}')
print(f' 网格坐标: {self.tradeTarget.grid_index}') print(f' 当前持仓: {self.tradeTarget.current_position}')
else: print(f' 网格坐标: {self.tradeTarget.grid_index}')
# 打印订单信息和订单状态 else:
print(f'|- 非策略内部订单,或订单状态不满足监控条件 {trade.order_id} {trade.stock_code}-{trade.instrument_name} {trade.commission}') # 打印订单信息和订单状态
print(f'|- 非策略内部订单,或订单状态不满足监控条件 {trade.order_id} {trade.stock_code}-{trade.instrument_name} {trade.commission}')
self.dataUpdateLock.release() finally:
print('onOrderTrade release lock')
self.dataUpdateLock.release()
print(f' |- 委托成交处理完成 {trade.stock_code}')
BIN
View File
Binary file not shown.