-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtools.py
More file actions
1296 lines (1019 loc) · 36.4 KB
/
tools.py
File metadata and controls
1296 lines (1019 loc) · 36.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pandas as pd
import os.path
from os import walk
import os
from urllib.parse import urlencode
import requests as req
import hmac
import hashlib
import math
from constants import *
from datetime import *
import time
from zipfile import ZipFile
"""
LOCAL TOOLS
"""
def create_folder_structure():
for fp in binance_folder_structure:
check_create_fp(fp)
def create_api_cfg(apikey, apisecret):
api_file = open(cfg_filepath, 'w+')
api_file.write(f'{api_file_lines[0]}\n')
api_file.write(f'{api_file_lines[1]}{apikey}\n')
api_file.write(f'{api_file_lines[2]}{apisecret}\n')
api_file.close()
def currenttime():
return int(time.time())
def get_margin_type(symbol):
inverse_exchange_info = pd.read_csv(binance_inverse_exchange_info_file)
linear_exchange_info = pd.read_csv(binance_linear_exchange_info_file)
if '_spot' in symbol:
return SPOT
elif inverse_exchange_info['symbol'].isin([symbol]).any():
return INVERSE
elif linear_exchange_info['symbol'].isin([symbol]).any():
return LINEAR
def create_structure():
"""
Only run this once,
:return:
"""
"""
DOWNLOAD TOOLS
"""
def link_exists(path):
"""
Checks if a link exists
:param path: str
:return: bool
"""
r = req.head(path)
return r.status_code == req.codes.ok
def download_file(url, path, filename):
"""
Downloads file to the path specified.
Filename is changed as well.
:param url: str
:param path: str
:param filename: str
:return: None
"""
r = req.get(f'{url}{filename}')
f = open(f'{path}{filename}', 'wb')
if r.status_code == 200:
for chunk in r.iter_content(1024):
f.write(chunk)
f.close()
def get_checksum(filename, hash_function):
"""Generate checksum for file baed on hash function (MD5 or SHA256).
Args:
filename (str): Path to file that will have the checksum generated.
hash_function (str): Hash function name - supports MD5 or SHA256
Returns:
str`: Checksum based on Hash function of choice.
Raises:
Exception: Invalid hash function is entered.
"""
hash_function = hash_function.lower()
with open(filename, "rb") as f:
bytes_data = f.read() # read file as bytes
if hash_function == "md5":
readable_hash = hashlib.md5(bytes_data).hexdigest()
elif hash_function == "sha256":
readable_hash = hashlib.sha256(bytes_data).hexdigest()
else:
print("Invalid hash function. Please Enter MD5 or SHA256")
return readable_hash
def unzip_file(filepath, dest_dir):
file_msg = filepath.split('/')[-1]
with ZipFile(filepath, 'r') as zf:
zf.extractall(
path=dest_dir
)
print(f'Unzipped: {file_msg}')
def compare_checksum(file, checksum):
"""
Compares checksum to file, making sure it is the correct file.
Currently set to SHA256
:return: bool
"""
f = open(checksum, "r")
if f.read().split(' ')[0] == get_checksum(file, 'SHA256'):
return True
else:
return False
def clean_ohlc(filepath):
"""
Filename to be parsed from filepath, and subsequently data scrubbed
- Time column to be changed to int
- Find repeat time data
- Find gaps in data (time gaps)
:param filepath: file PATH of the file - not file name
:return: None
"""
filename = filepath.split('/')[-1]
parsefilename = filename.split('.')[0].split('_')
del parsefilename[-1]
if 'PERP' in parsefilename:
parsefilename.remove('PERP')
parsefilename[1] = f'{parsefilename[1]}_PERP'
data = pd.read_csv(filepath)
data.drop_duplicates('time', inplace=True, ignore_index=True)
data['time'] = data.apply(lambda x: x['time']/1000 if x['time'] > 9999999999 else x['time'], axis=1)
data['time'] = data.apply(lambda x: int(x['time']), axis=1)
data.to_csv(filepath, index=False)
print('Completed cleanse!')
def clean_ohlcv_csv(file):
data = pd.read_csv(file,
header=None,
names=ohlc_col,
usecols=[0, 1, 2, 3, 4, 5]
)
data['time'] = data['time'].div(1000)
data['time'] = data.apply(lambda x: int(x['time']), axis=1)
return data
def list_of_files(folder_path):
return next(
walk(folder_path), (None, None, [])
)[2] # [] if no file
def check_create_fp(filepath):
# Debug Print
print(f"Checking filepath ({filepath})")
if not os.path.isdir(filepath): # If does not exist
os.mkdir(filepath) # Create filepath
print(f'Created filepath')
else: # If the filepath does exist
pass
# print(f'Filepath already exists!')
def check_fp(filepath):
if os.path.isdir(filepath):
return True
else:
return False
def check_fn(filename):
if os.path.isfile(filename):
return True
else:
return False
def convert_unix_interval_to_str(interval):
for val in binance_intervaltable.keys():
if binance_intervaltable[val] == interval:
return val
def convert_str_interval_to_unix(interval):
"""
:param interval: String of interval ex: '15m'
:type interval: str
:return: int
"""
return binance_intervaltable[interval]
"""
DATE TOOLS
"""
def get_last_closed_time(unixinterval):
return (int(time.time() - unixinterval) // unixinterval) * unixinterval
def round_nearest(x, a):
return round(x / a) * a
def round_down(x, a):
return math.floor(x / a) * a
def leapyr(n):
if n % 400 == 0:
return True
if n % 100 == 0:
return False
if n % 4 == 0:
return True
return False
def convert_timecode_to_readable(timecode):
"""
:param timecode: UTC timecode
:return: %Y-%m-%d %H:%M:%S
"""
return datetime.utcfromtimestamp(timecode).strftime('%Y-%m-%d %H:%M:%S')
def convert_readable_date_to_timecode(readable):
"""
:param readable: %Y-%m-%d
:return: UTC timecode
"""
# ASSUME TIME MIDNIGHT
return int(
datetime.strptime(
readable,
"%Y-%m-%d").replace(
tzinfo=timezone.utc
).timestamp()
)
def convert_readable_datetime_to_timecode(time_and_date, **kwargs):
"""
:param time_and_date: 2021-08-12 23:59
:return: default unit in seconds
"""
if 'unit' not in kwargs:
return int(
datetime.strptime(
time_and_date,
"%Y-%m-%d %H:%M").replace(
tzinfo=timezone.utc
).timestamp()
)
else:
if kwargs['unit'] == 'ms':
return int(
datetime.strptime(
time_and_date,
"%Y-%m-%d %H:%M").replace(
tzinfo=timezone.utc
).timestamp()
) * 1000
def convert_readable_datetime_seconds_to_timecode(time_and_date):
"""
:param time_and_date: 2021-08-12 23:59:00
:return:
"""
return int(
datetime.strptime(
time_and_date,
"%Y-%m-%d %H:%M:%S").replace(
tzinfo=timezone.utc
).timestamp()
)
def get_time_from_timecode(timecode):
"""
:param timecode: UTC timecode
:return: %H:%M:%S
"""
return datetime.utcfromtimestamp(timecode).strftime('%H:%M:%S')
def get_date_from_timecode(timecode):
readable = convert_timecode_to_readable(timecode)
return readable.split(' ')[0].split('-')[2]
def get_month_from_timecode(timecode):
readable = convert_timecode_to_readable(timecode)
return readable.split(' ')[0].split('-')[1]
def get_year_from_timecode(timecode):
readable = convert_timecode_to_readable(timecode)
return readable.split(' ')[0].split('-')[0]
def month_iterate(month):
if int(month) < 9:
return f'0{(int(month) + 1)}'
elif int(month) == 12:
return '01'
else:
return str(int(month) + 1)
def currentdate():
return convert_timecode_to_readable(currenttime()).split(' ')[0]
def yesterday():
return convert_timecode_to_readable(currenttime() - 86400).split(' ')[0]
def lastdate(the_date):
# 2021-08-12 23:59:00
day = the_date.split('-')[2]
month = the_date.split('-')[1]
year = the_date.split('-')[0]
if month == '01' and day == '01':
# GO TO END OF YEAR
year = int(year) - 1
month = '12'
day = '31'
elif day == '01' and \
leapyr(int(year)) and month == '03':
# LEAP YEAR FEB
day = '29'
month = '02'
elif day == '01' and leapyr(int(year)) is False:
if int(month) > 10:
month = str(int(month) + 1)
else:
month = f'0{int(month) - 1}'
# END OF MONTH
day = str(calendar_table[month])
elif int(day) > 10:
day = int(day) - 1
else:
day = f'0{int(day) - 1}'
return f'{year}-{month}-{day}'
def lastmonth(month):
if int(month) > 10:
return str(int(month) - 1)
elif month == '01':
return '12'
else:
return f'0{int(month) - 1}'
def last_month_full(the_date):
month = the_date.split('-')[1]
year = the_date.split('-')[0]
if int(month) > 9:
month = str(int(month) - 1)
elif month == '01':
month = '12'
year = str(int(year) - 1)
else:
month = f'0{int(month) - 1}'
return f'{year}-{month}-01'
def last_month_first(the_date):
month = the_date.split('-')[1]
year = the_date.split('-')[0]
if int(month) > 10:
month = str(int(month) - 1)
elif month == '01':
month = '12'
year = str(int(year) - 1)
else:
month = f'0{int(month) - 1}'
return f'{year}-{month}-01'
def last_year(the_date):
return f"{int(the_date.split('-')[0]) - 1}-{the_date.split('-')[1]}-{the_date.split('-')[2]}"
def nextmonth(month):
if int(month) < 9:
return f'0{int(month) + 1}'
elif month == '12':
return '01'
else:
return str(int(month) + 1)
def next_month_full(the_date):
month = the_date.split('-')[1]
year = the_date.split('-')[0]
if int(month) < 9:
month = f'0{int(month) + 1}'
elif month == '12':
month = '01'
year = str(int(year) + 1)
else:
month = str(int(month) + 1)
return f'{year}-{month}-xx'
def next_month_first(the_date):
month = the_date.split('-')[1]
year = the_date.split('-')[0]
if int(month) < 9:
month = f'0{int(month) + 1}'
elif month == '12':
month = '01'
year = str(int(year) + 1)
else:
month = str(int(month) + 1)
return f'{year}-{month}-01'
def nextdate(the_date):
# 2021-08-12 23:59:00
day = the_date.split('-')[2]
month = the_date.split('-')[1]
year = the_date.split('-')[0]
if month == '12' and day == '31':
# END OF YEAR
year = int(year) + 1
month = '01'
day = '01'
elif day == str(calendar_table[month] + 1) and \
leapyr(int(year)) and month == '02':
# LEAP YEAR FEB
month = '03'
day = '01'
elif day == str(calendar_table[month]) and leapyr(int(year)) is False:
if int(month) < 9:
month = f'0{int(month) + 1}'
else:
month = str(int(month) + 1)
# END OF MONTH
day = '01'
elif int(day) < 9:
# +1 WITH ZERO ADDED
day = f'0{int(day) + 1}'
else:
# ALL OTHER TIMES
day = int(day) + 1
return f'{year}-{month}-{day}'
def next_day_from(timecode):
# print(f'next day from {timecode} // {convert_timecode_to_readable(timecode)}')
return convert_readable_date_to_timecode(nextdate(convert_timecode_to_readable(timecode).split(' ')[0]))
def end_of_month_from(timecode):
return convert_readable_date_to_timecode(next_month_first(convert_timecode_to_readable(timecode)))
def is_midnight(timecode):
if convert_timecode_to_readable(timecode).split(' ')[1] == '00:00:00':
return True
else:
return False
def is_first_of_month(timecode):
if get_date_from_timecode(timecode) == '01':
return True
else:
return False
def month_exists(filepath, timecode):
"""
Finds out if the specific month is in the filepath
:param filepath: str
:param timecode: int
:return: bool
"""
filelist = list_of_files(filepath)
month = get_month_from_timecode(timecode)
year = get_year_from_timecode(timecode)
exchange = filepath.split('/')[2].upper()
symbol = filepath.split('/')[4].upper()
interval = filepath.split('/')[-1]
# f"{interval}_{symbol}_BINANCE_OHLCDB-{month}-{year}.csv"
filename = f"{interval}_{symbol}_{exchange}_OHLCDB-{month}-{year}.csv"
if filename in filelist:
return True
else:
return False
def ohlc_filename_timecode(interval, symbol, timecode):
month = get_month_from_timecode(timecode)
year = get_year_from_timecode(timecode)
return f"{interval}_{symbol}_BINANCE_OHLCDB-{month}-{year}.csv"
def db_link_exists(timecode, symbol, interval, mode):
the_date = convert_timecode_to_readable(timecode).split(' ')[0]
# https://data.binance.vision/data/futures/cm/monthly/klines/ADAUSD_PERP/1m/ADAUSD_PERP-1m-2020-08.zip
linkpath = 'https://data.binance.vision/data/futures/'
margin_type = get_margin_type(symbol)
dateparse = the_date.split('-')
if margin_type == INVERSE:
linkpath += 'cm/'
elif margin_type == LINEAR:
linkpath += 'um/'
linkpath += f'{mode}/klines/{symbol}/{interval}/'
filename = f"{symbol}-{interval}-{dateparse[0]}-{dateparse[1]}.zip"
print(f"Checking: {filename}")
if link_exists(linkpath+filename):
return True
else:
return False
def timecode_date_convert(timecode, new_func):
inter_val = new_func(
convert_timecode_to_readable(timecode).split(' ')[0]
)
return convert_readable_date_to_timecode(inter_val)
"""
REST API TOOLS
"""
def hashing(query_string):
# Hashing function
return hmac.new(binance_apisecret.encode('utf-8'), query_string.encode('utf-8'), hashlib.sha256).hexdigest()
def dispatch_request(http_method):
session = req.Session()
session.headers.update({
'Content-Type': 'application/json;charset=utf-8',
'X-MBX-APIKEY': binance_apikey
})
return {
'GET': session.get,
'DELETE': session.delete,
'PUT': session.put,
'POST': session.post,
}.get(http_method, 'GET')
# noinspection PyTypeChecker
def send_signed_request(http_method, url_path, base_url, payload=None):
if payload is None:
payload = {}
# Encode to be in URL
query_string = urlencode(payload)
# Timestamp
if query_string:
query_string = "{}×tamp={}".format(query_string, servertime())
else:
query_string = 'timestamp={}'.format(servertime())
# url = cm_base_url + url_path + '?' + query_string + '&signature=' + hashing(query_string)
url = base_url + url_path + '?' + query_string + '&signature=' + hashing(query_string)
print("{} {}".format(http_method, url))
params = {'url': url, 'params': {}}
response = dispatch_request(http_method)(**params)
return response.json()
# noinspection PyTypeChecker
def send_public_request(url_path, base_url, payload=None):
if payload is None:
payload = {}
query_string = urlencode(payload, True)
url = base_url + url_path
if query_string:
url = url + '?' + query_string
print("{}".format(url))
response = dispatch_request('GET')(url=url)
return response.json()
def servertime():
return int(req.get(binance_inverse_base_url + '/dapi/v1/time').json()['serverTime'] / 1000) * 1000
def get_exchange_info(mode):
"""
:param mode: int
:return: None
"""
# Variable Definition
endpoint = None
base_url = None
filepath = None
if mode == 0: # LINEAR
endpoint = binance_linear_exchange_info_ep
base_url = binance_linear_base_url
mode = 'linear'
filepath = binance_linear_exchange_info_file
elif mode == 1: # INVERSE
endpoint = binance_inverse_exchange_info_ep
base_url = binance_inverse_base_url
mode = 'inverse'
filepath = binance_inverse_exchange_info_file
elif mode == 2: # SPOT
endpoint = binance_spot_exchange_info_ep
base_url = binance_spot_base_url
mode = 'spot'
filepath = binance_spot_exchange_info_file
# Send Request
exchangeinfo = send_public_request(
endpoint,
base_url
)
# Create dataframe
df = pd.DataFrame(exchangeinfo['symbols'])
# Adding suffix to symbol due to name overlap
if mode == 'spot':
df['symbol'] = df['symbol'].apply(lambda symbol: f"{symbol}_SPOT")
# Write dataframe to csv
df.to_csv(filepath)
print(f'Updated and saved Binance {mode} exchange info to CSV.')
def get_klines(symbol, interval, **kwargs):
# Endpoint and URL declaration
kline_ep = None
base_url = None
# Result dataframe
res = pd.DataFrame(columns=ohlc_col)
# Parameters
params = {
'symbol': symbol,
'interval': interval
}
# Adding start time and end time to parameters
if 'starttime' in kwargs:
params['startTime'] = int(int(kwargs['starttime']) * 1000)
if 'limit' in kwargs:
params['limit'] = kwargs['limit']
if 'endtime' in kwargs:
params['endTime'] = int(int(kwargs['endtime']) * 1000)
# Changing endpoint depending on margin type
if get_margin_type(symbol) == INVERSE:
kline_ep = binance_inverse_klines_ep
base_url = binance_inverse_base_url
elif get_margin_type(symbol) == LINEAR:
kline_ep = binance_linear_klines_ep
base_url = binance_linear_base_url
elif get_margin_type(symbol) == SPOT:
kline_ep = binance_spot_klines_ep
base_url = binance_spot_base_url
# Sending request
result = send_public_request(
kline_ep,
base_url,
params
)
# Creation of kline dataframe
for klines in result:
kline = dict()
kline['time'] = int(klines[0] / 1000)
kline['open'] = klines[1]
kline['high'] = klines[2]
kline['low'] = klines[3]
kline['close'] = klines[4]
kline['volume'] = klines[5]
res = res.append(kline, ignore_index=True)
return res
"""
DATABASE TOOLS
"""
def get_latest_ohlc_filetime(filepath):
dbo = next(
walk(f'{filepath}'), (None, None, [])
)[2] # [] if no file
latest_file = None
for files in dbo:
parse = files.split('_')[-1].split('.')[0].split('-')
if latest_file is None:
latest_file = files
else:
if int(parse[-1]) >= int(latest_file.split('_')[-1].split('.')[0].split('-')[-1]):
latest_file = files
if int(parse[-2]) >= int(latest_file.split('_')[-1].split('.')[0].split('-')[-2]):
latest_file = files
lfile = pd.read_csv(f'{filepath}/{latest_file}')
return lfile['time'].iloc[-1]
def get_latest_ohlc_file(filepath):
dbo = next(
walk(f'{filepath}'), (None, None, [])
)[2] # [] if no file
latest_file = None
for files in dbo:
parse = files.split('_')[-1].split('.')[0].split('-')
if latest_file is None:
latest_file = files
else:
if int(parse[-1]) >= int(latest_file.split('_')[-1].split('.')[0].split('-')[-1]):
latest_file = files
if int(parse[-2]) >= int(latest_file.split('_')[-1].split('.')[0].split('-')[-2]):
latest_file = files
return latest_file
def get_earliest_ohlc_file(filepath):
dbo = next(
walk(filepath), (None, None, [])
)[2] # [] if no file
earliest_file = None
for files in dbo:
parse = files.split('_')[-1].split('.')[0].split('-')
if earliest_file is None:
earliest_file = files
else:
if int(parse[-1]) < int(earliest_file.split('_')[-1].split('.')[0].split('-')[-1]):
earliest_file = files
if int(parse[-2]) < int(earliest_file.split('_')[-1].split('.')[0].split('-')[-2]):
earliest_file = files
return earliest_file
def get_earliest_ohlc_filetime(filepath):
"""
Get the earliest ohlc filetime for specific filepath
Filepath Example: data/ohlc/binance/linear/BTCUSDT/5m/
:param filepath:
:return:
"""
# print(f'Getting earliest ohlc filetime for: {filepath}')
dbo = next(
walk(filepath), (None, None, [])
)[2] # [] if no file
earliest_file = None
for files in dbo:
parse = files.split('_')[-1].split('.')[0].split('-')
if earliest_file is None:
earliest_file = files
else:
if int(parse[-1]) < int(earliest_file.split('_')[-1].split('.')[0].split('-')[-1]):
earliest_file = files
if int(parse[-2]) < int(earliest_file.split('_')[-1].split('.')[0].split('-')[-2]):
earliest_file = files
efile = pd.read_csv(f'{filepath}/{earliest_file}')
return efile['time'].iloc[0]
def write_db(symbol, interval, str_date, dataframe):
"""
symbol,
intervals,
f'{year}-{month}-01',
new_month
"""
margin_type = get_margin_type(symbol)
month = str_date.split('-')[1]
year = str_date.split('-')[0]
filename = f"{interval}_{symbol}_BINANCE_OHLCDB-{month}-{year}.csv"
filepath = ''
if margin_type == INVERSE:
filepath += f'{binance_ohlc_filepath}{margin_type.lower()}/{symbol}/'
elif margin_type == LINEAR:
filepath += f'{binance_ohlc_filepath}{margin_type.lower()}/{symbol}/'
check_create_fp(filepath)
filepath += f'{interval}/'
check_create_fp(filepath)
dataframe.to_csv(
filepath+filename,
index=False
)
print(f'Written file: {filename}')
def write_exchangeinfo(mode=None):
"""
Function to write specific exchange information
:param mode: int
:return: None
"""
if mode is None:
for x in range(3):
get_exchange_info(x)
else:
get_exchange_info(mode)
def batch_binance_downloader(symbol, interval, batch_type, **kwargs):
"""
This function downloads each individual batch depending on the scale of download required.
That is, it can download klines from that day (through REST API requests) or download them from
data.binance.vision/ and unzip them accordingly.
"""
# Variable definition
margin_type = get_margin_type(symbol)
unix_interval = convert_str_interval_to_unix(interval)
filename = None
if batch_type != 'intraday': # Monthly or Daily
link_path = 'https://data.binance.vision/data/futures/'
# String for download link, INVERSE / LINEAR
if margin_type == INVERSE:
link_path += 'cm/'
elif margin_type == LINEAR:
link_path += 'um/'
# Update link path
link_path += batch_type + f'/klines/{symbol}/{interval}/'
# Start construction of filename
month = get_month_from_timecode(kwargs['starttime'])
year = get_year_from_timecode(kwargs['starttime'])
if batch_type == 'daily':
the_date = get_date_from_timecode(kwargs['starttime'])
# Filename construction
filename = f"{symbol}-{interval}-{year}-{month}-{the_date}.zip"
elif batch_type == 'monthly':
# Filename construction
filename = f"{symbol}-{interval}-{year}-{month}.zip"
print(f"Downloading from: {link_path + filename}")
if link_exists(f'{link_path}{filename}'): # If the file does not exist
temp_path = f"{binance_ohlc_filepath}temp/"
download_path = f"{binance_ohlc_filepath}{symbol}/{interval}/"
print(f"Downloading to: {download_path}")
# Download File
if link_exists(f'{link_path}{filename}'):
download_file(link_path, temp_path, filename)
print(f'Download Success! // {filename}')
else:
print('---- ERROR ----')
print('Download Unsuccessful! - OHLC file does not seem to exist!')
print(f"Filename: {filename}")
# Download Checksum
if link_exists(f'{link_path}{filename}.CHECKSUM'):
download_file(link_path, temp_path, f'{filename}.CHECKSUM')
print(f'Download Success! // {filename}.CHECKSUM')
else:
print('---- ERROR ----')
print('Download Unsuccessful! - Checksum does not seem to exist!')
print(f"Filename: {filename}")
# If checksum computes - unzip file
if compare_checksum(temp_path + filename, temp_path + filename + '.CHECKSUM'):
unzip_file(temp_path + filename, temp_path)
else: # Otherwise, give error message
print(f'The checksum does not seem to be right for filename: {filename}')
return pd.DataFrame()
# Create sanitised file
ret_file = clean_ohlcv_csv(temp_path + filename)
# Remove temp files
for files in list_of_files(temp_path):
os.remove(f"{temp_path}{files}")
print(f'Removed file: {temp_path}{files}')
return ret_file
else:
print('---- ERROR ----')
print('Download Unsuccessful! - OHLC file does not seem to exist!')
print(f"Filename: {filename}")
print('Adjusting to intraday')
# batch_type = 'intraday'
# Batch type tells us how large of a batch we are downloading, monthly, daily or intraday
else:
"""
Create db via REST API to be attached accordingly
endtime is REQUIRED
"""
# Return dataframe defined
ret_file = pd.DataFrame(columns=ohlc_col)
# Adjusting datatypes
start_time = int(kwargs['starttime'])
end_time = int(kwargs['endtime'])
# The kline grab loop
while True:
# Checking whether the distance to end_time is within limit
distance = int((end_time - start_time) / unix_interval)
# Some limit to distance
if distance < 500:
next_start = end_time - unix_interval
limit = distance
# Max distance
else:
limit = 500
next_start = start_time + (limit * unix_interval)
if limit == 0:
break
new_data = get_klines(
symbol,
interval,
starttime=start_time,
endtime=end_time,
limit=limit
)
# Append data to existing dataframe
ret_file = ret_file.append(
new_data,
ignore_index=True
)
# Check if we are at end_time
if next_start == end_time - unix_interval:
# Break loop to return data
break
else:
# Update start time to next one.
start_time = next_start