Hello,
Would it be possible to get code for a strategy please as follows:-
The trading timeframe is open to accommodate trading on all timeframes.
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.
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.
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.
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.
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).
Appreciate any assistance. Thank you
Hi, I think I this response may be for someone else. Thanks
I think that what you are asking for is not very complicated, here is the beginning of a code that may help you for the next step 😊
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
C2 = MyMACD < 0 AND MySLine < 0
C3 = Close < EMA200
BuyCondition = C1 AND C2 AND C3
IF BuyCondition THEN
Buy 1 Shares AT Market
SET STOP pLOSS 20
ENDIF
I took some time to add the final conditions of the strategy based on ZeroCafeines start.
Here’s the code. Looks pretty good on DAX Daily (backtest for 200k units attached)
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
Hi,
Thank you for your code for back testing the strategy. Just wondering would it be possible to get code to use this as an indicator please. Thanks again for your assistance.