-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinance-read-ids-to-allOrders-write-to-excel.py
76 lines (61 loc) · 2.51 KB
/
binance-read-ids-to-allOrders-write-to-excel.py
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
import urllib.parse
import hashlib
import hmac
import base64
import requests
import time
import pandas as pd
import io
from dotenv import load_dotenv
import os
# Load environment variables
load_dotenv()
api_url = "https://api.binance.us" # don't use wrong link scalar values error
# create a file named '.env' with the below keys like so: BINANCE_API_KEY="mysuperlongbinancekey"
api_key = os.environ.get("BINANCE_API_KEY")
secret_key = os.environ.get("BINANCE_SECRET_KEY")
def get_binanceus_signature(data, secret):
postdata = urllib.parse.urlencode(data)
message = postdata.encode()
byte_key = bytes(secret, 'UTF-8')
mac = hmac.new(byte_key, message, hashlib.sha256).hexdigest()
return mac
# Attach auth headers and return results from POST request
def binanceus_request_key(uri_path, data, api_key, api_sec):
headers = {}
headers['X-MBX-APIKEY'] = api_key
signature = get_binanceus_signature(data, api_sec)
params={
**data,
"signature": signature,
}
req = requests.get((api_url + uri_path), headers=headers, params=params)
return req.text
uri_path = "/api/v3/allOrders"
out_path = "E:\\prodigy\\Scripts\\api-testing\\binance\\" # windows needs double \\ escape the escape
df_nest_symbols = pd.read_excel(out_path + "binance.xlsx", sheet_name="BinanceCoinListManual", usecols="N", header=0) # using header none is the way, 0 = first row header
# print(df_nest_symbols) # prints pandas.DataFrame read from excel sheet
# time to loop that data in multiple calls. while also appending the data from the calls to another pandas.Dataframe
appended_data = []
for row in df_nest_symbols.itertuples():
data_symbol = {
"timestamp": int(time.time() * 1000),
"symbol": str.upper(row.binanceid)
}
print(data_symbol)
print("reading JSON data...")
result=binanceus_request_key(uri_path, data_symbol, api_key, secret_key)
print(result)
print("reading JSON data into DataFrame...")
df_nest = pd.read_json(io.StringIO(result))
print("DataFrame created succesfully")
print("DataFrame Shape:", df_nest.shape) # show dataframe is expanding
appended_data.append(df_nest)
appended_data = pd.concat(appended_data)
# now write all that data to excel sheet
try:
with pd.ExcelWriter(out_path + 'binance.xlsx', mode="a", engine="openpyxl", if_sheet_exists="replace") as writer:
appended_data.to_excel(writer, sheet_name="BinanceAllOrders", index=False)
print("DataFrame written to Excel")
except Exception as e:
print(f"Error: {e}")