Back to Community
Long and short values, counts and sids lists, plus other useful items

Long/short value of shares, total each

longs_values = sum([context.portfolio.positions[s].amount * data.current(s, 'price') for s in context.portfolio.positions if context.portfolio.positions[s].amount > 0])  
shrts_values = sum([context.portfolio.positions[s].amount * data.current(s, 'price') for s in context.portfolio.positions if context.portfolio.positions[s].amount < 0])  

Edit: Improvement from Dan Whitnable below ...

longs_values = sum([pos.amount * pos.last_sale_price for s, pos in context.portfolio.positions.items() if pos.amount > 0])  
shrts_values = sum([pos.amount * pos.last_sale_price for s, pos in context.portfolio.positions.items() if pos.amount < 0])  

Long/short counts, number of positions each

longs_count  = len([s for s in context.portfolio.positions if context.portfolio.positions[s].amount > 0])  
shrts_count  = len([s for s in context.portfolio.positions if context.portfolio.positions[s].amount < 0])  

Edit: Improvement from Dan Whitnable below ...

longs_count  = len([s for s, pos in context.portfolio.positions.items() if pos.amount > 0])  
shrts_count  = len([s for s, pos in context.portfolio.positions.items() if pos.amount < 0])  

Long/short sids list each

longs_sids_list = [s for s in context.portfolio.positions if context.portfolio.positions[s].amount > 0]  
shrts_sids_list = [s for s in context.portfolio.positions if context.portfolio.positions[s].amount < 0]  

Long/short symbols list each

longs_symbols_list = [s.symbol for s in context.portfolio.positions if context.portfolio.positions[s].amount > 0]  
shrts_symbols_list = [s.symbol for s in context.portfolio.positions if context.portfolio.positions[s].amount < 0]  

All positions sids list

context.portfolio.positions.keys()  

All positions symbols list

[s.symbol for s in context.portfolio.positions]

All positions symbols list as string (for print and debugger)

str([s.symbol for s in context.portfolio.positions])  

Positions total value (long and short)

context.portfolio.positions_value  
 float64: 1054.775  

Current cash

context.portfolio.cash  

Profit and loss

context.portfolio.pnl  
 float64: 264.89257026  

Portfolio value

 float64: 1264.89257026  

Returns

context.portfolio.returns  
 float64: 0.26489257026  

Starting cash

context.portfolio.starting_cash  

Backtest current date/time

get_datetime()  
 Timestamp: 2011-12-08 14:40:00+00:00  

Backtest start date/time

context.portfolio.start_date  
get_environment('start')  
 Timestamp: 2011-11-28 14:31:00+00:00  

Backtest start date

get_environment('start').date()  
 date: datetime.date(2011, 11, 28)  

Backtest start time

get_environment('start').time()  
 time: datetime.time(14, 31)  

Backtest end date/time

get_environment('end')  
 Timestamp: 2017-02-10 21:00:00+00:00  

Backtest end date as string

str(get_environment('end').date())  
 2017-02-10  

Backtest current date as string

str(get_datetime().date())  
 2011-12-08  

Arena: Backtest, live (paper trading, 15 minutes delayed) or IB (broker)

get_environment('arena')  
1 response

@Blue Nice Work! Your 'one line' implementations make for compact code.

another implementation... use the .'items()' method and get the position objects and then use the '.last_sale_price' instead of 'data.current'.

longs_values = sum([pos.amount * pos.last_sale_price for s, pos in context.portfolio.positions.items() if pos.amount > 0])  
shrts_values = sum([pos.amount * pos.last_sale_price for s, pos in context.portfolio.positions.items() if pos.amount < 0])  

similar for the counts

longs_count  = len([s for s, pos in context.portfolio.positions.items() if pos.amount > 0])  
shrts_count  = len([s for s, pos in context.portfolio.positions.items() if pos.amount < 0])