The context.portfolio.positions
object is indeed a dictionary. The keys of the dictionary are 'Asset' objects (which are also sometimes referred to as security and equity objects) and the values are 'Position' objects. It may not seem like a 'normal' Python dictionary maybe because both the keys and the values are objects and NOT the actual data itself. As an example, 'amount' is a property of each 'Position' object in the context.portfolio.positions
dictionary. So, to get the amount of a particular position one could do something like this (assuming you know that you hold SPY)
spy_asset = symbol('SPY')
my_spy_amount = context.portfolio.positions[spy_asset].amount
# or another way
my_spy_amount = context.portfolio.positions.get(spy_asset).amount
'context.portfolio.positions[spy_asset] returns the value indexed by 'spy_asset'. This 'value' is a position object. So, to get to the properties of that object a simple way is to use the '.' (dot) notation.
Typically, one doesn't know what's currently held in the portfolio. It's common to loop over all positions to do something. In this case, close all positions with less than $1000 in value.
for security, position in context.portfolio.positions.items():
value = abs(position.amount) * position.last_sale_price
if value < 1000 and data.can_trade(security):
order_target(security, 0)
The order
object is just a plain object with properties like 'amount' and 'status'. (See https://www.quantopian.com/help#api-orderobj for more properties.) The way to get a reference to an order object is with the get_order
method. Give it the order ID and it returns a reference to the object. Order IDs are returned by every order
method (they are really functions). Most of the time one doesn't care about the order id so it's often not programmed like this but, if you do want to store the id, do something like this.
my_order_id = order_target_percent(my_stock, my_percent)
Here's perhaps a more realistic example
for order_id in context.todays_orders[:]:
original_order = get_order(order_id)
if original_order and original_order.status == 2 :
# The order was somehow cancelled so retry
retry_id = order(
original_order.sid,
original_order.amount,
style=LimitOrder(original_order.limit)
)
log.info('order for %i shares of %s cancelled - retrying' % (original_order.amount, original_order.sid))
# Remove the original order (typically can't do but note the [:]) and store the new order
context.todays_orders.remove(original_order)
context.todays_orders.append(retry_id)
This will check each order previously stored in a user defined list 'context.todays_orders'. If it has a status of 'cancelled' (ie 2) then place another order using the properties in the original order. It then takes the new order id and put's it into the order list. Generally it's good practice to store order ids and NOT the actual references to the order.
There is another ordering example where the order id is used here https://www.quantopian.com/help#ide-ordering .
Hope this helps?