vn.py量化社区
By Traders, For Traders.
Member
avatar
加入于:
帖子: 8
声望: 0

from time import sleep

from vnpy.app.script_trader import ScriptEngine

def run(engine: ScriptEngine):

""""""

vt_symbols = ["ag2006.SHFE", "ag2008.SHFE"]

# 订阅行情

engine.subscribe(vt_symbols)

# 获取合约信息

for vt_symbol in vt_symbols:

    contract = engine.get_contract(vt_symbol)

    msg = f"合约信息,{contract}"

    engine.write_log(msg)
    positions=engine.get_all_positions(use_df=True)
    engine.write_log(positions)
    bp=engine.get_position(vt_positionid=f"{vt_symbol}.Direction.LONG")
    sp=engine.get_position(vt_positionid=f"{vt_symbol}.Direction.SHORT")
    engine.write_log(f"{vt_symbol} bp:{bp}  sp:{sp}")

bp=engine.get_position(vt_positionid="rb2005.SHFE.Direction.LONG")
engine.write_log(f"rb2005:  bp:{bp}")

2020-04-21 16:03:25.063435 策略交易脚本停止
2020-04-21 16:03:27.224664 策略交易脚本启动
2020-04-21 16:03:27.229683 合约信息,ContractData(gateway_name='CTP', symbol='ag2006', exchange=<Exchange.SHFE: 'SHFE'>, name='白银2006', product=<Product.FUTURES: '期货'>, size=15, pricetick=1.0, min_volume=1, stop_supported=False, net_position=False, history_data=False, option_strike=0, option_underlying='', option_type=None, option_expiry=None, option_portfolio='', option_index='')
2020-04-21 16:03:27.248596 direction exchange frozen gateway_name pnl ... symbol volume vt_positionid vt_symbol yd_volume
0 Direction.SHORT Exchange.CZCE 0 CTP -975.0 ... CF005 3 CF005.CZCE.空 CF005.CZCE 3
1 Direction.SHORT Exchange.SHFE 0 CTP 450.0 ... zn2102 3 zn2102.SHFE.空 zn2102.SHFE 3
2 Direction.SHORT Exchange.DCE 0 CTP 4320.0 ... y2005 6 y2005.DCE.空 y2005.DCE 6
3 Direction.LONG Exchange.SHFE 0 CTP -480.0 ... rb2005 3 rb2005.SHFE.多 rb2005.SHFE 3
4 Direction.SHORT Exchange.SHFE 0 CTP 910.0 ... fu2005 7 fu2005.SHFE.空 fu2005.SHFE 7
5 Direction.SHORT Exchange.DCE 0 CTP -3100.0 ... eg2005 5 eg2005.DCE.空 eg2005.DCE 5
6 Direction.SHORT Exchange.SHFE 0 CTP 3420.0 ... ag2006 6 ag2006.SHFE.空 ag2006.SHFE 6
7 Direction.SHORT Exchange.CZCE 0 CTP 60.0 ... ZC005 1 ZC005.CZCE.空 ZC005.CZCE 1
8 Direction.SHORT Exchange.CZCE 0 CTP 320.0 ... SR005 4 SR005.CZCE.空 SR005.CZCE 4
9 Direction.LONG Exchange.CZCE 0 CTP -990.0 ... RM005 3 RM005.CZCE.多 RM005.CZCE 3
10 Direction.SHORT Exchange.CZCE 0 CTP -1920.0 ... OI005 8 OI005.CZCE.空 OI005.CZCE 8
11 Direction.SHORT Exchange.CZCE 0 CTP 450.0 ... MA005 3 MA005.CZCE.空 MA005.CZCE 3

[12 rows x 11 columns]
2020-04-21 16:03:27.312420 ag2006.SHFE bp:None sp:None
2020-04-21 16:03:27.312420 合约信息,ContractData(gateway_name='CTP', symbol='ag2008', exchange=<Exchange.SHFE: 'SHFE'>, name='白银2008', product=<Product.FUTURES: '期货'>, size=15, pricetick=1.0, min_volume=1, stop_supported=False, net_position=False, history_data=False, option_strike=0, option_underlying='', option_type=None, option_expiry=None, option_portfolio='', option_index='')
2020-04-21 16:03:27.342339 direction exchange frozen gateway_name pnl ... symbol volume vt_positionid vt_symbol yd_volume
0 Direction.SHORT Exchange.CZCE 0 CTP -975.0 ... CF005 3 CF005.CZCE.空 CF005.CZCE 3
1 Direction.SHORT Exchange.SHFE 0 CTP 450.0 ... zn2102 3 zn2102.SHFE.空 zn2102.SHFE 3
2 Direction.SHORT Exchange.DCE 0 CTP 4320.0 ... y2005 6 y2005.DCE.空 y2005.DCE 6
3 Direction.LONG Exchange.SHFE 0 CTP -480.0 ... rb2005 3 rb2005.SHFE.多 rb2005.SHFE 3
4 Direction.SHORT Exchange.SHFE 0 CTP 910.0 ... fu2005 7 fu2005.SHFE.空 fu2005.SHFE 7
5 Direction.SHORT Exchange.DCE 0 CTP -3100.0 ... eg2005 5 eg2005.DCE.空 eg2005.DCE 5
6 Direction.SHORT Exchange.SHFE 0 CTP 3420.0 ... ag2006 6 ag2006.SHFE.空 ag2006.SHFE 6
7 Direction.SHORT Exchange.CZCE 0 CTP 60.0 ... ZC005 1 ZC005.CZCE.空 ZC005.CZCE 1
8 Direction.SHORT Exchange.CZCE 0 CTP 320.0 ... SR005 4 SR005.CZCE.空 SR005.CZCE 4
9 Direction.LONG Exchange.CZCE 0 CTP -990.0 ... RM005 3 RM005.CZCE.多 RM005.CZCE 3
10 Direction.SHORT Exchange.CZCE 0 CTP -1920.0 ... OI005 8 OI005.CZCE.空 OI005.CZCE 8
11 Direction.SHORT Exchange.CZCE 0 CTP 450.0 ... MA005 3 MA005.CZCE.空 MA005.CZCE 3

[12 rows x 11 columns]
2020-04-21 16:03:27.364280 ag2008.SHFE bp:None sp:None
2020-04-21 16:03:27.364280 rb2005: bp:None

持仓总是输出None,这是什么原因?我用的是最新版本2.1.2.1

Administrator
avatar
加入于:
帖子: 4033
声望: 222

vt_positionid=f"{vt_symbol}.LONG"
vt_positionid=f"{vt_symbol}.SHORT"

© 2015-2019 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号-3