LibIndicadoresUteis
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Fabricio_Guima/GuimaIndicadoresUteis
//@version=5
// @description Collection of useful indicators. This collection does not do any type of plotting on the graph, as the methods implemented can and should be used to get the return of mathematical formulas, in a way that speeds up the development of new scripts. The current version contains methods for stochastic return, slow stochastic, IFR, leverage calculation for B3 futures market, leverage calculation for B3 stock market, bollinger bands and the range of change.
library("LibIndicadoresUteis")

// @function Returns the value of stochastic
// @param PeriodoEstocastico Period for calculation basis
// @returns Float with the stochastic value of the period
export estocastico(int PeriodoEstocastico) =>
	var float d				= 0.0
	lowest                  = ta.lowest(PeriodoEstocastico)
	highest                 = ta.highest(PeriodoEstocastico)
	HN                      = highest - lowest
	k                       = 100 * (close - lowest) / HN // Valor do estocástico

// @function Returns the value of slow stochastic
// @param PeriodoEstocastico Stochastic period for calculation basis
// @param PeriodoMedia Average period for calculation basis
// @returns Float with the value of the slow stochastic of the period
export estocasticoLento(int PeriodoEstocastico, int PeriodoMedia) =>
	var float dLento		= 0.0
	stc 					= estocastico(PeriodoEstocastico)
	dLento                  := ta.sma(stc, PeriodoMedia) // Estocastico Lento

// @function Returns the value of the RSI/IFR Poisoned of Guima
// @param PeriodoIFR RSI/IFR period for calculation basis
// @param OrigemIFR Source of RSI/IFR for calculation basis
// @returns Float with the RSI/IFR value for the period
export ifrInvenenado(int PeriodoIFR, float OrigemIFR) =>
	up                      = ta.rma(math.max(ta.change(OrigemIFR), 0), PeriodoIFR)
	down                    = ta.rma(-math.min(ta.change(OrigemIFR), 0), PeriodoIFR)
	rsi                     = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))

// @function Returns the number of contracts to work based on margin
// @param margem Margin for contract unit
// @param alavancagemMaxima Maximum number of contracts to work
// @returns Integer with the number of contracts suggested for trading
export calculoAlavancagemFuturos(float margem, int alavancagemMaxima) =>
	contratos = 0
	contratos := int(strategy.equity/margem)
	if(strategy.equity < margem and strategy.equity > 10/100*strategy.equity)
		contratos := 1
	contratos := contratos > alavancagemMaxima ? alavancagemMaxima : contratos

// @function Returns the number of batches to work based on the margin
// @param alavancagemMaxima Maximum number of batches to work
// @returns Integer with the amount of lots suggested for trading
export calculoAlavancagemAcoes(int alavancagemMaxima) =>
	contratos = 0
	contratos := (math.round(math.round(strategy.equity/close)/100))*100
	if(contratos < 100)
		contratos := 100
	contratos := contratos > alavancagemMaxima ? alavancagemMaxima : contratos

// @function Returns the value of bollinger bands
// @param periodoBB Period of bollinger bands for calculation basis
// @param origemBB Origin of bollinger bands for calculation basis
// @param desvioPadrao Standard Deviation of bollinger bands for calculation basis
// @returns Two-position array with upper and lower band values ​​respectively
export bandasBollinger(int periodoBB, float origemBB, float desvioPadrao) =>
	length = periodoBB
	src = origemBB
	mult = desvioPadrao
	basis = ta.sma(src, length)
	dev = mult * ta.stdev(src, length)
	upper = basis + dev
	lower = basis - dev
	bandas = array.new_float(2)
	array.set(bandas, 0, upper)
	array.set(bandas, 1, lower)
	bandas

// @function Returns the value of Rate Of Change
// @param periodoROC Period for calculation basis
// @param origemROC Source of calculation basis
// @returns Float with the value of Rate Of Change
export theRoc(int periodoROC, float origemROC) =>
	length = periodoROC
	source = origemROC
	roc = 100 * (source - source[length])/source[length]