Skip to content

Instantly share code, notes, and snippets.

@melardev
Last active December 6, 2022 19:54
Show Gist options
  • Save melardev/a6aafc81bd3700409efec1180611eb13 to your computer and use it in GitHub Desktop.
Save melardev/a6aafc81bd3700409efec1180611eb13 to your computer and use it in GitHub Desktop.
"""
This code will fetch all trading pairs in Binance Futures, and generate a file named binance_futures.txt which is ready
to be imported into TradingView as a list of all Binance Futures trading pairs to monitor.
If the snippet is run on Google Colab, it will download the file we wrote to. Please note that as of today 6/12/2022 fetching Binance API
is not possible from Google Colab and so it will fail, this is Binance restricting by Google Colab's IP access.
"""
import requests
url = "https://fapi.binance.com/fapi/v1/exchangeInfo"
excluded_quotes = ['USD', 'USDD']
response = requests.get(url)
if response.status_code != 200:
raise ValueError(f'Invalid HTTP response {response.content.decode()}')
data = response.json()
message = ''
count = 0
for pair in data['symbols']:
if pair['status'] == "TRADING" and pair['contractType'] == "PERPETUAL":
if pair['quoteAsset'] not in excluded_quotes:
message += f"BINANCE:{pair['baseAsset']}{pair['quoteAsset']}PERP,"
count += 1
if count > 0:
print(f'{count} Trading pairs collected')
message = message[:-1]
with open('binance_futures.txt', 'w') as fd:
fd.write(message)
try:
import google.colab
running_in_colab = True
except:
running_in_colab = False
if running_in_colab:
files.download('binance_futures.txt')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment