Skip to content

Commit 415a705

Browse files
committed
plots1
1 parent 54efad1 commit 415a705

File tree

4 files changed

+117
-55
lines changed

4 files changed

+117
-55
lines changed

docs/source/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Getting Started Tutorials
5454
:maxdepth: 2
5555
:caption: Tutorials
5656

57-
tutorial/installation
57+
tutorial/ex0_install
5858
tutorial/ex1_plot
5959
tutorial/ex2_plot_intraday
6060

docs/source/tutorial.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ using python and MoabDB.
77
.. toctree::
88
:maxdepth: 2
99

10-
tutorial/installation
10+
tutorial/ex0_install
1111
tutorial/ex1_plot
1212
tutorial/ex2_plot_intraday

docs/source/tutorial/installation.rst renamed to docs/source/tutorial/ex0_install.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ Create a file named ``config.ini`` and structure it as follows:
9191
.. code-block:: ini
9292
9393
[Credentials]
94-
email = 'your-email@example.com'
95-
api_key = 'your-secret-api-key'
94+
email = your-email@example.com
95+
api_key = your-secret-api-key
9696
9797
**Read Config File, Login, and Access Data**
9898

@@ -104,8 +104,8 @@ Create a file named ``config.ini`` and structure it as follows:
104104
# Read credentials from config file
105105
config = configparser.ConfigParser()
106106
config.read('config.ini')
107-
email = config['Credentials']['email']
108-
api_key = config['Credentials']['api_key']
107+
email = config.get("Credentials","email")
108+
api_key = config.get("Credentials","api_key")
109109
110110
# Login and access data
111111
mdb.login(email, api_key)

docs/source/tutorial/ex2_plot_intraday.rst

Lines changed: 111 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,143 @@
11
Plotting Intraday Stock Prices
22
##############################
33

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
77

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`.
810

9-
.. Prerequisites
10-
.. =============
11+
Prerequisites
12+
=============
1113

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

15-
.. .. code-block:: ini
17+
.. code-block:: ini
1618
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
2022
2123
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:
2325

24-
.. .. code-block:: bash
26+
.. code-block:: bash
2527
26-
.. pip install matplotlib
28+
pip install matplotlib
2729
2830
29-
.. Plotting Single Stock with Daily-Level Data
30-
.. ===========================================
31+
Plotting Single Stock with Daily-Level Data
32+
===========================================
3133

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`):
3335

34-
.. Import MoabDB and fetch data
35-
.. ----------------------------
36+
Import MoabDB and fetch data
37+
----------------------------
3638

37-
.. .. code-block:: python
39+
.. code-block:: python
3840
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
4145
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'
4551
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)
4959
5060
51-
.. Visualizing Data with Matplotlib
52-
.. --------------------------------
61+
Plot 1 day of intraday data
62+
---------------------------
5363

54-
.. With our data in hand, we can now plot it:
64+
**Simple plot**
5565

56-
.. .. code-block:: python
66+
With our data in hand, we can now plot it:
5767

58-
.. import moabdb as mdb
59-
.. import matplotlib.pyplot as plt
68+
.. code-block:: python
6069
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
6474
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'
6880
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()
72140
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()
79141
80142
81143

0 commit comments

Comments
 (0)