Skip to content

Commit 8a7930d

Browse files
committed
Allow column names to be customized
1 parent 3e1cc72 commit 8a7930d

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/mplfinance/_arg_validators.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pandas as pd
33
import numpy as np
44

5-
def _check_and_prepare_data(data):
5+
def _check_and_prepare_data(data, config):
66
'''
77
Check and Prepare the data input:
88
For now, data must be a Pandas DataFrame with a DatetimeIndex
@@ -25,16 +25,17 @@ def _check_and_prepare_data(data):
2525
if not isinstance(data.index,pd.core.indexes.datetimes.DatetimeIndex):
2626
raise TypeError('Expect data.index as DatetimeIndex')
2727

28-
cols = ['Open','High','Low','Close']
28+
o, h, l, c, v = config["columns"]
29+
cols = [o, h, l, c]
2930

3031
dates = mdates.date2num(data.index.to_pydatetime())
31-
opens = data['Open'].values
32-
highs = data['High'].values
33-
lows = data['Low'].values
34-
closes = data['Close'].values
35-
if 'Volume' in data.columns:
36-
volumes = data['Volume'].values
37-
cols.append('Volume')
32+
opens = data[o].values
33+
highs = data[h].values
34+
lows = data[l].values
35+
closes = data[c].values
36+
if v in data.columns:
37+
volumes = data[v].values
38+
cols.append(v)
3839
else:
3940
volumes = None
4041

src/mplfinance/plotting.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ def _valid_plot_kwargs():
6464
'''
6565

6666
vkwargs = {
67+
'columns' : { 'Default' : ('Open', 'High', 'Low', 'Close', 'Volume'),
68+
'Validator' : lambda value: isinstance(value, (tuple, list))
69+
and len(value) == 5
70+
and all(isinstance(c, str) for c in value) },
6771
'type' : { 'Default' : 'ohlc',
6872
'Validator' : lambda value: value in ['candle','candlestick','ohlc','bars','ohlc_bars','line', 'renko'] },
6973

@@ -164,10 +168,10 @@ def plot( data, **kwargs ):
164168
Also provide ability to plot trading signals, and/or addtional user-defined data.
165169
"""
166170

167-
dates,opens,highs,lows,closes,volumes = _check_and_prepare_data(data)
168-
169171
config = _process_kwargs(kwargs, _valid_plot_kwargs())
170172

173+
dates,opens,highs,lows,closes,volumes = _check_and_prepare_data(data, config)
174+
171175
if config['type'] == 'renko' and config['addplot'] is not None:
172176
err = "`addplot` is not supported for `type='renko'`"
173177
raise ValueError(err)

0 commit comments

Comments
 (0)