1-
21import 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
554def 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
7927test_ema ()
0 commit comments