Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
What is the error? It says syntax error on line 30 (marked below)

Any feedback would be much appreciated. Cheers

import math

position_open = False  
position_closed = True  
postions_bought_price = 0;

def initialize(context):  
    context.stock = sid()  
    context.max_notional = 1000000.1  
    context.min_notional = -1000000.0  
    set_commission(commission.PerTrade(cost=5))

def handle_data(context, data):  
  last_sale_price = context.portfolio.positions[sid(24)].last_sale_price  
  vwap3 = data[context.stock].vwap(1)  
  vwap9 = data[context.stock].vwap(3)  
  profit = 0  
  loss = 0  
  price = data[context.stock].price  
  notional = context.portfolio.positions[context.stock].amount * price  

  global position_open  
  global position_closed  
    # When to open buy position  
  if vwap3 > vwap9*1.025 and position_open == False and position_closed == True and notional < context.max_notional:  
        #take a position of 20% of available cash  
    order_value(context.stock, (.2(context.portfolio.cash))  
    global positions_bought_price           #THIS IS LINE 30  
    global order_quantity  
    positions_bought_price = last_sale_price  
    order_quantity = math.floor(((.2(context.portfolio.cash))/positions_bought_price))  
    position_open = True  
    position_closed = False  
    print "Position Opened"  
    # When to take profit  
  if last_sale_price > positions_bought_price*1.2 and position_open == True and position_closed == False:  
    order(context.stock,-orderquantity)  
    position_open = False  
    position_closed = True  
    profit = last_sale_price*order_quantity - positions_bought_price*order_quantity  
    print "Closed by Take Profit with $%d profit" % (profit)  
    # When to stop loss  
  if last_sale_price < positions_bought_price*0.9 and position_open == True and position_closed == False:  
    order(context.stock,-order_quantity)  
    position_open = False  
    position_closed = True  
    #loss = last_sale_price*order_quantity - positions_bought_price*order_quantity  
    loss = positions_bought_price*order_quantity - last_sale_price*order_quantity  
    print "Closed by Stop Loss with $%d loss" % (loss)  
2 responses

You are one closing paren short on the line above the one that signals the error. After fixing that, there are still some issues like where you set context.stock = sid() but you must pass an int to sid.
Also, at a cursory glance, it looks like you are using functions to modify global variables. This kind of stateful programming often leads to race conditions and code that is difficult to reason about. Instead, I urge you to think about explicitly passing state around when needed. You can persist state across multiple calls by storing it in context.

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.

In C# you should not declare globals in a function body. So move them up. Best is to limit the use of globals. In Python declaration is slight different than regular programming languages if I am free to say so. Python is a bit weird. Like you need to tab/space instead using brackets {}.