ChartArt

MACD + SMA 200 Strategy (by ChartArt)

Here is a combination of the classic MACD ( moving average convergence divergence indicator) with the classic slow moving average SMA with period 200 together as a strategy.

This strategy goes long if the MACD histogram and the MACD momentum are both above zero and the fast MACD moving average is above the slow MACD moving average . As additional long filter the recent price has to be above the SMA 200. If the inverse logic is true, the strategy goes short. For the worst case there is a max intraday equity loss of 50% filter.

Save another $999 bucks with my free strategy.

This strategy works in the backtest on the daily chart of Bitcoin , as well as on the S&P 500             and the Dow Jones Industrial Average             daily charts . Current performance as of November 30, 2015 on the SPX500             CFD             daily is percent profitable: 68% since the year 1970 with a profit factor of 6.4. Current performance as of November 30, 2015 on the DOWI             index daily is percent profitable: 51% since the year 1915 with a profit factor of 10.8.

All trading involves high risk; past performance is not necessarily indicative of future results. Hypothetical or simulated performance results have certain inherent limitations. Unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not actually been executed, the results may have under- or over-compensated for the impact, if any, of certain market factors, such as lack of liquidity. Simulated trading programs in general are also subject to the fact that they are designed with the benefit of hindsight. No representation is being made that any account will or is likely to achieve profits or losses similar to those shown.
Remove from Favorite Scripts Add to Favorite Scripts
//@version=2
strategy("MACD + SMA 200 Strategy (by ChartArt)", shorttitle="CA_-_MACD_SMA_strategy", overlay=true)

// ChartArt's MACD + SMA 200 Strategy
//
// Version 1.0
// Idea by ChartArt on November 30, 2015.
//
// Here is a combination of the MACD with the
// slow moving average SMA 200 as a strategy.
//
// This strategy goes long if the MACD histogram
// and the MACD momentum are both above zero and
// the fast MACD moving average is above the
// slow MACD moving average. As additional long filter
// the recent price has to be above the SMA 200.
// If the inverse logic is true, the strategy
// goes short. For the worst case there is a
// max intraday equity loss of 50% filter.


// Input
source = input(close)
fastLength = input(12, minval=1, title="MACD fast moving average")
slowLength=input(26,minval=1, title="MACD slow moving average")
signalLength=input(9,minval=1, title="MACD signal line moving average")
veryslowLength=input(200,minval=1, title="Very slow moving average")
switch1=input(true, title="Enable Bar Color?")
switch2=input(true, title="Enable Moving Averages?")
switch3=input(true, title="Enable Background Color?")

// Calculation
fastMA = sma(source, fastLength)
slowMA = sma(source, slowLength)
veryslowMA = sma(source, veryslowLength)
macd = fastMA - slowMA
signal = sma(macd, signalLength)
hist = macd - signal

// Colors
MAtrendcolor = change(veryslowMA) > 0 ? green : red
trendcolor = fastMA > slowMA and change(veryslowMA) > 0 and close > slowMA ? green : fastMA < slowMA and change(veryslowMA) < 0 and close < slowMA ? red : blue
bartrendcolor = close > fastMA and close > slowMA and close > veryslowMA and change(slowMA) > 0 ? green : close < fastMA and close < slowMA and close < veryslowMA and change(slowMA) < 0 ? red : blue
backgroundcolor = slowMA > veryslowMA and crossover(hist, 0) and macd > 0 and fastMA > slowMA and close[slowLength] > veryslowMA ? green : slowMA < veryslowMA and crossunder(hist, 0) and macd < 0 and fastMA < slowMA and close[slowLength] < veryslowMA ? red : na
bgcolor(switch3?backgroundcolor:na,transp=80)
barcolor(switch1?bartrendcolor:na)

// Output
F=plot(switch2?fastMA:na,color=trendcolor)
S=plot(switch2?slowMA:na,color=trendcolor,linewidth=2)
V=plot(switch2?veryslowMA:na,color=MAtrendcolor,linewidth=4)
fill(F,V,color=gray)

// Strategy
buyprice = low
sellprice = high
cancelLong = slowMA < veryslowMA
cancelShort = slowMA > veryslowMA

if (cancelLong)
    strategy.cancel("MACDLE")

if crossover(hist, 0) and macd > 0 and fastMA > slowMA and close[slowLength] > veryslowMA 
    strategy.entry("MACDLE", strategy.long, stop=buyprice, comment="Bullish")

if (cancelShort)
    strategy.cancel("MACDSE")

if crossunder(hist, 0) and macd < 0 and fastMA < slowMA and close[slowLength] < veryslowMA 
    strategy.entry("MACDSE", strategy.short, stop=sellprice, comment="Bearish")

maxIdLossPcnt = input(50, "Max Intraday Loss(%)", type=float)
strategy.risk.max_intraday_loss(maxIdLossPcnt, strategy.percent_of_equity)

//plot(strategy.equity, title="equity", color=red, linewidth=2, style=areabr)
ChartArt
a year ago
Current performance as of November 30, 2015:

on the SPX500 (S&P 500) daily chart the strategy is profitable 68% since the year 1970 with a profit factor of 6.4

on the DOWI (Dow Jones) daily chart the strategy is profitable 51% since the year 1915 with a profit factor of 10.8

on the XAUUSD (Gold) daily chart the strategy is profitable 66.67% since the year 1920 with a profit factor of 7.2

(not a profitable strategy on several other assets though)
Reply
Really impressive. Great work!
Nice how you're using the 'classics' to implement it.
Let's see how it performs on lifetime and lower timeframes. :)
Reply
tradersf
9 months ago
Nice Work! I use it to trade currencies on a 1 minute chart with tight targets and stop loss. Is there away of manually adding the standard indicators (sma 200 + MACD) so that I can see when it is about to give a signal? As I trade now I just have to keep on eye on the chart and then suddenly a signal appears. It would be a lot easier if I could see the lead up to a signal. Do you know what I mean? Thanks
Reply
limitless
2 months ago
Hi thanks for the strategy, I m trying from your script to build strategy where you go long when both MACD cross over and a 2 Simple moving avarage crossover happen like a 20 and 50 sma.
I keep getting errors \,could you help me? this is the script I m trying.

//@version=2
strategy("MovingAvg2Line Cross", overlay=true)
fastLength = input(9)
slowLength = input(18)
price = close
veryfastLength = input(12)
veryslowlength = input(26)
MACDLength = input(9)

MACD = ema(close, veryfastLength) - ema(close, veryslowlength)
aMACD = ema(MACD, MACDLength)
delta = MACD - aMACD
mafast = sma(price, fastLength)
maslow = sma(price, slowLength)

if (crossover(mafast, maslow)and crossover(hist, 0) and macd > 0 and veryfastMA > veryslowMA
strategy.entry("MA2CrossLE", strategy.long, comment="MA2CrossLE")

if (crossunder(mafast, maslow))
strategy.entry("MA2CrossSE", strategy.short, comment="MA2CrossSE")
Reply
JustUncleL PRO limitless
a month ago
There are a number of errors I can see at line 16: hist, macd, veryfastMA, veryslowMA are all undefined series names: also missing a ')' at the end of the line.
Reply
JustUncleL PRO JustUncleL
a month ago
I made some quick corrections, maybe it should look like this (this compiles without errors):

//@version=2
strategy("MovingAvg2Line Cross", overlay=true)
fastLength = input(9)
slowLength = input(18)
price = close
veryfastLength = input(12)
veryslowlength = input(26)
MACDLength = input(9)

veryfastMA = ema(close, veryfastLength)
veryslowMA = ema(close, veryslowlength)
MACD = veryfastMA - veryslowMA
aMACD = ema(MACD, MACDLength)
delta = MACD - aMACD
mafast = sma(price, fastLength)
maslow = sma(price, slowLength)
hist = MACD - mafast

if (crossover(mafast, maslow) and crossover(hist, 0) and MACD > 0 and veryfastMA > veryslowMA)
strategy.entry("MA2CrossLE", strategy.long, comment="MA2CrossLE")

if (crossunder(mafast, maslow))
strategy.entry("MA2CrossSE", strategy.short, comment="MA2CrossSE")
Reply
limitless JustUncleL
a month ago
Thanks a lot JustUncle but I m still getting an error
Add to Chart operation failed, reason: line 20: missing BEGIN at '|B|'
Reply
JustUncleL PRO limitless
a month ago
Make sure you put 4 spaces at the start of line before "strategy.".
Reply
limitless JustUncleL
a month ago
I m still getting error line 24: missing BEGIN at '|B|'
Reply
ChartArt JustUncleL
a month ago
Please share your final code on "Pastebin" or a similar text sharing website. Thanks! :)
Reply
Ideas Scripts Chart
United States
United Kingdom
India
España
Italia
Brasil
Россия
Türkiye
日本
한국
Home Stock Screener Economic Calendar How It Works Chart Features House Rules Moderators For the WEB Widgets Stock Charting Library Priority Support Feature Request Blog & News FAQ Help & Wiki Twitter
Private Messages Chat Ideas Published Followers Following Priority Support Public Profile Profile Settings Billing Sign Out