|
| 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() |
0 commit comments