-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinance-read-coins-to-depositHistory-write-to-excel.py
84 lines (67 loc) · 3.23 KB
/
binance-read-coins-to-depositHistory-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
76
77
78
79
80
81
82
83
84
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
def binanceus_request(uri_path):
req = requests.get(api_url + uri_path)
return req.text
# uri_path = "/api/v3/allOrders"
uri_path = "/sapi/v1/capital/deposit/hisrec"
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="B", header=0) # using header none is the way, 0 = first row header
# df_nested_symbol_str = df_nest_symbols.to_string(index=False, header=False) # in compilation with headers none, this will read the single cell data to a variable
# 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 a pandas.Dataframe
appended_data = []
for row in df_nest_symbols.itertuples():
data_symbol = {
"timestamp": int(time.time() * 1000),
# "startTime": '90', # within 1-90 days for some reason trash but be a unix timestamp or something a LONG?! default is 90 days anyways, but you could roll this every 15 days and only pull last 15 days worth of data, check again x or something list goes on
"coin": str.upper(row.Ticker2) # might work if you use coin instead of symbol..
}
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)
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="BinanceDepositHistory", index=False)
print("DataFrame written to Excel")
except Exception as e:
print(f"Error: {e}")
# might work if you use coin instead of symbol in each call. if you use symbol, it will just pull data apparently.. idk, maybe it's actually not mandatory....