Skip to content

binance-stats

Scripts to compute some statistics starting from Binance transaction records CSV files.

⚠ Disclaimer: I am not responsible for any wrong results or any possible damage caused by the use of these scripts.

Note: this project contains fake data useful for testing (e.g. directories transactions and statements)

Usage

Important: this has been tested with Python 3.12.4 on Windows 10.

Set up a Python venv (virtual environment) and install some packages inside it:

Bash
python3 -mvenv venv
venv/bin/python3 -mpip install -r requirements.txt

Then download the necessary OHLCV data:

Bash
mkdir ohlcv

mapfile -t coins < <(cat transactions/records-*.csv |
    venv/bin/python3 coins.py | tr -d '\r')
echo "${coins[@]@Q}"

mapfile -t years < <(find transactions -name 'records-*.csv' \
    -printf '%P\n' | tr -cd '0-9\n')
echo "${years[@]@Q}"

i=0; for year in "${years[@]}"; do for coin in "${coins[@]}"; do
    echo "$((++i)): fetching year $year coin $coin"
    bash fetch.sh ../ohlcv-fetchers/binance.py \
        "$coin/USDT" 1d "$year" > "ohlcv/$year-$coin-USDT-1d.csv"
    sleep 2 # Just to be on the safe side
done; done

for file in ohlcv/*.csv; do
    if [ ! -s "$file" ]; then
        echo "Removing empty CSV file: $file"
        rm "$file"
    fi
done

Finally, you can compute the statistics:

Bash
1
2
3
4
5
cat transactions/records-*.csv |
    venv/bin/python3 stats.py -d2021-01-01T00Z -D2021-12-31T23:59Z \
        --real-spot=statements/spot-2021.csv,Coin,Total \
        --real-earn=statements/earn-2021.csv,Token,Amount \
        --ohlcv-dir=ohlcv --fiat=EUR

Unit tests

If you want to run the unit tests:

Bash
venv/bin/python3 -mpip install pytest
venv/bin/python3 -mpytest test