Hi,
I am reading this simple example provided by Quantopian. In this code, notional is calculated as
notional = context.portfolio.positions[context.aapl].amount * price
My question is that why can't we just use context.portfolio.positions_value ?
Is there a reasoning behind trying to calculate it rather than using context.portfolio.positions_value ?
# For this example, we're going to write a simple momentum script. When the
# stock goes up quickly, we're going to buy; when it goes down quickly, we're
# going to sell. Hopefully we'll ride the waves.
# To run an algorithm in Quantopian, you need two functions: initialize and
# handle_data.
def initialize(context):
context.aapl = sid(24)
context.max_notional = 1000000.1
context.min_notional = -1000000.0
def handle_data(context, data):
vwap = data[context.aapl].vwap(3)
# We need a variable for the current price of the security to compare to
# the average.
price = data[context.aapl].price
notional = context.portfolio.positions[context.aapl].amount * price
record(cash = context.portfolio.cash)
if price < vwap * 0.995 and notional > context.min_notional:
order(context.aapl,-100)
log.info("Selling %s" % (context.aapl))
elif price > vwap * 1.005 and notional < context.max_notional:
order(context.aapl,+100)
log.info("Buying %s" % (context.aapl))