|
| 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