Untitled

 avatar
unknown
plain_text
4 days ago
2.6 kB
4
Indexable
//@version=5
strategy(title="Leverage Bot (5m, 2-Day Exit)", shorttitle="5m Exit Bot", overlay=true, initial_capital=1000, default_qty_type=strategy.percent_of_equity, default_qty_value=100)

// Inputs

lengthEMA  = input.int(200, "EMA Length")
rsiPeriod  = input.int(14,  "RSI Period")


// Indicators: EMA & RSI

emaValue   = ta.ema(close, lengthEMA)
rsiValue   = ta.rsi(close, rsiPeriod)


// 24-Hour % Change on a 5-Minute Chart
//  - 24 hours = 288 bars (5 min each)

var int barsIn24Hrs = 288
float dailyChangePercent = na
if bar_index >= barsIn24Hrs
    dailyChangePercent := ((close - close[barsIn24Hrs]) / close[barsIn24Hrs]) * 100.0


// Entry Conditions

shortCondition = not na(dailyChangePercent) and (dailyChangePercent <= -3.0) and (close < emaValue) and (rsiValue < 70)
longCondition  = not na(dailyChangePercent) and (dailyChangePercent >= 1.0)  and (close > emaValue)  and (rsiValue > 30)


// Entry Logic

if strategy.position_size == 0
    if shortCondition
        strategy.entry("Short", strategy.short)
    else if longCondition
        strategy.entry("Long",  strategy.long)

// Time-Based Exit After 2 Days

var float entryTime = na
if barstate.isnew
    if strategy.position_size == 0
        entryTime := na
    else if na(entryTime)
        entryTime := time

daysToMs = 2 * 24 * 60 * 60 * 1000
if strategy.position_size != 0 and not na(entryTime)
    if time - entryTime >= daysToMs
        strategy.close_all("Time-Based Exit")


// Stop Loss & Take Profit (±10%)
// Also adjusts stop at +5% or +7% gain

if strategy.position_size != 0
    isLong   = strategy.position_size > 0
    avgPrice = strategy.position_avg_price

    float currentGainPercent = na
    if isLong
        currentGainPercent := (close - avgPrice) / avgPrice * 100.0
    else
        currentGainPercent := (avgPrice - close) / avgPrice * 100.0

    var float stopPrice  = na
    var float limitPrice = na

    if isLong
        stopPrice  := avgPrice * 0.90   // -10%
        limitPrice := avgPrice * 1.10   // +10%
    else
        stopPrice  := avgPrice * 1.10   // +10% above entry => -10% PnL short
        limitPrice := avgPrice * 0.90   // -10% below entry => +10% PnL short

    // Move SL if profit ≥ 7% or ≥ 5%
    if currentGainPercent >= 7
        if isLong
            stopPrice := avgPrice * 1.05
        else
            stopPrice := avgPrice * 0.95
    else if currentGainPercent >= 5
        stopPrice := avgPrice

    // Dynamic exit orders
    if isLong
        strategy.exit("Long Exit", stop=stopPrice, limit=limitPrice)
    else
        strategy.exit("Short Exit", stop=stopPrice, limit=limitPrice)
Leave a Comment