Untitled

 avatar
unknown
plain_text
9 days ago
18 kB
5
Indexable
// Copy below code to end of the desired strategy script
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                 INTEGRATED PERFORMANCE TABLES                                                     //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// COMMON TABLE SETTINGS (CONSOLIDATED)
show_performance = input.bool(true, 'SHOW ALL PERFORMANCE TABLES?', group='PERFORMANCE SETTINGS')

// Individual table toggles
show_monthly_table = input.bool(true, 'Show Monthly Performance Table', group='TABLE TOGGLES')
show_metrics_table = input.bool(true, 'Show Metrics Performance Table', group='TABLE TOGGLES')
show_trend_table = input.bool(true, 'Show Multi-Timeframe Trend Table', group='TABLE TOGGLES')

// Text and size settings for each table
monthly_text_size = input.session('Small', "Monthly Table Size", options=["Tiny", "Small", "Normal", "Large"], group=' TABLE SETTINGS', inline="monthly_size")
monthly_width = 14
monthly_height = 40

metrics_text_size = input.session('Small', "Metrics Table Size", options=["Tiny", "Small", "Normal", "Large"], group=' TABLE SETTINGS', inline="metrics_size")
metrics_width = 2
metrics_height = 10

trend_text_size = input.session('Small', "Trend Table Size", options=["Tiny", "Small", "Normal", "Large"], group=' TABLE SETTINGS', inline="trend_size")
trend_width = 6
trend_height = 2

// Location settings for each table
monthly_table_loc = input.session("Bottom Right", "Location", options=["Top Right", "Bottom Right", "Top Left", "Bottom Left", "Middle Right", "Bottom Center", "Top Center"], group=' TABLE SETTINGS', inline="monthly_size")
monthly_x_offset = 0
monthly_y_offset = 0

metrics_table_loc = input.session("Bottom Left", "Location", options=["Top Right", "Bottom Right", "Top Left", "Bottom Left", "Middle Right", "Bottom Center", "Top Center"], group=' TABLE SETTINGS', inline="metrics_size")
metrics_x_offset = 0
metrics_y_offset = 0

trend_table_loc = input.session("Top Center", "Location", options=["Top Right", "Bottom Right", "Top Left", "Bottom Left", "Middle Right", "Bottom Center", "Top Center"], group=' TABLE SETTINGS', inline="trend_size")
trend_x_offset = 0
trend_y_offset = 0

// Color settings
bg_color = color.rgb(91, 91, 94, 38)
text_head_color = #ffffff
data_text_color = color.white
border_color = color.white
profit_color = color.rgb(0, 150, 0)
loss_color = color.rgb(150, 0, 0)
neutral_color = color.rgb(0, 0, 150)

// Determine text sizes for each table
var monthly_table_text_size = monthly_text_size == 'Tiny' ? size.tiny :
  monthly_text_size == 'Small' ? size.small :
  monthly_text_size == 'Normal' ? size.normal : size.large

var metrics_table_text_size = metrics_text_size == 'Tiny' ? size.tiny :
  metrics_text_size == 'Small' ? size.small :
  metrics_text_size == 'Normal' ? size.normal : size.large
  
var trend_table_text_size = trend_text_size == 'Tiny' ? size.tiny :
  trend_text_size == 'Small' ? size.small :
  trend_text_size == 'Normal' ? size.normal : size.large

// Determine table positions based on selections
var monthly_table_position = monthly_table_loc == 'Bottom Right' ? position.bottom_right :
  monthly_table_loc == 'Bottom Left' ? position.bottom_left :
  monthly_table_loc == 'Middle Right' ? position.middle_right :
  monthly_table_loc == 'Bottom Center' ? position.bottom_center :
  monthly_table_loc == 'Top Right' ? position.top_right :
  monthly_table_loc == 'Top Center' ? position.top_center :
  monthly_table_loc == 'Top Left' ? position.top_left : position.bottom_right

