Added bybit source

This commit is contained in:
kalzu 2022-12-28 15:58:55 +02:00
parent 30b8c4b274
commit 1751a7f763
1 changed files with 35 additions and 3 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/python3
import krakenex
import krakenex, math
import json, sqlite3
import requests, os, time
import threading
@ -164,10 +164,41 @@ def fetch_gemini():
print(f"Error fetching data from Gemini API: {response.status_code}")
return empty_json
def fetch_bybit():
## BYBIT
#curl 'https://api-testnet.bybit.com/v2/public/kline/list?symbol=BTCUSD&interval=5&from=1672225349&limit=3'
base_url = 'https://api.bybit.com/v2/public/kline/list?symbol=BTCUSD&interval=5&from='
current_unixtime = int(time.time())
last_minute = math.floor(current_unixtime / 60)
last_minute_unixtime = str(last_minute * 60 - 300)
query_url = ''.join([base_url, last_minute_unixtime])
print(query_url)
response = requests.get(query_url)
if response.status_code == 200: # check if the request was successful
bybit_ohlc = response.json()
candle_stick_data = {
'exchange': 'bybit',
'timestamp': bybit_ohlc['result'][0]['open_time'],
'open': bybit_ohlc['result'][0]['open'],
'high': bybit_ohlc['result'][0]['high'],
'low': bybit_ohlc['result'][0]['low'],
'close': bybit_ohlc['result'][0]['close'],
'volume_quote': bybit_ohlc['result'][0]['volume'],
'volume_base': bybit_ohlc['result'][0]['turnover'],
'trades': 0
}
bybit_json = json.dumps(candle_stick_data, indent=2)
print(bybit_json)
return bybit_json
else:
print(f"Error fetching data from Bybit API: {response.status_code}")
return empty_json
def write_dict_to_database(in_dict, connection):
cursor = connection.cursor()
# Use placeholders for the values in the INSERT statement
insert_query = "INSERT INTO ohlc (exchange, timestamp, open, high, low, close, volume_quote, volume_base, trades) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
# use placeholders for the values in the insert statement
insert_query = "insert into ohlc (exchange, timestamp, open, high, low, close, volume_quote, volume_base, trades) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
values = (in_dict['exchange'],
in_dict['timestamp'],
@ -191,6 +222,7 @@ def get_the_data():
write_dict_to_database(json.loads(fetch_bitfinex()), db)
write_dict_to_database(json.loads(fetch_bitstamp()), db)
write_dict_to_database(json.loads(fetch_gemini()), db)
write_dict_to_database(json.loads(fetch_bybit()), db)
db.close()
print("fetches done at", time.time(), "sleeping now for 290")
time.sleep(290)