Good Mode RSI v2
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © corderomoraj


//@version=5
strategy("Good Mode RSI v2", overlay=true)

// Parámetros de la estrategia
rsiPeriod = input(2, "RSI Period")
sellLevel = input(96, "Sell Level")
buyLevel = input(4, "Buy Level")
takeProfitLevelSell = input(20, "Take Profit Level Sell")
takeProfitLevelBuy = input(80, "Take Profit Level Buy")
var float trailingStopPrice = na
var float trailingStopOffset = input(100, "Trailing Stop Offset (pips)")

// Capital inicial
initialCapital = 250
positionSize = initialCapital * 0.015

// Condiciones de entrada y salida
rsi = ta.rsi(close, rsiPeriod)

// Condiciones de entrada y salida para la orden de venta
sellCondition = rsi > sellLevel
closeSellCondition = rsi < takeProfitLevelSell

// Condiciones de entrada y salida para la orden de compra
buyCondition = rsi < buyLevel
closeBuyCondition = rsi > takeProfitLevelBuy

// Trailing Stop para las posiciones de venta
if strategy.position_size < 0
    if low < trailingStopPrice
        trailingStopPrice := low
    strategy.exit("Sell", "Sell", trail_offset = trailingStopOffset * syminfo.mintick, trail_price = trailingStopPrice)

// Trailing Stop para las posiciones de compra
if strategy.position_size > 0
    if high > trailingStopPrice
        trailingStopPrice := high
    strategy.exit("Buy", "Buy", trail_offset = trailingStopOffset * syminfo.mintick, trail_price = trailingStopPrice)

// Ejecutar orden de venta
if (sellCondition)
    strategy.entry("Sell", strategy.short, qty = positionSize)
    trailingStopPrice := high

// Cerrar orden de venta
if (closeSellCondition)
    strategy.close("Sell")

// Ejecutar orden de compra
if (buyCondition)
    strategy.entry("Buy", strategy.long, qty = positionSize)
    trailingStopPrice := low

// Cerrar orden de compra
if (closeBuyCondition)
    strategy.close("Buy")
