Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Moving averages help!

Hi Guys,

I am having a bit of a problem with this simple algo. It works based on a simple cross over of the 20 and 50 averages but it seems it only trades a bit at the beginning and then stops trading. I recorded the averages and the conditions seems to meet but it just doesn't trade.

please help :)

All the best,

Daniel

7 responses

Hi Daniel, I think your problem is in the conditionals. You have "if mvg20 == mvg50" and those are both floats with lots of precision. If they actually equal each other, it's pretty much luck. You want to use inequalities, like "if mvg20 > mvg50"

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.

That makes sense! On the graph it looks like they match but really they dont. I did have an algo before which used inequalities but since the inequality is not a single point, my algo kept buying every min they were unequal and as a result i kept averaging up or down depends if it was long or short buy. I am looking to write an algo which focuses on the intersection and buy or sell based on the turning point in the stocks movement. Any advice on how i can do something like that?

One way to do it is to set a flag. When you go long, set the flag as long, and only order when the flag is unset. It's a way of going long (or short) only once.

The more rigorous way would be to evaluate your position. When your signal tells you to buy, you also check if you're already long. Only buy if you a) have a signal and b) no position.

You'll see both of those as pretty common in some of the other shared algorithms.

After capitalizing on what Dun said I added some simple conditions

Hello David,

I fiddled with your code a bit...see the attached backtest. I opted to use the batch transform, with a 100-day window length. This way, it is straightforward to postpone trading until all of the moving averages can be computed (there is no "warm-up" for the built-in moving average when backtesting). I also added notional limits, in the same fashion as some of the examples on the help page. You have to constrain the algorithm in some way, or it can borrow unlimited amounts of money.

I also played around with your trading logic:

if price/mvg20>1 and mvg50/mvg20<1 and notional<context.max_notional:  
    order(context.stocks[0],100)  
if price/mvg20>1 and mvg50/mvg20>1 and mvg50/mvg100<1 and notional>context.min_notional:  
    order(context.stocks[0],-100)  

Grant

Hey grant,

I was wondering if you can perhaps elaborate on your code a bit. I tried analyzing it and am having a bit of a difficulty.

thank you,

Daniel

Hello Daniel,

Which part do you not understand?

Grant