Created
January 30, 2021 15:58
-
-
Save uneasyguy/efb826b9cab93471109cf35f6a45b95b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from time import sleep | |
from datetime import datetime | |
from binance.client import Client | |
from binance.helpers import date_to_milliseconds, interval_to_milliseconds | |
SECONDS_IN_MINUTE = 60 | |
def _sample_crude_api_limiting(headers, threshold=1190, delay_buffer=5, weight_updates=False, sleep_updates=True, force_sleep=False): | |
if headers: | |
used_api_weight = headers.get('x-mbx-used-weight-1m', 0) | |
if weight_updates: | |
print(f'Used API Weight: {used_api_weight}') | |
if force_sleep or int(used_api_weight) >= threshold: | |
seconds_into_current_minute = int(datetime.now().strftime("%S")) | |
wait_time = (SECONDS_IN_MINUTE + delay_buffer) - seconds_into_current_minute | |
if sleep_updates: | |
print(f'Going to sleep for {wait_time} seconds to avoid rate limit errors') | |
sleep(wait_time) | |
if sleep_updates: | |
print('I am alive again!') | |
def get_df_doc(symbol, **kwargs): | |
interval = kwargs.get('interval', '1m') | |
limit = kwargs.get('limit', 1000) | |
timeframe = interval_to_milliseconds(interval) | |
client = kwargs.get('client', Client()) | |
earliest_valid_timestamp = client._get_earliest_valid_timestamp(symbol, interval) | |
start_time = kwargs.get('start_time') | |
if not start_time: | |
start_time = earliest_valid_timestamp | |
elif isinstance(start_time,str): | |
start_time = date_to_milliseconds(start_time) | |
start_time = max(start_time, earliest_valid_timestamp) | |
end_time = kwargs.get('end_time') | |
if end_time and isinstance(end_time,str): | |
end_time = date_to_milliseconds(end_time) | |
request_params = { | |
'symbol':symbol, | |
'interval':interval, | |
'startTime':start_time, | |
'endTime':end_time, | |
'limit':limit | |
} | |
data_docv = [] | |
while True: | |
try: | |
klines = client.get_klines(**request_params) | |
if not klines: | |
break | |
print(klines[0]) | |
data_docv.extend([[line[0], line[1], line[4], line[5]] for line in klines]) | |
request_params['startTime'] = klines[-1][0] + timeframe | |
headers = client.response.headers | |
_sample_crude_api_limiting(headers) | |
except Exception as e: | |
print(f'Whoops, we ran into the following error: {e}') | |
_sample_crude_api_limiting(None,force_sleep=True) | |
def main(): | |
try: | |
symbol = 'BTCUSDT' | |
get_df_doc(symbol) | |
except Exception as e: | |
print(f'Whoops, we ran into the following error: {e}') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment