-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinance_script.py
81 lines (64 loc) · 2.65 KB
/
binance_script.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
import time
import requests
from google.cloud import bigtable
from google.cloud.bigtable import column_family
from google.cloud.bigtable import row
from google.cloud import bigquery
# Binance API endpoint for getting ticker data
TICKER_URL = "https://api.binance.com/api/v3/ticker/24hr"
# Symbol of the coins to retrieve data for
SYMBOLS = ["BTCUSDT", "ETHUSDT", "BNBUSDT", "XRPUSDT"]
# Interval between data retrieval in seconds
INTERVAL = 30
def write_to_bigtable():
client = bigtable.Client(project='project_id', admin=True)
instance = client.instance('instance_id')
table = instance.table('table_id')
while True:
for symbol in SYMBOLS:
# Retrieve the ticker data from Binance
url = TICKER_URL + "?symbol=" + symbol
response = requests.get(url)
ticker_data = response.json()
open_price = ticker_data["openPrice"]
close_price = ticker_data["lastPrice"]
high_price = ticker_data["highPrice"]
low_price = ticker_data["lowPrice"]
volume = ticker_data["volume"]
# Save the data in Bigtable
column_family_id = "data"
row_key = f"{symbol}_{int(time.time())}"
row = table.row(row_key)
row.set_cell(column_family_id, "open", open_price)
row.set_cell(column_family_id, "close", close_price)
row.set_cell(column_family_id, "high", high_price)
row.set_cell(column_family_id, "low", low_price)
row.set_cell(column_family_id, "volume", volume)
row.commit()
# Wait for the specified interval before retrieving the next set of data
time.sleep(INTERVAL)
def write_to_bigquery():
client = bigquery.Client()
table_ref='table_id'
while True:
ls=[]
for symbol in SYMBOLS:
# Retrieve the ticker data from Binance
url = TICKER_URL + "?symbol=" + symbol
response = requests.get(url)
ticker_data = response.json()
empt_dict={}
#Retrieve the values for the various data points
empt_dict['crypto_name']=f"{symbol}_{int(time.time())}"
empt_dict['open_price'] = ticker_data["openPrice"]
empt_dict['close_price'] = ticker_data["lastPrice"]
empt_dict['high_price'] = ticker_data["highPrice"]
empt_dict['low_price'] = ticker_data["lowPrice"]
empt_dict['volume'] = ticker_data["volume"]
ls.append(empt_dict)
#print(ls)
client.insert_rows_json(table_ref,ls)
time.sleep(INTERVAL)
if __name__ == '__main__':
write_to_bigtable()
#write_to_bigquery()