Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
My first algorithm - MACD crossover

Hi everybody,
I'm new to Python and TA-Lib. Just wanted to share my first algorithm, trading based on MACD crossover.

I wanted to do something that approximated how I might actually trade i.e.
- based on a small account size (I'm using $10k for my testing)
- buys positions of 10% ($1k) or so
- only trades if the cash is available (i.e. avoiding margin)
- allotting $5 for trade costs

Do you think my assumptions are realistic for how the automated trading works?

I'd appreciate any feedback you have.

4 responses

Hi Alex - this is a great algorithm, especially for a first try.

I think your assumptions are good. I think your trade costs are going to be $1 per trade at that level. That is IB's minimum commission, I think. You can use the set_commissions() function to write that into your algo explicitly.

The other thing you'll have to do before really trading is adapt this to a minute-level of data. Even if you're only trading once per month, the data will come in more frequently. Getting ta-lib to work in minutely mode is tricky, and a number of people are working through that right now.

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.

I'm confused about how to adapt algorithms which use daily sma and macd to use minute data. Is there someone with an example of how to make this work?

Hello Richard,

There aren't many examples that I am aware of. In the attached (untested) backtest I had a play at getting 1D and 15M timeframes into a minutely backtest. Grant is looking at the same sort of thing here: https://www.quantopian.com/posts/ma-cross-over-w-slash-rsi#523c20062eaae1e513000132

Thomas & Jess have both talked of a 'refactoring' which will more readily allow access to, or at least calculation of, multiple timeframe data. See here: https://www.quantopian.com/posts/timeframes-discussion (Jess) and here: https://www.quantopian.com/posts/new-feature-ta-lib-support (Thomas)

P.

Hello, I'm fairly new here regarding the big 3 (Python, Quantopian, and Stocks). Can someone help me understand something? The following snippet of code is confusing me from the post above:

    MA_1M   =  float(ztt.talib.MA( data_1M[context.stocks[0]], timeperiod=60)[-1:])  
    MA_15M  =  float(ztt.talib.MA(data_15M[context.stocks[0]], timeperiod=26)[-1:])  
    MA_1D   =  float(ztt.talib.MA( data_1D[context.stocks[0]], timeperiod=10)[-1:])  

timeperiod = 60. Does this mean 60 periods or the length of 1 period is defined as 60 ticks? So if I wanted to do MACD here, I would need to do 60 * 12 and 60 * 26 for 12 and 26 period EMAs? Or would I do timeperiod = 12 and timeperiod = 26?