该Jupyter Notebook用于展示如何使用ScriptTraderApp模块,在CLI命令行下进行交易指令的调用


In [1]:
from vnpy.app.script_trader import init_cli_trading
from vnpy.gateway.oes import OesGateway

In [2]:
# 连接到服务器
setting = {
    "td_ord_server": "tcp://106.15.58.119:6101",
    "td_rpt_server": "tcp://106.15.58.119:6301",
    "td_qry_server": "tcp://106.15.58.119:6401",
    "md_tcp_server": "tcp://139.196.228.232:5103",
    "md_qry_server": "tcp://139.196.228.232:5203",
    "username": "",
    "password": "",
    "hdd_serial": "",
    "customize_ip": "",
    "customize_mac": ""
}

engine = init_cli_trading([OesGateway])
engine.connect_gateway(setting, "OES")


2019-07-02 17:30:22.289718	成功连接到行情服务器
2019-07-02 17:30:22.427419	成功连接到交易服务器
2019-07-02 17:30:26.294755	合约信息查询成功
2019-07-02 17:30:26.327959	撤单失败,订单号: 1000107。原因:无效的订单状态
2019-07-02 17:30:26.328936	撤单失败,订单号: 1000115。原因:未找到委托信息
2019-07-02 17:30:26.328936	撤单失败,订单号: 1000117。原因:无效的订单状态

In [3]:
# 查询所有合约
engine.get_all_contracts(use_df=True)