var metrics_table_position = metrics_table_loc == 'Bottom Right' ? position.bottom_right :
  metrics_table_loc == 'Bottom Left' ? position.bottom_left :
  metrics_table_loc == 'Middle Right' ? position.middle_right :
  metrics_table_loc == 'Bottom Center' ? position.bottom_center :
  metrics_table_loc == 'Top Right' ? position.top_right :
  metrics_table_loc == 'Top Center' ? position.top_center :
  metrics_table_loc == 'Top Left' ? position.top_left : position.bottom_left

var trend_table_position = trend_table_loc == 'Bottom Right' ? position.bottom_right :
  trend_table_loc == 'Bottom Left' ? position.bottom_left :
  trend_table_loc == 'Middle Right' ? position.middle_right :
  trend_table_loc == 'Bottom Center' ? position.bottom_center :
  trend_table_loc == 'Top Right' ? position.top_right :
  trend_table_loc == 'Top Center' ? position.top_center :
  trend_table_loc == 'Top Left' ? position.top_left : position.top_center

/////////////////////////////////////////////
// MONTHLY PNL PERFORMANCE LOGIC
/////////////////////////////////////////////

// var bool new_month = na
new_month = ta.change(month) //> 0 ? true : false
newest_month = new_month and strategy.closedtrades >= 1

// profit
only_profit = strategy.netprofit
initial_balance = strategy.initial_capital

// month number
var int month_number = na
month_number := (ta.valuewhen(newest_month, month(time), 0)) //and month(time) > 1 ? (ta.valuewhen(newest_month, month(time), 0) - 1) :  12 //1 to 12

//month_year
var int month_time = na
month_time := ta.valuewhen(newest_month, time, 0) - 2419200000 

var int m_counter = 0
if newest_month
    m_counter += 1

// current month values
var bool new_year = na
new_year := ta.change(year) ? true : false
curr_m_pnl = only_profit - nz(ta.valuewhen(newest_month, only_profit, 0), 0)
curr_m_number = newest_month ? ta.valuewhen(newest_month, month(time), 0) : month(time)
curr_y_pnl = (only_profit - nz(ta.valuewhen(new_year, only_profit, 0),0)) 

var float [] net_profit_array = array.new_float()
var int [] month_array = array.new_int()
var int [] month_time_array = array.new_int()

if newest_month
    array.push(net_profit_array, only_profit)
    array.push(month_array, month_number)
    array.push(month_time_array, month_time)

var float [] y_pnl_array = array.new_float()
var int [] y_number_array = array.new_int()
var int [] y_time_array = array.new_int()

newest_year = new_year and strategy.closedtrades >= 1
get_yearly_pnl = nz(ta.valuewhen(newest_year, strategy.netprofit, 0) - nz(ta.valuewhen(newest_year, strategy.netprofit, 1), 0), 0)
get_m_year = ta.valuewhen(newest_year, year(time), 2)
get_y_time = ta.valuewhen(newest_year, time, 0)

if newest_year
    array.push(y_pnl_array, get_yearly_pnl)
    array.push(y_number_array, get_m_year)
    array.push(y_time_array, get_y_time)
    
var float monthly_profit = na
var int column_month_number = na
var int row_month_time = na
var int row_y = na

// Create monthly performance table
var monthlyTable = table.new(position = monthly_table_position, columns = monthly_width, rows = monthly_height, bgcolor = bg_color, border_color = border_color, border_width = 1)

if barstate.islast
// Use just:
    table.set_position(monthlyTable, monthly_table_position)

