Skip to content

Commit 61a45b2

Browse files
author
andrewrgarcia
committed
make necessary changes after reversion
1 parent 8d93bec commit 61a45b2

File tree

3 files changed

+92
-4
lines changed

3 files changed

+92
-4
lines changed

src/mplfinance/plotting.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,10 @@ def plot( data, **kwargs ):
532532

533533
if ptype in VALID_PMOVE_TYPES:
534534
mavprices = _plot_mav(axA1,config,xdates,pmove_avgvals)
535+
emaprices = _plot_ema(axA1, config, xdates, pmove_avgvals)
535536
else:
536537
mavprices = _plot_mav(axA1,config,xdates,closes)
538+
emaprices = _plot_ema(axA1, config, xdates, closes)
537539

538540
avg_dist_between_points = (xdates[-1] - xdates[0]) / float(len(xdates))
539541
if not config['tight_layout']:
@@ -599,6 +601,13 @@ def plot( data, **kwargs ):
599601
else:
600602
for jj in range(0,len(mav)):
601603
retdict['mav' + str(mav[jj])] = mavprices[jj]
604+
if config['ema'] is not None:
605+
ema = config['ema']
606+
if len(ema) != len(emaprices):
607+
warnings.warn('len(ema)='+str(len(ema))+' BUT len(emaprices)='+str(len(emaprices)))
608+
else:
609+
for jj in range(0, len(ema)):
610+
retdict['ema' + str(ema[jj])] = emaprices[jj]
602611
retdict['minx'] = minx
603612
retdict['maxx'] = maxx
604613
retdict['miny'] = miny
@@ -1180,13 +1189,13 @@ def _plot_ema(ax,config,xdates,prices,apmav=None,apwidth=None):
11801189
mean = pd.Series(prices).ewm(span=mav,adjust=False).mean()
11811190
if shift is not None:
11821191
mean = mean.shift(periods=shift[idx])
1183-
mavprices = mean.values
1192+
emaprices = mean.values
11841193
lw = config['_width_config']['line_width']
11851194
if mavc:
1186-
ax.plot(xdates, mavprices, linewidth=lw, color=next(mavc))
1195+
ax.plot(xdates, emaprices, linewidth=lw, color=next(mavc))
11871196
else:
1188-
ax.plot(xdates, mavprices, linewidth=lw)
1189-
mavp_list.append(mavprices)
1197+
ax.plot(xdates, emaprices, linewidth=lw)
1198+
mavp_list.append(emaprices)
11901199
return mavp_list
11911200

11921201

tests/test_ema.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
import mplfinance as mpf
3+
import requests # for making http requests to binance
4+
import json # for parsing what binance sends back to us
5+
import pandas as pd # for storing and manipulating the data we get back
6+
import numpy as np # numerical python, i usually need this somewhere
7+
# and so i import by habit nowadays
8+
9+
import matplotlib.pyplot as plt # for charts and such
10+
import datetime as dt # for dealing with times
11+
12+
INTERVAL = '1d'
13+
14+
15+
def get_bars(quote, interval=INTERVAL):
16+
17+
root_url = 'https://api.binance.com/api/v1/klines'
18+
url = root_url + '?symbol=' + quote + '&interval=' + interval
19+
data = json.loads(requests.get(url).text)
20+
df = pd.DataFrame(data)
21+
df.columns = ['open_time',
22+
'o', 'h', 'l', 'c', 'v',
23+
'close_time', 'qav', 'num_trades',
24+
'taker_base_vol', 'taker_quote_vol', 'ignore'
25+
]
26+
27+
df.index = [dt.datetime.fromtimestamp(x/1000.0) for x in df.close_time]
28+
29+
return df
30+
31+
32+
def coinpair(quote, interval='1d', base='USDT'):
33+
'''returns ohlc data of the quote cryptocurrency with
34+
the base currency (i.e. 'market'); base for alts must be either USDT or BTC'''
35+
36+
btcusd = 1 if quote == 'BTC' else \
37+
get_bars('BTCUSDT', interval=interval)['c'].astype('float') \
38+
if base == 'USDT' else 1
39+
40+
base0 = 'USDT' if quote == 'BTC' else 'BTC'
41+
42+
df = get_bars(quote + base0, interval=interval)
43+
44+
df['close'] = df['c'].astype('float')*btcusd
45+
df['open' ] = df['o'].astype('float')*btcusd
46+
df['high' ] = df['h'].astype('float')*btcusd
47+
df['low' ] = df['l'].astype('float')*btcusd
48+
49+
df.drop(['o', 'h', 'l', 'c'], axis=1, inplace=True)
50+
print(quote, base, 'on {} candles'.format(interval))
51+
52+
return df
53+
54+
55+
def test_ema():
56+
57+
coin = 'BTC'
58+
market = 'USDT'
59+
candles = '1M'
60+
61+
df = coinpair(coin, interval=candles, base=market)
62+
63+
ema25 = df['close'].ewm(span=25.0, adjust=False).mean()
64+
mav25 = df['close'].rolling(window=25).mean()
65+
66+
ap = [
67+
mpf.make_addplot(df, panel=1, type='ohlc', color='c',
68+
ylabel='mpf mav', mav=25, secondary_y=False),
69+
mpf.make_addplot(ema25, panel=2, type='line', width=2, color='c',
70+
ylabel='calculated', secondary_y=False),
71+
mpf.make_addplot(mav25, panel=2, type='line', width=2, color='blue',
72+
ylabel='calculated', secondary_y=False)
73+
]
74+
75+
mpf.plot(df, ylabel="mpf ema", type='ohlc',
76+
ema=25, addplot=ap, panel_ratios=(1, 1))
77+
78+
79+
test_ema()

tests/test_images/test_ema.png

55.7 KB
Loading

0 commit comments

Comments
 (0)