Out[3]:
exchange gateway_name history_data min_volume name net_position option_expiry option_strike option_type option_underlying pricetick product size stop_supported symbol vt_symbol
0 Exchange.SSE OES False 100 21国债⑺ True None 0 None 100 Product.EQUITY 10 False 010107 010107.SSE
1 Exchange.SSE OES False 100 03国债⑶ True None 0 None 100 Product.EQUITY 10 False 010303 010303.SSE
2 Exchange.SSE OES False 100 05国债⑷ True None 0 None 100 Product.EQUITY 10 False 010504 010504.SSE
3 Exchange.SSE OES False 100 05国债⑿ True None 0 None 100 Product.EQUITY 10 False 010512 010512.SSE
4 Exchange.SSE OES False 100 06国债⑼ True None 0 None 100 Product.EQUITY 10 False 010609 010609.SSE
5 Exchange.SSE OES False 100 06国债⒆ True None 0 None 100 Product.EQUITY 10 False 010619 010619.SSE
6 Exchange.SSE OES False 100 07国债06 True None 0 None 100 Product.EQUITY 10 False 010706 010706.SSE
7 Exchange.SSE OES False 100 07国债13 True None 0 None 100 Product.EQUITY 10 False 010713 010713.SSE
8 Exchange.SSE OES False 100 国开1401 True None 0 None 100 Product.EQUITY 10 False 018003 018003.SSE
9 Exchange.SSE OES False 100 国开1702 True None 0 None 100 Product.EQUITY 10 False 018006 018006.SSE
10 Exchange.SSE OES False 100 国开1801 True None 0 None 100 Product.EQUITY 10 False 018007 018007.SSE
11 Exchange.SSE OES False 100 国开1802 True None 0 None 100 Product.EQUITY 10 False 018008 018008.SSE
12 Exchange.SSE OES False 100 国开1803 True None 0 None 100 Product.EQUITY 10 False 018009 018009.SSE
13 Exchange.SSE OES False 100 10国债02 True None 0 None 100 Product.EQUITY 10 False 019002 019002.SSE
14 Exchange.SSE OES False 100 10国债03 True None 0 None 100 Product.EQUITY 10 False 019003 019003.SSE
15 Exchange.SSE OES False 100 10国债07 True None 0 None 100 Product.EQUITY 10 False 019007 019007.SSE
16 Exchange.SSE OES False 100 10国债09 True None 0 None 100 Product.EQUITY 10 False 019009 019009.SSE
17 Exchange.SSE OES False 100 10国债12 True None 0 None 100 Product.EQUITY 10 False 019012 019012.SSE
18 Exchange.SSE OES False 100 10国债14 True None 0 None 100 Product.EQUITY 10 False 019014 019014.SSE
19 Exchange.SSE OES False 100 10国债18 True None 0 None 100 Product.EQUITY 10 False 019018 019018.SSE
20 Exchange.SSE OES False 100 10国债19 True None 0 None 100 Product.EQUITY 10 False 019019 019019.SSE
21 Exchange.SSE OES False 100 10国债23 True None 0 None 100 Product.EQUITY 10 False 019023 019023.SSE
22 Exchange.SSE OES False 100 10国债24 True None 0 None 100 Product.EQUITY 10 False 019024 019024.SSE
23 Exchange.SSE OES False 100 10国债26 True None 0 None 100 Product.EQUITY 10 False 019026 019026.SSE
24 Exchange.SSE OES False 100 10国债29 True None 0 None 100 Product.EQUITY 10 False 019029 019029.SSE
25 Exchange.SSE OES False 100 10国债31 True None 0 None 100 Product.EQUITY 10 False 019031 019031.SSE
26 Exchange.SSE OES False 100 10国债34 True None 0 None 100 Product.EQUITY 10 False 019034 019034.SSE
27 Exchange.SSE OES False 100 10国债37 True None 0 None 100 Product.EQUITY 10 False 019037 019037.SSE
28 Exchange.SSE OES False 100 10国债40 True None 0 None 100 Product.EQUITY 10 False 019040 019040.SSE
29 Exchange.SSE OES False 100 10国债41 True None 0 None 100 Product.EQUITY 10 False 019041 019041.SSE
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
16289 Exchange.SZSE OES False 100 越博动力 True None 0 None 100 Product.EQUITY 100 False 300742 300742.SZSE
16290 Exchange.SZSE OES False 100 天地数码 True None 0 None 100 Product.EQUITY 100 False 300743 300743.SZSE
16291 Exchange.SZSE OES False 100 欣锐科技 True None 0 None 100 Product.EQUITY 100 False 300745 300745.SZSE
16292 Exchange.SZSE OES False 100 汉嘉设计 True None 0 None 100 Product.EQUITY 100 False 300746 300746.SZSE
16293 Exchange.SZSE OES False 100 锐科激光 True None 0 None 100 Product.EQUITY 100 False 300747 300747.SZSE
16294 Exchange.SZSE OES False 100 金力永磁 True None 0 None 100 Product.EQUITY 100 False 300748 300748.SZSE
16295 Exchange.SZSE OES False 100 顶固集创 True None 0 None 100 Product.EQUITY 100 False 300749 300749.SZSE
16296 Exchange.SZSE OES False 100 宁德时代 True None 0 None 100 Product.EQUITY 100 False 300750 300750.SZSE
16297 Exchange.SZSE OES False 100 迈为股份 True None 0 None 100 Product.EQUITY 100 False 300751 300751.SZSE
16298 Exchange.SZSE OES False 100 隆利科技 True None 0 None 100 Product.EQUITY 100 False 300752 300752.SZSE
16299 Exchange.SZSE OES False 100 爱朋医疗 True None 0 None 100 Product.EQUITY 100 False 300753 300753.SZSE
16300 Exchange.SZSE OES False 100 华致酒行 True None 0 None 100 Product.EQUITY 100 False 300755 300755.SZSE
16301 Exchange.SZSE OES False 100 中山金马 True None 0 None 100 Product.EQUITY 100 False 300756 300756.SZSE
16302 Exchange.SZSE OES False 100 罗博特科 True None 0 None 100 Product.EQUITY 100 False 300757 300757.SZSE
16303 Exchange.SZSE OES False 100 七彩化学 True None 0 None 100 Product.EQUITY 100 False 300758 300758.SZSE
16304 Exchange.SZSE OES False 100 康龙化成 True None 0 None 100 Product.EQUITY 100 False 300759 300759.SZSE
16305 Exchange.SZSE OES False 100 迈瑞医疗 True None 0 None 100 Product.EQUITY 100 False 300760 300760.SZSE
16306 Exchange.SZSE OES False 100 立华股份 True None 0 None 100 Product.EQUITY 100 False 300761 300761.SZSE
16307 Exchange.SZSE OES False 100 上海瀚讯 True None 0 None 100 Product.EQUITY 100 False 300762 300762.SZSE
16308 Exchange.SZSE OES False 100 锦浪科技 True None 0 None 100 Product.EQUITY 100 False 300763 300763.SZSE
16309 Exchange.SZSE OES False 100 新诺威 True None 0 None 100 Product.EQUITY 100 False 300765 300765.SZSE
16310 Exchange.SZSE OES False 100 每日互动 True None 0 None 100 Product.EQUITY 100 False 300766 300766.SZSE
16311 Exchange.SZSE OES False 100 震安科技 True None 0 None 100 Product.EQUITY 100 False 300767 300767.SZSE
16312 Exchange.SZSE OES False 100 迪普科技 True None 0 None 100 Product.EQUITY 100 False 300768 300768.SZSE
16313 Exchange.SZSE OES False 100 德方纳米 True None 0 None 100 Product.EQUITY 100 False 300769 300769.SZSE
16314 Exchange.SZSE OES False 100 新媒股份 True None 0 None 100 Product.EQUITY 100 False 300770 300770.SZSE
16315 Exchange.SZSE OES False 100 智莱科技 True None 0 None 100 Product.EQUITY 100 False 300771 300771.SZSE
16316 Exchange.SZSE OES False 100 运达股份 True None 0 None 100 Product.EQUITY 100 False 300772 300772.SZSE
16317 Exchange.SZSE OES False 100 拉卡拉 True None 0 None 100 Product.EQUITY 100 False 300773 300773.SZSE
16318 Exchange.SZSE OES False 100 标准券 True None 0 None 0 Product.EQUITY 0 False 131990 131990.SZSE

16319 rows × 16 columns


In [4]:
# 查询资金
engine.get_all_accounts(use_df=True)


Out[4]:
accountid available balance frozen gateway_name vt_accountid
0 1888000315 10011029372761 10011029372761 0 OES OES.1888000315

In [5]:
# 查询持仓
engine.get_all_positions(use_df=True)


Out[5]:
direction exchange frozen gateway_name pnl price symbol volume vt_positionid vt_symbol yd_volume
0 Direction.NET Exchange.SSE 0 OES 0 4.1881 600010 296900 600010.SSE.Direction.NET 600010.SSE 1000000
1 Direction.NET Exchange.SSE 0 OES 0 32.0068 600036 2300 600036.SSE.Direction.NET 600036.SSE 0

In [6]:
# 查询活动委托
engine.get_all_active_orders(use_df=True)

In [7]:
# 订阅行情
engine.subscribe(["600036.SSE"])

In [9]:
# 查询行情
engine.get_tick("600036.SSE", use_df=True)


Out[9]:
ask_price_1 ask_price_2 ask_price_3 ask_price_4 ask_price_5 ask_volume_1 ask_volume_2 ask_volume_3 ask_volume_4 ask_volume_5 ... limit_down limit_up low_price name open_interest open_price pre_close symbol volume vt_symbol
0 34.6 34.62 34.63 34.64 34.65 0 0 0 0 0 ... 0 0 33.85 0 34.57 0.0 600036 1900 600036.SSE

1 rows × 36 columns


In [10]:
# 委托下单
vt_orderid = engine.buy("600036.SSE", 32, 1000)
print(vt_orderid)


OES.1000124

In [11]:
# 查询特定委托
engine.get_order(vt_orderid)


Out[11]:
OrderData(gateway_name='OES', symbol='600036', exchange=<Exchange.SSE: 'SSE'>, orderid='1000124', type=<OrderType.LIMIT: '限价'>, direction=<Direction.NET: '净'>, offset=<Offset.OPEN: '开'>, price=32, volume=1000, traded=1000, status=<Status.ALLTRADED: '全部成交'>, time=datetime.datetime(2019, 4, 30, 17, 31, 56, 965000, tzinfo=datetime.timezone(datetime.timedelta(seconds=28800))))

In [12]:
# 委托撤单
engine.cancel_order(vt_orderid)

In [ ]: