|
1 | 1 | Plotting Intraday Stock Prices
|
2 | 2 | ##############################
|
3 | 3 |
|
4 |
| -In this guide, we will walk you through the steps to retrieve |
5 |
| -financial data using MoabDB and how to create a basic |
6 |
| -chart using the `matplotlib` library. |
| 4 | +.. image:: https://img.shields.io/pypi/v/moabdb.svg |
| 5 | + :target: https://pypi.python.org/pypi/moabdb |
| 6 | + :alt: PyPI Version |
7 | 7 |
|
| 8 | +Continuing with the theme of price plotting, this guide will walk |
| 9 | + you through how to plot intraday stock prices using MoabDB and `matplotlib`. |
8 | 10 |
|
9 |
| -.. Prerequisites |
10 |
| -.. ============= |
| 11 | +Prerequisites |
| 12 | +============= |
11 | 13 |
|
12 |
| -.. - For advanced plotting, ensure you've set up and authenticated with MoabDB as described in the Quick Start guide. |
13 |
| -.. The advanced plotting will assume you have a ``config.ini`` file in your working directory with the following structure: |
| 14 | +- For advanced plotting, ensure you've set up and authenticated with MoabDB as described in the Quick Start guide. |
| 15 | +- The advanced plotting will assume you have a ``config.ini`` file in your working directory with the following structure: |
14 | 16 |
|
15 |
| -.. .. code-block:: ini |
| 17 | + .. code-block:: ini |
16 | 18 |
|
17 |
| -.. [Credentials] |
18 |
| -.. email = 'your-email@example.com' |
19 |
| -.. api_key = 'your-secret-api-key' |
| 19 | + [Credentials] |
| 20 | + email = your-email@example.com |
| 21 | + api_key = your-secret-api-key |
20 | 22 |
|
21 | 23 |
|
22 |
| -.. - You will need `matplotlib` installed. If you haven't already, you can install it with: |
| 24 | +- You will need `matplotlib` installed. If you haven't already, you can install it with: |
23 | 25 |
|
24 |
| -.. .. code-block:: bash |
| 26 | + .. code-block:: bash |
25 | 27 |
|
26 |
| -.. pip install matplotlib |
| 28 | + pip install matplotlib |
27 | 29 |
|
28 | 30 |
|
29 |
| -.. Plotting Single Stock with Daily-Level Data |
30 |
| -.. =========================================== |
| 31 | +Plotting Single Stock with Daily-Level Data |
| 32 | +=========================================== |
31 | 33 |
|
32 |
| -.. First, let's retrieve some financial data. For this example, we'll fetch historical closing prices for a given stock (e.g., `AAPL`): |
| 34 | +First, let's retrieve some financial data. For this example, we'll fetch historical closing prices for a given stock (e.g., `AAPL`): |
33 | 35 |
|
34 |
| -.. Import MoabDB and fetch data |
35 |
| -.. ---------------------------- |
| 36 | +Import MoabDB and fetch data |
| 37 | +---------------------------- |
36 | 38 |
|
37 |
| -.. .. code-block:: python |
| 39 | +.. code-block:: python |
38 | 40 |
|
39 |
| -.. import moabdb as mdb |
40 |
| -.. import matplotlib.pyplot as plt |
| 41 | + import configparser |
| 42 | + import moabdb as mdb |
| 43 | + import matplotlib.pyplot as plt |
| 44 | + import matplotlib.dates as mdates |
41 | 45 |
|
42 |
| -.. # Constants defined here for flexibility |
43 |
| -.. TIC = 'MSFT' |
44 |
| -.. SAMPLE = '5y' |
| 46 | + # Constants defined here for flexibility |
| 47 | + TIC = 'MSFT' |
| 48 | + SAMPLE = '1d' |
| 49 | + DAY_START = '9:30' |
| 50 | + DAY_END = '16:00' |
45 | 51 |
|
46 |
| -.. # Load and Check Data |
47 |
| -.. data_df = mdb.get_equity(tickers=TIC, sample=SAMPLE) |
48 |
| -.. print(data_df.head()) |
| 52 | + # Reading in credentials from config.ini file |
| 53 | + # Read credentials from config file |
| 54 | + config = configparser.ConfigParser() |
| 55 | + config.read('config.ini') |
| 56 | + email = config.get("Credentials", "email") |
| 57 | + api_key = config.get("Credentials", "api_key") |
| 58 | + mdb.login(email, api_key) |
49 | 59 |
|
50 | 60 |
|
51 |
| -.. Visualizing Data with Matplotlib |
52 |
| -.. -------------------------------- |
| 61 | +Plot 1 day of intraday data |
| 62 | +--------------------------- |
53 | 63 |
|
54 |
| -.. With our data in hand, we can now plot it: |
| 64 | +**Simple plot** |
55 | 65 |
|
56 |
| -.. .. code-block:: python |
| 66 | +With our data in hand, we can now plot it: |
57 | 67 |
|
58 |
| -.. import moabdb as mdb |
59 |
| -.. import matplotlib.pyplot as plt |
| 68 | +.. code-block:: python |
60 | 69 |
|
61 |
| -.. # Constants defined here for flexibility |
62 |
| -.. TIC = 'MSFT' |
63 |
| -.. SAMPLE = '5y' |
| 70 | + import configparser |
| 71 | + import moabdb as mdb |
| 72 | + import matplotlib.pyplot as plt |
| 73 | + import matplotlib.dates as mdates |
64 | 74 |
|
65 |
| -.. # Load and Check Data |
66 |
| -.. data_df = mdb.get_equity(tickers=TIC, sample=SAMPLE) |
67 |
| -.. print(data_df.head()) |
| 75 | + # Constants defined here for flexibility |
| 76 | + TIC = 'MSFT' |
| 77 | + SAMPLE = '1d' |
| 78 | + DAY_START = '9:30' |
| 79 | + DAY_END = '16:00' |
68 | 80 |
|
69 |
| -.. # Creating the plot |
70 |
| -.. x = data_df.index |
71 |
| -.. y = data_df['Close'] |
| 81 | + # Reading in credentials from config.ini file |
| 82 | + # Read credentials from config file |
| 83 | + config = configparser.ConfigParser() |
| 84 | + config.read('config.ini') |
| 85 | + email = config.get("Credentials", "email") |
| 86 | + api_key = config.get("Credentials", "api_key") |
| 87 | + mdb.login(email, api_key) |
| 88 | +
|
| 89 | + # Load and get price data |
| 90 | + data_df = mdb.get_equity(tickers=TIC, sample=SAMPLE, intraday=True) |
| 91 | + price_df = data_df['Close'].between_time(DAY_START, DAY_END) |
| 92 | +
|
| 93 | + # Plot |
| 94 | + price_df.plot() |
| 95 | + plt.show() |
| 96 | +
|
| 97 | +**Customization with Matplotlib** |
| 98 | + |
| 99 | +The simple plot leaves a lot to be desired. Let's customize it with `matplotlib`: |
| 100 | + |
| 101 | +.. code-block:: python |
| 102 | +
|
| 103 | + import configparser |
| 104 | + import moabdb as mdb |
| 105 | + import matplotlib.pyplot as plt |
| 106 | + import matplotlib.dates as mdates |
| 107 | +
|
| 108 | + # Constants defined here for flexibility |
| 109 | + TIC = 'MSFT' |
| 110 | + SAMPLE = '1d' |
| 111 | + DAY_START = '9:30' |
| 112 | + DAY_END = '16:00' |
| 113 | +
|
| 114 | + # Reading in credentials from config.ini file |
| 115 | + # Read credentials from config file |
| 116 | + config = configparser.ConfigParser() |
| 117 | + config.read('config.ini') |
| 118 | + email = config.get("Credentials", "email") |
| 119 | + api_key = config.get("Credentials", "api_key") |
| 120 | + mdb.login(email, api_key) |
| 121 | +
|
| 122 | + # Load and get price data |
| 123 | + data_df = mdb.get_equity(tickers=TIC, sample=SAMPLE, intraday=True) |
| 124 | + price_df = data_df['Close'].between_time(DAY_START, DAY_END) |
| 125 | +
|
| 126 | + # Plot Data Values |
| 127 | + x = price_df.index |
| 128 | + y = price_df.values |
| 129 | +
|
| 130 | + # Plot |
| 131 | + fig, ax = plt.subplots(figsize=(8,4)) |
| 132 | + ax.plot(x, y, label=TIC, color='blue') |
| 133 | + ax.set_xlabel('Date') |
| 134 | + ax.set_ylabel('Closing Price (in $)') |
| 135 | + ax.xaxis.set_major_formatter( |
| 136 | + mdates.ConciseDateFormatter(ax.xaxis.get_major_locator())) |
| 137 | + fig.autofmt_xdate() |
| 138 | + plt.legend() |
| 139 | + plt.show() |
72 | 140 |
|
73 |
| -.. fig, ax = plt.subplots(figsize=(6,4)) |
74 |
| -.. ax.plot(x, y, label=TIC, color='blue') |
75 |
| -.. ax.set_xlabel('Date') |
76 |
| -.. ax.set_ylabel('Closing Price (in $)') |
77 |
| -.. plt.legend() |
78 |
| -.. plt.show() |
79 | 141 |
|
80 | 142 |
|
81 | 143 |
|
|
0 commit comments