OK, 差不多了
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
__pycache__/
|
||||||
@@ -0,0 +1,287 @@
|
|||||||
|
# coding:utf-8
|
||||||
|
from os import popen
|
||||||
|
import time, sys
|
||||||
|
|
||||||
|
from peewee import ModelSelect
|
||||||
|
|
||||||
|
import xtquant.xtconstant as xtconstant
|
||||||
|
sys.stdout.reconfigure(encoding='utf-8') # 设置标准输出编码为UTF-8
|
||||||
|
import core.strategy_db as strategy_db
|
||||||
|
import sfgrid_constants
|
||||||
|
from core.sfgrid_trade_controller import StockTradeController
|
||||||
|
from core.util import getInstrumentName, getStockPosition
|
||||||
|
from xtquant.xttrader import XtQuantTrader
|
||||||
|
from xtquant.xttype import StockAccount, XtOrder, XtTrade
|
||||||
|
from xtquant import xtdata
|
||||||
|
from xtquant.xttrader import XtQuantTraderCallback
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
# 量化核心控制对象
|
||||||
|
class SFGridController(XtQuantTraderCallback):
|
||||||
|
def __init__(self, account_no: str, miniQmtPath: str):
|
||||||
|
super().__init__()
|
||||||
|
xtdata.enable_hello = False
|
||||||
|
strategy_db.db.connect()
|
||||||
|
strategy_db.db.create_tables([strategy_db.TradeTarget])
|
||||||
|
print('- 数据库模块初始化完成')
|
||||||
|
|
||||||
|
session_id = int(time.time())
|
||||||
|
|
||||||
|
self.xt_trader = XtQuantTrader(miniQmtPath, session_id)
|
||||||
|
self.xt_trader.register_callback(self)
|
||||||
|
self.xt_trader.start()
|
||||||
|
connect_result = self.xt_trader.connect()
|
||||||
|
print(f'- 市场交易连接{connect_result}: {'成功' if self.xt_trader.connected else '失败'}')
|
||||||
|
|
||||||
|
self.account= StockAccount(account_no, 'STOCK')
|
||||||
|
print(f'- 交易账号对象初始化完成, 账号: {self.account.account_id}')
|
||||||
|
subscribe_result = self.xt_trader.subscribe(self.account)
|
||||||
|
print(f'- 交易状态订阅{'成功' if subscribe_result == 0 else '失败'}')
|
||||||
|
self.stock_trade_ctrl = {}
|
||||||
|
self.init_instrument_pool()
|
||||||
|
self.seq = None
|
||||||
|
print('- 三疯交易系统初始化完成')
|
||||||
|
|
||||||
|
|
||||||
|
def startMarketData(self):
|
||||||
|
print('- 启动市场数据订阅')
|
||||||
|
self.seq = xtdata.subscribe_whole_quote(['SH', 'SZ'], callback=self.onDataUpdate)
|
||||||
|
if self.seq == -1:
|
||||||
|
print('- 市场数据订阅失败')
|
||||||
|
else:
|
||||||
|
print(f'- 市场数据订阅成功, 订阅号={self.seq}')
|
||||||
|
|
||||||
|
|
||||||
|
def stopMarketData(self):
|
||||||
|
print('- 停止市场数据订阅')
|
||||||
|
if self.seq is not None and self.seq > 0:
|
||||||
|
xtdata.unsubscribe_quote(self.seq)
|
||||||
|
|
||||||
|
|
||||||
|
def add_trade_target(self, stock_code: str):
|
||||||
|
try:
|
||||||
|
stock_name = getInstrumentName(stock_code)
|
||||||
|
new_target = strategy_db.TradeTarget.create(
|
||||||
|
stock_name=stock_name,
|
||||||
|
stock_code=stock_code,
|
||||||
|
current_position=0,
|
||||||
|
grid_index=0,
|
||||||
|
last_trade_price=0.0,
|
||||||
|
current_buy_price=0.0,
|
||||||
|
current_buy_order_no='',
|
||||||
|
current_sell_price=0.0,
|
||||||
|
current_sell_order_no=''
|
||||||
|
)
|
||||||
|
new_target.save()
|
||||||
|
print(f'新增交易标的 {stock_code} {stock_name}, {new_target.id}')
|
||||||
|
# 刷新标的持仓
|
||||||
|
pos = getStockPosition(stock_code, self.xt_trader, self.account)
|
||||||
|
strategy_db.TradeTarget.update(current_position=pos).where(strategy_db.TradeTarget.stock_code == stock_code).execute()
|
||||||
|
# 更新标的池
|
||||||
|
self.refresh_targets()
|
||||||
|
# 添加交易控制器
|
||||||
|
stockTradeController = StockTradeController(new_target, self.xt_trader, self.account, new_target.enabled)
|
||||||
|
self.stock_trade_ctrl[stock_code] = stockTradeController
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f'新增交易标的失败 {stock_code} {e}')
|
||||||
|
|
||||||
|
|
||||||
|
def del_trade_target(self, index:int):
|
||||||
|
target: strategy_db.TradeTarget = self.instrument_pool[index]
|
||||||
|
# self.stock_trade_ctrl.
|
||||||
|
del self.stock_trade_ctrl[target.stock_code]
|
||||||
|
target.delete_instance()
|
||||||
|
self.refresh_targets()
|
||||||
|
|
||||||
|
|
||||||
|
def init_instrument_pool(self):
|
||||||
|
self.refresh_targets()
|
||||||
|
|
||||||
|
for tradeTarget in self.instrument_pool:
|
||||||
|
stockTradeController = StockTradeController(tradeTarget, self.xt_trader, self.account, tradeTarget.enabled)
|
||||||
|
self.stock_trade_ctrl[tradeTarget.stock_code] = stockTradeController
|
||||||
|
|
||||||
|
print(f'- 初始化标的池初始化完成 , 共 {len(self.instrument_pool)} 个标的')
|
||||||
|
|
||||||
|
|
||||||
|
def refresh_targets(self):
|
||||||
|
# 更新标的池
|
||||||
|
self.instrument_pool:ModelSelect = strategy_db.TradeTarget.select()
|
||||||
|
self.print_pool()
|
||||||
|
|
||||||
|
def print_pool(self):
|
||||||
|
print("- 标的池信息")
|
||||||
|
for i in range(len(self.instrument_pool)):
|
||||||
|
target: strategy_db.TradeTarget = self.instrument_pool[i]
|
||||||
|
status = "新建" if target.status == 0 else "已建初始仓"
|
||||||
|
print(f' [序号-{i}] 股票代码: {target.stock_code}-{target.stock_name} 当前持仓: {target.current_position} 网格索引: {target.grid_index} Price {sfgrid_constants.grid_price[target.grid_index]} 状态: {status} 启用交易线程: {target.enabled}')
|
||||||
|
|
||||||
|
def print_position_info(self):
|
||||||
|
positions = self.xt_trader.query_stock_positions(self.account)
|
||||||
|
if positions:
|
||||||
|
print("\n- 持仓信息")
|
||||||
|
for pos in positions:
|
||||||
|
if pos.m_nVolume <=0:
|
||||||
|
continue
|
||||||
|
print(f"股票代码: {pos.stock_code}-{getInstrumentName(pos.stock_code)}")
|
||||||
|
print(f"总持仓: {pos.m_nVolume}")
|
||||||
|
print(f"可用持仓: {pos.m_nCanUseVolume}")
|
||||||
|
print(f"持仓成本: {pos.avg_price}")
|
||||||
|
print("---")
|
||||||
|
else:
|
||||||
|
print("\n当前无持仓")
|
||||||
|
|
||||||
|
def print_account_info(self):
|
||||||
|
account_info = self.xt_trader.query_stock_asset(self.account)
|
||||||
|
|
||||||
|
print(f"\n=== 账户信息 {self.account.account_id} ===")
|
||||||
|
print(f"可用资金: {account_info.m_dCash}")
|
||||||
|
print(f"总资产: {account_info.m_dTotalAsset}")
|
||||||
|
print(f"证券市值: {account_info.m_dMarketValue}")
|
||||||
|
|
||||||
|
def print_stock_orders(self):
|
||||||
|
orders = self.xt_trader.query_stock_orders(self.account, cancelable_only=True)
|
||||||
|
if orders:
|
||||||
|
print("\n=== 委托信息 ===")
|
||||||
|
for order in orders:
|
||||||
|
print(f"委托编号: {order.order_id}")
|
||||||
|
print(f"股票代码: {order.stock_code} {getInstrumentName(order.stock_code)}")
|
||||||
|
print(f"委托方向: {order.offset_flag} ")
|
||||||
|
print(f"委托价格: {order.price}")
|
||||||
|
print(f"委托数量: {order.order_volume}")
|
||||||
|
print(f"已成交数量: {order.traded_volume}")
|
||||||
|
print(f"委托状态: {order.order_status} ")
|
||||||
|
print("---")
|
||||||
|
else:
|
||||||
|
print("\n当前无委托记录")
|
||||||
|
|
||||||
|
|
||||||
|
# 初始化指定标的交易控制器
|
||||||
|
def start_stock_trade(self, index: int):
|
||||||
|
tradeTarget = self.instrument_pool[index]
|
||||||
|
# check existing thread
|
||||||
|
if tradeTarget.stock_code in self.stock_trade_ctrl:
|
||||||
|
tradeController: StockTradeController = self.stock_trade_ctrl[tradeTarget.stock_code]
|
||||||
|
if tradeController.isEnabled():
|
||||||
|
print(f"标的交易控制器已存在且正在运行 {tradeTarget.stock_code} {getInstrumentName(tradeTarget.stock_code)}\n")
|
||||||
|
else:
|
||||||
|
print(f"标的交易控制器已存在但未运行,重新启动 {tradeTarget.stock_code} {getInstrumentName(tradeTarget.stock_code)}\n")
|
||||||
|
tradeController.enabledTrading(True)
|
||||||
|
else:
|
||||||
|
stockTradeController = StockTradeController(tradeTarget, self.xt_trader, self.account, tradeTarget.enabled)
|
||||||
|
self.stock_trade_ctrl[tradeTarget.stock_code] = stockTradeController
|
||||||
|
print(f"\t创建标的交易控制器 {tradeTarget.stock_code} {getInstrumentName(tradeTarget.stock_code)}")
|
||||||
|
|
||||||
|
|
||||||
|
def pause_stock_trade(self, index: int):
|
||||||
|
tradeTarget = self.instrument_pool[index]
|
||||||
|
if tradeTarget.stock_code in self.stock_trade_ctrl:
|
||||||
|
tradeController: StockTradeController = self.stock_trade_ctrl[tradeTarget.stock_code]
|
||||||
|
if tradeController.isEnabled():
|
||||||
|
print(f"暂停标的交易 {tradeTarget.stock_code} {getInstrumentName(tradeTarget.stock_code)}\n")
|
||||||
|
tradeController.enabledTrading(False)
|
||||||
|
else:
|
||||||
|
print(f"标的交易已暂停 {tradeTarget.stock_code} {getInstrumentName(tradeTarget.stock_code)}\n")
|
||||||
|
else:
|
||||||
|
print(f"标的交易控制器不存在 {tradeTarget.stock_code} {getInstrumentName(tradeTarget.stock_code)}\n")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# ====== 市场回调方法 -- 以下方法由XtQuantData调用 ======
|
||||||
|
def onDataUpdate(self, data):
|
||||||
|
if sfgrid_constants.max_enabled_targets <= 0: # 全推
|
||||||
|
for stock_code, tickData in data.items():
|
||||||
|
lastPrice = tickData['lastPrice']
|
||||||
|
if lastPrice == 10.0 and stock_code not in self.stock_trade_ctrl:
|
||||||
|
print(f'New trade target = {stock_code} - {getInstrumentName(stock_code)} {tickData['lastPrice']}')
|
||||||
|
self.add_trade_target(stock_code)
|
||||||
|
self.stock_trade_ctrl[stock_code].enabledTrading(True)
|
||||||
|
else: # 指定目标 当前主要使用这种模式
|
||||||
|
for target in self.instrument_pool:
|
||||||
|
stock_code = target.stock_code
|
||||||
|
# 如果存在对应的StockTradeController,则调用其onDataUpdate方法
|
||||||
|
if stock_code not in self.stock_trade_ctrl or stock_code not in data:
|
||||||
|
# print(f"股票代码 {stock_code} 未在交易控制器中找到,跳过处理。\n")
|
||||||
|
continue
|
||||||
|
stock_controller: StockTradeController = self.stock_trade_ctrl[stock_code]
|
||||||
|
stock_controller.onDataUpdate(data)
|
||||||
|
|
||||||
|
|
||||||
|
# ====== 市场回调方法 -- 以下方法由XtQuantTrader调用 ======
|
||||||
|
def on_connected(self):
|
||||||
|
"""
|
||||||
|
连接成功推送
|
||||||
|
"""
|
||||||
|
print(datetime.datetime.now(), '连接成功回调')
|
||||||
|
|
||||||
|
def on_disconnected(self):
|
||||||
|
"""
|
||||||
|
连接断开
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
print(datetime.datetime.now(), '连接断开回调')
|
||||||
|
|
||||||
|
def on_stock_order(self, order:XtOrder):
|
||||||
|
"""
|
||||||
|
委托回报推送
|
||||||
|
:param order: XtOrder对象
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
stockCode = order.stock_code
|
||||||
|
ctrl:StockTradeController = self.stock_trade_ctrl[stockCode]
|
||||||
|
# 如果存在对应的StockTradeController,则调用其onDataUpdate方法
|
||||||
|
if ctrl is not None and order.strategy_name == ctrl.getName():
|
||||||
|
ctrl.onOrderTrade(order)
|
||||||
|
else:
|
||||||
|
print(f"委托下单回调 投资备注 {order.order_remark} 不匹配 {ctrl.getName()}")
|
||||||
|
|
||||||
|
def test_sim_trade(self, index: int, price:float, orderType: int):
|
||||||
|
tradeTarget:strategy_db.TradeTarget = self.instrument_pool[index]
|
||||||
|
ctrl:StockTradeController = self.stock_trade_ctrl[tradeTarget.stock_code]
|
||||||
|
trade: XtTrade = None
|
||||||
|
if orderType == xtconstant.STOCK_BUY:
|
||||||
|
trade = XtTrade(
|
||||||
|
sfgrid_constants.account_no,
|
||||||
|
'300083.SZ',
|
||||||
|
xtconstant.STOCK_BUY,
|
||||||
|
1, 1, price, sfgrid_constants.grid_volume, 1000,
|
||||||
|
tradeTarget.current_buy_order_no,
|
||||||
|
None, ctrl.getName(), None, None, None, None, None, tradeTarget.stock_name)
|
||||||
|
else:
|
||||||
|
trade = XtTrade(sfgrid_constants.account_no, '300083.SZ', xtconstant.STOCK_SELL, 1, 1, price, sfgrid_constants.grid_volume, 1000, tradeTarget.current_sell_order_no, None, ctrl.getName(), None, None, None, None, None, tradeTarget.stock_name)
|
||||||
|
self.on_stock_trade(trade)
|
||||||
|
|
||||||
|
def on_stock_trade(self, trade:XtTrade):
|
||||||
|
"""
|
||||||
|
成交变动推送
|
||||||
|
:param trade: XtTrade对象
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
stockCode = trade.stock_code
|
||||||
|
ctrl:StockTradeController = self.stock_trade_ctrl[stockCode]
|
||||||
|
# 如果存在对应的StockTradeController,则调用其onDataUpdate方法
|
||||||
|
if ctrl is not None and trade.strategy_name == ctrl.getName():
|
||||||
|
ctrl.onOrderTrade(trade)
|
||||||
|
else:
|
||||||
|
print(f"委托回调 投资备注 {trade.strategy_name} 不匹配 {ctrl.getName()}")
|
||||||
|
|
||||||
|
def on_order_error(self, order_error):
|
||||||
|
"""
|
||||||
|
委托失败推送
|
||||||
|
:param order_error:XtOrderError 对象
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
# print("on order_error callback")
|
||||||
|
# print(order_error.order_id, order_error.error_id, order_error.error_msg)
|
||||||
|
print(f"\n委托报错回调 {order_error.order_remark} {order_error.error_msg}")
|
||||||
|
|
||||||
|
|
||||||
|
def on_account_status(self, status):
|
||||||
|
"""
|
||||||
|
:param response: XtAccountStatus 对象
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
print(datetime.datetime.now(), sys._getframe().f_code.co_name)
|
||||||
|
|
||||||
|
ctrl = SFGridController(sfgrid_constants.account_no, sfgrid_constants.miniQMTPath)
|
||||||
@@ -0,0 +1,158 @@
|
|||||||
|
from core.strategy_db import TradeTarget
|
||||||
|
from core.util import getStockPosition, queryPendingOrder
|
||||||
|
|
||||||
|
from xtquant import xttrader, xtconstant
|
||||||
|
from xtquant.xttype import StockAccount, XtOrder, XtTrade
|
||||||
|
import sfgrid_constants
|
||||||
|
|
||||||
|
|
||||||
|
class StockTradeController:
|
||||||
|
|
||||||
|
def __init__(self, tradeTarget: TradeTarget, xt_trader: xttrader.XtQuantTrader, account: StockAccount, enabled: bool = False):
|
||||||
|
self.tradeTarget = tradeTarget
|
||||||
|
self.xt_trader: xttrader.XtQuantTrader = xt_trader
|
||||||
|
self.account:StockAccount = account
|
||||||
|
self.enabledTrading(self.tradeTarget.enabled)
|
||||||
|
|
||||||
|
def getName(self):
|
||||||
|
return "SFGRID"
|
||||||
|
|
||||||
|
|
||||||
|
def enabledTrading(self, enabled: bool):
|
||||||
|
self.tradeTarget.enabled = enabled
|
||||||
|
self.tradeTarget.save()
|
||||||
|
pendingOrders = queryPendingOrder(self.tradeTarget.stock_code,self.getName(), self.xt_trader,self.account)
|
||||||
|
|
||||||
|
if len(pendingOrders) > 0:
|
||||||
|
print(f' |- 已存在{len(pendingOrders)}订单,全部取消,按需要重下。')
|
||||||
|
for order in pendingOrders:
|
||||||
|
self.xt_trader.cancel_order_stock(self.account, order.order_id)
|
||||||
|
|
||||||
|
if enabled:
|
||||||
|
print(f" |- 标的交易启动 {self.tradeTarget.stock_code}-{self.tradeTarget.stock_name}")
|
||||||
|
# 建仓状态检查
|
||||||
|
if self.tradeTarget.current_position == 0 and self.tradeTarget.status == 0:
|
||||||
|
print(f" |- 标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 建初始仓 买单准备中...")
|
||||||
|
self.tradeTarget.grid_index = 1
|
||||||
|
self.tradeTarget.save()
|
||||||
|
self.init_stock_position()
|
||||||
|
print(f" |- 标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 建初始仓 买单已发出 InitBuyOrderId: {self.initBuyOrderId} Price: {sfgrid_constants.grid_price[self.tradeTarget.grid_index]} Volume: {sfgrid_constants.grid_volume}")
|
||||||
|
else:
|
||||||
|
# 交易阶段,检查仓位,检查现有订单
|
||||||
|
print(f" |- 标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 已有仓位或非初始状态 无需建初始仓 当前仓位: {getStockPosition(self.tradeTarget.stock_code, self.xt_trader, self.account)} 状态: {self.tradeTarget.status}")
|
||||||
|
currentPosition = getStockPosition(self.tradeTarget.stock_code, self.xt_trader, self.account)
|
||||||
|
if sfgrid_constants.grid_volume * TradeTarget.current_position >= currentPosition:
|
||||||
|
print(f' |- 仓位检查: 持仓需求充足, (gridVolume*gridIndex)={sfgrid_constants.grid_volume * self.tradeTarget.current_position}, 当前持仓:{currentPosition}')
|
||||||
|
else:
|
||||||
|
print(f' |- 仓位检查: 持仓需求不足, (gridVolume*gridIndex)={sfgrid_constants.grid_volume * self.tradeTarget.current_position}, 当前持仓:{currentPosition}')
|
||||||
|
|
||||||
|
self.two_way_order(True, True)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def isEnabled(self) -> bool:
|
||||||
|
return 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]}")
|
||||||
|
|
||||||
|
def onOrderUpdate(self, order:XtOrder):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def onOrderTrade(self, trade:XtTrade):
|
||||||
|
indicator = False
|
||||||
|
if self.tradeTarget.status == 0 and trade.order_id == self.initBuyOrderId :
|
||||||
|
print(f'{trade.order_id} == {self.initBuyOrderId} {self.tradeTarget.status}')
|
||||||
|
# 此时为建仓成交
|
||||||
|
self.tradeTarget.current_position += sfgrid_constants.grid_volume # 当前持仓数,账户原有持仓不在策略范围内
|
||||||
|
self.tradeTarget.last_trade_price = trade.traded_price
|
||||||
|
self.tradeTarget.grid_index = 1
|
||||||
|
self.tradeTarget.status = 1
|
||||||
|
self.tradeTarget.save()
|
||||||
|
print(f"|- 标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 建初始仓订单ID: {self.initBuyOrderId}已成交 \n")
|
||||||
|
print(f' 成交价: {trade.traded_price} 成交量: {trade.traded_volume}\n')
|
||||||
|
print(f' 当前持仓: {self.tradeTarget.current_position}\n')
|
||||||
|
print(f' 网格坐标: {self.tradeTarget.grid_index}\n')
|
||||||
|
indicator = True # 双向下单
|
||||||
|
elif trade.order_id == self.tradeTarget.current_sell_order_no and self.tradeTarget.status == 1:
|
||||||
|
print(f'{trade.order_id} == {self.tradeTarget.current_sell_order_no} {self.tradeTarget.status}')
|
||||||
|
# 上涨一格:此时空单成交
|
||||||
|
print(f"|- 标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 上涨 卖单已成交 订单ID: {self.tradeTarget.current_sell_order_no} Price: {sfgrid_constants.grid_price[self.tradeTarget.grid_index]} Volume: {sfgrid_constants.grid_volume} 手续费: {trade.commission}\n")
|
||||||
|
self.tradeTarget.current_position = -sfgrid_constants.grid_volume
|
||||||
|
self.tradeTarget.last_trade_price = trade.traded_price
|
||||||
|
self.tradeTarget.grid_index += 1
|
||||||
|
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[self.tradeTarget.grid_index]} Volume: {sfgrid_constants.grid_volume} 手续费: {trade.commission}\n")
|
||||||
|
print(f' 成交价: {trade.traded_price} 成交量: {trade.traded_volume}\n')
|
||||||
|
print(f' 当前持仓: {self.tradeTarget.current_position}\n')
|
||||||
|
print(f' 网格坐标: {self.tradeTarget.grid_index}\n')
|
||||||
|
cancelResult = self.xt_trader.cancel_order_stock(self.account, self.tradeTarget.current_sell_order_no)
|
||||||
|
print(f' 上涨一格,空单成交,对侧买单已撤单 cancelResult: {cancelResult == 0}')
|
||||||
|
indicator = True # 双向下单
|
||||||
|
elif trade.order_id == self.tradeTarget.current_buy_order_no and self.tradeTarget.status == 1:
|
||||||
|
print(f'{trade.order_id} == {self.tradeTarget.current_buy_order_no} {self.tradeTarget.status}')
|
||||||
|
# 下跌一格:此时多单成交
|
||||||
|
self.tradeTarget.current_position = +sfgrid_constants.grid_volume
|
||||||
|
self.tradeTarget.last_trade_price = trade.traded_price
|
||||||
|
self.tradeTarget.grid_index -= 1
|
||||||
|
self.tradeTarget.save()
|
||||||
|
print(f"|- 标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 下跌 买单已成交 订单ID: {self.tradeTarget.current_buy_order_no} Price: {sfgrid_constants.grid_price[self.tradeTarget.grid_index]} Volume: {sfgrid_constants.grid_volume} 手续费: {trade.commission}\n")
|
||||||
|
print(f' 成交价: {trade.traded_price} 成交量: {trade.traded_volume}\n')
|
||||||
|
print(f' 当前持仓: {self.tradeTarget.current_position}\n')
|
||||||
|
print(f' 网格坐标: {self.tradeTarget.grid_index}\n')
|
||||||
|
cancelResult = self.xt_trader.cancel_order_stock(self.account, self.tradeTarget.current_buy_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():
|
||||||
|
self.two_way_order(True, True) # 双向下单
|
||||||
|
|
||||||
|
|
||||||
|
# Description: 新标的,建基础仓
|
||||||
|
def init_stock_position(self):
|
||||||
|
self.initBuyOrderId = self.xt_trader.order_stock(
|
||||||
|
self.account,
|
||||||
|
self.tradeTarget.stock_code,
|
||||||
|
xtconstant.STOCK_BUY,
|
||||||
|
sfgrid_constants.grid_volume,
|
||||||
|
xtconstant.FIX_PRICE,
|
||||||
|
sfgrid_constants.grid_price[self.tradeTarget.grid_index],
|
||||||
|
'sf_grid', f'{self.tradeTarget.stock_code}_init_buy')
|
||||||
|
print(f"|- 标的{self.tradeTarget.stock_code}-{self.tradeTarget.stock_name} 建初始仓 买单已发出 InitBuyOrderId: {self.initBuyOrderId} Price: {sfgrid_constants.grid_price[self.tradeTarget.grid_index]} Volume: {sfgrid_constants.grid_volume}\n")
|
||||||
|
|
||||||
|
|
||||||
|
# Description: 网格跳格,双向下单
|
||||||
|
def two_way_order(self, buy, sell):
|
||||||
|
if buy and self.tradeTarget.grid_index+1 < len(sfgrid_constants.grid_price): # 价格没有超过网格下边界,可以下多单
|
||||||
|
currentPrice = sfgrid_constants.grid_price[self.tradeTarget.grid_index]
|
||||||
|
buyPrice = sfgrid_constants.grid_price[self.tradeTarget.grid_index+1]
|
||||||
|
self.tradeTarget.current_buy_order_no = self.xt_trader.order_stock(
|
||||||
|
self.account,
|
||||||
|
self.tradeTarget.stock_code,
|
||||||
|
xtconstant.STOCK_BUY,
|
||||||
|
sfgrid_constants.grid_volume,
|
||||||
|
xtconstant.FIX_PRICE,
|
||||||
|
buyPrice,
|
||||||
|
self.getName(), f'{self.tradeTarget.stock_code}_grid_down_{self.tradeTarget.grid_index}_{currentPrice}_{buyPrice}')
|
||||||
|
self.tradeTarget.current_buy_price = buyPrice
|
||||||
|
self.tradeTarget.save()
|
||||||
|
print(f' |- 下网格多单 OrderId {self.tradeTarget.current_buy_order_no}, 下单价 {buyPrice}, 下单量 {sfgrid_constants.grid_volume}')
|
||||||
|
|
||||||
|
if sell and self.tradeTarget.grid_index-1 >=0: # 价格没有超过网格上边界,可以下空单
|
||||||
|
currentPrice = sfgrid_constants.grid_price[self.tradeTarget.grid_index]
|
||||||
|
sellPrice = sfgrid_constants.grid_price[self.tradeTarget.grid_index-1]
|
||||||
|
self.tradeTarget.current_sell_order_no = self.xt_trader.order_stock(
|
||||||
|
self.account,
|
||||||
|
self.tradeTarget.stock_code,
|
||||||
|
xtconstant.STOCK_SELL,
|
||||||
|
sfgrid_constants.grid_volume,
|
||||||
|
xtconstant.FIX_PRICE,
|
||||||
|
sellPrice,
|
||||||
|
self.getName(), f'{self.tradeTarget.stock_code}_grid_up_{self.tradeTarget.grid_index}_{currentPrice}_{sellPrice}')
|
||||||
|
self.tradeTarget.current_sell_price = sellPrice
|
||||||
|
print(f' |- 下网格多单 OrderId {self.tradeTarget.current_sell_order_no}, 下单价 {sellPrice}, 下单量 {sfgrid_constants.grid_volume}')
|
||||||
|
self.tradeTarget.save()
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
from peewee import SqliteDatabase, Model, CharField, IntegerField, FloatField, BooleanField
|
||||||
|
|
||||||
|
# 连接到SQLite数据库
|
||||||
|
db = SqliteDatabase('example.db')
|
||||||
|
|
||||||
|
# 定义基础模型类
|
||||||
|
class BaseModel(Model):
|
||||||
|
class Meta:
|
||||||
|
database = db
|
||||||
|
|
||||||
|
# 定义Target类,对应targets表
|
||||||
|
class TradeTarget(BaseModel):
|
||||||
|
stock_code = CharField(unique=True)
|
||||||
|
stock_name = CharField()
|
||||||
|
current_position = IntegerField()
|
||||||
|
grid_index = IntegerField()
|
||||||
|
last_trade_price = FloatField()
|
||||||
|
current_buy_price = FloatField()
|
||||||
|
current_buy_order_no = CharField(default='')
|
||||||
|
current_sell_price = FloatField()
|
||||||
|
current_sell_order_no = CharField(default='')
|
||||||
|
status = IntegerField(default=0) # 0表示新标的,1表示已建初始仓,正常交易中
|
||||||
|
enabled = BooleanField(default=False) # 是否启动交易线程
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import sfgrid_constants
|
||||||
|
import xtquant.xtconstant as xtconstant
|
||||||
|
from xtquant import xtdata, xttrader
|
||||||
|
from xtquant.xttype import StockAccount, XtOrder
|
||||||
|
|
||||||
|
def getInstrumentName(stock_code):
|
||||||
|
# print(f"getInstrumentName: 获取标的名称 {stock_code}")
|
||||||
|
detail = xtdata.get_instrument_detail(stock_code, False)
|
||||||
|
return detail['InstrumentName']
|
||||||
|
|
||||||
|
|
||||||
|
def getStockPosition(stock_code: str, xt_trader: xttrader.XtQuantTrader, account: StockAccount):
|
||||||
|
volume = 0
|
||||||
|
positions = xt_trader.query_stock_positions(account)
|
||||||
|
if positions:
|
||||||
|
for pos in positions:
|
||||||
|
if pos.stock_code == stock_code:
|
||||||
|
volume = pos.m_nVolume
|
||||||
|
break
|
||||||
|
return volume
|
||||||
|
|
||||||
|
def minPosition(gridIndex:int):
|
||||||
|
return sfgrid_constants.grid_volume * gridIndex
|
||||||
|
|
||||||
|
def queryPendingOrder(stock_code:str, tag: str, xt_trader: xttrader.XtQuantTrader, account: StockAccount):
|
||||||
|
if stock_code == None or tag == None:
|
||||||
|
return []
|
||||||
|
orders = xt_trader.query_stock_orders(account)
|
||||||
|
result = [order for order in orders if order.order_status == xtconstant.ORDER_REPORTED and order.stock_code == stock_code and order.strategy_name == tag]
|
||||||
|
return result
|
||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
miniQMTPath = r'D:\\Programs\\DTQMT_MN\\userdata_mini' # miniQMT软件的安装路径
|
miniQMTPath = r'D:\\Programs\\DTQMT_MN\\userdata_mini' # miniQMT软件的安装路径
|
||||||
# D:\Programs\DTQMT_MN\userdata_mini
|
# D:\Programs\DTQMT_MN\userdata_mini
|
||||||
# grid_price = [11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1] # 网格价格设置,从高到低
|
# grid_price = [11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1] # 网格价格设置,从高到低
|
||||||
grid_price = [10.1, 10.00, 9.9, 9.8, 9.7, 9.6, 9.5, 9.4, 9.3, 9.2, 9.1] # 网格价格设置,从高到低
|
grid_price = [10.05, 10.00, 9.95, 9.90, 9.85, 9.80, 9.75, 9.70, 9.65, 9.60, 9.55] # 网格价格设置,从高到低
|
||||||
grid_volume = 100 # 每个网格的交易手数
|
grid_volume = 100 # 每个网格的交易手数
|
||||||
# account_no = '99082560'
|
# account_no = '99082560'
|
||||||
account_no = '89009170' # 交易账号
|
account_no = '89009170' # 交易账号
|
||||||
|
|||||||
+8
-6
@@ -2,13 +2,10 @@
|
|||||||
import sys
|
import sys
|
||||||
import chardet
|
import chardet
|
||||||
|
|
||||||
from ui.ui import InitUI, Loop
|
# from ui.ui import InitUI, Loop
|
||||||
sys.stdout.reconfigure(encoding='utf-8') # 设置标准输出编码为UTF-8
|
sys.stdout.reconfigure(encoding='utf-8') # 设置标准输出编码为UTF-8
|
||||||
from core.main_controller import ctrl
|
from core.main_controller import ctrl
|
||||||
|
import core.util as util
|
||||||
if __name__ == '__main__':
|
|
||||||
InitUI()
|
|
||||||
Loop()
|
|
||||||
|
|
||||||
def interact():
|
def interact():
|
||||||
"""执行后进入repl模式"""
|
"""执行后进入repl模式"""
|
||||||
@@ -72,4 +69,9 @@ def help():
|
|||||||
print(" stockTradeCtrl(index) - 获取标的交易控制器")
|
print(" stockTradeCtrl(index) - 获取标的交易控制器")
|
||||||
print(" ctrl - 访问控制器实例")
|
print(" ctrl - 访问控制器实例")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
interact()
|
||||||
|
# InitUI()
|
||||||
|
# Loop()
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
from xtquant import xtdata, xttrader
|
|
||||||
from xtquant.xttype import StockAccount
|
|
||||||
|
|
||||||
def getInstrumentName(stock_code):
|
|
||||||
# print(f"getInstrumentName: 获取标的名称 {stock_code}")
|
|
||||||
detail = xtdata.get_instrument_detail(stock_code, False)
|
|
||||||
return detail['InstrumentName']
|
|
||||||
|
|
||||||
|
|
||||||
def getStockPosition(stock_code: str, xt_trader: xttrader.XtQuantTrader, account: StockAccount):
|
|
||||||
volume = 0
|
|
||||||
positions = xt_trader.query_stock_positions(account)
|
|
||||||
if positions:
|
|
||||||
for pos in positions:
|
|
||||||
if pos.stock_code == stock_code:
|
|
||||||
volume = pos.m_nVolume
|
|
||||||
break
|
|
||||||
return volume
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user