import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import random
import sys
import time
sys.path.append('D:\\tquant\\tqc\\api\\python')
import tquant_api
# 初始化
tqapi = tquant_api.TQuantApi("tcp://localhost:10001")
tapi = tqapi.trade_api()
dapi = tqapi.data_api()
# 查询交易帐号的状态
r, msg = tapi.account_status()
print msg
r
|
account |
account_id |
broker |
msg |
status |
0 |
056244 |
simnow |
SimNow仿真第一套 |
|
Connected |
# 查询资金
balances, _ = tapi.query_balance('simnow')
balances
{'account_id': 'simnow',
'avail': 930510.5239999999,
'balance': 997396.7329999999,
'close_pnl': 0.0,
'commission': 41.387,
'deposit': 0.0,
'float_pnl': 170.0,
'frozen_margin': 25935.0,
'margin': 40755.0,
'pre_balance': 997268.1199999999,
'pre_margin': 0.0,
'withdraw': 0.0}
# 查询持仓
pos, msg = tapi.query_positions('simnow')
print msg
pos
|
account_id |
close_pnl |
code |
commission |
cost |
cost_price |
current_size |
enable_size |
float_pnl |
frozen_size |
init_size |
last_price |
name |
side |
today_size |
0 |
simnow |
0.0 |
rb1801.SHF |
41.387 |
413870.0 |
0.0 |
11 |
4 |
170.0 |
7 |
0.0 |
0.0 |
螺纹钢1801 |
Long |
11.0 |
# 查询订单
orders, msg = tapi.query_orders('simnow')
print msg
orders.tail()
|
account_id |
code |
entrust_action |
entrust_date |
entrust_no |
entrust_price |
entrust_size |
entrust_time |
fill_price |
fill_size |
name |
order_id |
status |
status_msg |
15 |
simnow |
rb1801.SHF |
Buy |
20171109 |
216283 |
3741.0 |
1 |
135848000 |
0.0 |
0 |
螺纹钢1801 |
7 |
Accepted |
未成交 |
16 |
simnow |
rb1801.SHF |
Buy |
20171109 |
216063 |
3739.0 |
1 |
135838000 |
0.0 |
0 |
螺纹钢1801 |
6 |
Accepted |
未成交 |
17 |
simnow |
rb1801.SHF |
Buy |
20171109 |
215922 |
3741.0 |
1 |
135831000 |
0.0 |
0 |
螺纹钢1801 |
5 |
Accepted |
未成交 |
18 |
simnow |
rb1801.SHF |
Buy |
20171109 |
213574 |
3759.0 |
1 |
135617000 |
0.0 |
1 |
螺纹钢1801 |
3 |
Filled |
全部成交报单已提交 |
19 |
simnow |
rb1801.SHF |
Buy |
20171109 |
211035 |
3760.0 |
1 |
135301000 |
0.0 |
1 |
螺纹钢1801 |
2 |
Filled |
全部成交报单已提交 |
# 查询成交
trades, msg = tapi.query_trades('simnow')
print msg
trades
|
account_id |
code |
entrust_action |
entrust_no |
fill_date |
fill_no |
fill_price |
fill_size |
fill_time |
name |
order_id |
0 |
simnow |
rb1801.SHF |
Buy |
165070 |
20171109 |
99452 |
3787.0 |
1 |
110419000 |
|
1 |
1 |
simnow |
rb1801.SHF |
Buy |
237121 |
20171109 |
147158 |
3749.0 |
1 |
142957000 |
|
16 |
2 |
simnow |
rb1801.SHF |
Buy |
236775 |
20171109 |
147124 |
3750.0 |
1 |
142955000 |
|
15 |
3 |
simnow |
rb1801.SHF |
Buy |
226598 |
20171109 |
146988 |
3754.0 |
1 |
142947000 |
|
14 |
4 |
simnow |
rb1801.SHF |
Buy |
223634 |
20171109 |
146877 |
3756.0 |
1 |
142930000 |
|
9 |
5 |
simnow |
rb1801.SHF |
Buy |
225711 |
20171109 |
138881 |
3766.0 |
1 |
140844000 |
|
13 |
6 |
simnow |
rb1801.SHF |
Buy |
225452 |
20171109 |
138721 |
3767.0 |
1 |
140821000 |
|
12 |
7 |
simnow |
rb1801.SHF |
Buy |
225271 |
20171109 |
138604 |
3769.0 |
1 |
140809000 |
|
11 |
8 |
simnow |
rb1801.SHF |
Buy |
224930 |
20171109 |
138373 |
3770.0 |
1 |
140742000 |
|
10 |
9 |
simnow |
rb1801.SHF |
Buy |
213574 |
20171109 |
130300 |
3759.0 |
1 |
135617000 |
|
3 |
10 |
simnow |
rb1801.SHF |
Buy |
211035 |
20171109 |
128673 |
3760.0 |
1 |
135301000 |
|
2 |
# 下单
# action
# Buy 买入开仓
# Short 卖出开仓 股票无意义
# Cover 买入平仓 股票无意义
# Sell 卖出平仓
# 返回值
# 期货 { 'order_id': n}
# 股票 { 'entrust_no': 'abc' }
price = dapi.quote('rb1801.SHF')[0]['ask1']
r, msg = tapi.place_order('simnow', code='rb1801.SHF', price=price, size=1, action="Sell")
print msg
r
# 取消订单
# 可以通过 order_id 取消期货订单,必须用entrust_no取消股票订单
price = dapi.quote('rb1801.SHF')[0]['last'] - 10
r, msg = tapi.place_order('simnow', code='rb1801.SHF', price=price, size=1, action="Buy")
print "place_result", r
r, msg = tapi.cancel_order('simnow', code='rb1801.SHF', order_id=r['order_id'])
print "cancel_result", r, msg
place_result {'order_id': 25}
order: rb1801.SHF 25 Inserted 报单已提交
order: rb1801.SHF 246679 25 Accepted 未成交
order: rb1801.SHF 246679 25 Accepted 未成交
order: rb1801.SHF 246679 25 Cancelled 已撤单
cancel_result True None
# 订单状态和成交推送
def on_order_status(order):
print "order:", order['code'], order['entrust_no'], order['order_id'], order['status'], order['status_msg']
def on_trade(trade):
print "trade:", trade['code'], trade['entrust_no'], trade['fill_price'], trade['fill_size'], trade['fill_no']
tapi.set_on_order_status(on_order_status)
tapi.set_on_order_trade(on_trade)
price = dapi.quote('rb1801.SHF')[0]['ask1']
r, msg = tapi.place_order('simnow', code='rb1801.SHF', price=price, size=1, action="Buy")
print "place_result", r
place_result {'order_id': 26}
order: rb1801.SHF 26 Inserted 报单已提交
order: rb1801.SHF 246772 26 Accepted 未成交
order: rb1801.SHF 246642 24 Accepted 未成交
order: rb1801.SHF 246772 26 Accepted 未成交
order: rb1801.SHF 246642 24 Filled 全部成交
trade: rb1801.SHF 246642 3763.0 1 151701
order: rb1801.SHF 246772 26 Filled 全部成交
trade: rb1801.SHF 246772 3763.0 1 151702