Skip to content

Commit fccc683

Browse files
initial working version with addplot functionality
1 parent deda91a commit fccc683

File tree

9 files changed

+1215
-87
lines changed

9 files changed

+1215
-87
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ mpf.plot(daily,type='line')
158158
<br>
159159

160160
We can also plot moving averages with the `mav` keyword
161-
- use a scaler for a single moving average
161+
- use a scalar for a single moving average
162162
- use a tuple or list of integers for multiple moving averages
163163

164164

examples/addplot.ipynb

Lines changed: 708 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/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)