Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Just a quick Question

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))  
2 responses

Hi Nyan Paing,

Good question. The object context.portfolio.positions_value returns the sum value of all open positions plus the ending cash balance. So in some cases, like if you have all your cash in a single position, that could be equivalent to computing the number of shares in that stock * the current price, but in many cases (i.e. if you have other open positions or some cash balance in your account) it will not give the same result.

Best regards,
Jess
p.s. see docs on the portfolio object for more details.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Hi Jess,

Thank you so much for clarification.. I got it now... Appreciate it..

Best,

Nyan