Skip to content

Instantly share code, notes, and snippets.

@mvanduijker
Created January 10, 2022 14:20
Show Gist options
  • Save mvanduijker/cadb00258b4f3393365074bfe57884e8 to your computer and use it in GitHub Desktop.
Save mvanduijker/cadb00258b4f3393365074bfe57884e8 to your computer and use it in GitHub Desktop.
bash script to buy crypto on bitvavo you can use for dca
#!/usr/bin/env bash
# Script to buy crypto through https://www.bitvavo.com at market price, so you can automate for DCA (Dollar Cost Averaging)
# Request API KEY and API Secret in your bivavo account with trade permissions
# Pass in api key and secret as environment variables to the script
# Argument 1 is for quote amount (default: 10)
# Argument 2 is for market (default: BTC-EUR)
# Examples:
#
# Buy 20 euro's of Bitcoin:
# BITVAVO_API_KEY=yourkey BITVAVO_API_SECRET=yoursecret bitvavo_dca.sh 20 BTC-EUR
#
# Buy 10 euo's of Ethereum:
# BITVAVO_API_KEY=yourkey BITVAVO_API_SECRET=yoursecret bitvavo_dca.sh 10 ETH-EUR
#
# Use .env file to load secrets with default arguments
# env $(cat .env | xargs) ./bitvavo_dca.sh
# Required tools:
# curl, openssl, envsubst and xxd
set -e
if [ -z "${BITVAVO_API_KEY}" ]; then
echo "BITVAVO_API_KEY environment variable missing" && exit 1;
fi
if [ -z "${BITVAVO_API_SECRET}" ]; then
echo "BITVAVO_API_SECRET environment variable missing" && exit 1;
fi
export AMOUNT_QUOTE="${1:-10}"
export MARKET="${2:-BTC-EUR}"
TIMESTAMP=$(date +%s%N | cut -b1-13)
PAYLOAD=$(echo '{"market":"${MARKET}","side":"buy","orderType":"market","amountQuote":"${AMOUNT_QUOTE}"}' | envsubst)
METHOD='POST'
ENDPOINT='/v2/order'
SIGNATURE=$(echo -n "$TIMESTAMP$METHOD$ENDPOINT$PAYLOAD" | openssl dgst -sha256 -hmac "$BITVAVO_API_SECRET" -binary | xxd -p -c 256)
curl -X "$METHOD" "https://api.bitvavo.com$ENDPOINT" -H "Content-Type: application/json" -H "BITVAVO-ACCESS-KEY: $BITVAVO_API_KEY" -H "BITVAVO-ACCESS-SIGNATURE: $SIGNATURE" -H "BITVAVO-ACCESS-TIMESTAMP: $TIMESTAMP" -d "$PAYLOAD"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment