MACD & 200 EMA Strategy
Forums › ProRealTime English forum › ProOrder support › MACD & 200 EMA Strategy
- This topic has 3 replies, 2 voices, and was last updated 1 year ago by Marketpunk.
-
-
01/13/2023 at 5:30 PM #207376
Hello,
Would it be possible to code a strategy using the MACD and 200 period EMA please. The Strategy works as follows:
It takes a Long trade when the MACD crosses up over the Signal line, when both the signal line and MACD are below the Zero line, and price closed above the 200 EMA .
It takes a Short trade when the MACD crosses under the signal line, when both the Signal line and the MACD are above Zero line, and price closed below the 200 EMA.If possible, an alert could be developed to confirm when the conditions of the strategy are met.
The 100 EMA should also be enabled to provide earlier indication of a change in trend and to allow for a crossing strategy over or under the 200 EMA to be used.
ORDERS
You can have both Long and Short, or disable one.STOPLOSS
Stoploss is based on last swing (high or low). The lookback period of the swing should adjustable. The Stoploss can also be above or below the 200 EMA depending on the direction of the trade.TAKE PROFIT
For now, there is 2 possibilities for TP : Using adjustable Risk/Reward ratio (Trailing Stoploss need to be at 0) Using adjustable Trailing Stoploss percentage (RR Ratio need to be at 0)01/13/2023 at 9:36 PM #207410Hi @Marketpunk
Maybe you can start with this…
Stoploss has yet to be added… (based on swing high/low?)MACD & EMA200 Strategy1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071DefParam CumulateOrders=falsexRRRatio=2 //Adjustable Risk/Reward ratioxSwingPeriod=20 //Lookback period for Swing High/LowxLongTrades=1 //Switch Long Trades ON or OFFxShortTrades=1 //Switch Short Trades ON or OFFIf High[1]=Highest[xSwingPeriod](High) and High[2]<High[1] and High<High[1] thenxSwingHigh=High[1]ElsIf Low[1]=Lowest[xSwingPeriod](Low) and Low[2]>Low[1] and Low>Low[1] thenxSwingLow=Low[1]EndIfxMACDSignal=MACDSignal[12,26,9](close)xMACDLine=MACDline[12,26,9](close)xEMA200=Average[200,1](Close)If xMACDLine Crosses Over xMACDSignal and xMACDLine<0 and xMACDSignal<0 and Close>xEMA200 and xLongTrades thenBuy 1 contract at MarketEndIfIf xMACDLine Crosses Under xMACDSignal and xMACDLine>0 and xMACDSignal>0 and Close<xEMA200 and xShortTrades thenSellShort 1 contract at MarketEndIf//StopLoss based on xSwingHigh or xSwingLow???//Stop Loss based on xEMA200???//Take Profit xRRRatio * Stop Loss//Take Profit Trailing Stop Function//************************************************************************//trailing stop functiontrailingstart = 50 //trailing will start @trailinstart points profittrailingstep = 5 //trailing step to move the "stoploss"//reset the stoploss valueIF NOT ONMARKET THENnewSL=0ENDIF//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THENnewSL = tradeprice(1)+trailingstep*pipsizeENDIF//next movesIF newSL>0 AND close-newSL>=trailingstep*pipsize THENnewSL = newSL+trailingstep*pipsizeENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THENnewSL = tradeprice(1)-trailingstep*pipsizeENDIF//next movesIF newSL>0 AND newSL-close>=trailingstep*pipsize THENnewSL = newSL-trailingstep*pipsizeENDIFENDIF//stop order to exit the positionsIF newSL>0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIF//************************************************************************1 user thanked author for this post.
01/15/2023 at 10:10 PM #207484Hello JS
Thank you very much for supplying the code. Much appreciated. I’ll upload now. Was it working as expected?
The stop loss is based on the most recent swing low/high and/or the 200 period EMA.
Best regards
MP
1 user thanked author for this post.
02/02/2023 at 4:12 PM #208964Hi JS,
Thank you again for providing the code for the MACD & 200 EMA strategy. Much appreciated.
The stop loss is based on the most recent swing low/high and/or the 200 period EMA. If you could add this when you have an opportunity, it would be great please.
Best regards
MP
-
AuthorPosts
Find exclusive trading pro-tools on