top of page

MACD crossover strategy with EMA200

Writer's picture: ProRealAlgosProRealAlgos

The conditions of the strategy

  1. A buy signal is generated when the MACD crosses up over the MACD Signal line and the signal line and MACD are below the Zero line, and price closes above the 200 Exponential Moving Average.

  2. Stop loss for a buy signal is based on the most recent swing low and/or the 200 period Exponential Moving Average whichever is nearest the price when the trade is taken.

  3. A sell signal is generated when the MACD crosses under the MACD signal line and the signal line and MACD are above the MACD Zero line, and price closes below the 200 EMA.

  4. Stop loss for a sell signal is based on the most recent swing high and/or the 200 period Exponential Moving average whichever is nearest the price when the trade is taken.

  5. Take Profit can be based on either Option 1 an adjustable Risk/Reward ratio (Trailing Stop loss to be at 0) or Option 2 an adjustable Trailing Stop loss percentage (risk /reward ratio to be at 0).



The code

DEFPARAM CumulateOrders = False

MyMACD = MACDline[12,26,9](close)
MySLine = MACDSignal[12,26,9](close)
MyHis = MACD[12,26,9](close)
EMA200 = ExponentialAverage[200](close)

C1 = MyMACD Crosses Over MySLine AND MyMACD < 0 AND MySLine < 0
C2 = Close > EMA200
BuyCondition = C1 AND C2

IF BuyCondition THEN
Buy 1 Shares AT Market
SwingLow = lowest[10](low) // Assuming a 10-bar lookback for swing low
NearestLevel = Min(SwingLow, EMA200) // Selecting the nearest level to the current price

 slosslevel = Tradeprice - NearestLevel
set stop ploss slosslevel
set stop %trailing 0.5
ENDIF



C3 = MyMACD Crosses Under MySLine AND MyMACD > 0 AND MySLine > 0
C4 = Close < EMA200
SellCondition = C3 AND C4

IF SellCondition THEN
Sellshort 1 Shares AT Market
SwingHigh = highest[10](high) // Assuming a 10-bar lookback for swing high
NearestLevel = Max(SwingHigh, EMA200) // Selecting the nearest level to the current price
slosslevel = Tradeprice - NearestLevel
set stop ploss slosslevel
set stop %trailing 0.5
ENDIF

// Take Profit options
Option1RiskReward = 2 // Adjustable Risk/Reward ratio (e.g., 2 means 2:1)
Option2TrailStopPercent = 0.5 // Adjustable Trailing Stop loss percentage (e.g., 0.5%)
NearestStopLoss = 10 // minimum stop distance
TakeProfitOption1 = NearestLevel + (NearestLevel - NearestStopLoss) * Option1RiskReward

if (open < TakeProfitOption1 and close > TakeProfitOption1) or (open > TakeProfitOption1 and close < TakeProfitOption1) then
sell at market
exitshort at market
endif 



722 views0 comments

Recent Posts

See All

Comments


Try the products free for 7 days

Easy setup • full version • get-started support

ProRealTime made easy

CFDs are complex instruments and come with a high risk of losing money rapidly due to leverage. On average 76% of retail investor accounts lose money when trading CFDs. Past performance is no guarantee of future results. You should consider whether you understand how CFDs work before starting.

Read full terms and conditions

ProRealTime™ is a trademark registered by the third-party entity ProRealTime SAS

We have no link with the ProRealTime™ entity that offers the ProRealTime™ trading software

Copyright ProRealAlgos 580648-4231
Harju maakond, Tallinn, Kesklinna linnaosa, Pille 13, 10173

bottom of page