Back to Community
How to display current portfolio positions

I'm reading the documentation and want to make sure how to display current positions and not display current unfilled open orders.

There is get_open_orders(security) which appears to show unfilled orders

How to show current holdings?

Thanks

2 responses

Neil:
What you are looking for is context.portfolio.positions

Here is an example of how to use it.

cpp = context.portfolio.positions  
log.info(cpp)  

cpp_symbols = map(lambda x: x.symbol, cpp)  
log.info(cpp_symbols)

Assuming that you held SPY and TLT the above code would result in the following.

2007-02-01 09:31 handle_data:72 INFO {Equity(23921, symbol=u'TLT', asset_name=u'ISHARES 20+ YEAR TREASURY BOND', exchange=u'NASDAQ', start_date=Timestamp('2002-07-24 00:00:00+0000', tz='UTC'), end_date=Timestamp('2016-09-14 00:00:00+0000', tz='UTC'), first_traded=None, auto_close_date=Timestamp('2016-09-19 00:00:00+0000', tz='UTC'), exchange_full=u'NASDAQ GLOBAL MARKET'): Position({'last_sale_date': Timestamp('2007-01-03 14:32:00+0000', tz='UTC'), 'amount': 1064, 'last_sale_price': 87.56, 'cost_basis': 89.2816976167409, 'sid': Equity(23921, symbol=u'TLT', asset_name=u'ISHARES 20+ YEAR TREASURY BOND', exchange=u'NASDAQ', start_date=Timestamp('2002-07-24 00:00:00+0000', tz='UTC'), end_date=Timestamp('2016-09-14 00:00:00+0000', tz='UTC'), first_traded=None, auto_close_date=Timestamp('2016-09-19 00:00:00+0000', tz='UTC'), exchange_full=u'NASDAQ GLOBAL MARKET')}), Equity(8554, symbol=u'SPY', asset_name=u'SPDR S&P 500 ETF TRUST', exchange=u'NYSE', start_date=Timestamp('2002-01-01 00:00:00+0000', tz='UTC'), end_date=Timestamp('2016-09-14 00:00:00+0000'...
2007-02-01 09:31 handle_data:75 INFO [u'TLT', u'SPY']

I'd recommend converting the context.portfolio.positions result to a list of symbols before printing or logging.
Unless you have a very small set of holdings it is likely that log.info(cpp) will be truncated.
Unless you have a very large set of holdings is is unlikely that log.info(cpp_symbols) will be.

Awesome, thank you.