Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
No Trade in Backtest - Help

Hi everyone,
I am trying to develop basic moving average crossover algorithm to learn step by step. When I backtested the strategy it didn't take any position, neither short nor long. Can someone help me with this?

Trading logic:
When 55 sma > 100 sma > 200 trend is up for that stock and vice versa.

To take long position on stock:
21 sma > 55 sma

To take short position on stock:
21 sma < 55 sma

Please feel free to correct me, about any code in the algorithm, to develop a better algorithm

14 responses

Anyone?

Hi Gerard,

There are two issues with your code here.

First, on lines 53 and 54, double inequalities like that aren't supported. To get the appropriate functionality, you would do something like this instead:

isTrendUp = ((sma_55 > sma_100) & (sma_100 > sma_200))  
isTrendDown = ((sma_55 < sma_100) & (sma_100 < sma_200))  

Second, on lines 57 and 64: Note that isTrendUp and isTrendDown are mutually exclusive. So you should be checking if one or the other is true, not if both are true (which is never the case). So your mask should be isTrendUp | isTrendDown and your screen should be the following:

pipe.set_screen((isTrendUp | isTrendDown) & top20_market_cap)  

This can also be abbreviated to pipe.set_screen(top20_market_cap), since top20_market_cap is masked by the trends, meaning it will only give True for those that already follow one of the trends.

I hope this helps. If you ever have another specific code question like this, I would recommend asking Support by clicking on Learn & Support > Contact Support in the top menu bar. That will help us get to your question faster.

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 Nathan that worked perfect.

Thanks Nathan!

Last version of the code with RSI.
I also tried to add MACD but couldn't manage.

Hi Gerard,

I was able to incorporate a pipeline factor that calculates MACD.

It is by no means a finished product, but an example of using ta-lib within a Pipeline Factor.

Best,
Lotanna Ezenwa

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.

Thanks Lotenna! Great work.

I am improving the algorithm step by step. When I added MACD criteria algorithm gets long positions but short. There is no short position at all. When I delete the MACD < 0 from context.short_list it takes short position.
Can someone help me?

Hi Gerard,

I noticed an issue in your algorithm.
For your purposes, you'll be wanting to use the h signal from ta.MACD(), as it is the trading signal that calculates the difference between 'm' and 's'.

As for the logic under before_trading_start, you can set them all as pipeline screens, to speed up computations and help readability.
More information can be found in our API documentation covering screens and factors.

I would also suggest employing a mask on your MACD factor, as it is rather expensive. The mask will allow you to only run MACD on certain equities that you choose.

Best,

Lotanna Ezenwa

I changed MACD as 'h' but still no short position.

In line 97 and 99, you forgot to include parentheses around context.['MACD'] >< 0, so the logic is not executing properly.

Also, I apologize for suggesting switch from 'm' to 'h', either signal is a MACD signal, but they measure different things.

Hi Lotanna!
I've made the changes you suggested but there is no short position. I think I am doing something wrong in pipeline but I could't figure it out.

Here's the backtest showing the short positions

Thanks Lotanna!

I really appreciate the solution.

Best regards,
Gerard