MACD-histogram on the S&P 500 (Amibroker And Tradestation Code)

The strategy in plain English:

The strategy was first published in Alexander Elder’s Trading for A Living.

  1. The MACD histogram bar must have fallen 4 days in a row.
  2. The fourth latest bar must have been below zero.
  3. The current close of the ETF must be lower than the day before.
  4. If the above three criteria are true, then enter at the close.
  5. Exit on the close when today’s close is higher than yesterday’s high.

Click here to see the original article.

Amibroker code:

Fast = EMA(C,12) – EMA(C,26);
Slow = EMA(Fast,9);
Histogram = Fast – slow;

Buy= C<Ref(C,-1) AND Ref(Histogram,-4) < 0 AND Ref(Histogram,-4) > Ref(Histogram,-3) AND Ref(Histogram,-3) > Ref(Histogram,-2) AND Ref(Histogram,-2) > Ref(Histogram,-1) AND Ref(Histogram,-1) > Histogram;
buyPrice = Close;
Sell = Close > Ref(H,-1);
sellPrice = Close ;

Tradestation code:

{
MACD-histogram on the S&P 500  (strategy 7)
The strategy was first published in Alexander Elder's Trading for A Living.
https://www.quantifiedstrategies.com/macd-histogram/
The MACD histogram bar must have fallen 4 days in a row.
The fourth latest bar must have been below zero.
The current close of the ETF must be lower than the day before.
If the above three criteria are true, then enter at the close.
Exit on the close when today’s close is higher than yesterday’s high.
}

Inputs:
	EmaFastLookback1(12),
	EmaFastLookback2(26),
	EmaSlowLookback(9);
	
Vars:
	Fast(0),
	Slow(0),
	Histogram(0);

Fast = XAverage(Close, EmaFastLookback1)-XAverage(Close, EmaFastLookback2);
Slow = XAverage(Fast,EmaSlowLookback);
Histogram = Fast-Slow;

if (Close < Close[1]) And
   (Histogram[4] < 0) And
   (Histogram[4] > Histogram[3]) And
   (Histogram[3] > Histogram[2]) And
   (Histogram[2] > Histogram[1]) And
   (Histogram[1] > Histogram) Then
   Buy this bar on close;

if (Close > High[1]) Then
	Sell this bar on close;

Disclosure: We are not financial advisors. Please do your own due diligence and investment research or consult a financial professional. All articles are our opinions – they are not suggestions to buy or sell any securities.