OPEN-SOURCE SCRIPT

daniel

//version=5
strategy("Estrategia ADX con Stop Loss y Objetivo", overlay=true)

// Configuración de parámetros
adxLength = input.int(14, title="Periodo ADX")
adxThreshold = input.int(25, title="Umbral ADX")
stopLossPerc = input.float(1, title="Stop Loss (%)", step=0.1) / 100
takeProfitPerc = input.float(3, title="Take Profit (%)", step=0.1) / 100 // Cambiado a 3%

// Cálculo de TR (True Range) manualmente
highLow = high - low
highClosePrev = math.abs(high - close[1])
lowClosePrev = math.abs(low - close[1])
tr = math.max(highLow, math.max(highClosePrev, lowClosePrev))
atr = ta.sma(tr, adxLength)

// Cálculo de +DM y -DM
plusDM = (high - high[1] > low[1] - low) ? math.max(high - high[1], 0) : 0
minusDM = (low[1] - low > high - high[1]) ? math.max(low[1] - low, 0) : 0

// Suavizado de +DM y -DM
smoothedPlusDM = ta.sma(plusDM, adxLength)
smoothedMinusDM = ta.sma(minusDM, adxLength)

// Cálculo de +DI y -DI
plusDI = (smoothedPlusDM / atr) * 100
minusDI = (smoothedMinusDM / atr) * 100

// Cálculo del DX y ADX
dx = math.abs(plusDI - minusDI) / (plusDI + minusDI) * 100
adx = ta.sma(dx, adxLength)

// Condiciones de entrada
longCondition = ta.crossover(plusDI, minusDI) and adx > adxThreshold
shortCondition = ta.crossover(minusDI, plusDI) and adx > adxThreshold

// Ejecución de operaciones
if (longCondition)
strategy.entry("Compra", strategy.long)

if (shortCondition)
strategy.entry("Venta", strategy.short)

// Stop Loss y Take Profit
strategy.exit("Cerrar Compra", from_entry="Compra", loss=stopLossPerc, profit=takeProfitPerc)
strategy.exit("Cerrar Venta", from_entry="Venta", loss=stopLossPerc, profit=takeProfitPerc)
Bands and ChannelsCandlestick analysisChart patterns

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Disclaimer