CalulateWinLoss
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Nut_Satit

//@version=5

// @description TODO: add library description here
library("CalulateWinLoss", overlay=true)

// @function TODO: add function description here
// @param x TODO: add parameter x description here
// @returns TODO: add what function returns
export colorwhitered(float value2color) =>
    export_color = value2color < 0 ?  color.red : color.white
    export_color

export colorredwhite(float value2color) =>
    export_color = value2color > 0 ?  color.red : color.white
    export_color

export calwinloss() =>
	//TODO : add function body and return value here
	//Define for calulate win loss
    var count_loss  = 0
    var count_win   = 0
    var tmp_loss    = 0
    var tmp_win     = 0
    var max_loss    = 0
    var max_win     = 0
    var current_stat= 0
    var win_stat    = 0
    var loss_stat   = 0
    win  = ta.rising(strategy.wintrades, 1)
    loss = ta.rising(strategy.losstrades,1)
    win_long   = win  and strategy.position_size[1] > 0
    win_short  = win  and strategy.position_size[1] < 0
    loss_long  = loss and strategy.position_size[1] > 0
    loss_short = loss and strategy.position_size[1] < 0
    
    // set Current Status to win loss for Calulate
    if (win)
        win_stat  := win_stat+1
        loss_stat := 0
        count_win := count_win+1
        current_stat := 1
    if (loss)
        loss_stat := loss_stat+1
        win_stat  := 0
        count_loss:= count_loss+1
        current_stat := -1
    if (current_stat[1]!=win) and (current_stat[1]!=loss)
        current_stat := 0
    
    // Calulate max win status
    if (current_stat[1]==0 and current_stat==-1)
        tmp_win := 0
    else if (current_stat[1]==0 and current_stat==1)
        tmp_win := tmp_win+1
        if (tmp_win>max_win)
            max_win := tmp_win
    
    // Calulate max loss status
    if (current_stat[1]==0 and current_stat==1)
        tmp_loss := 0
    else if (current_stat[1]==0 and current_stat==-1)
        tmp_loss := tmp_loss+1
        if (tmp_loss>max_loss)
            max_loss := tmp_loss
    
    // Calulate value to Table
    me_close_trades = "Total:" + str.format("{0,number,#.#}", count_win+count_loss) + "\nWin:" + str.format("{0,number,#.#}", count_win) + " | Loss:" + str.format("{0,number,#.#}", count_loss)
    tv_close_trades = "Total:" + str.format("{0,number,#.#}", strategy.closedtrades) + "\nWin:" + str.format("{0,number,#.#}", strategy.wintrades) + " | Loss:" + str.format("{0,number,#.#}", strategy.losstrades)
    
    // Show table winlossDisplay
    var table winlossDisplay = table.new(position.bottom_left, 10, 4, bgcolor=color.new(color.white,70), frame_width=5, frame_color=color.black, border_color=color.black, border_width=1)
    table.cell(winlossDisplay, 0, 0, "", text_size=size.small,  bgcolor = color.silver, text_color= color.new(color.white,0))
    table.cell(winlossDisplay, 1, 0, "CloseTrades", text_size=size.small,  bgcolor = color.silver, text_color = color.new(color.white,0))
    table.cell(winlossDisplay, 2, 0, "Won", text_size=size.small,   bgcolor = color.silver, text_color = color.new(color.white,0))
    table.cell(winlossDisplay, 3, 0, "Lost", text_size=size.small,  bgcolor = color.silver, text_color = color.new(color.white,0))
    table.cell(winlossDisplay, 4, 0, "W/L Ratio", text_size=size.small,bgcolor = color.silver, text_color = color.new(color.white,0))
    table.cell(winlossDisplay, 5, 0, "WinRate%", text_size=size.small, bgcolor = color.silver, text_color = color.new(color.white,0))
    table.cell(winlossDisplay, 6, 0, "Net Profit", text_size=size.small, bgcolor = color.silver, text_color = color.new(color.white,0))
    table.cell(winlossDisplay, 7, 0, "Profit Factor", text_size=size.small, bgcolor = color.silver, text_color = color.new(color.white,0))
    table.cell(winlossDisplay, 8, 0, "Max Drawdown", text_size=size.small, bgcolor = color.silver, text_color = color.new(color.white,0))
    table.cell(winlossDisplay, 0, 1, "Me", text_size=size.small,  bgcolor = color.silver, text_color = color.new(color.white,0))
    table.cell(winlossDisplay, 1, 1, me_close_trades, text_size=size.small, text_color = color.new(color.white,0))
    table.cell(winlossDisplay, 2, 1, str.format("{0,number,#.#}", max_win), text_size=size.small, text_color = color.new(color.white,0))
    table.cell(winlossDisplay, 3, 1, str.format("{0,number,#.#}", max_loss), text_size=size.small, text_color = color.new(color.white,0))
    table.cell(winlossDisplay, 4, 1, str.format("{0,number,#.##}", count_win/count_loss), text_size=size.small, text_color = color.new(color.white,0))
    table.cell(winlossDisplay, 5, 1, str.format("{0,number,#.##}", 100*count_win/(count_win+count_loss)), text_size=size.small, text_color = color.new(colorwhitered(100*count_win/(count_win+count_loss)),0))
    table.cell(winlossDisplay, 6, 1, str.format("{0,number,#.##}", 0), text_size=size.small,text_color = color.new(color.white,0))
    table.cell(winlossDisplay, 7, 1, str.format("{0,number,#.##}", 0), text_size=size.small,text_color = color.new(color.white,0))
    table.cell(winlossDisplay, 8, 1, str.format("{0,number,#.##}", 0), text_size=size.small,text_color = color.new(color.white,0))
    table.cell(winlossDisplay, 0, 2, "TV", text_size=size.small,  bgcolor = color.silver, text_color = color.new(color.white,0))
    table.cell(winlossDisplay, 1, 2, tv_close_trades, text_size=size.small, text_color = color.new(color.white,0))
    table.cell(winlossDisplay, 2, 2, str.format("{0,number,#.#}", 0), text_size=size.small, text_color = color.new(color.white,0))
    table.cell(winlossDisplay, 3, 2, str.format("{0,number,#.#}", 0), text_size=size.small, text_color = color.new(color.white,0))
    table.cell(winlossDisplay, 4, 2, str.format("{0,number,#.##}", strategy.wintrades/strategy.losstrades), text_size=size.small, text_color = color.new(color.white,0))
    table.cell(winlossDisplay, 5, 2, str.format("{0,number,#.##}", 100*strategy.wintrades/strategy.closedtrades), text_size=size.small,text_color = color.new(color.white,0))
    table.cell(winlossDisplay, 6, 2, str.format("{0,number,#.##}", strategy.netprofit), text_size=size.small,text_color = color.new(colorwhitered(strategy.netprofit),0))
    table.cell(winlossDisplay, 7, 2, str.format("{0,number,#.##}", strategy.grossprofit / strategy.grossloss), text_size=size.small,text_color = color.new(colorredwhite(strategy.grossprofit / strategy.grossloss),0))
    table.cell(winlossDisplay, 8, 2, str.format("{0,number,#.##}", strategy.max_drawdown), text_size=size.small,text_color = color.new(colorredwhite(strategy.max_drawdown),0))
    
    //return value
    [win_long,win_short,loss_long,loss_short]
    
//[win_long,win_short,loss_long,loss_short] = calwinloss()

// Your insert undercode to Show plot Win Loss icon 
// import Nut_Satit/CalulateWinLoss/version as calwinloss
// [win_long,win_short,loss_long,loss_short] = calwinloss.calwinloss()
// plotshape(win_long,  title="Win Long",  color=color.lime,  location=location.belowbar,style=shape.triangleup,size=size.small,  text="")
// plotshape(win_short, title="Win Short", color=color.lime,  location=location.abovebar,style=shape.triangledown,size=size.small,text="")
// plotshape(loss_long, title="Loss Long", color=color.orange,location=location.belowbar,style=shape.triangleup,size=size.small,  text="")
// plotshape(loss_short,title="Loss Short",color=color.orange,location=location.abovebar,style=shape.triangledown,size=size.small,text="")

export rangdate(int startDate, int finishDate) =>
	//TODO : add function body and return value here
	// Backtesting day range 
	// Your insert undercode [$time_cond]to Buy Sell Condition
	// ($time_cond and [Condition])
	time_cond = time >= startDate and time <= finishDate