Skip to content

Commit

Permalink
Add 1 Month Interval(1M) for Historical Klines
Browse files Browse the repository at this point in the history
  • Loading branch information
lucky7323 committed Aug 23, 2020
1 parent d7bf15b commit 12896d4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
12 changes: 9 additions & 3 deletions binance/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import requests
import time
from operator import itemgetter
from .helpers import date_to_milliseconds, interval_to_milliseconds
from .helpers import date_to_milliseconds, interval_to_milliseconds, increment_month
from .exceptions import BinanceAPIException, BinanceRequestException, BinanceWithdrawException


Expand Down Expand Up @@ -831,7 +831,10 @@ def get_historical_klines(self, symbol, interval, start_str, end_str=None,
break

# increment next call by our timeframe
start_ts += timeframe
if interval == self.KLINE_INTERVAL_1MONTH:
start_ts = increment_month(start_ts)
else:
start_ts += timeframe

# sleep after every 3rd call to be kind to the API
if idx % 3 == 0:
Expand Down Expand Up @@ -912,7 +915,10 @@ def get_historical_klines_generator(self, symbol, interval, start_str, end_str=N
break

# increment next call by our timeframe
start_ts += timeframe
if interval == self.KLINE_INTERVAL_1MONTH:
start_ts = increment_month(start_ts)
else:
start_ts += timeframe

# sleep after every 3rd call to be kind to the API
if idx % 3 == 0:
Expand Down
18 changes: 18 additions & 0 deletions binance/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytz

from datetime import datetime
from dateutil.relativedelta import relativedelta


def date_to_milliseconds(date_str):
Expand Down Expand Up @@ -50,3 +51,20 @@ def interval_to_milliseconds(interval):
return int(interval[:-1]) * seconds_per_unit[interval[-1]] * 1000
except (ValueError, KeyError):
return None


def increment_month(origin_ts):
"""Increment a given timestamp by one month
:param origin_ts: original timestamp, e.g.: 1501545600000, 1504224000000, ...
:type origin_ts: int
:return:
Timestamp incremented by one month from a given timestamp
"""
d = datetime.fromtimestamp(origin_ts/1000) + relativedelta(months=1)
# if the date is not timezone aware apply UTC timezone
if d.tzinfo is None or d.tzinfo.utcoffset(d) is None:
d = d.replace(tzinfo=pytz.utc)

return int(datetime.timestamp(d))
2 changes: 1 addition & 1 deletion docs/helpers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ Helper Functions
================

.. autoclass:: binance.helpers
:members: date_to_milliseconds, interval_to_milliseconds
:members: date_to_milliseconds, interval_to_milliseconds, increment_month
:noindex:
29 changes: 27 additions & 2 deletions examples/save_historical_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import json

from datetime import datetime

from dateutil.relativedelta import relativedelta
from binance.client import Client


Expand Down Expand Up @@ -57,6 +59,23 @@ def interval_to_milliseconds(interval):
return ms


def increment_month(origin_ts):
"""Increment a given timestamp by one month
:param origin_ts: original timestamp, e.g.: 1501545600000, 1504224000000, ...
:type origin_ts: int
:return:
Timestamp incremented by one month from a given timestamp
"""
d = datetime.fromtimestamp(origin_ts/1000) + relativedelta(months=1)
# if the date is not timezone aware apply UTC timezone
if d.tzinfo is None or d.tzinfo.utcoffset(d) is None:
d = d.replace(tzinfo=pytz.utc)

return int(datetime.timestamp(d))


def get_historical_klines(symbol, interval, start_str, end_str=None):
"""Get Historical Klines from Binance
Expand Down Expand Up @@ -118,10 +137,16 @@ def get_historical_klines(symbol, interval, start_str, end_str=None):
output_data += temp_data

# update our start timestamp using the last value in the array and add the interval timeframe
start_ts = temp_data[len(temp_data) - 1][0] + timeframe
if interval == Client.KLINE_INTERVAL_1MONTH:
start_ts = increment_month(temp_data[len(temp_data) - 1][0])
else:
start_ts = temp_data[len(temp_data) - 1][0] + timeframe
else:
# it wasn't listed yet, increment our start date
start_ts += timeframe
if interval == Client.KLINE_INTERVAL_1MONTH:
start_ts = increment_month(start_ts)
else:
start_ts += timeframe

idx += 1
# check if we received less than the required limit and exit the loop
Expand Down

0 comments on commit 12896d4

Please sign in to comment.