diff --git a/turingquant/metrics.py b/turingquant/metrics.py index b1e94ff..37aeeba 100644 --- a/turingquant/metrics.py +++ b/turingquant/metrics.py @@ -332,4 +332,25 @@ def value_at_risk(returns, confidance_level = 0.95, window = 1, method = 'varian var = var * np.sqrt(window) return var - \ No newline at end of file + +def reverse_cummulative_returns(returns, log_returns=True): + """ + Calcula o retorno acumulativo inverso. + + Calcula a quantidade de capital que alguém teria se tivesse investido + uma unidade monetária em cada instante de tempo correspondente. + + Args: + returns (pd.Series): série de retornos para a qual será calculado o mar ratio. + log_returns (float): indica se o retorno utilizado é logaritmico (True, default) ou simples (False) + Returns: + cum_returns (pd.Series): retorno simples do instante `t` até o presente. + """ + if log_returns is True: + cum_returns = returns.iloc[::-1].cumsum().iloc[::-1] + cum_returns = returns.apply(np.exp) + else: + returns = 1 + returns + cum_returns = returns.iloc[::-1].cumprod().iloc[::-1] + + return cum_returns - 1 diff --git a/turingquant/plot_metrics.py b/turingquant/plot_metrics.py index ac4eedd..0ffd81b 100644 --- a/turingquant/plot_metrics.py +++ b/turingquant/plot_metrics.py @@ -261,4 +261,22 @@ def plot_allocation(dictionary): labels = list(dictionary.keys()) values = list(dictionary.values()) fig = px.pie(values=values, names=labels) - fig.show() \ No newline at end of file + fig.show() + +def plot_reverse_cummulative_returns(returns, log_returns=True): + """ + Plota o retorno acumulativo inverso. + + Calcula a quantidade de capital que alguém teria se tivesse investido + uma unidade monetária em cada instante de tempo correspondente. + + Args: + returns (pd.Series): série de retornos para a qual será calculado o mar ratio. + log_returns (float): indica se o retorno utilizado é logaritmico (True, default) ou simples (False) + """ + cum_returns = reverse_cummulative_returns(returns, log_returns) + fig = px.line(cum_returns, x=cum_returns.index, + y=cum_returns.name, title='Reverse Cummulative Returns') + fig.update_xaxes(title_text='Tempo') + fig.update_yaxes(title_text='Retorno Simples') + fig.show()