From 04bc97b2701cf42dd7d65e92c4bf4fd71e3e8662 Mon Sep 17 00:00:00 2001 From: Vinicius Date: Sat, 26 Jun 2021 10:37:10 -0300 Subject: [PATCH 1/2] add reverse cummulative returns metrics and plot --- turingquant/metrics.py | 22 +++++++++++++++++++++- turingquant/plot_metrics.py | 20 +++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/turingquant/metrics.py b/turingquant/metrics.py index b1e94ff..86577b4 100644 --- a/turingquant/metrics.py +++ b/turingquant/metrics.py @@ -332,4 +332,24 @@ 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 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() From 7d7fa084bde5a4f7aea96c4ef645a485a4433831 Mon Sep 17 00:00:00 2001 From: Vinicius Date: Sat, 26 Jun 2021 10:46:32 -0300 Subject: [PATCH 2/2] fix return calculation --- turingquant/metrics.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/turingquant/metrics.py b/turingquant/metrics.py index 86577b4..37aeeba 100644 --- a/turingquant/metrics.py +++ b/turingquant/metrics.py @@ -352,4 +352,5 @@ def reverse_cummulative_returns(returns, log_returns=True): else: returns = 1 + returns cum_returns = returns.iloc[::-1].cumprod().iloc[::-1] - return cum_returns + + return cum_returns - 1