// ======================================================================== // STRATEGY: DMI Crossover with Stop Loss + Opposite Signal Exit // ======================================================================== // BT-DMI-test-V3.0.2.4-VS-AQ // === INPUTS === myDMIperiod = myDMIperiod // 10 // optimize from 1 to 20 via PRT Platform myATRperiod = 14 myATRmultiplier = 2.5 myRiskPercent = 5 // Risk percentage (5%) myInitialBalance = 100000 // Starting capital // === INDICATORS === myDIplus = DIPLUS[myDMIperiod] myDIminus = DIMINUS[myDMIperiod] myATR = AVERAGETRUERANGE[myATRperiod] // === SIGNALS === // Basic DMI crossover signals - defined ONCE myLongSignal = myDIplus CROSSES OVER myDIminus myShortSignal = myDIminus CROSSES OVER myDIplus // === STOP LOSS DISTANCE === myStopLossDistance = myATR * myATRmultiplier // === RISK MANAGEMENT === // Initialize account balance tracking ONCE myCurrentBalance = myInitialBalance ONCE myPreviousEquity = myInitialBalance ONCE myProfitLoss = 0 ONCE myPositionOpened = 0 // === STATE FLAGS === // Initialize these variables only once to maintain state between bars ONCE myReverseToLong = 0 ONCE myReverseToShort = 0 ONCE myEntryPrice = 0 ONCE myLongStopLevel = 0 ONCE myShortStopLevel = 0 ONCE myExitType = 0 // 0=neutral, +1=opposite signal, -1=stop loss hit ONCE myExitPrice = 0 // Price at which exit occurred // === DELAY TRACKING === // Track when signals occur and when trades execute ONCE myLastLongSignalBar = 0 ONCE myLastShortSignalBar = 0 ONCE myLongDelay = 0 ONCE myShortDelay = 0 // Track when signals occur IF myLongSignal THEN myLastLongSignalBar = BARINDEX ENDIF IF myShortSignal THEN myLastShortSignalBar = BARINDEX ENDIF // Track position changes myNewLongPosition = LongOnMarket AND NOT LongOnMarket[1] myNewShortPosition = ShortOnMarket AND NOT ShortOnMarket[1] myClosedLongPosition = NOT LongOnMarket AND LongOnMarket[1] myClosedShortPosition = NOT ShortOnMarket AND ShortOnMarket[1] // Calculate delay when positions are opened IF myNewLongPosition THEN myLongDelay = BARINDEX - myLastLongSignalBar ENDIF IF myNewShortPosition THEN myShortDelay = BARINDEX - myLastShortSignalBar ENDIF // Update account balance based on closed positions IF myClosedLongPosition OR myClosedShortPosition THEN // Calculate profit/loss from the closed position IF myClosedLongPosition THEN myProfitLoss = (close - POSITIONPRICE[1]) * COUNTOFPOSITION[1] ELSE myProfitLoss = (POSITIONPRICE[1] - close) * ABS(COUNTOFPOSITION[1]) ENDIF // Update current balance myCurrentBalance = myCurrentBalance + myProfitLoss ENDIF // When new position is opened, store the previous equity IF myNewLongPosition OR myNewShortPosition THEN myPreviousEquity = myCurrentBalance myPositionOpened = 1 ENDIF // Calculate position size based on risk myRiskAmount = myCurrentBalance * (myRiskPercent / 100) // Calculate position size based on stop loss distance IF myStopLossDistance > 0 THEN myRiskBasedPositionSize = ROUND(myRiskAmount / myStopLossDistance) // Ensure minimum position size of 1 IF myRiskBasedPositionSize < 1 THEN myRiskBasedPositionSize = 1 ENDIF ELSE myRiskBasedPositionSize = 1 ENDIF // === POSITION MANAGEMENT === IF LongOnMarket THEN // Store entry price when entering a new position IF myNewLongPosition THEN myEntryPrice = close myLongStopLevel = myEntryPrice - myStopLossDistance myExitType = 0 myExitPrice = 0 ENDIF // 1. Check stop loss - check both current and previous bar IF low <= myLongStopLevel THEN SELL AT MARKET myExitType = -1 // Stop loss exit myExitPrice = myLongStopLevel myLongStopLevel = 0 ELSE // 2. Check for reversal signal IF myShortSignal THEN myReverseToShort = 1 // Set reversal flag before selling SELL AT MARKET myExitType = 1 // Signal exit myExitPrice = close myLongStopLevel = 0 ENDIF ENDIF ENDIF IF ShortOnMarket THEN // Store entry price when entering a new position IF myNewShortPosition THEN myEntryPrice = close myShortStopLevel = myEntryPrice + myStopLossDistance myExitType = 0 myExitPrice = 0 ENDIF // 1. Check stop loss - check both current and previous bar IF high >= myShortStopLevel THEN EXITSHORT AT MARKET myExitType = -1 // Stop loss exit myExitPrice = myShortStopLevel myShortStopLevel = 0 ELSE // 2. Check for reversal signal IF myLongSignal THEN myReverseToLong = 1 // Set reversal flag before exiting EXITSHORT AT MARKET myExitType = 1 // Signal exit myExitPrice = close myShortStopLevel = 0 ENDIF ENDIF ENDIF // === ENTRY WHEN FLAT === IF NOT LongOnMarket AND NOT ShortOnMarket THEN // Handle reversals first IF myReverseToLong = 1 THEN BUY myRiskBasedPositionSize SHARES AT MARKET myReverseToLong = 0 // Reset reversal flag after entry ELSE IF myReverseToShort = 1 THEN SELLSHORT myRiskBasedPositionSize SHARES AT MARKET myReverseToShort = 0 // Reset reversal flag after entry ELSE // Handle regular signals IF myLongSignal THEN BUY myRiskBasedPositionSize SHARES AT MARKET ELSE IF myShortSignal THEN SELLSHORT myRiskBasedPositionSize SHARES AT MARKET ENDIF ENDIF ENDIF ENDIF ENDIF // === VISUALIZATION === // Show stop levels on price chart IF myLongStopLevel > 0 THEN GRAPHONPRICE myLongStopLevel COLOURED(0,128,0) AS "Long Stop" ENDIF IF myShortStopLevel > 0 THEN GRAPHONPRICE myShortStopLevel COLOURED(128,0,0) AS "Short Stop" ENDIF // Track exit types in a separate panel GRAPH myExitType AS "Exit Type (+1=Signal, 0=Neutral, -1=Stop Loss)" // Track signal to execution delay // GRAPH myLongDelay AS "Long Signal Delay (bars)" // GRAPH myShortDelay AS "Short Signal Delay (bars)" // End