Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Issues with incremental cash use

Hi there - python beginner here. I am trying to build an algorithm that deploys cash incrementally and I am running into a frustrating error. I want the algorithm to buy in incremental waves of $100k (wave_A,wave_B etc) then once it has purchased using that code not run that code again. I am trying to do this by using an IF statement at the beginning of the code to test if the 'wave' variable is a 1 or a 0, if it is a 0 it will run the code. Make sense? However I am running into a syntax error when I define the variable and I think I might be defining it in the wrong spot.. any help would be appreciated. Thanks!

def initialize(context):  
    context.security = (sid(24))  
# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    # Implement your algorithm logic here.  
    lot_size = context.portfolio.portfolio_value/10  
    number_of_lots = context.portfolio.cash/lot_size  
    cash = context.portfolio.cash  
    current_price = data[context.security].price  
    number_of_shares = int((lot_size)/current_price)  
    current_price = data[context.security].price  
    bought_price = data[context.portfolio.positions[symbol('aapl')].cost_basis  
    wave_A = 0  
    if cash >= lot_size and wave_A == 0:  
        print "lot size"  
        print lot_size  
        wave_A = wave_A + 1  
        order(context.security, +number_of_shares)  
    elif current_price > bought_price*1.05:  
        order_target(context.security, 0)  
        print "selling %" % (context.security.symbol)  
6 responses

This syntax is not correct: bought_price = data[context.portfolio.positions[symbol('aapl')].cost_basis
Maybe you wanted to write: bought_price = context.portfolio.positions[symbol('aapl')].cost_basis

Anyway, that is not the price at which you bought 'appl'. You have to store that price in 'context' if you like to retrieve that value later on.

Follow your algorithm after I got rid of some major mistakes. So you have something to start from, but the logic has to be fixed:

def initialize(context):  
    context.security = sid(24)  
    context.number_of_lots = 10  
    context.lot_size = context.portfolio.cash/context.number_of_lots  
    context.wave_A = 0  
def handle_data(context, data):  
    cash = context.portfolio.cash  
    current_price = data[context.security].price  
    number_of_shares = int(context.lot_size/current_price)  
    bought_price = context.portfolio.positions[context.security].cost_basis  
    if cash >= context.lot_size and context.wave_A == 0:  
        print "lot size",  context.lot_size  
        context.wave_A += + 1  
        order(context.security, +number_of_shares)  
    elif current_price > bought_price*1.05:  
        order_target(context.security, 0)  
        print "selling %s" % (context.security.symbol)  

In any programming language, when you get a syntax error on one line, always look also at the preceding line, not counting empty lines and comments. Dot your 'i's, cross your 't's, balance your parentheses, square brackets (as here), and curly braces.

Thanks for the help guys! Mind touching a bit more on why we are storing the bought price and lot information under context?

Because context is the only place where we can store anything that will persist after the function returns.

Gotcha. Thank you!