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)
JSParticipant
Senior
Hi @Marketpunk
Maybe you can start with this…
Stoploss has yet to be added… (based on swing high/low?)
DefParam CumulateOrders=false
xRRRatio=2 //Adjustable Risk/Reward ratio
xSwingPeriod=20 //Lookback period for Swing High/Low
xLongTrades=1 //Switch Long Trades ON or OFF
xShortTrades=1 //Switch Short Trades ON or OFF
If High[1]=Highest[xSwingPeriod](High) and High[2]<High[1] and High<High[1] then
xSwingHigh=High[1]
ElsIf Low[1]=Lowest[xSwingPeriod](Low) and Low[2]>Low[1] and Low>Low[1] then
xSwingLow=Low[1]
EndIf
xMACDSignal=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 then
Buy 1 contract at Market
EndIf
If xMACDLine Crosses Under xMACDSignal and xMACDLine>0 and xMACDSignal>0 and Close<xEMA200 and xShortTrades then
SellShort 1 contract at Market
EndIf
//StopLoss based on xSwingHigh or xSwingLow???
//Stop Loss based on xEMA200???
//Take Profit xRRRatio * Stop Loss
//Take Profit Trailing Stop Function
//************************************************************************
//trailing stop function
trailingstart = 50 //trailing will start @trailinstart points profit
trailingstep = 5 //trailing step to move the "stoploss"
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
//************************************************************************
Hello 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
Hi 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