Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
SMA Q2 update problem

So I had a SMA crossover algorithm that doesnt seem to work with the update. It used to look a bit like this.

    MA25 = data[stock].mavg(25)  
    MA50 = data[stock].mavg(50)



    if MA25 > MA50:  
    order(context.security, 300, style=LimitOrder(order_price))

The Q2 migration guide looks like SMA changed to something like this

    MA25 = data.history(context.stocks, 'price', 25, '1m')  
    MA50 = data.history(context.stocks, 'price', 50, '1m')

but if I change it i get the error "ValueError: Can only compare identically-labeled DataFrame objects"

5 responses

Did you take the average of your history windows?

    MA25 = data.history(context.stocks, 'price', 25, '1m').mean()  
    MA50 = data.history(context.stocks, 'price', 50, '1m').mean()  

I think that should resolve your issue.

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.

If I do that i get the error "ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."

I tried it this way as well as defined in the Q2 migration guide

MA25X = data.history(asset, 'price', 25, '1m')
MA50X = data.history(asset, 'price', 50, '1m')

MA25 = MA25X.mean()
MA25 = MA50X.mean()

When I did that it would run but no orders were filled. I dont think its the syntax of the orders either, if I get SMAs derived from Q1 syntax it runs. Im really stumped on this one.

Hi Bradley,

Would you be willing to share more of your code so that I can try to help debug it? Without more of the algorithm, it's difficult to pinpoint the issue!

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.

import numpy as np  
import pandas as pd  
import scipy.stats as stats  
import time  
from quantopian.pipeline import Pipeline  
from quantopian.pipeline import CustomFactor  
from quantopian.pipeline.data import morningstar  
from quantopian.pipeline.factors import EWMA, Latest, SimpleMovingAverage  
from quantopian.pipeline.data.builtin import USEquityPricing  
from quantopian.algorithm import attach_pipeline, pipeline_output



def initialize(context):  


    context.stocks = symbols('AA')  







    set_commission(commission.PerShare(cost=0.003, min_trade_cost=1.00))

def handle_data(context, data):

    for stock in context.stocks:  
        MA5X = data.history(context.stocks, 'price', 5, '1d').mean()  
        MA10X = data.history(context.stocks, 'price', 10, '1d').mean()  
        MA50X = data.history(context.stocks, 'price', 50, '1d').mean()  
        MA5 = MA5X.mean()  
        MA10 = MA10X.mean()  
        MA50 = MA50X.mean()  



        position = context.portfolio.positions  
        cash = context.portfolio.cash

        if MA5 >= MA50:  
            if MA10 >= MA50:  
                if position == 0:  
                    if cash > 6000:  
                        order(stock, 300)

        if MA5 <= MA50:  
            if MA10 <= MA50:  
                if position == 0:  
                    if cash > 6000:  
                        order(stock, -300)  

        if MA5 >= MA10 and position < 0:  
            order_target(stock, 0)  

        if MA5 <= MA10 and position > 0:  
            order_target(stock, 0)  
# still troubleshooting it. It looks like its finding if MA5 >= MA50: to be always "true" for some reason....  

Hi Bradley,

The issue here is that position == 0 will never be True since position is a dictionary. Instead, you'll want to do something like if position[stock].amount == 0 or if stock in position. A good way to debug this issue is to either add print statements to your algo to figure out which of your conditions are evaluating to True and to print out certain values. Alternatively, you can use the builtin debugger tool.