Skip to content

Commit 840f9a3

Browse files
Merge pull request #8 from DanielGoldfarb/master
Version 0.12.x - Ability to plot arbitrary user data (in addition to basic OHLCV data)
2 parents c0c1f64 + f738cac commit 840f9a3

11 files changed

+2649
-92
lines changed

README.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,19 @@
1111

1212
---
1313

14-
# Usage
14+
# Contents
15+
16+
- **[Basic Usage](#usage)**
17+
- **[Adding Custom Data to Ohlcv Plots](https://github.com/matplotlib/mplfinance/blob/master/examples/addplot.ipynb)**
18+
- Customizing the Appearance of Plots (presently in development)
19+
- Technical Studies (presently in development)
20+
- **[Latest Release Info](#release)**
21+
- **[Some Background History About This Package](#history)**
22+
- **[Old API Availability](#oldapi)**
23+
24+
---
25+
26+
# <a name="usage"></a>Basic Usage
1527
Start with a Pandas DataFrame containing OHLC data. For example,
1628

1729
```python
@@ -158,7 +170,7 @@ mpf.plot(daily,type='line')
158170
<br>
159171

160172
We can also plot moving averages with the `mav` keyword
161-
- use a scaler for a single moving average
173+
- use a scalar for a single moving average
162174
- use a tuple or list of integers for multiple moving averages
163175

164176

@@ -500,14 +512,19 @@ For more examples of using mplfinance, please see the jupyter notebooks in the `
500512
- technical studies, such as:
501513
- Trading Envelope, Bollinger Bands
502514
- MACD
503-
- custom studies and/or additional data on plot
504-
- Ability to plot specified additional columns from DataFrame either within the main ohlc plot, or only the lower axis where volume may be displayed.
505515
- save plot to file
506516

517+
# <a name="release"></a>Release Notes
518+
519+
| Version | Description | Release Date |
520+
|----------|--------------|----------------|
521+
| 0.12.x | Ability plot arbitrary user data (in addition to basic OHLCV data).<br> - both line and scatter plots available.<br> - optionally plot on either the "main" or "lower" (aka "volume") axis. | 2020-01-09 |
522+
| 0.11.x | Basic Plotting from Pandas DataFrame of OHLC bars and candlesticks.<br> - optional display of volume<br> - optional display of (up to 3 different) moving averages.<br> - old API still available by importing from "mplfinance/original_flavor" | 2019-13-20 |
523+
| 0.10.x | Old mpl-finance API set up as its own package<br> (i.e. removed from the matplotlib package). | 2016-09-08 |
507524

508525
---
509526

510-
## Some History
527+
## <a name="history"></a>Some History
511528
My name is Daniel Goldfarb. In November 2019, I became the maintainer of `matplotlib/mpl-finance`. That module is being deprecated in favor of the current `matplotlib/mplfinance`. The old `mpl-finance` consisted of code extracted from the deprecated `matplotlib.finance` module along with a few examples of usage. It has been mostly un-maintained for the past three years.
512529

513530
It is my intention to archive the `matplotlib/mpl-finance` repository soon, and direct everyone to `matplotlib/mplfinance`. The main reason for the rename is to avoid confusion with the hyphen and the underscore: As it was, `mpl-finance` was *installed with the hyphen, but imported with an underscore `mpl_finance`.* Going forward it will be a simple matter of both installing and importing `mplfinance`.
@@ -535,7 +552,7 @@ The most common usage is to then call `mpf.plot(data)` where `data` is a `Pandas
535552
I am very interested to hear from you regarding how you were using the old `mpl-finance` (if you were), what you think of the new `mplfinance`, plus any suggestions you may have for improvement. You can reach me at [email protected]
536553

537554
---
538-
### old API availability
555+
### <a name="oldapi"></a>old API availability
539556

540557
With this new ` mplfinance ` package installed, in addition to the new API, users can still access the old API (at least for the next several months) by changing their import statments<br>
541558
**from:**

examples/addplot.ipynb

Lines changed: 708 additions & 0 deletions
Large diffs are not rendered by default.

examples/data/SP500_20191106_IDayBollinger.csv

Lines changed: 392 additions & 0 deletions
Large diffs are not rendered by default.

examples/data/SPY_20110701_20120630_Bollinger.csv

Lines changed: 253 additions & 0 deletions
Large diffs are not rendered by default.

examples/test_df.rolling.ipynb

Lines changed: 1020 additions & 0 deletions
Large diffs are not rendered by default.

examples/tplot.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pandas as pd
2+
daily = pd.read_csv('data/SP500_NOV2019_Hist.csv',index_col=0,parse_dates=True)
3+
daily.head(3)
4+
5+
# idf = pd.read_csv('data/SP500_NOV2019_IDay.csv',index_col=0,parse_dates=True)
6+
# idf = idf.drop('Volume',axis=1) # Volume is zero anyway for this intraday data set
7+
# idf.index.name = 'Date'
8+
# daily = pd.read_csv('data/SP600_NOV2019_Hist.csv',index_col=0,parse_dates=True)
9+
# daily = pd.read_csv('data/SP500_NOV2019_Hist.csv',index_col=0,parse_dates=True)
10+
# daily = pd.read_csv('data/SP500_NOV2019_Hist.csv',index_col=0,parse_dates=True)
11+
12+
import mplfinance as mpf
13+
mpf.__file__
14+
mpf.plot(daily,mav=7)
15+
mpf.plot(daily,volume=True)
16+
17+
df = pd.read_csv('data/yahoofinance-SPY-20080101-20180101.csv',index_col=0,parse_dates=True)
18+
mpf.plot(df[700:850],type='bars',no_xgaps=True,mav=(20,40),figscale=0.7)
19+
mpf.plot(df[700:850],type='bars',volume=True,no_xgaps=True,mav=(20,40),figscale=0.7)

readme.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@
297297
"source": [
298298
"---\n",
299299
"We can also plot moving averages with the `mav` keyword\n",
300-
"- use an scaler for a single moving average \n",
300+
"- use an scalar for a single moving average \n",
301301
"- use a tuple or list of integers for multiple moving averages"
302302
]
303303
},

src/mplfinance/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
from mplfinance.plotting import plot
2-
from mplfinance._version import __version__
1+
from mplfinance.plotting import plot
2+
from mplfinance.plotting import make_addplot
3+
from mplfinance._version import __version__

src/mplfinance/_arg_validators.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import matplotlib.dates as mdates
2+
import pandas as pd
3+
import numpy as np
4+
5+
def _check_and_prepare_data(data):
6+
'''
7+
Check and Prepare the data input:
8+
For now, data must be a Pandas DataFrame with a DatetimeIndex
9+
and columns named 'Open', 'High', 'Low', 'Close', and optionally 'Volume'
10+
11+
Later (if there is demand for it) we may accept all of the following data formats:
12+
1. Pandas DataFrame with DatetimeIndex (as described above)
13+
2. Pandas Series with DatetimeIndex:
14+
Values are close prices, and Series generates a line plot
15+
3. Tuple of Lists, or List of Lists:
16+
The inner Lists are each columns, in the order: DateTime, Open, High, Low, Close, Volume
17+
4. Tuple of Tuples or List of Tuples:
18+
The inner tuples are each row, containing values in the order: DateTime, Open, High, Low, Close, Volume
19+
20+
Return a Tuple of Lists: datetimes, opens, highs, lows, closes, volumes
21+
'''
22+
if not isinstance(data, pd.core.frame.DataFrame):
23+
raise TypeError('Expect data as DataFrame')
24+
25+
if not isinstance(data.index,pd.core.indexes.datetimes.DatetimeIndex):
26+
raise TypeError('Expect data.index as DatetimeIndex')
27+
28+
dates = mdates.date2num(data.index.to_pydatetime())
29+
opens = data['Open'].values
30+
highs = data['High'].values
31+
lows = data['Low'].values
32+
closes = data['Close'].values
33+
if 'Volume' in data.columns:
34+
volumes = data['Volume'].values
35+
else:
36+
volumes = None
37+
38+
return dates, opens, highs, lows, closes, volumes
39+
40+
41+
def _mav_validator(mav_value):
42+
'''
43+
Value for mav (moving average) keyword may be:
44+
scalar int greater than 1, or tuple of ints, or list of ints (greater than 1).
45+
tuple or list limited to length of 3 moving averages (to keep the plot clean).
46+
'''
47+
if isinstance(mav_value,int) and mav_value > 1:
48+
return True
49+
elif not isinstance(mav_value,tuple) and not isinstance(mav_value,list):
50+
return False
51+
52+
if not len(mav_value) < 4:
53+
return False
54+
for num in mav_value:
55+
if not isinstance(num,int) and num > 1:
56+
return False
57+
return True

src/mplfinance/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version_info = (0, 11, 1, 'alpha', 1)
1+
version_info = (0, 12, 0, 'alpha', 0)
22

33
_specifier_ = {'alpha': 'a', 'beta': 'b', 'candidate': 'rc', 'final': ''}
44

0 commit comments

Comments
 (0)