From 53d1b1958837dcf06721203b34bf4951637cc4cd Mon Sep 17 00:00:00 2001 From: PlatinumCD Date: Mon, 16 Jul 2018 23:34:05 -0600 Subject: [PATCH 1/3] Added moving average indicator --- binance/client.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/binance/client.py b/binance/client.py index c3e69a086..3dbf53590 100644 --- a/binance/client.py +++ b/binance/client.py @@ -793,6 +793,64 @@ def get_historical_klines(self, symbol, interval, start_str, end_str=None): return output_data + def get_moving_average(self, klines, period): + """ + Get the average of values of historical kline data + + :param klines: List of OHLCV values given from get_historical_kline + :type klines: list + :param period: Moving average period + :type period: int + + :return: list of average OHLCV values based on period + + + """ + # Transpose historical kline data + data = [list(i) for i in zip(*klines)] + + # Number of values for each timestamp + slots = len(klines[0]) + + # init our list + moving_avg = [] + + idx = 0 + # iterate through every day in kline + while idx < len(klines): + + # init each row + row = [] + + # add timestamp to row + row.append(data[0][idx]) + + # iterate through all values + for k in range(1, slots): + + # add closing time to row + if k == 6: + val = data[k][idx] + + # add "n/a" if we can't calculate average over period yet + elif idx < period: + val = "n/a" + + # add average over period + else: + val = 0 + for i in data[k][idx - period:idx]: + val = val + float(i) + val = str(round(val / period, 8)) + row.append(val) + + idx += 1 + + # add row to moving average list + moving_avg.append(row) + + return moving_avg + def get_ticker(self, **params): """24 hour price change statistics. From b3c542d2bf13576c3741e7b667746065ae89d573 Mon Sep 17 00:00:00 2001 From: Cameron Durbin <37963318+PlatinumCD@users.noreply.github.com> Date: Mon, 16 Jul 2018 23:48:50 -0600 Subject: [PATCH 2/3] Update README.rst added example usage of moving average --- README.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.rst b/README.rst index c618182cd..127e01dc1 100644 --- a/README.rst +++ b/README.rst @@ -126,6 +126,10 @@ Quick Start # fetch weekly klines since it listed klines = client.get_historical_klines("NEOBTC", Client.KLINE_INTERVAL_1WEEK, "1 Jan, 2017") + + # fetch moving average based on given kline and period + five_day_moving_average = client.get_moving_average(klines, 5) + For more `check out the documentation `_. From 47145963f1e6a0689cbf8da0eaf24e3863a929df Mon Sep 17 00:00:00 2001 From: Cameron Durbin Date: Mon, 16 Jul 2018 23:55:27 -0600 Subject: [PATCH 3/3] Update client.py --- binance/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/binance/client.py b/binance/client.py index 3dbf53590..90fcd664f 100644 --- a/binance/client.py +++ b/binance/client.py @@ -795,7 +795,7 @@ def get_historical_klines(self, symbol, interval, start_str, end_str=None): def get_moving_average(self, klines, period): """ - Get the average of values of historical kline data + Get the average values of historical kline data :param klines: List of OHLCV values given from get_historical_kline :type klines: list