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

// Other Works ------------------------------------------------------------------{
// Portfolio 
        // Portfolio Matrix, https://www.tradingview.com/script/agCQIVZJ-Portfolio-alpha-beta-stdev-variance-mean-max-drawdown/
        // https://www.tradingview.com/script/GxQp712Z-Portfolio-Laboratory-Kioseff-Trading/

// Strategy 
        //  https://www.tradingview.com/script/V7IfK3mr-Trailing-Take-Profit-Trailing-Stop-Loss/
        //  Monthly Return by QuantNomad, https://www.tradingview.com/script/kzp8e4X3-Monthly-Returns-in-PineScript-Strategies/
        //  Position Size by , https://www.tradingview.com/script/hoCPm5UY-%D0%A1alculation-a-position-size-based-on-risk/
        //  Position Size, Kelly Method by , https://www.tradingview.com/script/bFXf4IXh-Built-in-Kelly-ratio-for-dynamic-position-sizing/
// Trailing Stop 
        //  https://www.tradingview.com/script/EfbdipVs-Bjorgum-AutoTrail/
        //  Trail Stop Percent, https://www.tradingview.com/script/XAscppNW-Nick-Rypock-Trailing-Reverse-NRTR/
        //  https://www.tradingview.com/script/MvlwAzSg-3Commas-Bot/
        //  Super Trend, https://www.tradingview.com/script/VLWVV7tH-SuperTrend/
// }

// Functions ------------------------------------------------------------------{

// 1. Portfolio Holding List 
        // Fundamental anlysis and fair price <Future Plan>
        // Alert based on financial performance. <Future Plan> 
// 2. Stop Loss/Take Profit & Alert 
// 3. Portfolio Analysis <Funture Plan>
// 4. Monthly/Yearly Return. <Future Plan> 
// 5. Rebalancing <Future Plan>
    
// }


//@version=5
indicator("Portfolio", format=format.volume)

// FUNCTION START HERE ///////////////////////////////////////////////////////// {
// Get value based on tickerid
fGetValue(_tickerid, _value)=> 
    value = request.security(_tickerid, timeframe.period, _value, lookahead=barmerge.lookahead_on)
    
fGetPostion(isPositionActive, assetTicker, 
     entryDate, exitDate, shareQty, 
     costPriceType, costPriceManualInput,  
     stopLossMethod, stopLossInput, 
     takeProfitMethod, takeProfitInput,
     atrLength)=> 
    // BOOL
    bool    isHoldingPeriod     = time>=entryDate and time <=exitDate
    bool    isEntryDate         = time>= entryDate 
    var     arrayDividend       =   array.new_float()
    
    // VARIABLE
    float   avgEntryPrice       =   ta.valuewhen(isEntryDate and not isEntryDate[1], fGetValue(assetTicker, close),0)
    float   costValue           =   na
    float   marketPrice         =   isHoldingPeriod ? fGetValue(assetTicker,close) : na
    float   marketValue                 =   na,     float   marketValueWithDividend     =   na
    float   gainLossValue               =   na,     float   gainLossPercent             =   na    
    float   dailyGainLossValue          =   na,     float   dialyGainLossPercent        =   na
    float   stopLossPrice               =   na,     float   expectedLossValue           =   na
    float   expectedGainValue           =   na

    float   takeProfitPrice     =   na
    float   atr                 =   ta.valuewhen(isEntryDate and not isEntryDate[1], fGetValue(assetTicker, ta.atr(atrLength)),0)

    
    float   dvps                =   nz(request.dividends(assetTicker, dividends.gross, gaps=barmerge.gaps_on, ignore_invalid_symbol=true))
    float   dividend            =   na

    
    // CONDITION
    if isPositionActive and isHoldingPeriod
        avgEntryPrice           :=  costPriceType == "Auto" ? avgEntryPrice : costPriceManualInput
        costValue               :=  avgEntryPrice * shareQty
        marketValue             :=  marketPrice * shareQty
        gainLossValue           :=  (marketPrice - avgEntryPrice) * shareQty
        gainLossPercent         :=  (marketPrice - avgEntryPrice)/ avgEntryPrice *100
        dailyGainLossValue      := ((marketPrice-marketPrice[1])) * shareQty
        dialyGainLossPercent    := ((marketPrice/marketPrice[1])-1) * 100
        
        stopLossPrice           :=  stopLossMethod      == "Price" ?    stopLossInput      :stopLossMethod         == "Percent" ?  avgEntryPrice * (1 - (stopLossInput/100))       :   avgEntryPrice - (atr*stopLossInput)
        takeProfitPrice         :=  takeProfitMethod    == "Price" ?    takeProfitInput   :takeProfitMethod       == "Percent" ?  avgEntryPrice * (1 + (takeProfitInput/100))   :   avgEntryPrice + (atr*takeProfitInput)
        expectedLossValue       :=    (stopLossPrice - avgEntryPrice) * shareQty
        expectedGainValue       :=    (takeProfitPrice - avgEntryPrice) * shareQty
        
        if dvps
            array.push(arrayDividend, dvps * shareQty)
        dividend := nz(array.sum(arrayDividend))
        marketValueWithDividend := marketValue + nz(dividend)
    else 
        avgEntryPrice           :=  na,        costValue            := na
        marketPrice             :=  na,        marketValue          := na
        marketValueWithDividend :=  na,        gainLossValue        := na
        gainLossPercent         :=  na,        dailyGainLossValue   := na
        dialyGainLossPercent    :=  na,        stopLossPrice        := na
        takeProfitPrice         :=  na,        expectedLossValue    := na
        expectedGainValue       :=  na,        dividend             := na
    
    // RETURN
    [avgEntryPrice,costValue,  marketPrice, marketValue, marketValueWithDividend, gainLossValue, gainLossPercent, dailyGainLossValue, dialyGainLossPercent, stopLossPrice, expectedLossValue, takeProfitPrice, expectedGainValue, dividend]

// } END OF FUNCTION

// GROUP START HERE //////////////////////////////////////////////////////////// {

group01 =   "====================== Position 01 ======================"  
group02 =   "====================== Position 02 ======================"
group03 =   "====================== Position 03 ======================"
group04 =   "====================== Position 04 ======================"
group05 =   "====================== Position 05 ======================"
group06 =   "====================== Position 06 ======================"
group07 =   "====================== Position 07 ======================"
group08 =   "====================== Position 08 ======================"
group09 =   "====================== Position 09 ======================"
group10 =   "====================== Position 10 ======================"


    
// } END OF GROUP

// General Set-up ////////////////////////////////////////////////////////////// 

initialCapital  =   input.int(100000, "Initial Capital") 
initialIndex    =   input.int(100, "Initial Index")

plotType        =   input.string("Index", "Plot Type", 
     options =["Index", "Value", "Ticker 01", "Ticker 02","Ticker 03","Ticker 04","Ticker 05","Ticker 06","Ticker 07","Ticker 08","Ticker 09","Ticker 10"])



// INPUT START HERE //////////////////////////////////////////////////////////// {
// 1 Toggle on/off ------------------------------------------------------------
isPositionActive01       =   input.bool(true,   "", inline="1-1", group=group01)
isPositionActive02       =   input.bool(false,  "", inline="1-1", group=group02)
isPositionActive03       =   input.bool(false,  "", inline="1-1", group=group03)
isPositionActive04       =   input.bool(false,  "", inline="1-1", group=group04)
isPositionActive05       =   input.bool(false,  "", inline="1-1", group=group05)

isPositionActive06       =   input.bool(false,  "", inline="1-1", group=group06)
isPositionActive07       =   input.bool(false,  "", inline="1-1", group=group07)
isPositionActive08       =   input.bool(false,  "", inline="1-1", group=group08)
isPositionActive09       =   input.bool(false,  "", inline="1-1", group=group09)
isPositionActive10       =   input.bool(false,  "", inline="1-1", group=group10)

// 2 Asset Ticker ID ----------------------------------------------------------
assetTicker01           =   input.symbol("FB", "Position 1", inline="1-1",group=group01)
assetTicker02           =   input.symbol("FB", "Position 2", inline="1-1",group=group02)
assetTicker03           =   input.symbol("FB", "Position 3", inline="1-1",group=group03)
assetTicker04           =   input.symbol("FB", "Position 4", inline="1-1",group=group04)
assetTicker05           =   input.symbol("FB", "Position 5", inline="1-1",group=group05)

assetTicker06           =   input.symbol("FB", "Position 6", inline="1-1",group=group06)
assetTicker07           =   input.symbol("FB", "Position 7", inline="1-1",group=group07)
assetTicker08           =   input.symbol("FB", "Position 8", inline="1-1",group=group08)
assetTicker09           =   input.symbol("FB", "Position 9", inline="1-1",group=group09)
assetTicker10           =   input.symbol("FB", "Position 10", inline="1-1",group=group10)

// 3 Share Quantity ----------------------------------------------------------
shareQty01              =   input.float(100, "Quantity", minval=0.0, step=100, inline="1-1", group=group01)
shareQty02              =   input.float(100, "Quantity", minval=0.0, step=100, inline="1-1", group=group02)
shareQty03              =   input.float(100, "Quantity", minval=0.0, step=100, inline="1-1", group=group03)
shareQty04              =   input.float(100, "Quantity", minval=0.0, step=100, inline="1-1", group=group04)
shareQty05              =   input.float(100, "Quantity", minval=0.0, step=100, inline="1-1", group=group05)

shareQty06              =   input.float(100, "Quantity", minval=0.0, step=100, inline="1-1", group=group06)
shareQty07              =   input.float(100, "Quantity", minval=0.0, step=100, inline="1-1", group=group07)
shareQty08              =   input.float(100, "Quantity", minval=0.0, step=100, inline="1-1", group=group08)
shareQty09              =   input.float(100, "Quantity", minval=0.0, step=100, inline="1-1", group=group09)
shareQty10              =   input.float(100, "Quantity", minval=0.0, step=100, inline="1-1", group=group10)

// 4 Price -------------------------------------------------------------------
toolTipForCostPriceManualInput  ="Auto : Avg Cost Price based on Entry Date. \n Manual : Fill the entry price"
costPriceType01         =   input.string("Auto", "Avg Cost Price", options =["Auto", "Manual"], inline="cost price",   group=group01)
costPriceType02         =   input.string("Auto", "Avg Cost Price", options =["Auto", "Manual"], inline="cost price",   group=group02)
costPriceType03         =   input.string("Auto", "Avg Cost Price", options =["Auto", "Manual"], inline="cost price",   group=group03)
costPriceType04         =   input.string("Auto", "Avg Cost Price", options =["Auto", "Manual"], inline="cost price",   group=group04)
costPriceType05         =   input.string("Auto", "Avg Cost Price", options =["Auto", "Manual"], inline="cost price",   group=group05)

costPriceType06         =   input.string("Auto", "Avg Cost Price", options =["Auto", "Manual"], inline="cost price",   group=group06)
costPriceType07         =   input.string("Auto", "Avg Cost Price", options =["Auto", "Manual"], inline="cost price",   group=group07)
costPriceType08         =   input.string("Auto", "Avg Cost Price", options =["Auto", "Manual"], inline="cost price",   group=group08)
costPriceType09         =   input.string("Auto", "Avg Cost Price", options =["Auto", "Manual"], inline="cost price",   group=group09)
costPriceType10         =   input.string("Auto", "Avg Cost Price", options =["Auto", "Manual"], inline="cost price",   group=group10)

costPriceManualInput01  = input.float(0.0, "", minval=0.0, step=0.01, tooltip = toolTipForCostPriceManualInput, inline="cost price", group=group01)
costPriceManualInput02  = input.float(0.0, "", minval=0.0, step=0.01, tooltip = toolTipForCostPriceManualInput, inline="cost price", group=group02)
costPriceManualInput03  = input.float(0.0, "", minval=0.0, step=0.01, tooltip = toolTipForCostPriceManualInput, inline="cost price", group=group03)
costPriceManualInput04  = input.float(0.0, "", minval=0.0, step=0.01, tooltip = toolTipForCostPriceManualInput, inline="cost price", group=group04)
costPriceManualInput05  = input.float(0.0, "", minval=0.0, step=0.01, tooltip = toolTipForCostPriceManualInput, inline="cost price", group=group05)

costPriceManualInput06  = input.float(0.0, "", minval=0.0, step=0.01, tooltip = toolTipForCostPriceManualInput, inline="cost price", group=group06)
costPriceManualInput07  = input.float(0.0, "", minval=0.0, step=0.01, tooltip = toolTipForCostPriceManualInput, inline="cost price", group=group07)
costPriceManualInput08  = input.float(0.0, "", minval=0.0, step=0.01, tooltip = toolTipForCostPriceManualInput, inline="cost price", group=group08)
costPriceManualInput09  = input.float(0.0, "", minval=0.0, step=0.01, tooltip = toolTipForCostPriceManualInput, inline="cost price", group=group09)
costPriceManualInput10  = input.float(0.0, "", minval=0.0, step=0.01, tooltip = toolTipForCostPriceManualInput, inline="cost price", group=group10)

// 5 Entry and Exit Date -----------------------------------------------------
entryDate01     =   input.time(timestamp("01 Jan 2020"), "Entry Date", group=group01)
entryDate02     =   input.time(timestamp("01 Jan 2020"), "Entry Date", group=group02)
entryDate03     =   input.time(timestamp("01 Jan 2020"), "Entry Date", group=group03)
entryDate04     =   input.time(timestamp("01 Jan 2020"), "Entry Date", group=group04)
entryDate05     =   input.time(timestamp("01 Jan 2020"), "Entry Date", group=group05)

entryDate06     =   input.time(timestamp("01 Jan 2020"), "Entry Date", group=group06)
entryDate07     =   input.time(timestamp("01 Jan 2020"), "Entry Date", group=group07)
entryDate08     =   input.time(timestamp("01 Jan 2020"), "Entry Date", group=group08)
entryDate09     =   input.time(timestamp("01 Jan 2020"), "Entry Date", group=group09)
entryDate10     =   input.time(timestamp("01 Jan 2020"), "Entry Date", group=group10)

exitDate01      =   input.time(timestamp("31 Dec 2099"), "Exit Date", group=group01)
exitDate02      =   input.time(timestamp("31 Dec 2099"), "Exit Date", group=group02)
exitDate03      =   input.time(timestamp("31 Dec 2099"), "Exit Date", group=group03)
exitDate04      =   input.time(timestamp("31 Dec 2099"), "Exit Date", group=group04)
exitDate05      =   input.time(timestamp("31 Dec 2099"), "Exit Date", group=group05)

exitDate06      =   input.time(timestamp("31 Dec 2099"), "Exit Date", group=group06)
exitDate07      =   input.time(timestamp("31 Dec 2099"), "Exit Date", group=group07)
exitDate08      =   input.time(timestamp("31 Dec 2099"), "Exit Date", group=group08)
exitDate09      =   input.time(timestamp("31 Dec 2099"), "Exit Date", group=group09)
exitDate10      =   input.time(timestamp("31 Dec 2099"), "Exit Date", group=group10)

// 6 Stop Loss & Take Profit 

LineForStopLoss  =   "In Stop Loss Line",    
LineForTakeProfit  =   "In Take Profit Line",
tooltipForStopLossTakeProfit ="Price : 0.00. \n Percent : 0-100%. \n ATR : Atr Multiplier 0.00 "
// 6.1 Stop Loss
stopLossMethod01        =   input.string(   "Percent",  "S/L Method",       options =["Percent","Price","ATR"], inline=LineForStopLoss, group=group01)
stopLossMethod02        =   input.string(   "Percent",  "S/L Method",       options =["Percent","Price","ATR"], inline=LineForStopLoss, group=group02)
stopLossMethod03        =   input.string(   "Percent",  "S/L Method",       options =["Percent","Price","ATR"], inline=LineForStopLoss, group=group03)
stopLossMethod04        =   input.string(   "Percent",  "S/L Method",       options =["Percent","Price","ATR"], inline=LineForStopLoss, group=group04)
stopLossMethod05        =   input.string(   "Percent",  "S/L Method",       options =["Percent","Price","ATR"], inline=LineForStopLoss, group=group05)

stopLossMethod06        =   input.string(   "Percent",  "S/L Method",       options =["Percent","Price","ATR"], inline=LineForStopLoss, group=group06)
stopLossMethod07        =   input.string(   "Percent",  "S/L Method",       options =["Percent","Price","ATR"], inline=LineForStopLoss, group=group07)
stopLossMethod08        =   input.string(   "Percent",  "S/L Method",       options =["Percent","Price","ATR"], inline=LineForStopLoss, group=group08)
stopLossMethod09        =   input.string(   "Percent",  "S/L Method",       options =["Percent","Price","ATR"], inline=LineForStopLoss, group=group09)
stopLossMethod10        =   input.string(   "Percent",  "S/L Method",       options =["Percent","Price","ATR"], inline=LineForStopLoss, group=group10)

stopLossInput01         =   input.float(    0.0,        "",        minval=0,       tooltip = tooltipForStopLossTakeProfit, inline=LineForStopLoss,  group=group01)
stopLossInput02         =   input.float(    0.0,        "",        minval=0,       tooltip = tooltipForStopLossTakeProfit, inline=LineForStopLoss,  group=group02)
stopLossInput03         =   input.float(    0.0,        "",        minval=0,       tooltip = tooltipForStopLossTakeProfit, inline=LineForStopLoss,  group=group03)
stopLossInput04         =   input.float(    0.0,        "",        minval=0,       tooltip = tooltipForStopLossTakeProfit, inline=LineForStopLoss,  group=group04)
stopLossInput05         =   input.float(    0.0,        "",        minval=0,       tooltip = tooltipForStopLossTakeProfit, inline=LineForStopLoss,  group=group05)

stopLossInput06         =   input.float(    0.0,        "",        minval=0,       tooltip = tooltipForStopLossTakeProfit, inline=LineForStopLoss,  group=group06)
stopLossInput07         =   input.float(    0.0,        "",        minval=0,       tooltip = tooltipForStopLossTakeProfit, inline=LineForStopLoss,  group=group07)
stopLossInput08         =   input.float(    0.0,        "",        minval=0,       tooltip = tooltipForStopLossTakeProfit, inline=LineForStopLoss,  group=group08)
stopLossInput09         =   input.float(    0.0,        "",        minval=0,       tooltip = tooltipForStopLossTakeProfit, inline=LineForStopLoss,  group=group09)
stopLossInput10         =   input.float(    0.0,        "",        minval=0,       tooltip = tooltipForStopLossTakeProfit, inline=LineForStopLoss,  group=group10)

// 6.2 Take Profit
takeProfitMethod01      =   input.string(   "Percent",  "T/P Method",       options =["Percent","Price","ATR"],inline=LineForTakeProfit, group=group01)
takeProfitMethod02      =   input.string(   "Percent",  "T/P Method",       options =["Percent","Price","ATR"],inline=LineForTakeProfit, group=group02)
takeProfitMethod03      =   input.string(   "Percent",  "T/P Method",       options =["Percent","Price","ATR"],inline=LineForTakeProfit, group=group03)
takeProfitMethod04      =   input.string(   "Percent",  "T/P Method",       options =["Percent","Price","ATR"],inline=LineForTakeProfit, group=group04)
takeProfitMethod05      =   input.string(   "Percent",  "T/P Method",       options =["Percent","Price","ATR"],inline=LineForTakeProfit, group=group05)

takeProfitMethod06      =   input.string(   "Percent",  "T/P Method",       options =["Percent","Price","ATR"],inline=LineForTakeProfit, group=group06)
takeProfitMethod07      =   input.string(   "Percent",  "T/P Method",       options =["Percent","Price","ATR"],inline=LineForTakeProfit, group=group07)
takeProfitMethod08      =   input.string(   "Percent",  "T/P Method",       options =["Percent","Price","ATR"],inline=LineForTakeProfit, group=group08)
takeProfitMethod09      =   input.string(   "Percent",  "T/P Method",       options =["Percent","Price","ATR"],inline=LineForTakeProfit, group=group09)
takeProfitMethod10      =   input.string(   "Percent",  "T/P Method",       options =["Percent","Price","ATR"],inline=LineForTakeProfit, group=group10)

takeProfitInput01       =   input.float(    0.0,        "",        minval=0,       tooltip = tooltipForStopLossTakeProfit, inline=LineForTakeProfit,  group=group01)
takeProfitInput02       =   input.float(    0.0,        "",        minval=0,       tooltip = tooltipForStopLossTakeProfit, inline=LineForTakeProfit,  group=group02)
takeProfitInput03       =   input.float(    0.0,        "",        minval=0,       tooltip = tooltipForStopLossTakeProfit, inline=LineForTakeProfit,  group=group03)
takeProfitInput04       =   input.float(    0.0,        "",        minval=0,       tooltip = tooltipForStopLossTakeProfit, inline=LineForTakeProfit,  group=group04)
takeProfitInput05       =   input.float(    0.0,        "",        minval=0,       tooltip = tooltipForStopLossTakeProfit, inline=LineForTakeProfit,  group=group05)

takeProfitInput06       =   input.float(    0.0,        "",        minval=0,       tooltip = tooltipForStopLossTakeProfit, inline=LineForTakeProfit,  group=group06)
takeProfitInput07       =   input.float(    0.0,        "",        minval=0,       tooltip = tooltipForStopLossTakeProfit, inline=LineForTakeProfit,  group=group07)
takeProfitInput08       =   input.float(    0.0,        "",        minval=0,       tooltip = tooltipForStopLossTakeProfit, inline=LineForTakeProfit,  group=group08)
takeProfitInput09       =   input.float(    0.0,        "",        minval=0,       tooltip = tooltipForStopLossTakeProfit, inline=LineForTakeProfit,  group=group09)
takeProfitInput10       =   input.float(    0.0,        "",        minval=0,       tooltip = tooltipForStopLossTakeProfit, inline=LineForTakeProfit,  group=group10)

atrLength01             =   input.int(      22,         "ATR Length",       minval=0,       inline="ATR",  group=group01)
atrLength02             =   input.int(      22,         "ATR Length",       minval=0,       inline="ATR",  group=group02)
atrLength03             =   input.int(      22,         "ATR Length",       minval=0,       inline="ATR",  group=group03)
atrLength04             =   input.int(      22,         "ATR Length",       minval=0,       inline="ATR",  group=group04)
atrLength05             =   input.int(      22,         "ATR Length",       minval=0,       inline="ATR",  group=group05)

atrLength06             =   input.int(      22,         "ATR Length",       minval=0,       inline="ATR",  group=group06)
atrLength07             =   input.int(      22,         "ATR Length",       minval=0,       inline="ATR",  group=group07)
atrLength08             =   input.int(      22,         "ATR Length",       minval=0,       inline="ATR",  group=group08)
atrLength09             =   input.int(      22,         "ATR Length",       minval=0,       inline="ATR",  group=group09)
atrLength10             =   input.int(      22,         "ATR Length",       minval=0,       inline="ATR",  group=group10)

// } END of INPUT

// CALCUATION START HERE /////////////////////////////////////////////////////// {

[avgEntryPrice01, costValue01, marketPrice01, marketValue01, marketValueWithDividend01,gainLossValue01, gainLossPercent01, dailyGainLossValue01, dialyGainLossPercent01, stopLossPrice01, expectedLossValue01,  takeProfitPrice01, expectedGainValue01, dividend01] = fGetPostion(isPositionActive01, assetTicker01, entryDate01, exitDate01, shareQty01, costPriceType01, costPriceManualInput01, stopLossMethod01, stopLossInput01, takeProfitMethod01, takeProfitInput01, atrLength01)
[avgEntryPrice02, costValue02, marketPrice02, marketValue02, marketValueWithDividend02,gainLossValue02, gainLossPercent02, dailyGainLossValue02, dialyGainLossPercent02, stopLossPrice02, expectedLossValue02,  takeProfitPrice02, expectedGainValue02, dividend02] = fGetPostion(isPositionActive02, assetTicker02, entryDate02, exitDate02, shareQty02, costPriceType02, costPriceManualInput02, stopLossMethod02, stopLossInput02, takeProfitMethod02, takeProfitInput02, atrLength02)
[avgEntryPrice03, costValue03, marketPrice03, marketValue03, marketValueWithDividend03,gainLossValue03, gainLossPercent03, dailyGainLossValue03, dialyGainLossPercent03, stopLossPrice03, expectedLossValue03,  takeProfitPrice03, expectedGainValue03, dividend03] = fGetPostion(isPositionActive03, assetTicker03, entryDate03, exitDate03, shareQty03, costPriceType03, costPriceManualInput03, stopLossMethod03, stopLossInput03, takeProfitMethod03, takeProfitInput03, atrLength03)
[avgEntryPrice04, costValue04, marketPrice04, marketValue04, marketValueWithDividend04,gainLossValue04, gainLossPercent04, dailyGainLossValue04, dialyGainLossPercent04, stopLossPrice04, expectedLossValue04,  takeProfitPrice04, expectedGainValue04, dividend04] = fGetPostion(isPositionActive04, assetTicker04, entryDate04, exitDate04, shareQty04, costPriceType04, costPriceManualInput04, stopLossMethod04, stopLossInput04, takeProfitMethod04, takeProfitInput04, atrLength04)
[avgEntryPrice05, costValue05, marketPrice05, marketValue05, marketValueWithDividend05,gainLossValue05, gainLossPercent05, dailyGainLossValue05, dialyGainLossPercent05, stopLossPrice05, expectedLossValue05,  takeProfitPrice05, expectedGainValue05, dividend05] = fGetPostion(isPositionActive05, assetTicker05, entryDate05, exitDate05, shareQty05, costPriceType05, costPriceManualInput05, stopLossMethod05, stopLossInput05, takeProfitMethod05, takeProfitInput05, atrLength05)

