Table1 -snippet Table2 - snippet ======================================================================================================================= // ======================================================================== // STRATEGY: DMI-ADX Crossover with Stop Loss + Opposite Signal Exit + Enhanced Filters // ======================================================================== // BT-DMI-testV3.0.0 // === INPUTS === DMIperiod = DMIPeriod // optimize from 1 to 20 ATRperiod = 14 ATRmultiplier = 2.5 RiskPercent = 5 // Risk percentage (5%) InitialBalance = 100000 // Starting capital ADXThreshold = 20 // Minimum ADX value for trend strength confirmation ADXRising = 3 // Number of bars ADX must be rising // === INDICATORS === myDIplus = DIPLUS[DMIperiod] myDIminus = DIMINUS[DMIperiod] myADX = ADX[DMIperiod] myATR = AVERAGETRUERANGE[ATRperiod] // === ENHANCED SIGNALS WITH FILTERS === // Basic DMI crossover signals BasicLongSignal = myDIplus CROSSES OVER myDIminus BasicShortSignal = myDIminus CROSSES OVER myDIplus // ADX trend strength filter ADXStrong = myADX > ADXThreshold ADXRisingFilter = myADX > myADX[1] AND myADX[1] > myADX[2] AND myADX[2] > myADX[3] // Final signals with filters applied LongSignal = BasicLongSignal AND ADXStrong AND ADXRisingFilter ShortSignal = BasicShortSignal AND ADXStrong AND ADXRisingFilter // === STOP LOSS DISTANCE === StopLossDistance = myATR * ATRmultiplier // === VISUALIZATION VARIABLES === // For stop loss lines ONCE LongStopLevel = 0 ONCE ShortStopLevel = 0 // === RISK MANAGEMENT === // Initialize account balance tracking ONCE CurrentBalance = InitialBalance ONCE PreviousEquity = InitialBalance ONCE ProfitLoss = 0 // Update account balance based on closed positions IF BarsSince(LongOnMarket) = 0 OR BarsSince(ShortOnMarket) = 0 THEN // New position opened PreviousEquity = CurrentBalance ENDIF // If position was closed on previous bar, update the balance IF (LongOnMarket[1] AND NOT LongOnMarket) OR (ShortOnMarket[1] AND NOT ShortOnMarket) THEN // Calculate profit/loss from the closed position IF LongOnMarket[1] THEN ProfitLoss = (close[1] - POSITIONPRICE[1]) * COUNTOFPOSITION[1] ELSE ProfitLoss = (POSITIONPRICE[1] - close[1]) * ABS(COUNTOFPOSITION[1]) ENDIF // Update current balance CurrentBalance = CurrentBalance + ProfitLoss ENDIF // Calculate position size based on risk RiskAmount = CurrentBalance * (RiskPercent / 100) // Calculate position size based on stop loss distance IF StopLossDistance > 0 THEN RiskBasedPositionSize = ROUND(RiskAmount / StopLossDistance) // Ensure minimum position size of 1 IF RiskBasedPositionSize < 1 THEN RiskBasedPositionSize = 1 ENDIF ELSE RiskBasedPositionSize = 1 ENDIF // === STATE FLAGS === // Initialize these variables only once to maintain state between bars ONCE reverseToLong = 0 ONCE reverseToShort = 0 ONCE myEntryPrice = 0 // === POSITION MANAGEMENT === IF LongOnMarket THEN // Store entry price when entering a position IF BarsSince(LongOnMarket) = 0 THEN myEntryPrice = close // Set stop loss level for visualization LongStopLevel = myEntryPrice - StopLossDistance ENDIF // 1. Check stop loss IF low <= (myEntryPrice - StopLossDistance) THEN SELL AT MARKET // Reset stop level after exit LongStopLevel = 0 ELSE // 2. Check for reversal IF ShortSignal THEN SELL AT MARKET reverseToShort = 1 // Reset stop level after exit LongStopLevel = 0 ENDIF ENDIF // Draw stop loss line for long positions IF LongStopLevel > 0 THEN DRAWHLINE(LongStopLevel) COLOURED(0,255,0) STYLE(DOTTEDLINE, 2) ENDIF ENDIF IF ShortOnMarket THEN // Store entry price when entering a position IF BarsSince(ShortOnMarket) = 0 THEN myEntryPrice = close // Set stop loss level for visualization ShortStopLevel = myEntryPrice + StopLossDistance ENDIF // 1. Check stop loss IF high >= (myEntryPrice + StopLossDistance) THEN EXITSHORT AT MARKET // Reset stop level after exit ShortStopLevel = 0 ELSE // 2. Check for reversal IF LongSignal THEN EXITSHORT AT MARKET reverseToLong = 1 // Reset stop level after exit ShortStopLevel = 0 ENDIF ENDIF // Draw stop loss line for short positions IF ShortStopLevel > 0 THEN DRAWHLINE(ShortStopLevel) COLOURED(255,0,0) STYLE(DOTTEDLINE, 2) ENDIF ENDIF // === ENTRY WHEN FLAT === IF NOT LongOnMarket AND NOT ShortOnMarket THEN IF reverseToLong THEN BUY RiskBasedPositionSize SHARES AT MARKET reverseToLong = 0 ELSE IF reverseToShort THEN SELLSHORT RiskBasedPositionSize SHARES AT MARKET reverseToShort = 0 ELSE IF LongSignal THEN BUY RiskBasedPositionSize SHARES AT MARKET ELSE IF ShortSignal THEN SELLSHORT RiskBasedPositionSize SHARES AT MARKET ENDIF ENDIF ENDIF ENDIF ENDIF // === VISUALIZATION === // Signal visualization for chart signalVisualization = 0 IF LongSignal THEN signalVisualization = 1 // Draw up arrow for long signal DRAWARROWUP(barindex, low - (myATR * 0.5)) COLOURED(0,255,0) ELSIF ShortSignal THEN signalVisualization = -1 // Draw down arrow for short signal DRAWARROWDOWN(barindex, high + (myATR * 0.5)) COLOURED(255,0,0) ENDIF // Draw ADX line for reference DRAWHLINE(ADXThreshold) COLOURED(128,128,128) STYLE(DOTTEDLINE, 1) // === PERFORMANCE TRACKING === // Track consecutive wins/losses ONCE ConsecutiveWins = 0 ONCE ConsecutiveLosses = 0 ONCE LastTradeProfit = 0 // Update consecutive win/loss counters IF (LongOnMarket[1] AND NOT LongOnMarket) OR (ShortOnMarket[1] AND NOT ShortOnMarket) THEN IF ProfitLoss > 0 THEN ConsecutiveWins = ConsecutiveWins + 1 ConsecutiveLosses = 0 ELSIF ProfitLoss < 0 THEN ConsecutiveLosses = ConsecutiveLosses + 1 ConsecutiveWins = 0 ENDIF LastTradeProfit = ProfitLoss ENDIF // Display performance metrics DRAWTEXT("Balance: " + ROUND(CurrentBalance, 2), barindex, high + (myATR * 2)) ANCHOR(TOPRIGHT, XSHIFT, YSHIFT) COLOURED(0,0,255) ============================================================================================================================================================ // ======================================================================== // STRATEGY: DMI-ADX Crossover with Stop Loss + Opposite Signal Exit + Delayed Re-entry // ======================================================================== // BT-DMI-testV2.6.0 // === INPUTS === DMIperiod = DMIPeriod // optimize from 1 to 20 ATRperiod = 14 ATRmultiplier = 2.5 RiskPercent = 5 // Risk percentage (5%) InitialBalance = 100000 // Starting capital ADXThreshold = 20 // Minimum ADX value for trend strength confirmation // === INDICATORS === myDIplus = DIPLUS[DMIperiod] myDIminus = DIMINUS[DMIperiod] myADX = ADX[DMIperiod] myATR = AVERAGETRUERANGE[ATRperiod] // === SIGNALS === // Basic DMI crossover signals BasicLongSignal = myDIplus CROSSES OVER myDIminus BasicShortSignal = myDIminus CROSSES OVER myDIplus // ADX trend strength filter ADXStrong = myADX > ADXThreshold // Final signals with filter applied LongSignal = BasicLongSignal AND ADXStrong ShortSignal = BasicShortSignal AND ADXStrong // === STOP LOSS DISTANCE === StopLossDistance = myATR * ATRmultiplier // === VISUALIZATION VARIABLES === // For stop loss lines ONCE LongStopLevel = 0 ONCE ShortStopLevel = 0 // === RISK MANAGEMENT === // Initialize account balance tracking ONCE CurrentBalance = InitialBalance ONCE PreviousEquity = InitialBalance ONCE ProfitLoss = 0 // Update account balance based on closed positions IF BarsSince(LongOnMarket) = 0 OR BarsSince(ShortOnMarket) = 0 THEN // New position opened PreviousEquity = CurrentBalance ENDIF // If position was closed on previous bar, update the balance IF (LongOnMarket[1] AND NOT LongOnMarket) OR (ShortOnMarket[1] AND NOT ShortOnMarket) THEN // Calculate profit/loss from the closed position IF LongOnMarket[1] THEN ProfitLoss = (close[1] - POSITIONPRICE[1]) * COUNTOFPOSITION[1] ELSE ProfitLoss = (POSITIONPRICE[1] - close[1]) * ABS(COUNTOFPOSITION[1]) ENDIF // Update current balance CurrentBalance = CurrentBalance + ProfitLoss ENDIF // Calculate position size based on risk RiskAmount = CurrentBalance * (RiskPercent / 100) // Calculate position size based on stop loss distance IF StopLossDistance > 0 THEN RiskBasedPositionSize = ROUND(RiskAmount / StopLossDistance) // Ensure minimum position size of 1 IF RiskBasedPositionSize < 1 THEN RiskBasedPositionSize = 1 ENDIF ELSE RiskBasedPositionSize = 1 ENDIF // === STATE FLAGS === // Initialize these variables only once to maintain state between bars ONCE reverseToLong = 0 ONCE reverseToShort = 0 ONCE myEntryPrice = 0 // === POSITION MANAGEMENT === IF LongOnMarket THEN // Store entry price when entering a position IF BarsSince(LongOnMarket) = 0 THEN myEntryPrice = close // Set stop loss level for visualization LongStopLevel = myEntryPrice - StopLossDistance ENDIF // 1. Check stop loss IF low <= (myEntryPrice - StopLossDistance) THEN SELL AT MARKET // Reset stop level after exit LongStopLevel = 0 ELSE // 2. Check for reversal IF ShortSignal THEN SELL AT MARKET reverseToShort = 1 // Reset stop level after exit LongStopLevel = 0 ENDIF ENDIF // Draw stop loss line for long positions IF LongStopLevel > 0 THEN DRAWHLINE(LongStopLevel) COLOURED(0,255,0) STYLE(DOTTEDLINE, 2) ENDIF ENDIF IF ShortOnMarket THEN // Store entry price when entering a position IF BarsSince(ShortOnMarket) = 0 THEN myEntryPrice = close // Set stop loss level for visualization ShortStopLevel = myEntryPrice + StopLossDistance ENDIF // 1. Check stop loss IF high >= (myEntryPrice + StopLossDistance) THEN EXITSHORT AT MARKET // Reset stop level after exit ShortStopLevel = 0 ELSE // 2. Check for reversal IF LongSignal THEN EXITSHORT AT MARKET reverseToLong = 1 // Reset stop level after exit ShortStopLevel = 0 ENDIF ENDIF // Draw stop loss line for short positions IF ShortStopLevel > 0 THEN DRAWHLINE(ShortStopLevel) COLOURED(255,0,0) STYLE(DOTTEDLINE, 2) ENDIF ENDIF // === ENTRY WHEN FLAT === IF NOT LongOnMarket AND NOT ShortOnMarket THEN IF reverseToLong THEN BUY RiskBasedPositionSize SHARES AT MARKET reverseToLong = 0 ELSE IF reverseToShort THEN SELLSHORT RiskBasedPositionSize SHARES AT MARKET reverseToShort = 0 ELSE IF LongSignal THEN BUY RiskBasedPositionSize SHARES AT MARKET ELSE IF ShortSignal THEN SELLSHORT RiskBasedPositionSize SHARES AT MARKET ENDIF ENDIF ENDIF ENDIF ENDIF // === VISUALIZATION === // Signal visualization for chart signalVisualization = 0 IF LongSignal THEN signalVisualization = 1 // Draw up arrow for long signal DRAWARROWUP(barindex, low - (myATR * 0.5)) COLOURED(0,255,0) ELSIF ShortSignal THEN signalVisualization = -1 // Draw down arrow for short signal DRAWARROWDOWN(barindex, high + (myATR * 0.5)) COLOURED(255,0,0) ENDIF