Skip to content

Commit 47e4c0c

Browse files
author
andrewrgarcia
committed
start dev: exponential mav for Issue #506
1 parent bf1a603 commit 47e4c0c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/mplfinance/plotting.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,45 @@ def _plot_mav(ax,config,xdates,prices,apmav=None,apwidth=None):
11471147
mavp_list.append(mavprices)
11481148
return mavp_list
11491149

1150+
1151+
def _plot_ema(ax,config,xdates,prices,apmav=None,apwidth=None):
1152+
'''ema: exponential moving average'''
1153+
style = config['style']
1154+
if apmav is not None:
1155+
mavgs = apmav
1156+
else:
1157+
mavgs = config['ema']
1158+
mavp_list = []
1159+
if mavgs is not None:
1160+
shift = None
1161+
if isinstance(mavgs,dict):
1162+
shift = mavgs['shift']
1163+
mavgs = mavgs['period']
1164+
if isinstance(mavgs,int):
1165+
mavgs = mavgs, # convert to tuple
1166+
if len(mavgs) > 7:
1167+
mavgs = mavgs[0:7] # take at most 7
1168+
1169+
if style['mavcolors'] is not None:
1170+
mavc = cycle(style['mavcolors'])
1171+
else:
1172+
mavc = None
1173+
1174+
for idx,mav in enumerate(mavgs):
1175+
# mean = pd.Series(prices).rolling(mav).mean()
1176+
mean = pd.Series(prices).ewm(span=mav,adjust=False).mean()
1177+
if shift is not None:
1178+
mean = mean.shift(periods=shift[idx])
1179+
mavprices = mean.values
1180+
lw = config['_width_config']['line_width']
1181+
if mavc:
1182+
ax.plot(xdates, mavprices, linewidth=lw, color=next(mavc))
1183+
else:
1184+
ax.plot(xdates, mavprices, linewidth=lw)
1185+
mavp_list.append(mavprices)
1186+
return mavp_list
1187+
1188+
11501189
def _auto_secondary_y( panels, panid, ylo, yhi ):
11511190
# If mag(nitude) for this panel is not yet set, then set it
11521191
# here, as this is the first ydata to be plotted on this panel:

0 commit comments

Comments
 (0)