From 582fc10dce8decc49fd0ce9754ae2e160e64ffc9 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 11 Feb 2025 15:53:50 +0000 Subject: [PATCH] Add get_binance_md.sh for quick/easy troubleshooting --- tools/binance/binance.env | 43 ++++++++++ tools/binance/get_binance_md.sh | 141 ++++++++++++++++++++++++++++++++ 2 files changed, 184 insertions(+) create mode 100644 tools/binance/binance.env create mode 100755 tools/binance/get_binance_md.sh diff --git a/tools/binance/binance.env b/tools/binance/binance.env new file mode 100644 index 000000000..f5d423092 --- /dev/null +++ b/tools/binance/binance.env @@ -0,0 +1,43 @@ +BINANCE_USDS_FUTURES_PROD_URL="https://fapi.binance.com" +BINANCE_USDS_FUTURES_DEV_URL="https://testnet.binancefuture.com" +BINANCE_USDS_FUTURES_EXCHANGE_INFO_PATH="/fapi/v1/exchangeInfo" + +BINANCE_USDS_FUTURES_DEPTH_PATH="/fapi/v1/depth" +BINANCE_USDS_FUTURES_DEPTH_PARAMS='symbol=${SYMBOL}&limit=${LIMIT:-5}' + +BINANCE_USDS_FUTURES_RECENT_TRADES_PATH="/fapi/v1/trades" +BINANCE_USDS_FUTURES_RECENT_TRADES_PARAMS='symbol=${SYMBOL}&limit=${LIMIT:-500}' + +BINANCE_USDS_FUTURES_HISTORICAL_TRADES_PATH="/fapi/v1/historicalTrades" +BINANCE_USDS_FUTURES_HISTORICAL_TRADES_PARAMS='symbol=${SYMBOL}&limit=${LIMIT:-500}' + +BINANCE_USDS_FUTURES_MARK_PRICE_PATH="/fapi/v1/premiumIndex" +BINANCE_USDS_FUTURES_MARK_PRICE_PARAMS='symbol=${SYMBOL}' + +BINANCE_USDS_FUTURES_PRICE_TICKER_PATH="/fapi/v1/ticker/price" +BINANCE_USDS_FUTURES_PRICE_TICKER_PARAMS='symbol=${SYMBOL}' + +BINANCE_USDS_FUTURES_BBO_PATH="/fapi/v1/ticker/bookTicker" +BINANCE_USDS_FUTURES_BBO_PARAMS='symbol=${SYMBOL}' + +BINANCE_USDS_FUTURES_OPEN_INTEREST_PATH="/dapi/v1/openInterest" +BINANCE_USDS_FUTURES_OPEN_INTEREST_PARAMS='symbol=${SYMBOL}' + +BINANCE_USDS_FUTURES_LONG_SHORT_RATIO_PATH="/futures/data/globalLongShortAccountRatio" +BINANCE_USDS_FUTURES_LONG_SHORT_RATIO_PATH='symbol=${SYMBOL}&period=${PERIOD:-30}&limit=${LIMIT:-10}' + +BINANCE_COIN_FUTURES_PROD_URL="https://dapi.binance.com" +BINANCE_COIN_FUTURES_DEV_URL="https://testnet.binancefuture.com" +BINANCE_COIN_FUTURES_EXCHANGE_INFO_PATH="/dapi/v1/exchangeInfo" +BINANCE_COIN_FUTURES_DEPTH_PATH="/dapi/v1/depth" +BINANCE_COIN_FUTURES_RECENT_TRADES_PATH="/dapi/v1/trades" +BINANCE_COIN_FUTURES_HISTORICAL_TRADES_PATH="/dapi/v1/historicalTrades" +BINANCE_COIN_FUTURES_MARK_PRICE_PATH="/dapi/v1/premiumIndex" +BINANCE_COIN_FUTURES_PRICE_TICKER_PATH="/dapi/v1/ticker/price" +BINANCE_COIN_FUTURES_BBO_PATH="/dapi/v1/ticker/bookTicker" +BINANCE_COIN_FUTURES_OPEN_INTEREST_PATH="/dapi/v1/openInterest" +BINANCE_COIN_FUTURES_LONG_SHORT_RATIO_PATH="/futures/data/globalLongShortAccountRatio" + +BINANCE_PROD_URL="https://api.binance.com" +BINANCE_DEV_URL="https://testnet.binance.vision" +BINANCE_PATH="/api/v3/depth" diff --git a/tools/binance/get_binance_md.sh b/tools/binance/get_binance_md.sh new file mode 100755 index 000000000..797bea145 --- /dev/null +++ b/tools/binance/get_binance_md.sh @@ -0,0 +1,141 @@ +#!/usr/bin/env bash + +help_text="\ +Get Binance market data. + +Usage: get_binance_md.sh [OPTIONS] + +Options: + --usds-future Indicates the symbol is a USDS Future + --coin-future Indicates the symbol is a Coin Future + --spot Indicates the symbol is a Spot + --us Indicates the symbol is a Binance US Spot + --prod Use production url + --dev Use development url + --dry Print the command to console without actually running it + --data DEPTH | RECENT_TRADES | HISTORICAL_TRADES | MARK_PRICE | PRICE_TICKER + --= Query parameter (e.g., --symbol=btcusdt, --limit=5) + +" + +function print_help_and_exit() { + echo "$help_text" + exit "$1" +} + +declare this_dir="$(readlink -f $(dirname ${BASH_SOURCE[0]}))" +source "$this_dir/binance.env" + +env_name="binance" +venue="BINANCE" +declare -u mode +declare -u data +declare -A query_params +declare -i dry +dry=0 + +while [[ $# -gt 0 ]]; do + case "$1" in + --usds-future) + venue="BINANCE_USDS_FUTURES" + shift 1 + ;; + --coin-future) + venue="BINANCE_COIN_FUTURES" + shift 1 + ;; + --spot) + venue="BINANCE" + shift 1 + ;; + --us) + venue="BINANCE_US" + shift 1 + ;; + --prod) + mode="PROD" + shift 1 + ;; + --dev) + mode="DEV" + shift 1 + ;; + --data) + if [[ -n "$data" ]]; then + echo "Unexpected argument '$1'" + print_help_and_exit 1 + elif [[ -z "$2" ]]; then + echo "Data (--data) must be non-empty" + print_help_and_exit 1 + fi + data="$2" + shift 2 + ;; + --*=*) + key="${1%%=*}" + key="${key#--}" + value="${1#*=}" + if [[ -z "$key" ]]; then + echo "Invalid --key=value arg (key is empty). $1" + print_help_and_exit 1 + fi + query_params["$key"]="$value" + shift 1 + ;; + --dry) + dry=1 + shift 1 + ;; + -h) + ;& + --help) + print_help_and_exit 0 + ;; + *) + if [[ -z "$1" ]]; then + echo "Invalid positional argument '$1'" + print_help_and_exit 1 + elif [[ -z "$symbol" ]]; then + symbol="$1" + else + echo "Unexpected argument '$1'" + print_help_and_exit 1 + fi + shift 1 + ;; + esac +done + +if [ -z "$mode" ]; then + mode="DEV" +fi +if [ -z "$data" ]; then + data="DEPTH" +fi + +base_url_name="${venue}_${mode}_URL" +base_url_name="${venue}_${mode}_URL" +path_name="${venue}_${data}_PATH" +params_name="${venue}_${data}_PARAMS" +if [ ! -f "$this_dir/$env_name.env" ]; then + echo "Env file '$this_dir/$env_name.env' does not exist" + print_help_and_exit 1 +fi + +for key in "${!query_params[@]}"; do + val=${query_params[$key]} + declare -u keyu + keyu="$key" + eval "$keyu=$val" +done + +source "$this_dir/$env_name.env" + +params_template=${!params_name} +eval "params=\"$params_template\"" +url="${!base_url_name}${!path_name}?${params}" +if [ $dry -ne 0 ]; then + echo curl --get $url +else + curl --get $url +fi