Skip to content

Instantly share code, notes, and snippets.

@stalequant
Created December 17, 2024 20:07
Show Gist options
  • Save stalequant/776abd88efb920e758041e4283afce2a to your computer and use it in GitHub Desktop.
Save stalequant/776abd88efb920e758041e4283afce2a to your computer and use it in GitHub Desktop.
basic hl ratios
# -*- coding: utf-8 -*-
"""
CODE BY STALEQUANT BASED ON CODE FROM GAUTIER https://x.com/phoenixstealthy
"""
import matplotlib.pyplot as plt
import ccxt
import pandas as pd
from datetime import datetime, timedelta
# Initialize exchanges
binance = ccxt.binance({
'options': {
'defaultType': 'future', # Set to 'future' for Binance Futures
}
})
hyperliquid = ccxt.hyperliquid()
# Helper function to fetch daily volume since a specific date
def fetch_daily_volumes_since(exchange, markets, since_timestamp):
daily_volumes = {}
for market in markets:
try:
if ((exchange.id == 'binance' and market.endswith('USDT:USDT'))
or (exchange.id == 'hyperliquid' and market.endswith('USDC:USDC'))):
# Fetch daily OHLCV data since the specified timestamp
ohlcv = exchange.fetch_ohlcv(market, timeframe='1d', since=since_timestamp)
for candle in ohlcv:
timestamp = candle[0]
date_str = exchange.iso8601(timestamp).split('T')[0] # Convert to YYYY-MM-DD
close_price = candle[4]
volume = candle[5]
if date_str not in daily_volumes:
daily_volumes[date_str] = 0
daily_volumes[date_str] += close_price * volume
except Exception as e:
print(f"Error processing market {market} on {exchange.id}: {e}")
return daily_volumes
# Calculate the timestamp for 180 days ago
n_days = 180
since_date = datetime.now() - timedelta(days=n_days)
since_timestamp = int(since_date.timestamp() * 1000)
# Fetch all markets for Binance Futures and Hyperliquid
binance_markets = binance.load_markets()
hyperliquid_markets = hyperliquid.load_markets()
# Fetch daily volumes since 180 days ago for Binance Futures
binance_volumes_since = fetch_daily_volumes_since(binance, binance_markets, since_timestamp)
# Fetch daily volumes since 180 days ago for Hyperliquid
hyperliquid_volumes_since = fetch_daily_volumes_since(hyperliquid, hyperliquid_markets, since_timestamp)
# Convert the results to DataFrames
binance_df = pd.DataFrame.from_dict(binance_volumes_since, orient='index', columns=['Daily Volume (USD)'])
hyperliquid_df = pd.DataFrame.from_dict(hyperliquid_volumes_since, orient='index', columns=['Daily Volume (USD)'])
# Merge the DataFrames based on index (dates)
merged_df = pd.merge(binance_df, hyperliquid_df, left_index=True, right_index=True, how='outer', suffixes=('_Binance', '_Hyperliquid'))
# STALEQUANT CODE STARTS
#%%
bnb_ohlcv = pd.DataFrame(binance.fetch_ohlcv('BNB/USDT', timeframe='1d', since=since_timestamp)).set_index(0)[4]
hype_ohlcv = pd.DataFrame(hyperliquid.fetch_ohlcv('HYPE/USDC', timeframe='1d', since=since_timestamp)).set_index(0)[4]
#%%
merged_mc = pd.concat({'BNB':bnb_ohlcv,
'HYPE':hype_ohlcv},axis=1)#
merged_mc.index = pd.to_datetime(merged_mc.index*1000000).astype('str')
#%%
ratios = {
'HYPE Lin. Perp Volume / Binance': merged_df['Daily Volume (USD)_Hyperliquid'] / merged_df['Daily Volume (USD)_Binance'] ,
'HYPE Circ. MC / BNB Circ. MC': (merged_mc['HYPE'] * 333_928_180) / (merged_mc['BNB'] * 145_887_575 ),
'HYPE MC / BNB MC ': (merged_mc['HYPE'] * 1_000_000_000 * .6 ) / (merged_mc['BNB'] * 145_887_575 ),
'HYPE MC / Binance EV (5 x BNB MC)': (merged_mc['HYPE'] * 1_000_000_000 * .6 ) / (merged_mc['BNB'] * 145_887_575 / .2 ),
}
pd.concat(ratios,axis=1).iloc[-30:].plot()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment