Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Having trouble calculating moving averages on new Quantopian 2

Hi everyone,

I am new to Quantopian, but have previous C++ experience. I am trying to build an algorithm that compares different SMAs. The problem is I don't think I am using the right coding, or the coding I interpreted from the Quant 2 transition guide wasn't correctly represented.

Anyhow, I am thinking that the proper way to call the SMAs for any price history, in minute mode, would now be:

price_history = data.history(context.security,'close',50,'1m')
sma10 = price_history[-10:].mean()
sma20 = price_history[-20:].mean()
sma50 = price_history[-50:].mean()

The above is just an example of what I have tried (please note: I have moved the colon to the left and taken it out and I still have a problem). So the problem is that when I compare my buy and sell timing in the logs to a stock chart's SMA overlays, the algorithm is not buying or selling at the right time. For instance, the start of a bullish trend on the price is when the SMA20 crosses above the SMA50, so I try and buy to hold until selling when the SMA10 crosses below the SMA20. Then start the whole process over again.

With the above coding, the algorithm buys when the SMA20 is below the SMA50, though is tell it order(context.security,1) when SMA20 > SMA50. Any help would be appreciated, I'm kind of losing hair over this...

Thank you all.

Danny

3 responses

Your calculation of the SMAs look correct. I suspect that the problem is actually in your decision-making logic, but that's impossible to know without sharing the code.

I've implemented your rule set in this backtest. For debugging purposes, I changed it to 1d rather than 1m - it makes the graphs more useful. When you've got your logic running you can change it back to 1m.

I notice that when using your rule set, it's possible for sma20>sma50 (bullish) but sma10<sma20(bearish) at the same time, and that leads to flapping. Easy enough to modify when you decide what you want.

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, Dan.

This seems to have corrected my issue. I wasn't executing my 'if' loops as you did. Btw, are the templates that Quantopian has developed able to be browsed by the community? I don't see a link to take us there...

Thanks, again.

Danny

I'm not sure what templates you're referring to? We have lots of algorithms for you to copy (clone) and tinker with:

And of course everything here in the forums.