[avgEntryPrice06, costValue06, marketPrice06, marketValue06, marketValueWithDividend06,gainLossValue06, gainLossPercent06, dailyGainLossValue06, dialyGainLossPercent06, stopLossPrice06, expectedLossValue06,  takeProfitPrice06, expectedGainValue06, dividend06] = fGetPostion(isPositionActive06, assetTicker06, entryDate06, exitDate06, shareQty06, costPriceType06, costPriceManualInput06, stopLossMethod06, stopLossInput06, takeProfitMethod06, takeProfitInput06, atrLength06)
[avgEntryPrice07, costValue07, marketPrice07, marketValue07, marketValueWithDividend07,gainLossValue07, gainLossPercent07, dailyGainLossValue07, dialyGainLossPercent07, stopLossPrice07, expectedLossValue07,  takeProfitPrice07, expectedGainValue07, dividend07] = fGetPostion(isPositionActive07, assetTicker07, entryDate07, exitDate07, shareQty07, costPriceType07, costPriceManualInput07, stopLossMethod07, stopLossInput07, takeProfitMethod07, takeProfitInput07, atrLength07)
[avgEntryPrice08, costValue08, marketPrice08, marketValue08, marketValueWithDividend08,gainLossValue08, gainLossPercent08, dailyGainLossValue08, dialyGainLossPercent08, stopLossPrice08, expectedLossValue08,  takeProfitPrice08, expectedGainValue08, dividend08] = fGetPostion(isPositionActive08, assetTicker08, entryDate08, exitDate08, shareQty08, costPriceType08, costPriceManualInput08, stopLossMethod08, stopLossInput08, takeProfitMethod08, takeProfitInput08, atrLength08)
[avgEntryPrice09, costValue09, marketPrice09, marketValue09, marketValueWithDividend09,gainLossValue09, gainLossPercent09, dailyGainLossValue09, dialyGainLossPercent09, stopLossPrice09, expectedLossValue09,  takeProfitPrice09, expectedGainValue09, dividend09] = fGetPostion(isPositionActive09, assetTicker09, entryDate09, exitDate09, shareQty09, costPriceType09, costPriceManualInput09, stopLossMethod09, stopLossInput09, takeProfitMethod09, takeProfitInput09, atrLength09)
[avgEntryPrice10, costValue10, marketPrice10, marketValue10, marketValueWithDividend10,gainLossValue10, gainLossPercent10, dailyGainLossValue10, dialyGainLossPercent10, stopLossPrice10, expectedLossValue10,  takeProfitPrice10, expectedGainValue10, dividend10] = fGetPostion(isPositionActive10, assetTicker10, entryDate10, exitDate10, shareQty10, costPriceType10, costPriceManualInput10, stopLossMethod10, stopLossInput10, takeProfitMethod10, takeProfitInput10, atrLength10)

totalDividend   =  
     nz(dividend01)+     nz(dividend02)+     nz(dividend03)+     nz(dividend04)+     nz(dividend05)+     nz(dividend06)+     nz(dividend07)+     nz(dividend08)+     nz(dividend09)+     nz(dividend10)

totalMarketValue   =   
     nz(marketValue01)+     nz(marketValue02)+     nz(marketValue03)+     nz(marketValue04)+     nz(marketValue05)+     nz(marketValue06)+     nz(marketValue07)+     nz(marketValue08)+     nz(marketValue09)+     nz(marketValue10)

totalCostValue  =   
     nz(costValue01)+     nz(costValue02)+     nz(costValue03)+     nz(costValue04)+     nz(costValue05)+     nz(costValue06)+     nz(costValue07)+     nz(costValue08)+     nz(costValue09)+     nz(costValue10)

totalDailyGainLossValue  =   
     nz(dailyGainLossValue01)+     nz(dailyGainLossValue02)+     nz(dailyGainLossValue03)+     nz(dailyGainLossValue04)+     nz(dailyGainLossValue05)+     nz(dailyGainLossValue06)+     nz(dailyGainLossValue07)+     nz(dailyGainLossValue08)+     nz(dailyGainLossValue09)+     nz(dailyGainLossValue10)

totalGainLossValue  =   
     nz(gainLossValue01)+     nz(gainLossValue02)+     nz(gainLossValue03)+     nz(gainLossValue04)+     nz(gainLossValue05)+     nz(gainLossValue06)+     nz(gainLossValue07)+     nz(gainLossValue08)+     nz(gainLossValue09)+     nz(gainLossValue10)

totalExpectedLoss = 
     nz(expectedLossValue01)+      nz(expectedLossValue02)+      nz(expectedLossValue03)+      nz(expectedLossValue04)+      nz(expectedLossValue05)+      nz(expectedLossValue06)+      nz(expectedLossValue07)+      nz(expectedLossValue08)+      nz(expectedLossValue09)+      nz(expectedLossValue10)

totalMarketValueWithDividend    =   totalMarketValue + totalDividend

index               =   totalMarketValue * initialIndex / totalCostValue  
indexWithDividend   =   (totalMarketValue + totalDividend) * initialIndex / totalCostValue 

//  } END OF CALCULATION 

// PLOT START HERE ///////////////////////////////////////////////////////////// {

float   avgEntryPrice   = na
float   marketPrice     =   na
float   stopLossPrice   =   na
float   takeProfitPrice =   na

if plotType ==                      "Ticker 01"
    avgEntryPrice   :=         avgEntryPrice01    
    marketPrice     :=           marketPrice01
    stopLossPrice   :=         stopLossPrice01
    takeProfitPrice :=       takeProfitPrice01
else if plotType ==                 "Ticker 02"
    avgEntryPrice   :=         avgEntryPrice02    
    marketPrice     :=           marketPrice02
    stopLossPrice   :=         stopLossPrice02
    takeProfitPrice :=       takeProfitPrice02
else if plotType ==                 "Ticker 03"
    avgEntryPrice   :=         avgEntryPrice03    
    marketPrice     :=           marketPrice03
    stopLossPrice   :=         stopLossPrice03
    takeProfitPrice :=       takeProfitPrice03
else if plotType ==                 "Ticker 04"
    avgEntryPrice   :=         avgEntryPrice04    
    marketPrice     :=           marketPrice04
    stopLossPrice   :=         stopLossPrice04
    takeProfitPrice :=       takeProfitPrice04
else if plotType ==                 "Ticker 05"
    avgEntryPrice   :=         avgEntryPrice05    
    marketPrice     :=           marketPrice05
    stopLossPrice   :=         stopLossPrice05
    takeProfitPrice :=       takeProfitPrice05
else if plotType ==                 "Ticker 06"
    avgEntryPrice   :=         avgEntryPrice06    
    marketPrice     :=           marketPrice06
    stopLossPrice   :=         stopLossPrice06
    takeProfitPrice :=       takeProfitPrice06
else if plotType ==                 "Ticker 07"
    avgEntryPrice   :=         avgEntryPrice07    
    marketPrice     :=           marketPrice07
    stopLossPrice   :=         stopLossPrice07
    takeProfitPrice :=       takeProfitPrice07
else if plotType ==                 "Ticker 08"
    avgEntryPrice   :=         avgEntryPrice08    
    marketPrice     :=           marketPrice08
    stopLossPrice   :=         stopLossPrice08
    takeProfitPrice :=       takeProfitPrice08
else if plotType ==                 "Ticker 09"
    avgEntryPrice   :=         avgEntryPrice09    
    marketPrice     :=           marketPrice09
    stopLossPrice   :=         stopLossPrice09
    takeProfitPrice :=       takeProfitPrice09
else if plotType ==                 "Ticker 10"
    avgEntryPrice   :=         avgEntryPrice10    
    marketPrice     :=           marketPrice10
    stopLossPrice   :=         stopLossPrice10
    takeProfitPrice :=       takeProfitPrice10
