Back to Community
Swing trading in Robinhood using the MACD

So far I've been able to successfully code my 'in-person' manual swing trading strategies on a handpicked basket of stocks. Normally, for each stock traded I use charts from Google Finance to visually inspect the chart and parameterize MACD settings specific to each stock. Its also worth nothing that I view stocks in the 1-month, 30-minute interval view and manually fit MACD values that produce crossovers at my desired entry and exit points. There are no mathematic optimizations involved and I only hold long positions until a sell signal has been reached.

In the example algorithm attached, I focused on the stock AMD, emulating how to trade in real life. Here are some considerations in the logic of my algorithm:

  1. Long Positions only (utilizing Robinhood)
  2. Produce charting data that is similar to my view in Google Finance (1-month window, 30-minute intervals)
  3. Calculate MACD values using TA-LIB
  4. Trade on MACD crossovers - if 'MACD_hist > 0' then BUY, if 'MACD < 0' then SELL

Essentially the meat of the algorithm is described in pseudo-code below:

Start_of_Day (before_trading_start):  
    traded_today_stock1 = False  
    traded_today_stock2 = False  
    traded_today_stock3 = False  
    etc...

Every_minute (handle_data)  
   macd_function_stock1  
   macd_function_stock2  
   macd_function_stock3  
   etc...

macd_function_stock1(context, data)  
   talib_calculate_MACD_variables  
    if MACD crossover positive and not traded today:  
        BUY  
        traded_today_stock1 = True  
    elif MACD crossover negative and not traded today:  
        SELL  
        traded_today_stock1 = True  

Essentially, the results of the backtest reveal that as the MACD parameters are tuned correctly, the algorithm works so long as you are staying updated on the stock's volatility. This post was meant for other programming noobs transitioning from technical or fundamental trading to automated algorithmic trading. Any questions or comments are highly appreciated!

Clone Algorithm
114
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5983a993a1a95054cedc0cea
There was a runtime error.
5 responses

Hi, Patrick. Thanks for sharing your algorithm.

AMD is a gift that keeps giving. For some perspective, a simple buy-and-hold of AMD during the same period nets 373% gains. But of course, your algorithm significantly reduces beta and volatility, and that cash that's freed up every time you sell can theoretically be used on other profitable low-volatility trades between AMD trades.

I tried swapping AMD out for MU and GOOG_L and the results were terrible. Do you use a different MACD periods tailored to the different stocks in your basket? If so, how do you determine those values? Or do you use entirely different strategies for each stock? I'm curious how you apply this outside of AMD. Do you have an out-of-sample success-rate with it? I'm curious how you approach this issue with statistical rigor.

What's the screening criteria for your basket of stocks? Is it something you could recreate through pipeline?

If you want to run the MACD check once every 30 minutes instead of minutely it won't affect the results too much and speeds up the backtester quite a bit, this is one way you can do that:

def handle_data(context, data):  
    if get_datetime().minute % 30 == 0:  
        macd_AMD(context, data)  

Thanks for the input Viridian - I'll be testing out the modification to execute the MACD function at various time intervals. To address your main question, I use different MACD settings that are calibrated/parameterized for each stock (usually by eye and examining the Google Finance stock chart at the 30min interval, 30day time window). I would imagine that you could utilize some kind of Machine Learning algorithm or an Iterative research function in 'Research' to determine the most appropriate MACD settings for each stock. Once I learn enough about Python coding I think it may be possible to execute these kinds of things (I'm a non-coder MATLAB user)

I keep reading things about Pipeline but I don't fully grasp its overall concept. I think its a module that allows automatic security picking based on set values? If so, then I'm sure pipeline can pick stocks similar to AMD which have Beta > 1.5.

That's interesting -- so in your experience do specific stocks have their own momentum characteristics that they more or less stay true to? I would have assumed that maybe for a while a specific MACD would work, then it would fail hard, and another would work for a while maybe, and so on.

Basically with pipeline you set a bunch of criteria, such as beta > 1.5 or price > sma_20 or whatever you want and it'll return a basket of stocks for you before each trading day that fit those criteria.

if you back test over a longer period, the results aren't nearly as good. how could you modify this to improve the results over longer time periods?

Hey Tony, that's because for AMD the stock metrics and trading behavior changed from the past vs now. The example that I provided are my MACD settings for this specific time window that works. Once the behavior of the stock changes, I'll re-parameterize the MACD. For now, just enjoy the algo for what it is (a way for technical swing traders to execute their 'manual' strategies automatically) and for noobs like me to utilize as a framework for other trading algorithms.

Essentially, the results of the backtest reveal that as the MACD parameters are tuned correctly, the algorithm works so long as you are staying updated on the stock's volatility. This post was meant for other programming noobs transitioning from technical or fundamental trading to automated algorithmic trading.

As far as the other stocks and MACD settings that I use for my portfolio, I am opting to keep those private. Hope you understand