if barstate.islastconfirmedhistory and show_performance and show_monthly_table
    table.cell(table_id = monthlyTable, column = 0, row = 0, text = "YIL", text_color = text_head_color, text_size=monthly_table_text_size)
    table.cell(table_id = monthlyTable, column = 1, row = 0, text = "OCK", text_color = text_head_color, text_size=monthly_table_text_size)
    table.cell(table_id = monthlyTable, column = 2, row = 0, text = "SBT", text_color = text_head_color, text_size=monthly_table_text_size)
    table.cell(table_id = monthlyTable, column = 3, row = 0, text = "MAR", text_color = text_head_color, text_size=monthly_table_text_size)
    table.cell(table_id = monthlyTable, column = 4, row = 0, text = "NİS", text_color = text_head_color, text_size=monthly_table_text_size)
    table.cell(table_id = monthlyTable, column = 5, row = 0, text = "MAY", text_color = text_head_color, text_size=monthly_table_text_size)
    table.cell(table_id = monthlyTable, column = 6, row = 0, text = "HAZ", text_color = text_head_color, text_size=monthly_table_text_size)
    table.cell(table_id = monthlyTable, column = 7, row = 0, text = "TEM", text_color = text_head_color, text_size=monthly_table_text_size)
    table.cell(table_id = monthlyTable, column = 8, row = 0, text = "AUG", text_color = text_head_color, text_size=monthly_table_text_size)
    table.cell(table_id = monthlyTable, column = 9, row = 0, text = "EYL", text_color = text_head_color, text_size=monthly_table_text_size)
    table.cell(table_id = monthlyTable, column = 10, row = 0, text = "EKM", text_color = text_head_color, text_size=monthly_table_text_size)
    table.cell(table_id = monthlyTable, column = 11, row = 0, text = "KAS", text_color = text_head_color, text_size=monthly_table_text_size)
    table.cell(table_id = monthlyTable, column = 12, row = 0, text = "ARL", text_color =text_head_color, text_size=monthly_table_text_size)
    table.cell(table_id = monthlyTable, column = 13, row = 0, text = "YIL P/L", text_color = text_head_color, text_size=monthly_table_text_size)

    for i = 0 to (array.size(y_number_array) == 0 ? na : array.size(y_number_array) - 1)
        row_y := year(array.get(y_time_array, i)) - year(array.get(y_time_array, 0)) + 1
        table.cell(table_id = monthlyTable, column = 13, row = row_y, text = str.tostring(array.get(y_pnl_array , i), "##.##") + '\n' + '(' + str.tostring(array.get(y_pnl_array , i)*100/initial_balance, "##.##") + ' %)', bgcolor = array.get(y_pnl_array , i) > 0 ? profit_color : array.get(y_pnl_array , i) < 0 ? loss_color : neutral_color, text_color = data_text_color, text_size=monthly_table_text_size)
    
    curr_row_y = array.size(month_time_array) == 0 ? 1 : (year(array.get(month_time_array, array.size(month_time_array) - 1))) - (year(array.get(month_time_array, 0))) + 1
    table.cell(table_id = monthlyTable, column = 13, row = row_y >= 0 ? row_y + 1 : 1, text = str.tostring(curr_y_pnl, "##.##") + '\n' + '(' + str.tostring(curr_y_pnl*100/initial_balance, "##.##") + ' %)', bgcolor = curr_y_pnl > 0 ? profit_color : curr_y_pnl < 0 ? loss_color : neutral_color, text_color = data_text_color, text_size=monthly_table_text_size)
    
    for i = 0 to (array.size(net_profit_array) == 0 ? na : array.size(net_profit_array) - 1)
        monthly_profit := i > 0 ? (array.get(net_profit_array, i) - array.get(net_profit_array, i - 1)) : array.get(net_profit_array, i) 
        column_month_number := month(array.get(month_time_array, i)) 
        row_month_time :=((year(array.get(month_time_array, i))) - year(array.get(month_time_array, 0)) ) + 1 
        table.cell(table_id = monthlyTable, column = column_month_number, row = row_month_time, text = str.tostring(monthly_profit, "##.##") + '\n' + '(' + str.tostring(monthly_profit*100/initial_balance, "##.##") + ' %)', bgcolor = monthly_profit > 0 ? profit_color : monthly_profit < 0 ? loss_color : neutral_color, text_color = data_text_color, text_size=monthly_table_text_size)
        table.cell(table_id = monthlyTable, column = 0, row =row_month_time, text = str.tostring(year(array.get(month_time_array, i)), "##.##"), text_color = text_head_color, text_size=monthly_table_text_size)
       
    table.cell(table_id = monthlyTable, column = curr_m_number, row = nz(row_month_time) <= 0 ? 1 : row_month_time, text = str.tostring(curr_m_pnl, "##.##") + '\n' + '(' + str.tostring(curr_m_pnl*100/initial_balance, "##.##") + ' %)', bgcolor = curr_m_pnl > 0 ? profit_color : curr_m_pnl < 0 ? loss_color : neutral_color, text_color = data_text_color, text_size=monthly_table_text_size)
    table.cell(table_id = monthlyTable, column = 0, row =nz(row_month_time) <= 0 ? 1 : row_month_time, text = str.tostring(year(time), "##.##"), text_color = text_head_color, text_size=monthly_table_text_size)