else 
    avgEntryPrice   :=                      na   
    marketPrice     :=                      na
    stopLossPrice   :=                      na
    takeProfitPrice :=                      na
    
plot(plotType   =="Value" ?  totalDividend                  : na , "Total Dividend", color=color.lime, style=plot.style_stepline_diamond)
plot(plotType   =="Value" ?  totalCostValue                 : na , "Total Cost Value", color=color.gray)
plot(plotType   =="Value" ?  totalMarketValue               : na , "Total Market Value", color=color.purple)
plot(plotType   =="Value" ?  totalMarketValueWithDividend   : na , "Total Market Value with Dividend", color=color.blue)

plot(plotType   == "Index" ? initialIndex                   : na, "Index", color=color.gray)
plot(plotType   == "Index" ? index                          : na, "Index", color=color.lime)
plot(plotType   == "Index" ? indexWithDividend              : na, "Index w/ Dividend", color=color.blue)

plot(marketPrice  ,      "Market Price"          , color = color.blue)
plot(avgEntryPrice  ,    "Avg Entry/Cost Price"  , color = color.yellow)
plot(stopLossPrice  ,    "Stop Loss Price"       , color = color.red)
plot(takeProfitPrice  ,  "Target Price"          , color = color.green)
// } END OF PLOT

// ALERT START HERE //////////////////////////////////////////////////////////// {
fAlert(_tickerid, _costPrice, _mktPrice, _stopLossPrice, _takeProfitPrice)=> 
    cond1 = ta.crossunder(  _mktPrice, _costPrice)
    cond2 = ta.crossunder(  _mktPrice, _stopLossPrice)
    cond3 = ta.crossover(   _mktPrice, _takeProfitPrice)
    if cond1
        alert("🟡 "+_tickerid + " has crossed under Cost Price" +"\n"+
             "Current Price : "     + str.tostring(_mktPrice        ,"#,##0.00") +"\n"+
             "Cost Price : "        + str.tostring(_costPrice       ,"#,##0.00") +"\n"+
             "Stop Loss Price : "   + str.tostring(_stopLossPrice   ,"#,##0.00")          
             , alert.freq_once_per_bar)
    else if cond2
        alert("❌ "+_tickerid + " has crossed under Stop Loss Price" +"\n"+
             "Current Price : "     + str.tostring(_mktPrice        ,"#,##0.00") +"\n"+
             "Cost Price : "        + str.tostring(_costPrice       ,"#,##0.00") +"\n"+
             "Stop Loss Price : "   + str.tostring(_stopLossPrice   ,"#,##0.00")              
             , alert.freq_once_per_bar)
    else if cond3
        alert("✅ "+_tickerid + " has reached Target Price " +"\n"+
             "Current Price : "     + str.tostring(_mktPrice        ,"#,##0.00") +"\n"+
             "Cost Price : "        + str.tostring(_costPrice       ,"#,##0.00") +"\n"+
             "Target Price : "      + str.tostring(_takeProfitPrice ,"#,##0.00")              
             , alert.freq_once_per_bar)

fAlert(assetTicker01, avgEntryPrice01, marketPrice01, stopLossPrice01, takeProfitPrice01)
fAlert(assetTicker02, avgEntryPrice02, marketPrice02, stopLossPrice02, takeProfitPrice02)
fAlert(assetTicker03, avgEntryPrice03, marketPrice03, stopLossPrice03, takeProfitPrice03)
fAlert(assetTicker04, avgEntryPrice04, marketPrice04, stopLossPrice04, takeProfitPrice04)
fAlert(assetTicker05, avgEntryPrice05, marketPrice05, stopLossPrice05, takeProfitPrice05)

fAlert(assetTicker06, avgEntryPrice06, marketPrice06, stopLossPrice06, takeProfitPrice06)
fAlert(assetTicker07, avgEntryPrice07, marketPrice07, stopLossPrice07, takeProfitPrice07)
fAlert(assetTicker08, avgEntryPrice08, marketPrice08, stopLossPrice08, takeProfitPrice08)
fAlert(assetTicker09, avgEntryPrice09, marketPrice09, stopLossPrice09, takeProfitPrice09)
fAlert(assetTicker10, avgEntryPrice10, marketPrice10, stopLossPrice10, takeProfitPrice10)
// } End of Alert

// TABLE START HERE //////////////////////////////////////////////////////////// {
// Function for table ---------------------------------------------------------
fBgColor(_value)=> 
    green   =   color.new(color.green, 50)
    red   =   color.new(color.red, 50)
    yellow   =   color.new(color.yellow, 50)
    var color bgColor =  na
    bgColor :=   _value > 0 ? green : _value < 0 ? red : yellow
    
// Table Customization --------------------------------------------------------
groupTable  =   "Table Customization"

isTableOn           =   input.bool(true, "Show Table?", group=groupTable)
tbTxtSizeOption     =   input.string("Normal", "Table Text Size", options =["Normal", "Small", "Tiny", "Auto", "Large"], group=groupTable)    

tbXLocOption        =   input.string("Right", "Location", options = ["Left","Center","Right"], inline="Table Location", group=groupTable)
tbYLocOption        =   input.string("Bottom", "", options = ["Bottom","Middle","Top"], inline="Table Location", group=groupTable)

// Table Color -----------------------------------------------------------------
tbBgColor           =   input.color(color.white, "Color BG/Frame/Border", inline ="Table Color", group=groupTable)  
tbFrameColor        =   input.color(color.black, "", inline ="Table Color", group=groupTable)  
tbBorderColor       =   input.color(color.black, "", inline ="Table Color", group=groupTable)  

// Header Row
tbHdTxtColor        =   input.color(color.white, "Header Color Text/BG", inline="Table Header", group=groupTable)  
tbHdBgColor         =   input.color(color.purple, "",    inline="Table Header",group=groupTable)  

// Constant --------------------------------------------------------------------
tbX = switch tbXLocOption 
    "Right" => "right"
    "Center" => "center"
    => "left"
    
tbY = switch tbYLocOption
    "Bottom" => "bottom"
    "Middle" => "middle"
    => "top"
tbLoc   =   tbY + "_" + tbX

tbTxtSize   = switch tbTxtSizeOption
    "Normal" => size.normal
    "Small" => size.small
    "Tiny"  => size.tiny
    "Auto"  => size.auto
    => size.large
