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

//@version=5

// @description TODO: add library description here
library("Lukashere")

// @function TODO: add function description here
// @param x TODO: add parameter x description here
// @returns TODO: add what function returns
export fun(float x) =>
	//TODO : add function body and return value here
	x


export Percentual2nu(float a , float b) =>
	vp_result = a < b ? ((b - a)/b) * 100 : ((a - b)/a) * 100
	vp_result
	
	

//iNDICADORES fUNCTION

// @function Gets a Moving Average based on type
// @param int _length The MA period
// @param string _maType The type of MA
// @returns A moving average with the given parameters
// ex: basis = lib.getMA(20,'EMA')
export getMA(simple int _length, string _maType , float _src) =>
    switch _maType
        "EMA" => ta.ema(_src, _length)
        "SMA" => ta.sma(_src, _length)
        "HMA" => ta.hma(_src, _length)
        "WMA" => ta.wma(_src, _length)
        "VWMA" => ta.vwma(_src, _length)
        "VWAP" => ta.vwap
        => 
            e1 = ta.ema(_src, _length)
            e2 = ta.ema(e1, _length)
            e3 = ta.ema(e2, _length)
            3 * (e1 - e2) + e3
       
// ex: [atrup, atrdown] = lib.getStopAtr(1 , 'Max-Min' , 0.61)
export getStopAtr(simple int _length, string _Type , float _mult) =>
    atr = ta.atr(_length)
    atrup   = _Type == "Close" ? close + atr * _mult : high + atr * _mult
    atrdown = _Type == "Close" ? close - atr * _mult : low - atr * _mult
    [atrup, atrdown]



//Example use array = lib.SymbolsTopMarketcap()   array.get(array, 0) = BINANCE:BTCUSDT  
export SymbolsTopMarketcap() =>
    arrayExchangeSymbol = array.new_string(31)
    array.set(arrayExchangeSymbol, 0, "BINANCE:BTCUSDT")
    array.set(arrayExchangeSymbol, 1, "BINANCE:ETHUSDT")
    array.set(arrayExchangeSymbol, 2, "BINANCE:BNBUSDT")
    array.set(arrayExchangeSymbol, 3, "BINANCE:XRPUSDT")
    array.set(arrayExchangeSymbol, 4, "BINANCE:LUNAUSDT")
    array.set(arrayExchangeSymbol, 5, "BINANCE:ADAUSDT")
    array.set(arrayExchangeSymbol, 6, "BINANCE:SOLUSDT")
    array.set(arrayExchangeSymbol, 7, "BINANCE:AVAXUSDT")
    array.set(arrayExchangeSymbol, 8, "BINANCE:DOTUSDT")
    array.set(arrayExchangeSymbol, 9, "BINANCE:DOGEUSDT")   
    array.set(arrayExchangeSymbol, 10, "BINANCE:SHIBUSDT")
    array.set(arrayExchangeSymbol, 11, "BINANCE:MATICUSDT")
    array.set(arrayExchangeSymbol, 12, "BINANCE:LTCUSDT")
    array.set(arrayExchangeSymbol, 13, "BINANCE:ATOMUSDT")
    array.set(arrayExchangeSymbol, 14, "BINANCE:NEARUSDT")
    array.set(arrayExchangeSymbol, 15, "BINANCE:LINKUSDT")
    array.set(arrayExchangeSymbol, 16, "BINANCE:BCHUSDT")
    array.set(arrayExchangeSymbol, 17, "BINANCE:UNIUSDT")
    array.set(arrayExchangeSymbol, 18, "BINANCE:TRXUSDT")
    array.set(arrayExchangeSymbol, 19, "BINANCE:ETCUSDT")    
    array.set(arrayExchangeSymbol, 20, "BINANCE:FTTUSDT")
    array.set(arrayExchangeSymbol, 21, "BINANCE:ALGOUSDT")
    array.set(arrayExchangeSymbol, 22, "BINANCE:XLMUSDT")
    array.set(arrayExchangeSymbol, 23, "BINANCE:MANAUSDT")
    array.set(arrayExchangeSymbol, 24, "BINANCE:HBARUSDT")
    array.set(arrayExchangeSymbol, 25, "BINANCE:EGLDUSDT")	
	array.set(arrayExchangeSymbol, 26, "BINANCE:ICPUSDT")
    array.set(arrayExchangeSymbol, 27, "BINANCE:SANDUSDT")
    array.set(arrayExchangeSymbol, 28, "BINANCE:XMRUSDT")
    array.set(arrayExchangeSymbol, 29, "BINANCE:WAVESUSDT")
    array.set(arrayExchangeSymbol, 30, "BINANCE:VETUSDT")
	
	arraySymbol = array.new_string(array.size(arrayExchangeSymbol))
	for i = 0 to array.size(arrayExchangeSymbol)		
		array.set(arraySymbol, i, str.replace_all(array.get(arrayExchangeSymbol, i), 'BINANCE:', '') )
		
    [arrayExchangeSymbol , arraySymbol]