//============================================================================================================================================================================
// STRATEGY PERFORMANCE TABLE LOGIC
//============================================================================================================================================================================

// Değişken tanımlamaları
var float peakEquity = na
var float maxDrawdownValue = 0.0
var float maxDrawdownPercent = 0.0
var float firstTradePrice = na

// Create performance table
var tablePerformance = table.new(position=metrics_table_position, columns=metrics_width, rows=metrics_height, border_color=border_color, border_width=2, bgcolor=bg_color, frame_color=color.rgb(71, 71, 71), frame_width=1)

// 📌 Adjust table position based on user setting
if barstate.islast
    table.set_position(tablePerformance, metrics_table_position)

// 📌 Metrikler Hesaplamaları
totalTrades = strategy.closedtrades
openTrades = strategy.opentrades
closedTrades = totalTrades - openTrades
winTrades = strategy.wintrades
loseTrades = strategy.losstrades

netProfit = strategy.netprofit
initialCapital = strategy.initial_capital
netProfitPercent = (netProfit / initialCapital) * 100

profitFactor = strategy.grossloss == 0 ? na : strategy.grossprofit / strategy.grossloss
percentProfitable = totalTrades == 0 ? na : (winTrades / totalTrades) * 100

// Drawdown hesaplamaları
peakEquity := na(peakEquity) ? strategy.equity : math.max(peakEquity, strategy.equity)
drawdown = peakEquity - strategy.equity

if drawdown > maxDrawdownValue
    maxDrawdownValue := drawdown
    maxDrawdownPercent := (maxDrawdownValue / peakEquity) * 100

// Buy & Hold hesaplamaları
if na(firstTradePrice) and strategy.position_size != 0
    firstTradePrice := close

buyHoldReturnPercent = na(firstTradePrice) ? na : ((close - firstTradePrice) / firstTradePrice) * 100
buyHoldReturnAmount = na(firstTradePrice) ? na : ((close / firstTradePrice) * initialCapital) - initialCapital