// Function
fTable(table _tableName, int _rowNumber, string _symbol, float _entryPrice, float _mktPrice,  float _priceChangePcnt, float _dailyGainLoss, float _share, float _costVal, float _mktVal, float _cumDividend, float _totalCost,  float _expectedLossValue, int _initialCapital)=>
    GREEN   =   color.new(color.green, 50)
    RED     =   color.new(color.red,    50)
    YELLOW  =   color.new(color.yellow, 50)
    unRealizedVal = 0.0 
    unRealizedVal := _mktVal - _costVal
    unRealizedPcnt  =   0.0 
    unRealizedPcnt  := ((_mktVal - _costVal)/_costVal)*100
    boughtWeight    =   0.0
    boughtWeight    := _costVal / _totalCost *100
    portWeight  = 0.0 
    portWeight  := _costVal / _initialCapital *100 
    gainLossSymbol = _mktPrice < _entryPrice ? "🔻" : "🟢"

    table.cell(table_id = _tableName, column = 0 , row = _rowNumber, text = str.tostring(_rowNumber                     ), text_size = tbTxtSize)
    table.cell(table_id = _tableName, column = 1 , row = _rowNumber, text = str.tostring(_symbol                        ), text_size = tbTxtSize)
    table.cell(table_id = _tableName, column = 2 , row = _rowNumber, text = str.tostring(_share,             "#,##0.00"    ), text_size = tbTxtSize,    text_halign    = text.align_right)
    table.cell(table_id = _tableName, column = 3 , row = _rowNumber, text = str.tostring(_entryPrice,        "#,##0.00" ), text_size = tbTxtSize,       text_halign    = text.align_right)    
    table.cell(table_id = _tableName, column = 4 , row = _rowNumber, text = str.tostring(_mktPrice,          "#,##0.00" ), text_size = tbTxtSize,       text_halign    = text.align_right)
    table.cell(table_id = _tableName, column = 5 , row = _rowNumber, text = str.tostring(_priceChangePcnt,   "#,##0.00" )+gainLossSymbol, text_size = tbTxtSize, text_halign    = text.align_right, bgcolor=fBgColor(_priceChangePcnt)) 
    table.cell(table_id = _tableName, column = 6 , row = _rowNumber, text = str.tostring(_dailyGainLoss,     "#,##0"    ), text_size = tbTxtSize,       text_halign    = text.align_right,bgcolor=fBgColor(_dailyGainLoss)) 
    table.cell(table_id = _tableName, column = 7 , row = _rowNumber, text = str.tostring(_costVal,           "#,##0"    ), text_size = tbTxtSize,       text_halign    = text.align_right)
    table.cell(table_id = _tableName, column = 8 , row = _rowNumber, text = str.tostring(_mktVal,            "#,##0"    ), text_size = tbTxtSize,       text_halign    = text.align_right)
    table.cell(table_id = _tableName, column = 9 , row = _rowNumber, 
         text           = str.tostring(unRealizedVal,      "#,##0"    ) +       " (" +str.tostring(unRealizedPcnt,     "#,##0.0"  )+")", 
         text_size      = tbTxtSize,          text_halign    = text.align_right,          bgcolor        = fBgColor(unRealizedVal)) 
    table.cell(table_id = _tableName, column = 10, row = _rowNumber, 
         text =  str.tostring(_cumDividend,       "#,##0"    ) + " (" +              str.tostring(_cumDividend/_costVal*100, "#,##0")+"%" +")"
         , text_size = tbTxtSize)    
    table.cell(table_id = _tableName, column = 11, row = _rowNumber, text = str.tostring(boughtWeight,       "#,##0.0"  ), text_size = tbTxtSize)
    table.cell(table_id = _tableName, column = 12, row = _rowNumber, 
         text = str.tostring(_expectedLossValue, "#,###"), text_size = tbTxtSize)
    table.cell(table_id = _tableName, column = 13, row = _rowNumber, text = str.tostring(portWeight,         "#,##0.0"  ), text_size = tbTxtSize, bgcolor = fBgColor(portWeight) )     // Unrealized %       


