Moving average Help

7
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
 Returns 1 Month 3 Month 6 Month 12 Month
 Alpha 1 Month 3 Month 6 Month 12 Month
 Beta 1 Month 3 Month 6 Month 12 Month
 Sharpe 1 Month 3 Month 6 Month 12 Month
 Sortino 1 Month 3 Month 6 Month 12 Month
 Volatility 1 Month 3 Month 6 Month 12 Month
 Max Drawdown 1 Month 3 Month 6 Month 12 Month

# universe and define any parameters.
def initialize(context):
# Robinhood only allows long positions, use this trading
# guard in case
set_long_only()

# Since we are trading with Robinhood we can set this to \$0!

# Define our five assets.
context.assets = [sid(8554),
sid(33729),
sid(42118),
sid(2938),
sid(41579)]

# Set the allocation per stock
pct_per_algo = 0.95 / len(context.assets)

# Make a separate algo for each stock.
context.algos = [MovingAverage(assets, pct_per_algo) for sid in context.assets]

def handle_data(context, data):
# Call each individual algorithm's handle_data method.
for algo in context.algos:
algo.handle_data(context, data)

# This is the original algorithm but it has been turned into its own class
# with its own handle data method. Now you can create multiple instances of it.

class MovingAverage(object):

# Initialize with a single stock and assign a proportion of the account.
def __init__(self, security, pct_of_account):
self.security = security
self.pct = pct_of_account

def handle_data(self, context, data):

average_price = data[self.security].mavg(5)
current_price = data[self.security].price

# Adjust the cash by the proportion allocated to this stock
cash = context.portfolio.cash * self.pct

if current_price > 1.01*average_price and cash > current_price:

number_of_shares = int(cash / current_price)

order(self.security, number_of_shares)

elif current_price < average_price:

order_target(self.security, 0)
log.info("Selling %s" % (self.security.symbol))

There was a runtime error.
1 response

Hi Nick,

The error here is occurring because the sid method is getting overridden by your use of sid as an iterator variable on line 24. Simply change sid to some other name and the code will run.

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.