Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Problem With Algo - Build Error with no Deatails

Hi everyone.

I've been interested in using evolving algos in trading for a while and created a simple one just so I can have a place t start. However, when I run it, I get a Build Error - Line 29, but no details on the error. I look at Line 29, and there is no error. Any input?

import random

def initialize(context):  
  context.base_order = 100  
  context.sec_id = 24  
  context.amat = sid(24)  
  context.max_notional = 1000000.1  
  context.min_notional = -1000000.0  
  sells = [-1, -2, -3]  
  buys = [1, 2, 3]  
  buyPoints = [5, 15, 25]  
  sellPoints = [10, 20, 30]  
  positions = [0, 0, 0]  
  count = 0

def handle_data(context, data):  
  mavg = data[context.amat].mavg(20)  
  price = data[context.amat].price  
  notional = context.portfolio.positions[context.amat].amount * price  
 # if price > mavg:  
 #     order(context.amat, -25)  
 # if price < 450:  
 #     order(context.amat, +20)  
  for i in range(0, 3):  
      if price > sellPoints[i]:  
          order(context.amat, sells[i])  
          positions[i]-=((price)*(sells[i])  
      if price < buyPoints[i]:  
          #Line above in Line 29, the one with the error  
          order(context.amat, buys[i])  
          positions[i]-=((price)*(buys[i])  
  count++  
  if count == 7:  
      temp = 0  
      best = null  
      for i in range(0,3):  
          if positions[i] >= temp:  
              temp = positions[i]  
              best = i  
      bestSell = sells[best]  
      bestBuy = buys[best]  
      bestSellPoint = sellPoints[best]  
      bestBuyPoint = buyPoints[best]  
      for i in range(0,3):  
          sells[i] = bestSellPoint*random.randrange(-2,2,.01)  
          buys[i] = bestBuyPoint*random.randrange(-2,2,.01)  
          sellPoints[i] = bestSellPoint*random.randrange(-2,2,.01)  
          buyPoints[i] = bestBuyPoint*random.randrange(-2,2,.01)  
      count = 0  
3 responses

bump

Hi Edward:

I fixed up your algo here. The problems I saw were:
- to keep state around, you should use the context variable (or declare global variables). I stored all your state variables (sellPoints, buyPoints, etc) in context.
- Python doesn't have the ++ operator. I replaced it with += 1.

hope that helps,
Jean

import random

def initialize(context):  
  context.base_order = 100  
  context.sec_id = 24  
  context.amat = sid(24)  
  context.max_notional = 1000000.1  
  context.min_notional = -1000000.0  
  context.sells = [-1, -2, -3]  
  context.buys = [1, 2, 3]  
  context.buyPoints = [5, 15, 25]  
  context.sellPoints = [10, 20, 30]  
  context.positions = [0, 0, 0]  
  context.count = 0

def handle_data(context, data):  
  mavg = data[context.amat].mavg(20)  
  price = data[context.amat].price  
  notional = context.portfolio.positions[context.amat].amount * price  
 # if price > mavg:  
 #     order(context.amat, -25)  
 # if price < 450:  
 #     order(context.amat, +20)  
  for i in range(0, 3):  
      if price > context.sellPoints[i]:  
          order(context.amat, context.sells[i])  
          context.positions[i] -= (price * context.sells[i])  
      if price < context.buyPoints[i]:  
          #Line above in Line 29, the one with the error  
          order(context.amat, context.buys[i])  
          context.positions[i] -= (price * context.buys[i])  
  context.count += 1  
  if context.count == 7:  
      temp = 0  
      best = null  
      for i in range(0,3):  
          if context.positions[i] >= temp:  
              temp = positions[i]  
              best = i  
      bestSell = sells[best]  
      bestBuy = buys[best]  
      bestSellPoint = sellPoints[best]  
      bestBuyPoint = buyPoints[best]  
      for i in range(0,3):  
          sells[i] = bestSellPoint*random.randrange(-2,2,.01)  
          buys[i] = bestBuyPoint*random.randrange(-2,2,.01)  
          context.sellPoints[i] = bestSellPoint*random.randrange(-2,2,.01)  
          context.buyPoints[i] = bestBuyPoint*random.randrange(-2,2,.01)  
      context.count = 0  
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.

thank you!