// CREATE TABLE -------------------------------------------------------------------------
var tb = table(na)
if barstate.islast and isTableOn
    tb := table.new(tbLoc, 15, 22 , bgcolor=tbBgColor,          frame_color    = tbFrameColor,     frame_width=1,         border_color   = tbBorderColor,    border_width=1)
    table.cell(tb, 0,  0, "No"               ,      text_size = tbTxtSize, text_color=tbHdTxtColor, bgcolor=tbHdBgColor)
    table.cell(tb, 1,  0, "Symbol"           ,      text_size = tbTxtSize, text_color=tbHdTxtColor, bgcolor=tbHdBgColor)
    table.cell(tb, 2,  0, "Shares"           ,      text_size = tbTxtSize, text_color=tbHdTxtColor, bgcolor=tbHdBgColor)
    table.cell(tb, 3,  0, "Entry\n Price"     ,     text_size = tbTxtSize, text_color=tbHdTxtColor, bgcolor=tbHdBgColor)    
    table.cell(tb, 4,  0, "Market\n Price"     ,    text_size = tbTxtSize, text_color=tbHdTxtColor, bgcolor=tbHdBgColor)
    table.cell(tb, 5,  0, "Change,\n %"     ,       text_size = tbTxtSize, text_color=tbHdTxtColor, bgcolor=tbHdBgColor)
    table.cell(tb, 6,  0, "Gain/\nLoss"     ,       text_size = tbTxtSize, text_color=tbHdTxtColor, bgcolor=tbHdBgColor)
    table.cell(tb, 7,  0, "Total\n Cost"        ,   text_size = tbTxtSize, text_color=tbHdTxtColor, bgcolor=tbHdBgColor)
    table.cell(tb, 8,  0, "Market\n Value"       ,  text_size = tbTxtSize, text_color=tbHdTxtColor, bgcolor=tbHdBgColor)
    table.cell(tb, 9,  0, "Unrealized\n Value (%)", text_size = tbTxtSize, text_color=tbHdTxtColor, bgcolor=tbHdBgColor)

    table.cell(tb, 10, 0, "Cumulative \n Dividend", text_size = tbTxtSize, text_color=tbHdTxtColor, bgcolor=tbHdBgColor)
    table.cell(tb, 11, 0, "% Total \nBuy"        ,  text_size = tbTxtSize, text_color=tbHdTxtColor, bgcolor=tbHdBgColor)
    table.cell(tb, 12, 0, "Expected \nLoss"      ,  text_size = tbTxtSize, text_color=tbHdTxtColor, bgcolor=tbHdBgColor)
    table.cell(tb, 13, 0, "%Port"            ,      text_size = tbTxtSize, text_color=tbHdTxtColor, bgcolor=tbHdBgColor)

    fTable(tb, 01, assetTicker01, avgEntryPrice01, marketPrice01, dialyGainLossPercent01, dailyGainLossValue01, shareQty01, costValue01, marketValue01, dividend01, totalCostValue, expectedLossValue01, initialCapital)
    fTable(tb, 02, assetTicker02, avgEntryPrice02, marketPrice02, dialyGainLossPercent02, dailyGainLossValue02, shareQty02, costValue02, marketValue02, dividend02, totalCostValue, expectedLossValue02, initialCapital)
    fTable(tb, 03, assetTicker03, avgEntryPrice03, marketPrice03, dialyGainLossPercent03, dailyGainLossValue03, shareQty03, costValue03, marketValue03, dividend03, totalCostValue, expectedLossValue03, initialCapital)
    fTable(tb, 04, assetTicker04, avgEntryPrice04, marketPrice04, dialyGainLossPercent04, dailyGainLossValue04, shareQty04, costValue04, marketValue04, dividend04, totalCostValue, expectedLossValue04, initialCapital)
    fTable(tb, 05, assetTicker05, avgEntryPrice05, marketPrice05, dialyGainLossPercent05, dailyGainLossValue05, shareQty05, costValue05, marketValue05, dividend05, totalCostValue, expectedLossValue05, initialCapital)
    fTable(tb, 06, assetTicker06, avgEntryPrice06, marketPrice06, dialyGainLossPercent06, dailyGainLossValue06, shareQty06, costValue06, marketValue06, dividend06, totalCostValue, expectedLossValue06, initialCapital)
    fTable(tb, 07, assetTicker07, avgEntryPrice07, marketPrice07, dialyGainLossPercent07, dailyGainLossValue07, shareQty07, costValue07, marketValue07, dividend07, totalCostValue, expectedLossValue07, initialCapital)
    fTable(tb, 08, assetTicker08, avgEntryPrice08, marketPrice08, dialyGainLossPercent08, dailyGainLossValue08, shareQty08, costValue08, marketValue08, dividend08, totalCostValue, expectedLossValue08, initialCapital)
    fTable(tb, 09, assetTicker09, avgEntryPrice09, marketPrice09, dialyGainLossPercent09, dailyGainLossValue09, shareQty09, costValue09, marketValue09, dividend09, totalCostValue, expectedLossValue09, initialCapital)
    fTable(tb, 10, assetTicker10, avgEntryPrice10, marketPrice10, dialyGainLossPercent10, dailyGainLossValue10, shareQty10, costValue10, marketValue10, dividend10, totalCostValue, expectedLossValue10, initialCapital)

    table.cell(tb, 6, 21,    str.tostring(totalDailyGainLossValue   ,"#,##0"), text_size=tbTxtSize, bgcolor=fBgColor(totalGainLossValue))      
    table.cell(tb, 7, 21,    str.tostring(totalCostValue            ,"#,##0"), text_size=tbTxtSize)   
    table.cell(tb, 8, 21,    str.tostring(totalMarketValue          ,"#,##0"), text_size=tbTxtSize)      
    table.cell(tb, 9, 21,    str.tostring(totalMarketValue-totalCostValue              ,"#,##0") +         " ("+str.tostring(((totalMarketValue/totalCostValue)-1)*100    , "#,##0.0")+"%"+")"
         , text_size=tbTxtSize,         bgcolor=fBgColor(totalMarketValue-totalCostValue)) 
    table.cell(tb, 10, 21,    str.tostring(totalDividend         ,"#,##0")+"("         +str.tostring(totalDividend/totalCostValue*100, "#,##0")+")"
         , text_size=tbTxtSize)     
    table.cell(tb, 12, 21,   str.tostring(totalExpectedLoss         ,"#,##0")         + " ("+str.tostring(totalExpectedLoss/totalCostValue*100,"#,##0")+"%)"
         , text_size=tbTxtSize)     
    table.cell(tb, 13, 21,   str.tostring(totalCostValue/initialCapital*100         ,"#,##0"), text_size=tbTxtSize)     

// } END OF TABLE

// LABEL AND LINE START HERE /////////////////////////////////////////////////// {
groupLabel  =   "Label Customization"
isLabelOn   =   input.bool(true, "Show label?", group=groupLabel)
lbColor     =   input.color(color.green, "Color BG/Text", inline="Label Color",group=groupLabel) 
lbTxtColor  =   input.color(color.white, "", inline="Label Color",group=groupLabel) 

lbTxtSizeOption     =   input.string("Normal", "Text Size", options =["Normal", "Small", "Tiny", "Auto", "Large"], group=groupLabel) 

yValueAxis = (plotType == "Index" ? index : plotType == "Value" ? totalMarketValue : marketPrice)

lbTxtSize   = switch lbTxtSizeOption
    "Normal" => size.normal
    "Small" => size.small
    "Tiny"  => size.tiny
    "Large" => size.large
    => size.auto

fCreateLabel(string _id, bool _isOn, string _symbol, float _share, float _cost, int _entryDate,   float _y)=> 
    int locationMultiplier = switch _id 
        "01" => 10
        "02" => 20
        "03" => 30
        "04" => 40
        "05" => 50
        "06" => 60
        "07" => 70
        "08" => 80
        "09" => 90
        => 100
    yLoc = _y*(1+(locationMultiplier/100))
    if _isOn and time >= _entryDate and not (time >= _entryDate)[1]
        label.new(time, yLoc, 
             text = _id + " : " +_symbol + " : "
             +"Share :"         + str.tostring(_share,      "#,##0") + " / "
             +"Total Cost :"      + str.tostring(_cost,       "#,##0") , 
             xloc   =xloc.bar_time, 
             color  =lbColor, 
             size   =lbTxtSize, 
             style  =label.style_label_right,
             textcolor = lbTxtColor, 
             textalign = text.align_left)
             

if isLabelOn 
    fCreateLabel("01",isPositionActive01, assetTicker01, shareQty01, costValue01,  entryDate01, yValueAxis)
    fCreateLabel("02",isPositionActive02, assetTicker02, shareQty02, costValue02,  entryDate02, yValueAxis)
    fCreateLabel("03",isPositionActive03, assetTicker03, shareQty03, costValue03,  entryDate03, yValueAxis)
    fCreateLabel("04",isPositionActive04, assetTicker04, shareQty04, costValue04,  entryDate04, yValueAxis)
    fCreateLabel("05",isPositionActive05, assetTicker05, shareQty05, costValue05,  entryDate05, yValueAxis)

    fCreateLabel("06",isPositionActive06, assetTicker06, shareQty06, costValue06,  entryDate06, yValueAxis)
    fCreateLabel("07",isPositionActive07, assetTicker07, shareQty07, costValue07,  entryDate07, yValueAxis)
    fCreateLabel("08",isPositionActive08, assetTicker08, shareQty08, costValue08,  entryDate08, yValueAxis)
    fCreateLabel("09",isPositionActive09, assetTicker09, shareQty09, costValue09,  entryDate09, yValueAxis)
    fCreateLabel("10",isPositionActive10, assetTicker10, shareQty10, costValue10,  entryDate10, yValueAxis)
    
// } END of LABEL

// FORTFOLIO ANALYSIS START HERE /////////////////////////////////////////////// {------------------------------------------------------------------

// } END of ANALYSIS