Untitled

 avatar
unknown
plain_text
11 days ago
4.3 kB
8
Indexable
//@version=5
indicator("Dynamic Multiplier Optimization", overlay=true)

// Kullanıcı girdi parametreleri
AP = input(14, 'Common Period')
showsignalsk = input(true, title='Show Signals?')
novolumedata = input(false, title='Change calculation (no volume data)?')

// ATR hesaplama
ATR = ta.sma(ta.tr, AP)
src = input(close)

// Initial multiplier starting value
initial_coeff = 0.5
multiplier_step = 0.1

// Performans hesaplama değişkenleri
var float best_coeff = na
var float best_performance = na

startDate = input.time(timestamp("2024-01-01 00:00"), title="Başlangıç Tarihi")
endDate = input.time(timestamp("2024-12-31 23:59"), title="Bitiş Tarihi")

// Performans hesaplama fonksiyonu
calc_performance(coeff) =>
    upT = low - ATR * coeff
    downT = high + ATR * coeff
    AlphaTrend = 0.0
    AlphaTrend := (novolumedata ? ta.rsi(src, AP) >= 50 : ta.mfi(hlc3, AP) >= 50) ? upT < nz(AlphaTrend[1]) ? nz(AlphaTrend[1]) : upT : downT > nz(AlphaTrend[1]) ? nz(AlphaTrend[1]) : downT

    buySignalk = ta.crossover(AlphaTrend, AlphaTrend[2])
    sellSignalk = ta.crossunder(AlphaTrend, AlphaTrend[2])

    K1 = ta.barssince(buySignalk)
    K2 = ta.barssince(sellSignalk)
    O1 = ta.barssince(buySignalk[1])
    O2 = ta.barssince(sellSignalk[1])

    longCondition = buySignalk and showsignalsk and O1 > K2
    shortCondition = sellSignalk and showsignalsk and O2 > K1

    // İşlem Geçmişi için Değişkenler
    varip float pnl = 0
    varip int totalTrades = 0
    varip float grossProfit = 0
    varip float grossLoss = 0
    var float lastBuyPrice = na
    varip int wins = 0
    varip int losses = 0

    inDateRange = (time >= startDate and time <= endDate)

    if (longCondition and inDateRange)
        lastBuyPrice := close
        totalTrades := totalTrades + 1

    if (shortCondition and inDateRange and not na(lastBuyPrice))
        tradeProfit = close - lastBuyPrice
        pnl := pnl + tradeProfit

        if (tradeProfit > 0)
            wins := wins + 1
            grossProfit := grossProfit + tradeProfit
        else
            losses := losses + 1
            grossLoss := grossLoss + math.abs(tradeProfit)

        lastBuyPrice := na

    // Profit Factor & Win Rate Hesaplama
    pnl

// En iyi çarpanı bulma döngüsü
for i = 0 to 100
    coeff = initial_coeff + i * multiplier_step
    current_performance = calc_performance(coeff)
    if (na(best_performance) or current_performance > best_performance)
        best_performance := current_performance
        best_coeff := coeff



// Performansı en iyi olan çarpanı kullanarak AlphaTrend hesaplama
upT = low - ATR * best_coeff
downT = high + ATR * best_coeff
AlphaTrend = 0.0
AlphaTrend := (novolumedata ? ta.rsi(src, AP) >= 50 : ta.mfi(hlc3, AP) >= 50) ? upT < nz(AlphaTrend[1]) ? nz(AlphaTrend[1]) : upT : downT > nz(AlphaTrend[1]) ? nz(AlphaTrend[1]) : downT

color1 = AlphaTrend > AlphaTrend[2] ? #00E60F : AlphaTrend < AlphaTrend[2] ? #80000B : AlphaTrend[1] > AlphaTrend[3] ? #00E60F : #80000B
k1 = plot(AlphaTrend, color=color.new(#0022FC, 0), linewidth=3)
k2 = plot(AlphaTrend[2], color=color.new(#FC0400, 0), linewidth=3)

fill(k1, k2, color=color1)

buySignalk = ta.crossover(AlphaTrend, AlphaTrend[2])
sellSignalk = ta.crossunder(AlphaTrend, AlphaTrend[2])

K1 = ta.barssince(buySignalk)
K2 = ta.barssince(sellSignalk)
O1 = ta.barssince(buySignalk[1])
O2 = ta.barssince(sellSignalk[1])

plotshape(buySignalk and showsignalsk and O1 > K2 ? AlphaTrend[2] * 0.9999 : na, title='BUY', text='BUY', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(#0022FC, 0), textcolor=color.new(color.white, 0))
plotshape(sellSignalk and showsignalsk and O2 > K1 ? AlphaTrend[2] * 1.0001 : na, title='SELL', text='SELL', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.maroon, 0), textcolor=color.new(color.white, 0))

// En iyi çarpanı ekranda gösterme
if (not na(best_coeff) and bar_index % 100 == 0)
    label.new(bar_index, high, text="Best Coeff: " + str.tostring(best_coeff), color=color.new(color.green, 0), textcolor=color.white, style=label.style_label_up, size=size.small)
Leave a Comment