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

Hi Everyone,

I'm new to Quantopian, like this is my first week seriously paying attention to it.

Interestingly enough, I found an algorithm that pretty closely approximates my trading approach. I was concerned, however, with the performance so I adjusted the strategy slightly and got these results for my backtest.

Specifically, I don't like shorting the market so I set context.min_notional to 0. Also, the algorithm was actually buying when it should've been selling and selling when it should've been buying. This fix was simple. The less-than logical operators should be greater-than.

Great start!

4 responses

Hey Thomas,
Nice :). Your algorithm is great as you have it, and I have a few suggestions that you might be interested in. First, it you may find it easier to use an order_target function like order_target_percent instead of just order. This way you don't have to check if you own the position in the if, you can just set the target for .1 of your total portfolio. But you are obviously doing some other stuff here, so the way you are doing it may be ideal. You can also remove the lines about the notional — those are left over from the example algo, and aren't currently doing anything in your algo besides being defined.

Another thing, the way you have it now you don't actually need to import talib. This is because you are using the built-in talib functions, not the ones imported from the module. Although we are deprecating the ones you are currently using, they will still work, but they can be very buggy at times (which is why they are being deprecated). See my example here using RSI in talib. There is also a 26 day warm-up period in your strategy that you will have to account for if you use this.

You might also find the record feature interesting. But again, nice strategy!

Gus

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.

Thomas,
Welcome to Quantopian, I took some time go through your algorithm. I got it using minute data and talib rather than the ta transforms. I also found an accounting error that was allowing you to buy more securities than the imposed limit of 10.

When handle_data is called, context.portfolio contains the current account values as of the time when the function was called. The portfolio variables will not be updated until handle data is called again. That means context.portfolio.cash will not change within that bar, even if you place several orders. You have to do a little extra work to track any money spent within handle_data calls. Here is an example of how you can do that.

# Managing cash spent within a call to handle_data

cash = context.portfolio.cash # Cash as of the beginning of the handle_data call

for stock in data:  
    price = data[stock].price  
    if some_buy_signal:  
        order(stock, 10)  
        cash -= 10 * price

    if some_sell_signal:  
        order(stock, -10)  
        cash += 10 * price  

The algo respects its 10 stock limit now, so the rest of your logic was good. It trades less often now also, only when a new slot opens up. Maybe you can add something to select the 'more ideal' stocks to fill those open slots, whatever that might mean : )

happy tinkering,
David

Thanks Gus & Dave! There's a lot more I want to do with this algo but just haven't been able to get back to it in the last few days. I'll definitely incorporate your suggestions and I will definitely be filtering based on the best stocks fitting MACD crossover criteria. Hopefully I can reach out to you when I get stuck again!

Hi Calhoun, I am new to Quantopian. I cloned your algorithm and trying to understand it. This might be a very naive question :) You have "9.99" in the code, is this the trading fee? In the comments, you mentioned $5 as trading fee.