Py.Cafe

Angella062022/

stock data analysis

Malawi stock data analysis

DocsPricing
  • app.py
  • merged_stock_data_iso_format.csv
  • requirements.txt
app.py
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
import vizro.plotly.express as px
from vizro import Vizro
import vizro.models as vm
import pandas as pd

# Read and process data
df = pd.read_csv('merged_stock_data_iso_format.csv')
df['date'] = pd.to_datetime(df['date'], format='mixed', dayfirst=True)
df = df.sort_values('date')

# Define the date range selector buttons
fin_buttons = [
    {'count': 48, 'label': '48HR', 'step': "hour", 'stepmode': "todate"},
    {'count': 7, 'label': "1WTD", 'step': "day", 'stepmode': "todate"},
    {'count': 1, 'label': "1M", 'step': "month", 'stepmode': "backward"},
    {'count': 3, 'label': "3M", 'step': "month", 'stepmode': "backward"},
    {'count': 6, 'label': "6M", 'step': "month", 'stepmode': "backward"},
    {'count': 1, 'label': "YTD", 'step': "year", 'stepmode': "todate"},
    {"step": "all"}
]

# Create the figure and apply layout changes
fig = px.line(
    data_frame=df,
    x='date',
    y='adjusted_close',
    color='dataset_name',
    title='Stock Prices Over Time'
)

fig.update_xaxes(
    title_text = 'Date',
    autotickangles = [45, 60, 90],
    dtick="M1",
    tickformat="%b\n%Y"
    )

fig.update_yaxes(
    title_text = 'Adjusted Closing Price (MWK)'
)

fig.update_layout(
    showlegend = False
)

fig.update_layout(
    xaxis=dict(
        rangeselector=dict(buttons=fin_buttons),
        type="date"
    )
)

# Define the Graph component
graph = vm.Graph(
    id="stock_graph",
    figure=fig
)

# Define the page with filters
page = vm.Page(
    title="Stock prices trend lines over time",
    id="stock_prices_trend_page",
    components=[graph],
    controls=[
        vm.Filter(
            column="dataset_name",
            selector=vm.Dropdown(title='Stock name:'),
            targets=["stock_graph"]
        ),
        vm.Filter(
            column="date",
            selector=vm.DatePicker(title='Select date:'),
            targets=["stock_graph"]
        ),
    ],
)

# Build and run the dashboard
dashboard = vm.Dashboard(pages=[page])
Vizro().build(dashboard).run()