聚宽量化交易Portfolio与Context对象学习笔记

聚宽的API文档对Portfolio,Context对象的描述理解不清晰,自己动手输出了Portfolio,Context对象的详细属性。(遇到不理解不明白的地方,自己动手实践输出)

Portfolio对象

Portfolio({
'available_cash': 26581.98,

'subportfolios': [SubPortfolio({'available_cash': 26581.98, 'total_value': 99891.979999999996, 'transferable_cash': 26581.98, 'type': 'stock', 'locked_cash': 0.0, 'inout_cash': 100000.0, 'interest': 0, 'short_positions': {}, 'positions_value': 73310.0, 'margin': 0, 'long_positions': {'600196.XSHG': Position({'hold_cost': 19.72, 'total_amount': 1300, 'closeable_amount': 0, 'price': 19.699999999999999, 'today_amount': 1300, 'avg_cost': 19.72, 'pindex': None, 'value': 25610.0, 'interest': None, 'locked_amount': 0, 'security': '600196.XSHG', 'side': 'long'}), '000858.XSHE': Position({'hold_cost': 31.84, 'total_amount': 1500, 'closeable_amount': 0, 'price': 31.800000000000001, 'today_amount': 1500, 'avg_cost': 31.84, 'pindex': None, 'value': 47700.0, 'interest': None, 'locked_amount': 0, 'security': '000858.XSHE', 'side': 'long'})}})],

'_unsell_positions': {'600196.XSHG': UnsellPosition({'avg_cost': 19.72, 'amount': 1300, 'price': 19.699999999999999, 'security': '600196.XSHG', 'side': 'long'}), '000858.XSHE': UnsellPosition({'avg_cost': 31.84, 'amount': 1500, 'price': 31.800000000000001, 'security': '000858.XSHE', 'side': 'long'})},

'transferable_cash': 26581.98,

'portfolio_value': 99891.979999999996,

'locked_cash': 0.0,

'starting_cash': 100000.0,

'inout_cash': 100000.0,

'short_positions': {},

'positions_value': 73310.0,

'margin': 0,

'long_positions': {'600196.XSHG': Position({'hold_cost': 19.72, 'total_amount': 1300, 'closeable_amount': 0, 'price': 19.699999999999999, 'today_amount': 1300, 'avg_cost': 19.72, 'pindex': None, 'value': 25610.0, 'interest': None, 'locked_amount': 0, 'security': '600196.XSHG', 'side': 'long'}), '000858.XSHE': Position({'hold_cost': 31.84, 'total_amount': 1500, 'closeable_amount': 0, 'price': 31.800000000000001, 'today_amount': 1500, 'avg_cost': 31.84, 'pindex': None, 'value': 47700.0, 'interest': None, 'locked_amount': 0, 'security': '000858.XSHE', 'side': 'long'})}})

在类似Portfolio({})的数据结构中,在Portfolio({})结构下”:” 前的字段为上一级的属性名称,”:” 后的字段为属性值。Python语言里通过”.”访问属性。

subportfolios属性是一个列表

long_positions和_unsell_positions属性是字典,字典的键是股票代码,字典的值是UnsellPosition对象和Position对象。

 

Subportfolio对象

[SubPortfolio({
'available_cash': 26581.98,
'total_value': 99891.979999999996,
'transferable_cash': 26581.98,
'type': 'stock',
'locked_cash': 0.0,
'inout_cash': 100000.0,
'interest': 0,
'short_positions': {},
'positions_value': 73310.0,
'margin': 0,
'long_positions': {'600196.XSHG': Position({'hold_cost': 19.72, 'total_amount': 1300, 'closeable_amount': 0, 'price': 19.699999999999999, 'today_amount': 1300, 'avg_cost': 19.72, 'pindex': None, 'value': 25610.0, 'interest': None, 'locked_amount': 0, 'security': '600196.XSHG', 'side': 'long'}), '000858.XSHE': Position({'hold_cost': 31.84, 'total_amount': 1500, 'closeable_amount': 0, 'price': 31.800000000000001, 'today_amount': 1500, 'avg_cost': 31.84, 'pindex': None, 'value': 47700.0, 'interest': None, 'locked_amount': 0, 'security': '000858.XSHE', 'side': 'long'})}})]

 

