Are you trying to run an algo within a notebook or did you just copy your algo code into a notebook for demonstration? Algos don't run in notebooks and it would be helpful if you attached the actual algo. That said, a couple of things.
Looking at this statement
for security in context.portfolio.positions:
# check to see if our longs have broken down. otherwise, do nothing.
if security['latest price'] < security['50D MA']:
order_target_percent(security,0)
else:
continue
'security' is an equity object. It only has a limited number of attributes (see https://www.quantopian.com/help#api-sidinfo). The fields you're trying to reference ('latest price' and '50D MA') are columns in the pipeline output (ie context.output) which is a pandas dataframe. The best way to fetch single values from a dataframe is to use the .at
method (https://pandas.pydata.org/pandas-docs/version/0.18/generated/pandas.DataFrame.at.html). So maybe the code could be something like this:
for stock in context.portfolio.positions:
yesterday_close = context.output.at[stock, 'latest price']
sma_50 = context.output.at[stock, '50D MA']
if yesterday_close < sma_50 and data.can_trade(stock):
order_target_percent(stock, 0)
That fetches 'latest price' and '50D MA' for that specific security from the context.output dataframe. It then compares those values. BTW it's always a good idea to check if 'data.can_trade' in case a security became delisted. Doesn't happen often but the algo will error if it does.
While the above should work, some may consider it not very 'pythonic'. Generally, python (and especially pandas) is better at doing things across entire groups of data in a parallel manner and then working with those groups. Additionally, it's often very advantageous to NOT use spaces in dataframe column names. Best to use an underscore instead. So this would be another way to do the same thing:
# Select all the stocks meeting the query criteria then turn that into a list
close_these = context.output.query('latest_price < 50D_MA').index.tolist()
for stock in context.portfolio.positions:
if stock in close_these and data.can_trade(stock):
order_target_percent(stock, 0)
Part of the benefit of this approach is by using the query method the logic can be as involved and complex as one wishes and still remain very readable. See https://pandas.pydata.org/pandas-docs/version/0.18/generated/pandas.DataFrame.query.html and https://pandas.pydata.org/pandas-docs/version/0.18/indexing.html#indexing-query for some good examples.
Hope that get's the error resolved.
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.