FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-binance/examples/save_historical_data.py at develop · EncodingJerry/python-binance · GitHub
EncodingJerry
/
python-binance
Public
forked from
kbai/python-binance
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-binance
/
examples
/
save_historical_data.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
156 lines (121 loc) · 4.52 KB
Breadcrumbs
python-binance
/
examples
/
save_historical_data.py
Copy path
File metadata and controls
156 lines (121 loc) · 4.52 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
time
import
dateparser
import
pytz
import
json
from
datetime
import
datetime
from
binance
.
client
import
Client
def
date_to_milliseconds
(
date_str
):
"""Convert UTC date to milliseconds
If using offset strings add "UTC" to date string e.g. "now UTC", "11 hours ago UTC"
See dateparse docs for formats http://dateparser.readthedocs.io/en/latest/
:param date_str: date in readable format, i.e. "January 01, 2018", "11 hours ago UTC", "now UTC"
:type date_str: str
"""
# get epoch value in UTC
epoch
=
datetime
.
utcfromtimestamp
(
0
).
replace
(
tzinfo
=
pytz
.
utc
)
# parse our date string
d
=
dateparser
.
parse
(
date_str
)
# 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 the difference in time
return
int
((
d
-
epoch
).
total_seconds
()
*
1000.0
)
def
interval_to_milliseconds
(
interval
):
"""Convert a Binance interval string to milliseconds
:param interval: Binance interval string 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w
:type interval: str
:return:
None if unit not one of m, h, d or w
None if string not in correct format
int value of interval in milliseconds
"""
ms
=
None
seconds_per_unit
=
{
"m"
:
60
,
"h"
:
60
*
60
,
"d"
:
24
*
60
*
60
,
"w"
:
7
*
24
*
60
*
60
}
unit
=
interval
[
-
1
]
if
unit
in
seconds_per_unit
:
try
:
ms
=
int
(
interval
[:
-
1
])
*
seconds_per_unit
[
unit
]
*
1000
except
ValueError
:
pass
return
ms
def
get_historical_klines
(
symbol
,
interval
,
start_str
,
end_str
=
None
):
"""Get Historical Klines from Binance
See dateparse docs for valid start and end string formats http://dateparser.readthedocs.io/en/latest/
If using offset strings for dates add "UTC" to date string e.g. "now UTC", "11 hours ago UTC"
:param symbol: Name of symbol pair e.g BNBBTC
:type symbol: str
:param interval: Biannce Kline interval
:type interval: str
:param start_str: Start date string in UTC format
:type start_str: str
:param end_str: optional - end date string in UTC format
:type end_str: str
:return: list of OHLCV values
"""
# create the Binance client, no need for api key
client
=
Client
(
""
,
""
)
# init our list
output_data
=
[]
# setup the max limit
limit
=
500
# convert interval to useful value in seconds
timeframe
=
interval_to_milliseconds
(
interval
)
# convert our date strings to milliseconds
start_ts
=
date_to_milliseconds
(
start_str
)
# if an end time was passed convert it
end_ts
=
None
if
end_str
:
end_ts
=
date_to_milliseconds
(
end_str
)
idx
=
0
# it can be difficult to know when a symbol was listed on Binance so allow start time to be before list date
symbol_existed
=
False
while
True
:
# fetch the klines from start_ts up to max 500 entries or the end_ts if set
temp_data
=
client
.
get_klines
(
symbol
=
symbol
,
interval
=
interval
,
limit
=
limit
,
startTime
=
start_ts
,
endTime
=
end_ts
)
# handle the case where our start date is before the symbol pair listed on Binance
if
not
symbol_existed
and
len
(
temp_data
):
symbol_existed
=
True
if
symbol_existed
:
# append this loops data to our output data
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
else
:
# it wasn't listed yet, increment our start date
start_ts
+=
timeframe
idx
+=
1
# check if we received less than the required limit and exit the loop
if
len
(
temp_data
)
<
limit
:
# exit the while loop
break
# sleep after every 3rd call to be kind to the API
if
idx
%
3
==
0
:
time
.
sleep
(
1
)
return
output_data
symbol
=
"ETHBTC"
start
=
"1 Dec, 2017"
end
=
"1 Jan, 2018"
interval
=
Client
.
KLINE_INTERVAL_30MINUTE
klines
=
get_historical_klines
(
symbol
,
interval
,
start
,
end
)
# open a file with filename including symbol, interval and start and end converted to milliseconds
with
open
(
"Binance_{}_{}_{}-{}.json"
.
format
(
symbol
,
interval
,
date_to_milliseconds
(
start
),
date_to_milliseconds
(
end
)
),
'w'
# set file write mode
)
as
f
:
f
.
write
(
json
.
dumps
(
klines
))
Back
|
FazBrowse Home
|
New Git URL