Context对象

UserContext({
'current_dt': datetime.datetime(2010, 1, 4, 9, 30),

'portfolio': Portfolio({'available_cash': 26581.98, 'subportfolios': [SubPortfolio({'available_cash': 26581.98, 'total_value': 99891.979999999996, 'transferable_cash': 26581.98, 'type': 'stock', 'locked_cash': 0.0, 'inout_cash': 100000.0, 'interest': 0, 'short_positions': {}, 'positions_value': 73310.0, 'margin': 0, 'long_positions': {'600196.XSHG': Position({'hold_cost': 19.72, 'total_amount': 1300, 'closeable_amount': 0, 'price': 19.699999999999999, 'today_amount': 1300, 'avg_cost': 19.72, 'pindex': None, 'value': 25610.0, 'interest': None, 'locked_amount': 0, 'security': '600196.XSHG', 'side': 'long'}), '000858.XSHE': Position({'hold_cost': 31.84, 'total_amount': 1500, 'closeable_amount': 0, 'price': 31.800000000000001, 'today_amount': 1500, 'avg_cost': 31.84, 'pindex': None, 'value': 47700.0, 'interest': None, 'locked_amount': 0, 'security': '000858.XSHE', 'side': 'long'})}})], '_unsell_positions': {'600196.XSHG': UnsellPosition({'avg_cost': 19.72, 'amount': 1300, 'price': 19.699999999999999, 'security': '600196.XSHG', 'side': 'long'}), '000858.XSHE': UnsellPosition({'avg_cost': 31.84, 'amount': 1500, 'price': 31.800000000000001, 'security': '000858.XSHE', 'side': 'long'})}, 'transferable_cash': 26581.98, 'portfolio_value': 99891.979999999996, 'locked_cash': 0.0, 'starting_cash': 100000.0, 'inout_cash': 100000.0, 'short_positions': {}, 'positions_value': 73310.0, 'margin': 0, 'long_positions': {'600196.XSHG': Position({'hold_cost': 19.72, 'total_amount': 1300, 'closeable_amount': 0, 'price': 19.699999999999999, 'today_amount': 1300, 'avg_cost': 19.72, 'pindex': None, 'value': 25610.0, 'interest': None, 'locked_amount': 0, 'security': '600196.XSHG', 'side': 'long'}), '000858.XSHE': Position({'hold_cost': 31.84, 'total_amount': 1500, 'closeable_amount': 0, 'price': 31.800000000000001, 'today_amount': 1500, 'avg_cost': 31.84, 'pindex': None, 'value': 47700.0, 'interest': None, 'locked_amount': 0, 'security': '000858.XSHE', 'side': 'long'})}}),

'previous_date': datetime.date(2009, 12, 31),

'run_params': RunParams({'end_date': datetime.date(2016, 12, 1), 'frequency': u'day', 'type': 'simple_backtest', 'start_date': datetime.date(2010, 1, 1), 'extra_gloabl_vars': {}}),

'universe': ['600196.XSHG', '000858.XSHE']})

 

SecurityFullData对象

SecurityFullData({
'volume': 9526558.0,
'high_limit': 21.530000000000001,
'money': 185187712.0,
'low_limit': 17.609999999999999,
'high': 19.699999999999999,
'paused': 0.0,
'pre_close': 19.57,
'low': 19.359999999999999,
'factor': 1.0,
'close': 19.370000000000001,
'security': '600196.XSHG',
'avg': 19.440000000000001,
'open': 19.699999999999999,
'dt': datetime.datetime(2010, 1, 4, 0, 0)})

SecurityFullData对象可由data[security]返回,security不限于当前context的universe股票池。返回的股票信息是Context.previous_date的。