Untitled

 avatar
unknown
plain_text
16 days ago
9.0 kB
22
Indexable
'Grid Bot Backtestli (Türkçe

//@version=6
strategy('Grid Bot Backtestli (Türkçe)', overlay = false, pyramiding = 3000, close_entries_rule = 'ANY', default_qty_type = strategy.cash, initial_capital = 100.0, currency = 'TRY', commission_type = strategy.commission.percent, commission_value = 0.025)
//overlay=true olursa yukardaki tablo gözükür. false olursa gözükmez
// GRID AYARLARI
i_autoBounds = input.bool(group = 'Grid Sınırları', title = 'Otomatik Sınır Kullan?', defval = true)
i_boundSrc = input.string(group = 'Grid Sınırları', title = '(Otomatik) Sınır Kaynağı', defval = 'Yüksek & Düşük', options = ['Yüksek & Düşük', 'Ortalama'])
i_boundLookback = input.int(group = 'Grid Sınırları', title = '(Otomatik) Sınır Geri Bakış', defval = 250, maxval = 500, minval = 0)
i_boundDev = input.float(group = 'Grid Sınırları', title = '(Otomatik) Sınır Sapması', defval = 0.10, maxval = 1, minval = -1)
i_upperBound = input.float(group = 'Grid Sınırları', title = '(Manuel) Üst Sınır', defval = 0.285)
i_lowerBound = input.float(group = 'Grid Sınırları', title = '(Manuel) Alt Sınır', defval = 0.225)
i_gridQty = input.int(group = 'Grid Çizgileri', title = 'Grid Çizgi Sayısı', defval = 30, maxval = 999, minval = 1)

// İŞLEM AYARLARI
initial_balance = input.float(group = 'İşlem Seçenekleri', title = 'Başlangıç Bakiyesi (TL)', defval = 10000, step = 0.01)
start_time = input.time(group = 'İşlem Seçenekleri', defval = timestamp('15 March 2023 06:00'), title = 'Başlangıç Zamanı')
end_time = input.time(group = 'İşlem Seçenekleri', defval = timestamp('31 Dec 2035 20:00'), title = 'Bitiş Zamanı')

isAfterStartDate = true
tradingtime = (timenow - start_time) / (86400000 * 30)
yeartime = tradingtime / 12

// FONKSİYONLAR (Aynı kaldı)
f_getGridBounds(_bs, _bl, _bd, _up) =>
    if _bs == 'Yüksek & Düşük'
        _up ? ta.highest(close, _bl) * (1 + _bd) : ta.lowest(close, _bl) * (1 - _bd)
    else
        avg = ta.sma(close, _bl)
        _up ? avg * (1 + _bd) : avg * (1 - _bd)

f_buildGrid(_lb, _gw, _gq) =>
    gridArr = array.new_float(0)
    for i = 0 to _gq - 1 by 1
        array.push(gridArr, _lb + _gw * i)
    gridArr

f_getNearGridLines(_gridArr, _price) =>
    arr = array.new_int(3)
    for i = 0 to array.size(_gridArr) - 1 by 1
        if array.get(_gridArr, i) > _price
            array.set(arr, 0, i == array.size(_gridArr) - 1 ? i : i + 1)
            array.set(arr, 1, i == 0 ? i : i - 1)
            break
    arr

// DEĞİŞKENLER (Aynı kaldı)
var upperBound = i_autoBounds ? f_getGridBounds(i_boundSrc, i_boundLookback, i_boundDev, true) : i_upperBound
var lowerBound = i_autoBounds ? f_getGridBounds(i_boundSrc, i_boundLookback, i_boundDev, false) : i_lowerBound
var gridWidth = (upperBound - lowerBound) / (i_gridQty - 1)
var gridLineArr = f_buildGrid(lowerBound, gridWidth, i_gridQty)
var orderArr = array.new_bool(i_gridQty, false)

var closeLineArr = f_getNearGridLines(gridLineArr, close)
var nearTopGridLine = array.get(closeLineArr, 0)
var nearBotGridLine = array.get(closeLineArr, 1)

// STRATEJİ MANTIĞI (Aynı kaldı)
if isAfterStartDate
    for i = 0 to array.size(gridLineArr) - 1 by 1
        if close < array.get(gridLineArr, i) and not array.get(orderArr, i) and i < array.size(gridLineArr) - 1
            buyId = i
            array.set(orderArr, buyId, true)
            strategy.entry(id = str.tostring(buyId), direction = strategy.long, qty = initial_balance / (i_gridQty - 1) / close, comment = '#' + str.tostring(buyId))
        if close > array.get(gridLineArr, i) and i != 0
            if array.get(orderArr, i - 1)
                sellId = i - 1
                array.set(orderArr, sellId, false)
                strategy.close(id = str.tostring(sellId), comment = '#' + str.tostring(sellId))

    if i_autoBounds
        upperBound := f_getGridBounds(i_boundSrc, i_boundLookback, i_boundDev, true)
        lowerBound := f_getGridBounds(i_boundSrc, i_boundLookback, i_boundDev, false)
        gridWidth := (upperBound - lowerBound) / (i_gridQty - 1)
        gridLineArr := f_buildGrid(lowerBound, gridWidth, i_gridQty)
        gridLineArr

    closeLineArr := f_getNearGridLines(gridLineArr, close)
    nearTopGridLine := array.get(closeLineArr, 0)
    nearBotGridLine := array.get(closeLineArr, 1)
    nearBotGridLine

