调整下单逻辑,避免超过涨跌停价位无法下单的情况。
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
from re import L
|
||||
from core import util
|
||||
from core.strategy_db import TradeTarget
|
||||
from core.util import is_trading_time, queryPendingOrder
|
||||
|
||||
from xtquant import xttrader, xtconstant
|
||||
from xtquant.xttype import StockAccount, XtOrder, XtOrderResponse, XtTrade
|
||||
import sfgrid_constants
|
||||
import threading
|
||||
|
||||
|
||||
class StockTradeController:
|
||||
@@ -13,6 +16,7 @@ class StockTradeController:
|
||||
self.xt_trader: xttrader.XtQuantTrader = xt_trader
|
||||
self.account:StockAccount = account
|
||||
self.enabledTrading(enabled)
|
||||
self.dataUpdateLock = threading.Lock()
|
||||
|
||||
def getName(self):
|
||||
return "SFGRID"
|
||||
@@ -44,18 +48,68 @@ class StockTradeController:
|
||||
print(f' |- 仓位检查: 持仓需求充足, (gridVolume*gridIndex)={minRequirePosition}, 当前持仓:{self.tradeTarget.current_position}')
|
||||
else:
|
||||
print(f' |- 仓位检查: 持仓需求不足, (gridVolume*gridIndex)={minRequirePosition}, 当前持仓:{self.tradeTarget.current_position}')
|
||||
if is_trading_time():
|
||||
self.two_way_order(True, True)
|
||||
|
||||
|
||||
def isEnabled(self) -> bool:
|
||||
return bool(self.tradeTarget.enabled)
|
||||
|
||||
def onDataUpdate(self, data):
|
||||
if self.isEnabled():
|
||||
print(f"\n标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 行情数据更新 {data[self.tradeTarget.stock_code]}")
|
||||
if not self.isEnabled():
|
||||
return
|
||||
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]
|
||||
|
||||
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(
|
||||
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.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()
|
||||
|
||||
|
||||
def onAsyncOrderResponse(self, order:XtOrderResponse):
|
||||
self.dataUpdateLock.acquire()
|
||||
stockCode = order.order_remark
|
||||
orderSeq = order.seq
|
||||
if self.tradeTarget.current_buy_order_no == order.seq:
|
||||
@@ -65,12 +119,12 @@ class StockTradeController:
|
||||
else:
|
||||
print(f' |- 委托回调: 委托单 {order.order_id} {stockCode} {orderSeq} 不在策略监控范围内')
|
||||
return
|
||||
|
||||
rc = self.tradeTarget.save()
|
||||
print(f' |- 委托回调: 委托单 {order.order_id} {stockCode} {orderSeq} 处理结果: {rc}')
|
||||
self.dataUpdateLock.release()
|
||||
|
||||
def onOrderTrade(self, trade:XtTrade):
|
||||
indicator = False
|
||||
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
|
||||
@@ -82,7 +136,6 @@ class StockTradeController:
|
||||
print(f' 成交价: {trade.traded_price} 成交量: {trade.traded_volume}')
|
||||
print(f' 当前持仓: {self.tradeTarget.current_position}')
|
||||
print(f' 网格坐标: {self.tradeTarget.grid_index}')
|
||||
indicator = True # 双向下单
|
||||
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
|
||||
@@ -95,7 +148,6 @@ class StockTradeController:
|
||||
print(f' 网格坐标: {self.tradeTarget.grid_index}')
|
||||
cancelResult = self.xt_trader.cancel_order_stock_async(self.account, self.tradeTarget.current_buy_order_no)
|
||||
print(f' 上涨一格,空单成交,对侧买单已撤单 cancelResult: {cancelResult > 0}')
|
||||
indicator = True # 双向下单
|
||||
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
|
||||
@@ -108,14 +160,11 @@ class StockTradeController:
|
||||
print(f' 网格坐标: {self.tradeTarget.grid_index}')
|
||||
cancelResult = self.xt_trader.cancel_order_stock_async(self.account, self.tradeTarget.current_sell_order_no)
|
||||
print(f' 下跌一格,多单成交,对侧卖单已撤单 cancelResult: {cancelResult > 0}')
|
||||
indicator = True # 双向下单
|
||||
else:
|
||||
# 打印订单信息和订单状态
|
||||
print(f'|- 非策略内部订单,或订单状态不满足监控条件 {trade.order_id} {trade.stock_code}-{trade.instrument_name} {trade.commission}')
|
||||
|
||||
if indicator and self.isEnabled() and is_trading_time():
|
||||
print(f'goto two way order')
|
||||
self.two_way_order(True, True) # 双向下单
|
||||
|
||||
self.dataUpdateLock.release()
|
||||
|
||||
|
||||
# Description: 新标的,建基础仓
|
||||
@@ -132,37 +181,37 @@ class StockTradeController:
|
||||
|
||||
|
||||
# Description: 网格跳格,双向下单
|
||||
def two_way_order(self, buy, sell):
|
||||
print(f'|- 标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 网格跳格,双向下单')
|
||||
print(f' |- 网格坐标: {self.tradeTarget.grid_index}, buy:{buy}, sell:{sell}')
|
||||
if buy and int(self.tradeTarget.grid_index)+1 < len(sfgrid_constants.grid_price): # 价格没有超过网格下边界,可以下多单 # type: ignore
|
||||
currentPrice = sfgrid_constants.grid_price[int(self.tradeTarget.grid_index)] # type: ignore
|
||||
buyPrice = sfgrid_constants.grid_price[int(self.tradeTarget.grid_index)+1] # type: ignore
|
||||
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,
|
||||
buyPrice,
|
||||
self.getName(), # strategy_name
|
||||
self.tradeTarget.stock_code # remark # type: ignore
|
||||
)
|
||||
self.tradeTarget.current_buy_price = float(buyPrice) # type: ignore
|
||||
print(f' |- 下网格多单 OrderId {self.tradeTarget.current_buy_order_no}, 网格基准价 {currentPrice}, 下单价 {buyPrice}, 下单量 {sfgrid_constants.grid_volume}')
|
||||
# def two_way_order(self, buy, sell):
|
||||
# print(f'|- 标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 网格跳格,双向下单')
|
||||
# print(f' |- 网格坐标: {self.tradeTarget.grid_index}, buy:{buy}, sell:{sell}')
|
||||
# if buy and int(self.tradeTarget.grid_index)+1 < len(sfgrid_constants.grid_price): # 价格没有超过网格下边界,可以下多单 # type: ignore
|
||||
# currentPrice = sfgrid_constants.grid_price[int(self.tradeTarget.grid_index)] # type: ignore
|
||||
# buyPrice = sfgrid_constants.grid_price[int(self.tradeTarget.grid_index)+1] # type: ignore
|
||||
# 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,
|
||||
# buyPrice,
|
||||
# self.getName(), # strategy_name
|
||||
# self.tradeTarget.stock_code # remark # type: ignore
|
||||
# )
|
||||
# self.tradeTarget.current_buy_price = float(buyPrice) # type: ignore
|
||||
# print(f' |- 下网格多单 OrderId {self.tradeTarget.current_buy_order_no}, 网格基准价 {currentPrice}, 下单价 {buyPrice}, 下单量 {sfgrid_constants.grid_volume}')
|
||||
|
||||
if sell and int(self.tradeTarget.grid_index)-1 >=0: # 价格没有超过网格上边界,可以下空单 # type: ignore
|
||||
currentPrice = sfgrid_constants.grid_price[int(self.tradeTarget.grid_index)] # type: ignore
|
||||
sellPrice = sfgrid_constants.grid_price[int(self.tradeTarget.grid_index)-1] # type: ignore
|
||||
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,
|
||||
sellPrice,
|
||||
self.getName(),
|
||||
self.tradeTarget.stock_code) # type: ignore
|
||||
self.tradeTarget.current_sell_price = float(sellPrice) # type: ignore
|
||||
print(f' |- 下网格空单 OrderId {self.tradeTarget.current_sell_order_no}, 网格基准价 {currentPrice}, 下单价 {sellPrice}, 下单量 {sfgrid_constants.grid_volume}')
|
||||
self.tradeTarget.save()
|
||||
# if sell and int(self.tradeTarget.grid_index)-1 >=0: # 价格没有超过网格上边界,可以下空单 # type: ignore
|
||||
# currentPrice = sfgrid_constants.grid_price[int(self.tradeTarget.grid_index)] # type: ignore
|
||||
# sellPrice = sfgrid_constants.grid_price[int(self.tradeTarget.grid_index)-1] # type: ignore
|
||||
# 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,
|
||||
# sellPrice,
|
||||
# self.getName(),
|
||||
# self.tradeTarget.stock_code) # type: ignore
|
||||
# self.tradeTarget.current_sell_price = float(sellPrice) # type: ignore
|
||||
# print(f' |- 下网格空单 OrderId {self.tradeTarget.current_sell_order_no}, 网格基准价 {currentPrice}, 下单价 {sellPrice}, 下单量 {sfgrid_constants.grid_volume}')
|
||||
# self.tradeTarget.save()
|
||||
Reference in New Issue
Block a user