toWhole(float number) =>
    _return = number < 1.0 ? number / syminfo.mintick / (10 / syminfo.pointvalue) : number
    _return := number >= 1.0 and number <= 100.0 ? _return * 100 : _return
    
//--------------POSITION SIZE------------------
getPositionSize(EntradaPrice, Sl , riskPerTrade , accountBalance) =>
    longDiffSL = math.abs(EntradaPrice - Sl)
    positionValue = accountBalance * (riskPerTrade / 100) / (longDiffSL / EntradaPrice)
    positionSize = positionValue / EntradaPrice

// Forex
getPositionSizeForex(stopLossSizePoints , riskPerTrade , accountBalance ,conversionRate) =>
    riskAmount   = accountBalance * (riskPerTrade / 100) * conversionRate
    riskPerPoint = stopLossSizePoints * syminfo.pointvalue
    positionSize = riskAmount / riskPerPoint / syminfo.mintick 
    math.round(positionSize)
    
// @function Gets position Size
// @param float accountBalance    - balance for account
         // @param string typeOrder    - Long OR Short
// @param string typePosition - 3 type (Tamanho Risco , Contrato Fixo , Value USD)
// @param float inputPrice    - input for price
// @param float StopLoss      - input for stop
// @param riskPerTrade        - risk for trade %
// ex: basis = tradePositionSize := lib.positionSize(accountBalance, GerenciarPosicao , EntradaPrice , shortStop , riskPerTrade , conversionRate)
export positionSize(float accountBalance , string typePosition , float inputPrice , float StopLoss = 0 , float riskPerTrade = 0 , float conversionRate = 1.0) =>
    float positionSize = 0
    if syminfo.type == 'crypto'
        if(syminfo.currency == 'USDT' or syminfo.currency == 'USD' or syminfo.currency == 'BUSD' or syminfo.currency == 'USDC')
            positionSize := typePosition == 'Tamanho Risco' ? getPositionSize(inputPrice, StopLoss , riskPerTrade , accountBalance) :
             typePosition == 'Contrato Fixo'                ? riskPerTrade :
             typePosition == 'Value USD'                    ? (accountBalance / strategy.initial_capital) * (riskPerTrade / inputPrice) : 0
    if syminfo.type == 'forex' or syminfo.type == 'cfd'
        if(syminfo.currency == 'USD')
            positionSize := typePosition == 'Tamanho Risco' ? 
             getPositionSizeForex(toWhole(math.abs(inputPrice - StopLoss))* (syminfo.type == 'cfd' ?  syminfo.mintick * 10000 : 10) , riskPerTrade , accountBalance , conversionRate):
             typePosition == 'Contrato Fixo'                ? riskPerTrade :
             typePosition == 'Value USD'                    ? math.round( (accountBalance / strategy.initial_capital) * (riskPerTrade / inputPrice) * conversionRate)  : 0
    positionSize




    