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

//@version=5
indicator( "Manage trade", "", true )

// --------------------------------------- INPUTS ------------------------------------------- //
// Primeiro preço de entrada
currentPrice    = input.float( 0.0, "Current price.........", inline = "entry" )            // First entry price
// Total inicial de moedas
coins           = input.float( 0,   "Coins",                  inline = "entry" )            // Initial total coins
// Simular preço segunda entrada
simulateEntry   = input.float( 0.0, "Simulate entry.......",  inline = "simulateEntry" )    // Simulate second entry price
// Simular nova quantidade de moedas
simulateCoins   = input.float( 0,   "Coins",                  inline = "simulateEntry" )    // Simulate new amount of coins
// Simular preço parcial 
simulatePartial = input.float( 5.0, "Simulate partial %",     inline = "simulatePartial" )  // Simulate partial price
// Simular quantidade parcial de moedas
partialCoins    = input.float( 0,    "Coins",                 inline = "simulatePartial" )  // Simulate partial amount of coins
// Montante, dinheiro ou moedas
valueConversion = input.float( 0.0, "Value $",      inline = "conversion", group = "currency conversion $ ⮕ ₿" )  // Amount, money or coins
// Preço estimado
targetPrice     = input.float( 0.0, "Target price", inline = "conversion", group = "currency conversion $ ⮕ ₿" )  // Estimated price
// Colors
colorCurrPrice  = input.color( color.new(#e92907, 37),   "Current price", inline = "colors" )
colorNewEntry   = input.color( color.new(#00bcd4, 40),   "New entry",     inline = "colors" )
colorAverage    = input.color( color.new(#ff9800, 40),   "New avg price", inline = "colors" )
colorPartial    = input.color( color.new(#ffeb3b, 40),   "Partial price", inline = "colors" )
colorText       = input.color( color.rgb(247, 240, 240), "Text",          inline = "colors" )
//
iniLine = bar_index+10
endLine = iniLine+10
simbol  = syminfo.ticker

// ------------------------------------ CALCULATIONS ---------------------------------------- //
// Conversão de dinheiro para moeda, retorna total de moedas
amountForCoins  = valueConversion > 0 ? str.tonumber( str.format( "{0,number,#.#####}", (valueConversion/targetPrice) )) : 0    // Money to currency conversion, return total coins
// Total investido, retorna valor investido
initialAmount   = coins*currentPrice                                              // Get total invested, returns invested amount
// Total investimento simulado, retorna valor necessário para investir
simulatedAmount = simulateCoins*simulateEntry                                     // Get total invested simulated, returns amount needed to invest
// Soma total do valor investido+simulado
sumOfAmount     = initialAmount+simulatedAmount                                   // Sum invested amount and simulated amount
// Soma total de moedas
sumOfCoins      = coins+simulateCoins                                             // Sum invested coins and simulated coins
// Cálculo ganho ou perda sobre o preço médio atual
profitLoss      = str.format( "{0,number,#.###}", (coins*(close-currentPrice)) )  // Investment variation
// Atualiza o montante com lucro ou perda
updatedValue    = str.format( "{0,number,#.###}", (str.tonumber(profitLoss)+initialAmount) )  // Update amount with profit or loss

// ------------------------------------- FUNCTIONS ------------------------------------------ //
// ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲

// ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼ 
plotTable( _partialProfit ) =>

    var table _tbl = table.new(position.bottom_right, 7, 2, border_width = 1)
    _colorText     = (str.tonumber(profitLoss) < 0) ? color.rgb(204, 20, 20) : color.rgb(30, 128, 17) 
    table.cell_set_text_font_family( _tbl, 2, 1, font.family_monospace )

    if (barstate.islast)
        table.cell( _tbl, 0, 0, "Simbol",         bgcolor = color.new(#d4c78c, 20), width = 6, height = 4 )
        table.cell( _tbl, 1, 0, "Current price",  bgcolor = color.new(#d4c78c, 20), width = 6, height = 4 )
        table.cell( _tbl, 2, 0, "Coins",          bgcolor = color.new(#d4c78c, 20), width = 4, height = 4 )
        table.cell( _tbl, 3, 0, "Profit / Loss",  bgcolor = color.new(#d4c78c, 20), width = 5, height = 4 )
        table.cell( _tbl, 4, 0, "Updated value",  bgcolor = color.new(#d4c78c, 20), width = 6, height = 4 )
        table.cell( _tbl, 5, 0, "Partial profit", bgcolor = color.new(#eeb76f, 20), width = 5, height = 4 )
        table.cell( _tbl, 6, 0, "$ ⮕ ₿",         bgcolor = color.new(#81ebe2, 20), width = 5, height = 4 )

        table.cell( _tbl, 0, 1, str.tostring(simbol),         bgcolor = colorCurrPrice, width = 5, height = 4 , text_color = color.white ) 
        table.cell( _tbl, 1, 1, str.tostring(currentPrice),   bgcolor = colorCurrPrice, width = 5, height = 4 , text_color = color.white )
        table.cell( _tbl, 2, 1, str.tostring(coins),          bgcolor = #d6d6d6,      width = 5, height = 4 )
        table.cell( _tbl, 3, 1, str.tostring(profitLoss),     bgcolor = #d6d6d6,      width = 5, height = 4, text_color = _colorText )
        table.cell( _tbl, 4, 1, str.tostring(updatedValue),   bgcolor = #d6d6d6,      width = 5, height = 4 )
        table.cell( _tbl, 5, 1, str.tostring(_partialProfit), bgcolor = colorPartial,   width = 5, height = 4 )
        table.cell( _tbl, 6, 1, str.tostring(amountForCoins), bgcolor = #22b5da,      width = 5, height = 4 )

// ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲

// ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼ 
plotLabel( string _style, string _text, color _color, color _colorText, float _price ) =>

    var label _lb = na
    label.delete(_lb) 
    _lb := label.new( endLine, _price, _text )

    label.set_text_font_family( _lb, font.family_monospace )
    label.set_textalign( _lb, text.align_left  )
    label.set_color(     _lb, _color )
    label.set_textcolor( _lb, _colorText )
    label.set_style(     _lb, _style )

// ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲

// ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
plotLines( _price, _color ) =>

    var line _ln = na
    line.delete(_ln)
    _ln := line.new( endLine, _price, iniLine, _price )

    line.set_color( _ln, _color )
    line.set_style( _ln, line.style_solid )
    line.set_width( _ln, 2 )
 
// ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲

// ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
startPlots( _strPrice, _price, _color, _colorText ) =>

    plotLines( _price, _color )
    plotLabel( label.style_label_left, _strPrice, _color, _colorText, _price )

// ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲

// ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
averagePrice( _financial, _coins ) =>

    _value = _financial/_coins
    _avg   = str.tonumber( str.format( "{0,number,#.#########}", _value ) )

// ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲

// ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
// ----------------------------------------- PLOTS ------------------------------------------ //
// Novo preço médio
averagePrice  = averagePrice( sumOfAmount, sumOfCoins )                                         // New average price
// Simular preço parcial
partialPrice  = averagePrice+((simulatePartial*averagePrice)/100)                               // After new entry, simulate partial
// Lucro parcial
partialProfit = str.format( "{0,number,#.##}", nz( partialCoins*(partialPrice-averagePrice) ))  // Calculate partial profit

if simulateCoins > 0
    startPlots( "AVG PRICE: "+str.tostring(averagePrice),  averagePrice,  colorAverage,  colorText )
    startPlots( "NEW ENTRY: "+str.tostring(simulateEntry), simulateEntry, colorNewEntry, colorText )

if partialCoins > 0
    startPlots( "PARTIAL: "+str.tostring(partialPrice), partialPrice, colorPartial, colorText )

if coins > 0 or valueConversion > 0 
    startPlots( "CURRENT PRICE: "+str.tostring(currentPrice), currentPrice, colorCurrPrice, colorText )
    plotTable( partialProfit )


