Skip to content

Commit 12896d4

Browse files
committed
Add 1 Month Interval(1M) for Historical Klines
1 parent d7bf15b commit 12896d4

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

binance/client.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import requests
66
import time
77
from operator import itemgetter
8-
from .helpers import date_to_milliseconds, interval_to_milliseconds
8+
from .helpers import date_to_milliseconds, interval_to_milliseconds, increment_month
99
from .exceptions import BinanceAPIException, BinanceRequestException, BinanceWithdrawException
1010

1111

@@ -831,7 +831,10 @@ def get_historical_klines(self, symbol, interval, start_str, end_str=None,
831831
break
832832

833833
# increment next call by our timeframe
834-
start_ts += timeframe
834+
if interval == self.KLINE_INTERVAL_1MONTH:
835+
start_ts = increment_month(start_ts)
836+
else:
837+
start_ts += timeframe
835838

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

914917
# increment next call by our timeframe
915-
start_ts += timeframe
918+
if interval == self.KLINE_INTERVAL_1MONTH:
919+
start_ts = increment_month(start_ts)
920+
else:
921+
start_ts += timeframe
916922

917923
# sleep after every 3rd call to be kind to the API
918924
if idx % 3 == 0:

binance/helpers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytz
55

66
from datetime import datetime
7+
from dateutil.relativedelta import relativedelta
78

89

910
def date_to_milliseconds(date_str):
@@ -50,3 +51,20 @@ def interval_to_milliseconds(interval):
5051
return int(interval[:-1]) * seconds_per_unit[interval[-1]] * 1000
5152
except (ValueError, KeyError):
5253
return None
54+
55+
56+
def increment_month(origin_ts):
57+
"""Increment a given timestamp by one month
58+
59+
:param origin_ts: original timestamp, e.g.: 1501545600000, 1504224000000, ...
60+
:type origin_ts: int
61+
62+
:return:
63+
Timestamp incremented by one month from a given timestamp
64+
"""
65+
d = datetime.fromtimestamp(origin_ts/1000) + relativedelta(months=1)
66+
# if the date is not timezone aware apply UTC timezone
67+
if d.tzinfo is None or d.tzinfo.utcoffset(d) is None:
68+
d = d.replace(tzinfo=pytz.utc)
69+
70+
return int(datetime.timestamp(d))

docs/helpers.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ Helper Functions
22
================
33

44
.. autoclass:: binance.helpers
5-
:members: date_to_milliseconds, interval_to_milliseconds
5+
:members: date_to_milliseconds, interval_to_milliseconds, increment_month
66
:noindex:

examples/save_historical_data.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import json
55

66
from datetime import datetime
7+
8+
from dateutil.relativedelta import relativedelta
79
from binance.client import Client
810

911

@@ -57,6 +59,23 @@ def interval_to_milliseconds(interval):
5759
return ms
5860

5961

62+
def increment_month(origin_ts):
63+
"""Increment a given timestamp by one month
64+
65+
:param origin_ts: original timestamp, e.g.: 1501545600000, 1504224000000, ...
66+
:type origin_ts: int
67+
68+
:return:
69+
Timestamp incremented by one month from a given timestamp
70+
"""
71+
d = datetime.fromtimestamp(origin_ts/1000) + relativedelta(months=1)
72+
# if the date is not timezone aware apply UTC timezone
73+
if d.tzinfo is None or d.tzinfo.utcoffset(d) is None:
74+
d = d.replace(tzinfo=pytz.utc)
75+
76+
return int(datetime.timestamp(d))
77+
78+
6079
def get_historical_klines(symbol, interval, start_str, end_str=None):
6180
"""Get Historical Klines from Binance
6281
@@ -118,10 +137,16 @@ def get_historical_klines(symbol, interval, start_str, end_str=None):
118137
output_data += temp_data
119138

120139
# update our start timestamp using the last value in the array and add the interval timeframe
121-
start_ts = temp_data[len(temp_data) - 1][0] + timeframe
140+
if interval == Client.KLINE_INTERVAL_1MONTH:
141+
start_ts = increment_month(temp_data[len(temp_data) - 1][0])
142+
else:
143+
start_ts = temp_data[len(temp_data) - 1][0] + timeframe
122144
else:
123145
# it wasn't listed yet, increment our start date
124-
start_ts += timeframe
146+
if interval == Client.KLINE_INTERVAL_1MONTH:
147+
start_ts = increment_month(start_ts)
148+
else:
149+
start_ts += timeframe
125150

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

0 commit comments

Comments
 (0)