Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
MA Crossover

This is a fairly simple algorithm of mine that seems to work well with blue chip stocks (Google is used in this example). It uses moving average crossovers as trading signals. If the moving average of the past five days is greater than that of the past twenty days then it buys. If the moving average of the past three days is greater than that of the past day then it sells.

I made the time frame shorter for the criteria the algorithm uses to determine whether to sell in order to quickly identify negative momentum and minimize losses.

9 responses

Nice algo Jordan. It would be interesting to see how it compares with a buy-and-hold strategy for Google. Here's a post that showed how to do the comparison: https://www.quantopian.com/posts/your-algorithm-v-slash-s-buy-and-hold-strategy

In your next iteration, have you thought about using order_target_percent()? This is a more robust method that will scale with your portfolio size ( ie always allocate 10% to Google stock) instead of simply buying/selling when a threshold is met. Thanks for sharing it!

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.

Hey Jordan
Thanks for posting!! This is great! So I'm a student who can't program, but I'm interested in trading and love this website. I have a Behavioral Finance assignment I'd like to use something like this for that class, but was hoping to use the idea that bitcoin would be good for momentum trading as it is purely sentiment based not value-based. Is there any way to use your algorithm on bitcoin data? like: http://pastebin.com/44z36pie
Unfortunately, I cant program it :(
When I try i get: "CParserError: Error tokenizing data. C error: Expected 1 fields in line 27, saw 2, There was a runtime error on line 13."
Can one of you guys help out?
Thanks!!
JD

Hi I want to ask someone who knows more about programming, how often does this algorithm check MAVG and perform orders based on that?

You can record the mavg variables to see how often they intersect: https://www.quantopian.com/help#ide-record

Or you can run the algo through pyfolio to get a more detailed view about the algo's turnover: https://www.quantopian.com/posts/new-feature-comprehensive-backtest-analysis

I don't want to know how often they intersect, just when the trade is made. If I choose daily backtest then mavg(5) is average for past 5 days, and on the sixth day, at which hour will algorithm compare 5 MAVG with 20 MAVG?

On the 6th day, it will make the comparison at the time your code says to check the MAVG.

The data will be ready for your algo to process, whenever it requires it. If the MAVG calculation is in handle_data, it will run at 9:31AM. If it's in a schedule_function, it will run at the custom time.

thank you this is what I wanted to know :) and if I select minute backtest, then MAVGs are averages over last few minutes?

data[stock].mavg() will always be the daily-moving average, no matter if the backtest is run in daily or minute mode: https://www.quantopian.com/help#ide-transforms

If you want a minute-level moving average, you can use history. Try running the snippet below in minute mode (since you're requesting minute-level data):

# gives a 30 minute moving average  
mavg = history(30, '1m', 'price')  

thank you Alisa :)