// Fill performance table
if (barstate.islast) and show_performance and show_metrics_table
    // Strateji İsmi ve Mevcut Pozisyon
    table.cell(tablePerformance, 0, 0, "⚙️ STRATEJİ: Kule Dönence bar bekleme", bgcolor=color.rgb(27, 27, 27, 24), text_color=text_head_color, text_size=metrics_table_text_size)
    if strategy.position_size > 0
        float mevcutKar = ((close - strategy.position_avg_price) / strategy.position_avg_price) * 100
        table.cell(tablePerformance, 1, 0, "LONG ▲ (" + str.tostring(mevcutKar, "#.##") + "%)", 
         bgcolor=mevcutKar > 0 ? profit_color : loss_color, text_color=data_text_color, text_size=metrics_table_text_size)
    else
        table.cell(tablePerformance, 1, 0, "POZİSYON YOK", bgcolor=color.rgb(27, 27, 27), text_color=data_text_color, text_size=metrics_table_text_size)

    // Net Kâr + Yüzdesi
    table.cell(tablePerformance, 0, 1, "NET KAZANÇ", bgcolor=color.rgb(27, 27, 27), text_color=text_head_color, text_size=metrics_table_text_size)
    table.cell(tablePerformance, 1, 1, str.tostring(netProfit, "#.##") + " TRY (" + str.tostring(netProfitPercent, "#.##") + "%)", 
     bgcolor=netProfit > 0 ? profit_color : loss_color, text_color=data_text_color, text_size=metrics_table_text_size)

    // Maksimum Kayıp + Yüzdesi
    table.cell(tablePerformance, 0, 2, "MAKS. KAYIP", bgcolor=color.rgb(27, 27, 27), text_color=text_head_color, text_size=metrics_table_text_size)
    table.cell(tablePerformance, 1, 2, str.tostring(maxDrawdownValue, "#.##") + " TRY (" + str.tostring(maxDrawdownPercent, "#.##") + "%)", 
     bgcolor=loss_color, text_color=data_text_color, text_size=metrics_table_text_size)

    // Karlılık Oranı
    table.cell(tablePerformance, 0, 3, "KARLI %", bgcolor=color.rgb(27, 27, 27), text_color=text_head_color, text_size=metrics_table_text_size)
    table.cell(tablePerformance, 1, 3, str.tostring(percentProfitable, "#.##") + "%", 
     bgcolor=neutral_color, text_color=data_text_color, text_size=metrics_table_text_size)

    // Al Tut
    table.cell(tablePerformance, 0, 4, "AL TUT", bgcolor=color.rgb(27, 27, 27), text_color=text_head_color, text_size=metrics_table_text_size)
    table.cell(tablePerformance, 1, 4, str.tostring(buyHoldReturnAmount, "#.##") + " TRY (" + str.tostring(buyHoldReturnPercent, "#.##") + "%)", 
     bgcolor=buyHoldReturnAmount > 0 ? profit_color : loss_color, text_color=data_text_color, text_size=metrics_table_text_size)

    // Kazanç Katsayısı
    table.cell(tablePerformance, 0, 5, "KAZANÇ KTS.", bgcolor=color.rgb(27, 27, 27), text_color=text_head_color, text_size=metrics_table_text_size)
    table.cell(tablePerformance, 1, 5, str.tostring(profitFactor, "#.##"), 
     bgcolor=neutral_color, text_color=data_text_color, text_size=metrics_table_text_size)

    // Açık Pozisyonlar
    table.cell(tablePerformance, 0, 6, "AÇIK PZS.", bgcolor=color.rgb(27, 27, 27), text_color=text_head_color, text_size=metrics_table_text_size)
    table.cell(tablePerformance, 1, 6, str.tostring(openTrades), 
     bgcolor=neutral_color, text_color=data_text_color, text_size=metrics_table_text_size)

    // Kapanmış Pozisyonlar
    table.cell(tablePerformance, 0, 7, "KAPANMIŞ PZS.", bgcolor=color.rgb(27, 27, 27), text_color=text_head_color, text_size=metrics_table_text_size)
    table.cell(tablePerformance, 1, 7, str.tostring(closedTrades), 
     bgcolor=neutral_color, text_color=data_text_color, text_size=metrics_table_text_size)

    // Kazançlı Pozisyonlar
    table.cell(tablePerformance, 0, 8, "KARLI İŞL.", bgcolor=color.rgb(27, 27, 27), text_color=text_head_color, text_size=metrics_table_text_size)
    table.cell(tablePerformance, 1, 8, str.tostring(winTrades), 
     bgcolor=profit_color, text_color=data_text_color, text_size=metrics_table_text_size)

    // Kayıplı Pozisyonlar
    table.cell(tablePerformance, 0, 9, "ZARARLI İŞL.", bgcolor=color.rgb(27, 27, 27), text_color=text_head_color, text_size=metrics_table_text_size)
    table.cell(tablePerformance, 1, 9, str.tostring(loseTrades), 
     bgcolor=loss_color, text_color=data_text_color, text_size=metrics_table_text_size)
Editor is loading...
Leave a Comment