// ===== PARAMETERS =====
emaPeriod = 30 //
// ===== INDICATORS =====
ema30 = ExponentialAverage[emaPeriod](close)
// ===== TIME FILTER (Berlin 14:00-18:00) =====
tradeTime = (time >= 140000 AND time <= 180000)
// ===== ENTRY CONDITION =====
crossAbove = close crosses over ema30
// ===== RESET WHEN FLAT =====
IF NOT longonmarket THEN
secondEntryTaken = 0
thirdEntryTaken = 0
ENDIF
// ===== FIRST ENTRY =====
IF NOT longonmarket THEN
IF tradeTime AND crossAbove THEN
entry1 = close
// Levels for averaging entries
entry2Trigger = entry1 * 0.975 // -2.5%
slTotal = entry1 * 0.90 // -10% stop
BUY 0.05 CONTRACT AT MARKET
ENDIF
ENDIF
// ===== SECOND ENTRY =====
IF longonmarket AND secondEntryTaken = 0 THEN
IF close <= entry2Trigger THEN
entry2 = close
secondEntryTaken = 1
// Third entry trigger (another -2.5% drop)
entry3Trigger = entry2 * 0.975
BUY 0.05 CONTRACT AT MARKET
ENDIF
ENDIF
// ===== THIRD ENTRY =====
IF longonmarket AND secondEntryTaken = 1 AND thirdEntryTaken = 0 THEN
IF close <= entry3Trigger THEN
entry3 = close
thirdEntryTaken = 1
BUY 0.05 CONTRACT AT MARKET
ENDIF
ENDIF
// ===== EXIT LOGIC =====
IF longonmarket THEN
// Determine how many positions are open and compute average price
IF thirdEntryTaken = 1 THEN
avgPrice = (entry1 + entry2 + entry3) / 3
ELSIF secondEntryTaken = 1 THEN
avgPrice = (entry1 + entry2) / 2
ELSE
avgPrice = entry1
ENDIF
// Combined +3% target based on average price
tpCombined = avgPrice * 1.03
// ---- TAKE PROFIT ----
IF close >= tpCombined THEN
SELL AT MARKET
ENDIF
// ---- STOP LOSS (based on first entry) ----
IF close <= slTotal THEN
SELL AT MARKET
ENDIF
ENDIF