//@version=5
strategy("Demo GPT - Daily Fibs Levels Strategy with Magic MA", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=5, commission_type=strategy.commission.percent, commission_value=0.1, slippage=3)

// Inputs for Fibonacci Strategy
firstLength = input(50, title='ADR Calculation Period (Days)')
calcType = input.string("Basic", title="Fibs Calculation", options=["Basic", "Standard Deviation", "Variance"])
i_timeframe = input.timeframe(title='Custom Timeframe', defval='D')

// Date Filters
startDate = input.time(timestamp("2020-01-01 00:00"), title="Start Date")
endDate = input.time(timestamp("2050-12-31 23:59"), title="End Date")

// Ensure trading is only done within the specified date range
withinDateRange = (time >= startDate) and (time <= endDate)

// Inputs for Fibonacci Levels
showFibBuySell = input.bool(true, title='', group='Fibonacci Levels', inline='1')
fibLevelBuySell = input.float(0.786, title='', group='Fibonacci Levels', inline='1')
fibColorBuySell = input.color(#F44336, title='', group='Fibonacci Levels', inline='1')
showFibBuySell_line = input.bool(true, title='Line', group='Fibonacci Levels', inline='1')

showFibStopLoss = input.bool(true, title='', group='Fibonacci Levels', inline='2')
fibLevelStopLoss = input.float(1.05, title='', group='Fibonacci Levels', inline='2')
fibColorStopLoss = input.color(#FFFFFF, title='', group='Fibonacci Levels', inline='2')
showFibStopLoss_line = input.bool(true, title='Line', group='Fibonacci Levels', inline='2')

// Function to enable plotting
enabled() =>
    not (timeframe.isdaily or timeframe.isweekly or timeframe.ismonthly)

// Initialization Variables for Fibonacci (Include desired Fibonacci levels for calculation)
openColor = input.color(#ffffff, title='Open Plot Color')
fibScale = input.float(1, step=0.1, title='Fibonacci Scale')

// Inputs for Magic MA
type3 = input.string(defval="HullMA", title="Slow MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"])
len3 = input.int(defval=34, title="Slow Length", minval=1)

// Function to calculate MA variants
variant(_type, _src, _len) =>
    v1 = ta.sma(_src, _len)
    v2 = ta.ema(_src, _len)
    v3 = ta.wma(_src, _len)
    v4 = ta.vwma(_src, _len)
    var float v5 = na
    v5 := na(v5[1]) ? ta.sma(_src, _len) : (v5[1] * (_len - 1) + _src) / _len
    v6 = 2 * v2 - ta.ema(v2, _len)
    v7 = 3 * (v2 - ta.ema(v2, _len)) + ta.ema(ta.ema(v2, _len), _len)
    v8 = ta.wma(2 * ta.wma(_src, _len / 2) - ta.wma(_src, _len), math.round(math.sqrt(_len)))
    v11 = ta.sma(ta.sma(_src, _len), _len)
    a1 = math.exp(-1.414 * 3.14159 / _len)
    b1 = 2 * a1 * math.cos(1.414 * 3.14159 / _len)
    c2 = b1
    c3 = (-a1) * a1
    c1 = 1 - c2 - c3
    var float v9 = na
    v9 := c1 * (_src + nz(_src[1])) / 2 + c2 * nz(v9[1]) + c3 * nz(v9[2])
    e = ta.ema(v1, _len)
    v10 = v1 + (v1 - e)
    _type == "EMA" ? v2 : _type == "WMA" ? v3 : _type == "VWMA" ? v4 : _type == "SMMA" ? v5 : _type == "DEMA" ? v6 : _type == "TEMA" ? v7 : _type == "HullMA" ? v8 : _type == "SSMA" ? v9 : _type == "ZEMA" ? v10 : _type == "TMA" ? v11 : v1

// Calculate Slow MA
srcclose = close
ma_slow = variant(type3, srcclose, len3)

// Data Retrieval for Fibonacci
dayOpen = request.security(syminfo.tickerid, i_timeframe, open, barmerge.gaps_off, barmerge.lookahead_on)
dayHigh = request.security(syminfo.tickerid, i_timeframe, high)
dayLow = request.security(syminfo.tickerid, i_timeframe, low)

// 1-hour based data for trend determination
hourClose = request.security(syminfo.tickerid, "60", close)

// Uptrend and Downtrend determination using 1H close price
uptrend = hourClose > hourClose[1]
downtrend = hourClose < hourClose[1]

dayRange = dayHigh - dayLow
avgRange = request.security(syminfo.tickerid, i_timeframe, ta.sma(dayRange, firstLength))
dailyRange_calc = calcType == "Variance" ? request.security(syminfo.tickerid, i_timeframe, ta.variance(dayRange, firstLength)) : request.security(syminfo.tickerid, i_timeframe, ta.stdev(dayRange, firstLength))

// Fibonacci Calculations
fibRangeBuySell = calcType != "Basic" ? dailyRange_calc * fibLevelBuySell * fibScale / 2 : avgRange * fibLevelBuySell * fibScale / 2
fibRangeStopLoss = calcType != "Basic" ? dailyRange_calc * fibLevelStopLoss * fibScale / 2 : avgRange * fibLevelStopLoss * fibScale / 2

// Plot Fibonacci Levels
plot(enabled() and showFibBuySell ? dayOpen + fibRangeBuySell : na, color=not showFibBuySell_line ? na : color.new(fibColorBuySell, 0), title="Fib BuySell Level High")
plot(enabled() and showFibBuySell ? dayOpen - fibRangeBuySell : na, color=not showFibBuySell_line ? na : color.new(fibColorBuySell, 0), title="Fib BuySell Level Low")
plot(enabled() and showFibStopLoss ? dayOpen + fibRangeStopLoss : na, color=not showFibStopLoss_line ? na : color.new(fibColorStopLoss, 0), title="Fib StopLoss Level High")
plot(enabled() and showFibStopLoss ? dayOpen - fibRangeStopLoss : na, color=not showFibStopLoss_line ? na : color.new(fibColorStopLoss, 0), title="Fib StopLoss Level Low")

// Plot Slow MA
plot(ma_slow, title="MA Slow", color=color.new(color.green, 0), linewidth=2)

// Strategy Logic
if uptrend and withinDateRange
    stopLossLevelBuy = dayOpen - fibRangeStopLoss
    riskBuy = math.abs(stopLossLevelBuy - close)
    takeProfitLevelBuy = close + 3 * riskBuy  // 1:3 Risk-Reward Ratio
    if close <= dayOpen - fibRangeBuySell
        strategy.entry("Long", strategy.long, stop=stopLossLevelBuy, limit=takeProfitLevelBuy)

if downtrend and withinDateRange
    stopLossLevelSell = dayOpen + fibRangeStopLoss
    riskSell = math.abs(stopLossLevelSell - close)
    takeProfitLevelSell = close - 3 * riskSell  // 1:3 Risk-Reward Ratio
    if close >= dayOpen + fibRangeBuySell
        strategy.entry("Short", strategy.short, stop=stopLossLevelSell, limit=takeProfitLevelSell)

if withinDateRange and ta.crossunder(close, dayOpen - fibRangeBuySell)
    strategy.close("Long")
if withinDateRange and ta.crossover(close, dayOpen + fibRangeBuySell)
    strategy.close("Short")