Skip to content

add ability to shift moving average on plots #391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/mplfinance/_arg_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,34 @@ def _mav_validator(mav_value):
'''
if isinstance(mav_value,int) and mav_value > 1:
return True
elif not isinstance(mav_value,tuple) and not isinstance(mav_value,list):
elif not isinstance(mav_value,(tuple,list,dict)):
return False

if not len(mav_value) < 8:
return False
for num in mav_value:
if not isinstance(num,int) and num > 1:
if isinstance(mav_value,dict):
if 'period' not in mav_value or not isinstance(mav_value['period'],(tuple,list,dict)):
return False
if 'shift' in mav_value:
if not isinstance(mav_value['shift'],(tuple,list,dict)):
return False
if isinstance(mav_value['period'], int) and isinstance(mav_value['shift'], int):
return True
elif isinstance(mav_value['period'], int) or isinstance(mav_value['shift'], int):
return False
if len(mav_value['period']) == len(mav_value['shift']):
for num in mav_value['period']:
if not isinstance(num, int) and num > 1:
return False
for num in mav_value['shift']:
if not isinstance(num, int) and num > 1:
return False
return True
return False
elif not len(mav_value) < 8:
return False
else:
for num in mav_value:
if not isinstance(num,int) and num > 1:
return False
return True

def _hlines_validator(value):
Expand Down
2 changes: 1 addition & 1 deletion src/mplfinance/_version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

version_info = (0, 12, 7, 'alpha', 17)
version_info = (0, 12, 7, 'alpha', 18)

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

Expand Down
13 changes: 10 additions & 3 deletions src/mplfinance/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,8 +978,12 @@ def _plot_mav(ax,config,xdates,prices,apmav=None,apwidth=None):
mavgs = config['mav']
mavp_list = []
if mavgs is not None:
shift = None
if isinstance(mavgs,dict):
shift = mavgs['shift']
mavgs = mavgs['period']
if isinstance(mavgs,int):
mavgs = mavgs, # convert to tuple
mavgs = mavgs, # convert to tuple
if len(mavgs) > 7:
mavgs = mavgs[0:7] # take at most 7

Expand All @@ -988,8 +992,11 @@ def _plot_mav(ax,config,xdates,prices,apmav=None,apwidth=None):
else:
mavc = None

for mav in mavgs:
mavprices = pd.Series(prices).rolling(mav).mean().values
for idx,mav in enumerate(mavgs):
mean = pd.Series(prices).rolling(mav).mean()
if shift is not None:
mean = mean.shift(periods=shift[idx])
mavprices = mean.values
lw = config['_width_config']['line_width']
if mavc:
ax.plot(xdates, mavprices, linewidth=lw, color=next(mavc))
Expand Down
Binary file added tests/reference_images/addplot12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions tests/test_addplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,23 @@ def test_addplot11(bolldata):
print('result=',result)
assert result is None

def test_addplot12(bolldata):

df = bolldata

fname = base+'12.png'
tname = os.path.join(tdir,fname)
rname = os.path.join(refd,fname)

mpf.plot(df,type='candle',volume=True,savefig=tname,mav={'period':(20,40,60), 'shift': [5,10,20]})

tsize = os.path.getsize(tname)
print(glob.glob(tname),'[',tsize,'bytes',']')

rsize = os.path.getsize(rname)
print(glob.glob(rname),'[',rsize,'bytes',']')

result = compare_images(rname,tname,tol=IMGCOMP_TOLERANCE)
if result is not None:
print('result=',result)
assert result is None