// Define the moving averages
DefParam CumulateOrders=false
Defparam FlatAfter = 215900
EMA20 = ExponentialAverage[20](close)
EMA50 = ExponentialAverage[50](close)
EMA200 = ExponentialAverage[200](close)
EMAAusstieg = ExponentialAverage[28](close)
positionSize = 25000
maxLossPerTrade = 100 // Risk per Trade
entrySignal = CALL "Signal_Trendhandel4" // Call the custom indicator
noTradeHours = (Hour >= 19 AND Hour < 19)
isUptrend = (EMA20 < EMA50) AND (close > EMA20) // Define the conditions for the uptrend
trailingDistance = 1 // Define your trailing stop distance (in points/USD/EUR)
// Entry condition: Only enter after 3 consecutive correction candles followed by a recovery and outside the noTradeHours period
IF isUptrend AND entrySignal AND NOT noTradeHours THEN
maxPrice = close // Variables to track the highest price and trailing stop level
Buy positionSize CASH ROUNDEDDOWN AT MARKET
// Calculate and store the approximate number of contracts bought
numberOfContracts = positionSize / close // Estimate the number of contracts based on entry price
ENDIF
// If the position is open, initialize stop loss and trailing stop logic
IF longonmarket THEN
// Update the highest price after entry
maxPrice = max(maxPrice, close) // Track the highest price reached after entry
stopLossPrice = maxPrice - (maxLossPerTrade / numberOfContracts) // Calculate stop loss price based on the max loss
// Calculate the trailing stop based on the highest price reached
currentTrailingStop = maxPrice - trailingDistance
// Update the stop loss to the trailing stop value, ensuring it does not exceed the original stop loss
SET STOP LOSS max(currentTrailingStop, stopLossPrice)
ENDIF
GRAPHONPRICE currentTrailingStop AS "SL" coloured(255,0,255, 255)
GRAPHONPRICE stopLossPrice AS "SLPrice" coloured(0,255,0, 255)
GRAPHONPRICE maxPrice AS "max Price" COLOURED(0,0,255,200)