Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atualiza gráficos para usar plotly #25

Merged
merged 3 commits into from
Oct 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions turingquant/benchmark.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px
from datetime import datetime
from matplotlib.ticker import PercentFormatter
from pandas_datareader import data


Expand All @@ -20,11 +19,16 @@ def benchmark(ticker, start: datetime, end: datetime, source='yahoo', plot=True)
"""

asset = data.DataReader(ticker, data_source=source, start=start, end=end)
returns_daily = asset['Close'].pct_change().fillna(0)
cumulative = pd.DataFrame.cumprod(1 + returns_daily) - 1
asset['Returns'] = asset['Close'].pct_change().fillna(0)
asset['Cumulative Returns'] = pd.DataFrame.cumprod(1 + asset['Returns']) - 1

if plot:
cumulative.plot()
return cumulative
fig = px.line(asset, x=asset.index, y='Cumulative Returns', title='Retorno cumulativo ' + ticker)
fig.update_xaxes(title_text='Tempo')
fig.update_yaxes(title_text='Retorno cumulativo')
fig.show()

return asset['Cumulative Returns']


def benchmark_ibov(start: datetime, end: datetime, source='yahoo', plot=True):
Expand Down