-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbybit_funding_rate.py
58 lines (45 loc) · 1.8 KB
/
bybit_funding_rate.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
import requests
import csv
from datetime import datetime
# Bybit API endpoint for funding rate history
url = "https://api.bybit.com/v5/market/funding/history"
# API Key and Secret
api_key = "insert your api key"
api_secret = "insert your secret key"
# Define the query parameters
params = {
"category": "linear",
"symbol": "ETHUSD",
}
# Add the API key to the headers
headers = {
"api_key": api_key,
}
# Send a GET request to the Bybit API
response = requests.get(url, params=params, headers=headers)
# Check if the request was successful
if response.status_code == 200:
data = response.json()["result"]["list"]
# Specify the CSV file name
csv_file_name = "eth_funding_rate.csv"
# Open the CSV file for writing
with open(csv_file_name, 'w', newline='') as csvfile:
# Create a CSV writer
csv_writer = csv.writer(csvfile)
# Define the header row
header = ["symbol", "fundingRate", "fundingRateTimestamp", "fundingTimeDate"]
csv_writer.writerow(header)
# Write the data rows
for row in data:
# Convert the timestamp to a string date format
timestamp_ms = int(row["fundingRateTimestamp"]) # Convert the string to an integer
timestamp_sec = timestamp_ms / 1000 # Convert milliseconds to seconds
date_str = datetime.utcfromtimestamp(timestamp_sec).strftime('%Y-%m-%d %H:%M:%S')
# Append the date string to the row
row["fundingTimeDate"] = date_str
# Write the row to the CSV file
csv_writer.writerow([row["symbol"], row["fundingRate"], row["fundingRateTimestamp"], row["fundingTimeDate"]])
print(f"Funding rate data saved to {csv_file_name}")
else:
print(f"Failed to fetch data. Status code: {response.status_code}")
print(response.text)