Skip to content

Instantly share code, notes, and snippets.

@melardev
Created April 20, 2024 09:42
Show Gist options
  • Save melardev/504513a7ae01e62b2d7d6f40bea01b57 to your computer and use it in GitHub Desktop.
Save melardev/504513a7ae01e62b2d7d6f40bea01b57 to your computer and use it in GitHub Desktop.
import ccxt
exchange = 'mexc'
if exchange == 'binance':
client = ccxt.binance()
elif exchange == 'coinbase':
client = ccxt.coinbase()
elif exchange == 'bitfinex':
client = ccxt.bitfinex()
elif exchange == 'okx':
client = ccxt.okx()
elif exchange == 'mexc':
client = ccxt.mexc()
else:
raise ValueError('Unknown exchange value')
exchange_upper = exchange.upper()
client.load_markets()
added = set()
count = 0
with open(f'{exchange}_btc_pairs.txt', 'w') as fd:
for market_key, market_info in client.markets.items():
if not market_info['active']:
# De-listed
continue
if market_info["quote"] != 'BTC':
continue
base = market_info['base']
if base in added:
continue
added.add(base)
quote = market_info["quote"]
ms = f'{exchange_upper}:{base}{quote}'
fd.write(ms + '\n')
count += 1
for market_key, market_info in client.markets.items():
if not market_info['active']:
# De-listed
continue
if market_info["quote"] != 'USDT':
continue
base = market_info['base']
if base in added:
continue
quote = market_info['quote']
added.add(base)
ms = f'{exchange_upper}:{market_info["base"]}{market_info["quote"]}/{exchange_upper}:BTCUSDT'
fd.write(ms + '\n')
count += 1
print(f'Written {exchange}_btc_pairs.txt with {count} lines')
# TradingView does not let us import lists with more than 1000 tickers, so split the file into smaller files
if count > 1000:
with open(f'{exchange}_btc_pairs.txt') as fd:
data = fd.read()
lines = data.split('\n')
file_number = 1
lines_written = 0
current_lines = []
# Iterate through the lines and write to files
for i, line in enumerate(lines):
current_lines.append(line)
lines_written += 1
# Check if it's time to write to a new file
if lines_written == 1000 or i == len(lines) - 1:
# Write current lines to a file
file_name = f'{exchange}_btc_pairs_{file_number}.txt'
with open(file_name, 'w') as output_fd:
output_fd.write('\n'.join(current_lines))
print(f'{file_name} completed')
# Reset variables
file_number += 1
lines_written = 0
current_lines = []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment