Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to ensure that stock holding dont exceed my cash

After running the code, anyone can advise why the total position for stocks will exceed max 100000 or min notional -100000?
What is the purpose for this setting our maximum position size?
context.max_notional = 1000000.1
context.min_notional = -1000000.0

My cash is 100,000 how to ensure not to exceed the total cash? Is there any control on my total short and long position do not exceed my cash position? Sometimes the total position can go up to $200,000?

Appreciate some help on this. Thank you

import datetime  
import pytz

def initialize(context):  
# Here we initialize each stock.  
# By calling symbols('AAPL', 'IBM', 'CSCO') we're storing the Security objects.  
context.stocks = symbols('AAPL', 'IBM', 'CSCO')  
context.vwap = {}  
context.price = {}

# Setting our maximum position size, like previous example  
context.max_notional = 1000000.1  
context.min_notional = -1000000.0

# Initializing the time variables we use for logging  
# Convert timezone to US EST to avoid confusion  
est = pytz.timezone('EST')  
context.d=datetime.datetime(2000, 1, 1, 0, 0, 0, tzinfo=est)  

def handle_data(context, data):  
# Initializing the position as zero at the start of each frame  
notional=0

# This runs through each stock.  It computes  
# our position at the start of each frame.  
for stock in context.stocks:  
    price = data[stock].price  
    notional = notional + context.portfolio.positions[stock].amount * price  
    tradeday = data[stock].datetime  

# This runs through each stock again.  It finds the price and calculates  
# the volume-weighted average price.  If the price is moving quickly, and  
# we have not exceeded our position limits, it executes the order and  
# updates our position.  
for stock in context.stocks:  
    vwap = data[stock].vwap(3)  
    price = data[stock].price  

    if price < vwap * 0.995 and notional > context.min_notional:  
        order(stock,-100)  
        notional = notional - price*100  
    elif price > vwap * 1.005 and notional < context.max_notional:  
        order(stock,+100)  
        notional = notional + price*100

# If this is the first trade of the day, it logs the notional.  
if (context.d + datetime.timedelta(days=1)) < tradeday:  
    log.debug(str(notional) + ' - notional start ' + tradeday.strftime('%m/%d/%y'))  
    context.d = tradeday  
2 responses

In this line you are shorting 100 shares of the stock

order(stock,-100)  

And in this line you are going long 100 shares of the stock,

order(stock,+100)  

The minimum and maximum notionals exist so your balance stays within those limits. A more robust way to control your cash is to use order_target_percent and the family of order_target() functions. These will automatically seek to their target positions, based on the number of filled orders, and keeping your cash management under control. Give it a try!

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.

many thks :) for ur help and advice