DEFPARAM cumulateOrders = false
// Define parameters
Len = 7
Filter = 0.0
ATRMultiplier = 1.5 // Multiplier for ATR to set stop loss
// Calculate MBFX Timing
ld24 = 50.0
if (BarIndex > Len) then
upSum = 0
downSum = 0
for i = 1 to Len do
upMove = Max(Close[i] - Close[i - 1], 0)
downMove = Max(Close[i - 1] - Close[i], 0)
upSum = upSum + upMove
downSum = downSum + downMove
next
RS = upSum / downSum
ld24 = 100 - (100 / (1 + RS))
// Trading signals
A1 = (ld24 > ld24[1] - Filter) AND (ld24[1] < ld24[2]) // Buy when MBFX Timing increases
A2 = (ld24 < ld24[1] + Filter) AND (ld24[1] > ld24[2]) // Sell when MBFX Timing decreases
// Calculate ATR for stop loss
ATRValue = AverageTrueRange[9]
// Define entry and exit conditions
IF A1 THEN
BUY 1 CONTRACT AT MARKET // Buy 1 contract at market
ENDIF
IF A2 THEN
SELL 1 CONTRACT AT MARKET // Sell 1 contract at market
ENDIF
// Exit if stop loss is hit
IF LONGONMARKET AND close < close[1] - ATRMultiplier * ATRValue THEN
SELL AT MARKET // Sell to close position at market
ENDIF
IF SHORTONMARKET AND close > close[1] + ATRMultiplier * ATRValue THEN
BUY AT MARKET // Buy to cover position at market
ENDIF
endif
SET STOP %LOSS 5