Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
146 lines (123 loc) · 3.97 KB
/
Copy pathmain.py
File metadata and controls
146 lines (123 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from datetime import datetime
import dash_bootstrap_components as dbc
import pandas as pd
import plotly.express as px
from dash import Dash, Input, Output, State, dcc, html
from dateutil.relativedelta import relativedelta
from pandas.tseries.offsets import DateOffset
from src import financial
from src.selic import get_selic
app = Dash(__name__, external_stylesheets=[dbc.themes.SANDSTONE])
investiment_type = html.Div(
[
dbc.Label("Tipo de investimento"),
dbc.RadioItems(
options=[
{"label": "Prefixado", "value": "prefixado"},
{"label": "Tesouro Selic", "value": "selic"},
],
value="prefixado",
id="investiment_type",
),
],
className="mb-3",
)
principal_input = html.Div(
[
dbc.Label("Montante inicial"),
dbc.Input(id="principal", value=1000, type="number"),
],
className="mb-3",
)
rate_input = html.Div(
[
dbc.Label("Taxa anual (%)", id="yearly_rate_label"),
dbc.Input(id="yearly_rate", value=10, type="number", readonly="readOnly"),
],
className="mb-3",
id="rate_input",
)
date_input = html.Div(
[
dbc.Label("Vencimento"),
html.Br(),
dcc.DatePickerSingle(
id="date",
display_format="Y/MM/DD",
date=datetime.now().date() + relativedelta(years=1, days=1),
),
],
className="mb-3",
)
investiment_form = dbc.Row(
[
dbc.Col(investiment_type),
dbc.Col(principal_input),
dbc.Col(rate_input),
dbc.Col(date_input),
]
)
root_layout = [
html.H1("Calculadora de investimento"),
investiment_form,
dbc.Button("Simular", id="simulate_btn", color="primary", className="me-1"),
dcc.Graph(id="time-series-chart"),
]
app.layout = html.Div(
dbc.Row(
dbc.Col(
root_layout,
width=10,
),
justify="center",
)
)
def convert_yearly_to_monthly_rate(yearly_rate):
monthly_rate = (1 + yearly_rate) ** (1 / 12) - 1
return monthly_rate
def get_month_difference(end_date):
start_datetime = datetime.now()
end_datetime = datetime.strptime(end_date, "%Y-%m-%d")
difference = relativedelta(end_datetime, start_datetime)
return difference.years * 12 + difference.months
@app.callback(
Output("time-series-chart", "figure"),
Input("simulate_btn", "n_clicks"),
State("investiment_type", "value"),
State("principal", "value"),
State("yearly_rate", "value"),
State("date", "date"),
)
def display_simulate(_, investiment_type, principal, yearly_rate, date):
current_time = datetime.now()
due_in_months = get_month_difference(date)
if investiment_type == "prefixado":
monthly_rate = convert_yearly_to_monthly_rate(yearly_rate / 100)
else:
expected_selic = get_selic(current_time - relativedelta(months=1))[-1]["value"]
monthly_rate = convert_yearly_to_monthly_rate(expected_selic / 100)
ts = financial.calculate_compound_interest(principal, monthly_rate, due_in_months)
df = pd.DataFrame(ts, columns=["amount"])
df["month"] = pd.date_range(
pd.Timestamp.now().date(), periods=due_in_months + 1, freq=DateOffset(months=1)
)
df["amount"] = df["amount"].apply(lambda x: round(x, 2))
fig = px.line(df, x="month", y="amount")
fig.update_layout(
title="Evolução do investimento",
xaxis_title="Período",
yaxis_title="Montante",
)
return fig
@app.callback(
Output(component_id="yearly_rate_label", component_property="children"),
Output(component_id="yearly_rate", component_property="value"),
Input(component_id="investiment_type", component_property="value"),
)
def toggle_non_editable_rate_input(investiment_type):
selic_value = get_selic(datetime.now() - relativedelta(months=1))[-1]["value"]
if investiment_type == "selic":
return "SELIC", selic_value
else:
return "Taxa anual (%)", 10
app.run_server(debug=True)