Skip to content

Commit f718021

Browse files
committed
plots
1 parent e7d7981 commit f718021

File tree

3 files changed

+112
-64
lines changed

3 files changed

+112
-64
lines changed

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Examples and Guides
9696
:caption: Simple Tutorials
9797

9898
tutorial/simple_plot
99+
tutorial/simple_returns
99100
tutorial/return_distributions
100101
tutorial/calc_beta
101102

docs/source/tutorial/simple_plot.rst

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@ financial data using MoabDB and how to create a basic
1212
chart using the `matplotlib` library.
1313

1414
We'll be using the `get_equity` function to retrieve daily-level and show
15-
three examples of plotting data:
15+
two examples of plotting data:
1616

1717
- Single Stock with Daily-Level Data
1818
- Multiple Stocks with Daily-Level Data
19-
- Multiple Stocks with Cumulative Returns
20-
21-
2219

2320

2421
Prerequisites
@@ -148,66 +145,6 @@ With our data in hand, we can now plot it:
148145
:align: center
149146
:width: 80%
150147

151-
Plotting Cumulative Returns for Multiple Stocks
152-
-----------------------------------------------
153-
154-
To visualize the performance of various stocks over time,
155-
we'll compute and plot their cumulative returns. Mathematically, the
156-
formula for cumulative returns is:
157-
158-
.. math::
159-
160-
\text{Cumulative Return(t)} = \frac{\text{Price(t)}}{\text{Price(0)}} - 1
161-
162-
Which is that every price is divided by the initial price and then
163-
subtracted by 1. This gives us a percentage that we can plot over time.
164-
165-
166-
Compute and Visualize Cumulative Returns with Matplotlib
167-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
168-
169-
We can easily calculate cumulative return as price `t` divided by
170-
the initial price, and then subtracting 1 Let's compute this for our stocks:
171-
172-
.. code-block:: python
173-
174-
import moabdb as mdb
175-
import matplotlib.pyplot as plt
176-
177-
# Constants defined here for flexibility
178-
TICS = ['MSFT','INTC']
179-
SAMPLE = '5y'
180-
181-
# Load and Check Data, Get Prices
182-
data_df = mdb.get_equity(tickers=TICS, sample=SAMPLE)
183-
price_df = data_df['Close']
184-
print(price_df.head())
185-
186-
# Calculate the cumulative returns
187-
price0 = price_df.iloc[0]
188-
cum_returns = (price_df[TICS] / price0[TICS]) - 1
189-
cum_returns *= 100
190-
191-
# Creating the plot
192-
fig, ax = plt.subplots(figsize=(6,4))
193-
for tic in TICS:
194-
ax.plot(cum_returns.index, cum_returns[tic], label=tic)
195-
196-
ax.set_title('Cumulative Returns Over Time')
197-
ax.set_xlabel('Date')
198-
ax.set_ylabel('Cumulative Return (in %)')
199-
ax.axhline(y=0, color='black', linestyle='-', linewidth=1)
200-
plt.grid(True)
201-
plt.legend()
202-
plt.tight_layout()
203-
plt.show()
204-
205-
206-
.. figure:: /_static/images/ex1_fig3.jpg
207-
:alt: Single Stock with Daily-Level Data
208-
:align: center
209-
:width: 80%
210-
211148
With these simple steps, you've fetched financial data using MoabDB
212149
and visualized it with a basic chart. Explore more with
213150
different stocks, date ranges, or chart types to gain richer insights!
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
2+
=================================
3+
Plotting Cumulative Returns
4+
=================================
5+
6+
.. image:: https://img.shields.io/pypi/v/moabdb.svg
7+
:target: https://pypi.python.org/pypi/moabdb
8+
:alt: PyPI Version
9+
10+
This guide continues from the previous example. We will retrieve
11+
financial data using MoabDB and how create a chart of cumulative returns
12+
using the `matplotlib` library.
13+
14+
We'll be using the `get_equity` function to retrieve daily-level and show
15+
how to plut cumulative returns for multiple stocks.
16+
17+
18+
19+
20+
Prerequisites
21+
-------------
22+
23+
- You will need `matplotlib` installed. If you haven't already, you can install it with:
24+
25+
.. code-block:: bash
26+
27+
pip install matplotlib
28+
29+
30+
First, let's retrieve some financial data. For this example, we'll fetch historical closing prices for a given stock (e.g., `MSFT`):
31+
32+
Import MoabDB and fetch data
33+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
34+
35+
.. code-block:: python
36+
37+
import moabdb as mdb
38+
import matplotlib.pyplot as plt
39+
40+
# Constants defined here for flexibility
41+
TIC = 'MSFT'
42+
SAMPLE = '5y'
43+
44+
# Load and Check Data
45+
data_df = mdb.get_equity(tickers=TIC, sample=SAMPLE)
46+
print(data_df.head())
47+
48+
Plotting Cumulative Returns for Multiple Stocks
49+
-----------------------------------------------
50+
51+
To visualize the performance of various stocks over time,
52+
we'll compute and plot their cumulative returns. Mathematically, the
53+
formula for cumulative returns is:
54+
55+
.. math::
56+
57+
\text{Cumulative Return(t)} = \frac{\text{Price(t)}}{\text{Price(0)}} - 1
58+
59+
Which is that every price is divided by the initial price and then
60+
subtracted by 1. This gives us a percentage that we can plot over time.
61+
62+
63+
Compute and Visualize Cumulative Returns with Matplotlib
64+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
65+
66+
We can easily calculate cumulative return as price `t` divided by
67+
the initial price, and then subtracting 1 Let's compute this for our stocks:
68+
69+
.. code-block:: python
70+
71+
import moabdb as mdb
72+
import matplotlib.pyplot as plt
73+
74+
# Constants defined here for flexibility
75+
TICS = ['MSFT','INTC']
76+
SAMPLE = '5y'
77+
78+
# Load and Check Data, Get Prices
79+
data_df = mdb.get_equity(tickers=TICS, sample=SAMPLE)
80+
price_df = data_df['Close']
81+
print(price_df.head())
82+
83+
# Calculate the cumulative returns
84+
price0 = price_df.iloc[0]
85+
cum_returns = (price_df[TICS] / price0[TICS]) - 1
86+
cum_returns *= 100
87+
88+
# Creating the plot
89+
fig, ax = plt.subplots(figsize=(6,4))
90+
for tic in TICS:
91+
ax.plot(cum_returns.index, cum_returns[tic], label=tic)
92+
93+
ax.set_title('Cumulative Returns Over Time')
94+
ax.set_xlabel('Date')
95+
ax.set_ylabel('Cumulative Return (in %)')
96+
ax.axhline(y=0, color='black', linestyle='-', linewidth=1)
97+
plt.grid(True)
98+
plt.legend()
99+
plt.tight_layout()
100+
plt.show()
101+
102+
103+
.. figure:: /_static/images/ex1_fig3.jpg
104+
:alt: Single Stock with Daily-Level Data
105+
:align: center
106+
:width: 80%
107+
108+
With these simple steps, you've fetched financial data using MoabDB
109+
and visualized it with a basic chart. Explore more with
110+
different stocks, date ranges, or chart types to gain richer insights!

0 commit comments

Comments
 (0)