Files
sanguo_vnpy_v2/vnpy_qmt_v0.3.3/vnpy_qmt/utils.py
T
claude_dev 70d72f35bb chore(vendor): vendor vnpy_qmt 0.3.3 源码自行维护
vnpy_qmt 是唯一券商交易 gateway(关键路径),社区包断更有风险。
按 vnpy_v4.4.0 同一套约定 vendor 源码、sys.path 引用、不 pip install。
- vnpy_qmt_v0.3.3/vnpy_qmt/ (5文件587行) + LICENSE(Apache-2.0) + README
- run_web.py 加 if-exists 路径块,import 方式不变 (from vnpy_qmt import QmtGateway)
Mac 验证: py_compile 全过 + import 解析到 vendor 副本(仅卡 xtquant Win-only,符合预期)
VPS 生产切换(Part B: rsync+pip uninstall+重启+实证) 待确认
2026-07-15 21:15:53 +08:00

88 lines
2.2 KiB
Python

# -*- coding:utf-8 -*-
"""
@FileName :utils.py
@Time :2022/11/8 17:07
@Author :fsksf
"""
import datetime
from vnpy.trader.object import OrderRequest
from vnpy.trader.constant import Exchange, Product, OrderType, Direction, Status
from xtquant import xtconstant
From_VN_Exchange_map = {
Exchange.CFFEX: 'CFF',
Exchange.SSE: 'SH',
Exchange.SZSE: 'SZ',
Exchange.SHFE: 'SHF',
Exchange.CZCE: 'CZC',
Exchange.DCE: 'DCE',
}
TO_VN_Exchange_map = {v: k for k, v in From_VN_Exchange_map.items()}
From_VN_Trade_Type = {
Direction.LONG: xtconstant.STOCK_BUY,
Direction.SHORT: xtconstant.STOCK_SELL,
}
TO_VN_Trade_Type = {v: k for k, v in From_VN_Trade_Type.items()}
TO_VN_ORDER_STATUS = {
xtconstant.ORDER_UNREPORTED: Status.SUBMITTING,
xtconstant.ORDER_WAIT_REPORTING: Status.SUBMITTING,
xtconstant.ORDER_REPORTED: Status.NOTTRADED,
xtconstant.ORDER_REPORTED_CANCEL: Status.NOTTRADED,
xtconstant.ORDER_PARTSUCC_CANCEL: Status.PARTTRADED,
xtconstant.ORDER_PART_CANCEL: Status.CANCELLED,
xtconstant.ORDER_CANCELED: Status.CANCELLED,
xtconstant.ORDER_PART_SUCC: Status.PARTTRADED,
xtconstant.ORDER_SUCCEEDED: Status.ALLTRADED,
xtconstant.ORDER_JUNK: Status.REJECTED,
xtconstant.ORDER_UNKNOWN: Status.REJECTED
}
def from_vn_price_type(req: OrderRequest):
if req.type == OrderType.LIMIT:
return xtconstant.FIX_PRICE
elif req.type == OrderType.MARKET:
return xtconstant.LATEST_PRICE
def to_vn_contract(symbol):
code, suffix = symbol.rsplit('.')
exchange = TO_VN_Exchange_map[suffix]
return code, exchange
TO_VN_Product = {
'index': Product.INDEX,
'stock': Product.EQUITY,
'fund': Product.FUND,
'etf': Product.ETF,
}
def to_vn_product(dic: dict):
if dic.get('etf'):
return Product.ETF
for k, v in dic.items():
if v:
break
return TO_VN_Product[k]
def to_qmt_code(symbol, exchange):
suffix = From_VN_Exchange_map[exchange]
return f'{symbol}.{suffix}'
def timestamp_to_datetime(tint):
st = len(str(tint))
if st != 10:
p = st - 10
tint = tint / 10**p
return datetime.datetime.fromtimestamp(tint)