Skip to content

Commit 4a38e07

Browse files
author
andrewrgarcia
committed
replace former example with csvread of google data; remove binance API call
1 parent 61a45b2 commit 4a38e07

File tree

2 files changed

+8
-60
lines changed

2 files changed

+8
-60
lines changed

tests/test_ema.py

Lines changed: 8 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,15 @@
1-
21
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-
2+
import pandas as pd
543

554
def test_ema():
565

57-
coin = 'BTC'
58-
market = 'USDT'
59-
candles = '1M'
6+
df = pd.read_csv('./examples/data/yahoofinance-GOOG-20040819-20180120.csv', parse_dates=True)
7+
df.index = pd.DatetimeIndex(df['Date'])
608

61-
df = coinpair(coin, interval=candles, base=market)
9+
df = df[-50:] # show last 50 data points only
6210

63-
ema25 = df['close'].ewm(span=25.0, adjust=False).mean()
64-
mav25 = df['close'].rolling(window=25).mean()
11+
ema25 = df['Close'].ewm(span=25.0, adjust=False).mean()
12+
mav25 = df['Close'].rolling(window=25).mean()
6513

6614
ap = [
6715
mpf.make_addplot(df, panel=1, type='ohlc', color='c',
@@ -73,7 +21,7 @@ def test_ema():
7321
]
7422

7523
mpf.plot(df, ylabel="mpf ema", type='ohlc',
76-
ema=25, addplot=ap, panel_ratios=(1, 1))
77-
24+
ema=25, addplot=ap, panel_ratios=(1, 1)
25+
)
7826

7927
test_ema()

tests/test_images/test_ema.png

-7.44 KB
Loading

0 commit comments

Comments
 (0)