// BİLGİ TABLOSU (Türkçeleştirildi)
var table infoTable = table.new(position.top_right, 6, 8, frame_color = color.rgb(255, 255, 255), frame_width = 2, border_width = 2, border_color = color.rgb(255, 255, 255))

// Başlıklar
table.cell(infoTable, 0, 0, 'Üst Sınır Fiyatı:', bgcolor = color.new(color.black, 0), text_color = color.white)
table.cell(infoTable, 0, 1, 'Alt Sınır Fiyatı:', bgcolor = color.new(color.black, 0), text_color = color.white)
table.cell(infoTable, 0, 2, 'Grid Sayısı:', bgcolor = color.new(color.black, 0), text_color = color.white)
table.cell(infoTable, 0, 3, 'Yatırım:', text_color = color.white, bgcolor = color.new(color.black, 0))
table.cell(infoTable, 0, 4, 'Grid Başına TL:', text_color = color.white, bgcolor = color.new(color.black, 0))

// Değerler
table.cell(infoTable, 1, 0, str.tostring(upperBound, '###.#####') + ' TL', bgcolor = color.new(#5a637e, 0), text_color = color.white)
table.cell(infoTable, 1, 1, str.tostring(lowerBound, '###.#####') + ' TL', bgcolor = color.new(#5a637e, 0), text_color = color.white)
table.cell(infoTable, 1, 2, str.tostring(i_gridQty, '###'), bgcolor = color.new(#5a637e, 0), text_color = color.white)
table.cell(infoTable, 1, 3, str.tostring(initial_balance, '###.##') + ' TL', bgcolor = color.new(#5a637e, 0), text_color = color.white)
table.cell(infoTable, 1, 4, str.tostring(initial_balance / i_gridQty, '###.##') + ' TL', bgcolor = color.new(#5a637e, 0), text_color = color.white)

// Başlıklar
table.cell(infoTable, 2, 0, 'Mevcut Pozisyon:', text_color = color.white, bgcolor = color.new(color.black, 0))
table.cell(infoTable, 2, 1, 'Pozisyon Maliyeti:', text_color = color.white, bgcolor = color.new(color.black, 0))
table.cell(infoTable, 2, 2, 'Gerçekleşmemiş Kar:', bgcolor = color.new(color.black, 0), text_color = color.white)
table.cell(infoTable, 2, 3, 'Gerçekleşmemiş Kar %:', bgcolor = color.new(color.black, 0), text_color = color.white)
table.cell(infoTable, 2, 4, 'Komisyon:', text_color = color.white, bgcolor = color.new(color.black, 0))

// Değerler
table.cell(infoTable, 3, 0, str.tostring(strategy.position_size) + syminfo.basecurrency + '\n' + str.tostring(strategy.position_size * strategy.position_avg_price / 1, '###.##') + ' TL', text_color = color.white, bgcolor = color.new(#5a637e, 0))
table.cell(infoTable, 3, 1, text = strategy.position_size > 0 ? str.tostring(strategy.position_avg_price, '###.####') + ' TL' : 'İŞLEM YOK', text_color = color.white, bgcolor = color.new(#5a637e, 0))
table.cell(infoTable, 3, 2, str.tostring(strategy.openprofit, '###.##') + ' TL', text_color = color.white, bgcolor = strategy.openprofit > 0 ? color.teal : color.maroon)
table.cell(infoTable, 3, 3, str.tostring(strategy.openprofit / initial_balance * 100, '###.##') + '%', text_color = color.white, bgcolor = strategy.openprofit > 0 ? color.teal : color.maroon)
table.cell(infoTable, 3, 4, '-' + str.tostring(strategy.position_avg_price * strategy.position_size * 0.025 / 100, '###.##') + ' TL', text_color = color.white, bgcolor = color.new(#5a637e, 0))

// Başlıklar
table.cell(infoTable, 4, 0, 'Grid Karı:', text_color = color.white, bgcolor = color.new(color.black, 0))
table.cell(infoTable, 4, 1, 'Grid Karı %:', text_color = color.white, bgcolor = color.new(color.black, 0))
table.cell(infoTable, 4, 2, 'Net Kar:', bgcolor = color.new(color.black, 0), text_color = color.white)
table.cell(infoTable, 4, 3, 'Net Kar %:', bgcolor = color.new(color.black, 0), text_color = color.white)
table.cell(infoTable, 4, 4, 'Bakiye TL:', bgcolor = color.new(color.black, 0), text_color = color.white)

// Değerler
table.cell(infoTable, 5, 0, str.tostring(strategy.netprofit, '###.#####') + ' TL', text_color = color.white, bgcolor = strategy.netprofit > 0 ? color.teal : color.maroon)
table.cell(infoTable, 5, 1, str.tostring(strategy.netprofit / initial_balance * 100 / tradingtime, '####.##') + '%', text_color = color.white, bgcolor = strategy.netprofit > 0 ? color.teal : color.maroon)
table.cell(infoTable, 5, 2, str.tostring(strategy.netprofit + strategy.openprofit, '###.##') + ' TL', text_color = color.white, bgcolor = strategy.netprofit + strategy.openprofit > 0 ? color.teal : color.maroon)
table.cell(infoTable, 5, 3, str.tostring((strategy.netprofit + strategy.openprofit) / initial_balance * 100, '####.##') + '%', text_color = color.white, bgcolor = strategy.netprofit + strategy.openprofit > 0 ? color.teal : color.maroon)
table.cell(infoTable, 5, 4, str.tostring(initial_balance + strategy.netprofit + strategy.openprofit, '###.##') + ' TL', text_color = color.white, bgcolor = color.new(#3d4d7c, 0))






Editor is loading...
Leave a Comment