Created
April 13, 2021 18:57
-
-
Save jeff47/37dab217176c5db5fbd8becdcc59b3cf to your computer and use it in GitHub Desktop.
Get funding fee history from Binance for all tokens in one spreadsheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import json | |
import requests | |
import csv | |
# This will obtain the funding fee history from Binance for all the perpetual crypto contracts, for the given time period. | |
# Results will be saved in JSON and CSV format. It's easiest to load the CSV into Sheets and use a pivot table for analysis. | |
# All the data here is also available at https://www.binance.com/en/futures/funding-history/1, but the history is available | |
# only for single tokens at a time. | |
# How many days of history to get? 333 seems to be the max. | |
days = 90 | |
# Get all the symbols available | |
page = requests.get('https://fapi.binance.com/fapi/v1/premiumIndex') | |
futures = json.loads(page.content) | |
# Fetch the history for each symbol. | |
symbols = [] | |
history = [] | |
for i in futures: | |
page = requests.get('https://fapi.binance.com/fapi/v1/fundingRate', params={'symbol':i["symbol"],'limit':days*3}) | |
history += json.loads(page.content) | |
# Write history to a json file. | |
filename = "funding_fees.json" | |
with open(filename, 'w') as json_file: | |
json_file.write('{"funding_data":') | |
json.dump(history, json_file) | |
json_file.write('}') | |
# Read our JSON history file. There might be a better way to wrangle the data into the correct structure but this was easy | |
with open("funding_fees.json", 'r') as json_file: | |
json_data = json.load(json_file) | |
# Write history to CSV file. | |
fee_data = json_data['funding_data'] | |
data_file = open('funding_fees.csv', 'w') | |
csv_writer = csv.writer(data_file) | |
header = fee_data[0].keys() | |
csv_writer.writerow(header) | |
for fee in fee_data: | |
csv_writer.writerow(fee.values()) | |
data_file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I mean stdev of the token price, not stdev of the funding fee.