Skip to content

Instantly share code, notes, and snippets.

@jeff47
Created April 13, 2021 18:57
Show Gist options
  • Save jeff47/37dab217176c5db5fbd8becdcc59b3cf to your computer and use it in GitHub Desktop.
Save jeff47/37dab217176c5db5fbd8becdcc59b3cf to your computer and use it in GitHub Desktop.
Get funding fee history from Binance for all tokens in one spreadsheet
#!/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()
@xuefeng-huang
Copy link

I have added pandas to do the analysis part instead of using pivot table, to my surprise they have different average values, not sure which one is correct. I am confused

import json
import requests
import csv
import pandas as pd
# %%
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.
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)
# %%
filename = "funding_fees.json"
with open(filename, 'w') as json_file:
    json_file.write('{"funding_data":')
    json.dump(history, json_file)
    json_file.write('}')
# %%
with open("funding_fees.json", 'r') as json_file:
    json_data = json.load(json_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()

df = pd.read_csv('funding_fees.csv')
agg_df = df.groupby('symbol').fundingRate.agg(['mean', 'std', 'min', 'max', 'count'])
agg_df.to_csv('agg_stats.csv')

@jeff47
Copy link
Author

jeff47 commented Apr 18, 2021

Wonderful! This was on my project list. I might move this over to regular GitHub.

You said the numbers didn't match my spreadsheet when you ran it - I think that's because the spreadsheet only gets updated when I run it manually. So the numbers are from the 15th I think.

@jeff47
Copy link
Author

jeff47 commented Apr 18, 2021

I would also like to add a column showing the stdev in the coin price, so that we can get a sense of the volatility of the token. That gives you an idea of potential deleverage/liquidation risk.

@xuefeng-huang
Copy link

stdev is already included under the column std

@jeff47
Copy link
Author

jeff47 commented Apr 18, 2021

stdev is already included under the column std

I mean stdev of the token price, not stdev of the funding fee.

@jeff47
Copy link
Author

jeff47 commented Apr 19, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment