Skip to content

bcattle/optimal-buy-gdax

 
 

Repository files navigation

optimal-buy-gdax

Scheduled buying of BTC, ETH, and LTC from GDAX optimally! Be your own index/hedge fund, and stop paying those greasy management fees.

crypto

What is this?

This is a Python script you can use to automatically buy Bitcoin, Ethereum, and Litecoin using the GDAX API. It buys these 3 currencies, weighted by market cap (as reported by coinmarketcap.com), using a form of dollar cost averaging. according to the following logic:

  1. Check current balances of fiat (USD by default), BTC, ETH, and LTC
  2. If the fiat balance is above $100, buy BTC, ETH, and LTC weighted by market cap, as follows:
    • If there's enough fiat available, place 5 discounted limit orders at the current price minus 0.5% up to 4.5%, each order with 1/5th of the remaining amount to buy for each coin (see "Details on the orders placed", below)
    • If there isn't enough USD available, place 1 buy order at 0.5% off the current price (see "Order Minimums")
  3. If the fiat account balance is below $100 (or whatever you specify), withdraw coins to desired addresses

You can also use the same script to schedule deposits from your bank account periodically, such as when you're paid.

Orders, deposits, and withdrawals are tracked in a SQLite DB, and the withdrawn balances are added to the balances on GDAX to make sure the weights are maintained over time. The SQLite DB can be swapped out for any DB that SQLAlchemy supports.

Ideally, this script would help to make sure that when we dip—

dip

we buy.

USE AT OWN RISK

Duh. Not my fault if you lose everything.

Unless you place absolute trust in me, some guy from the Internet, I suggest you clone the repo and build your own container to protect yourself from any sort of funny business.

How do I use it?

  1. Get yourself a hardware wallet, such as a Ledger or TREZOR.

  2. Set up a GDAX account, and link your bank account

  3. Create the necessary API credentials for GDAX, with permissions to manage funds, withdraw without 2FA, and trade

  4. Determine the payment_method_id value by using the GDAX API (you can use your browser's developer toolbar, here's a quick video showing how)

  5. Get a machine somewhere (GCE, EC2, Digital Ocean) with Docker and systemd

  6. Copy systemd files over:

     $ sudo cp optimal-buy-gdax-*.{service,timer} /etc/systemd/system
    
  7. Edit /etc/systemd/system/optimal-buy-gdax-buy.service, /etc/systemd/system/optimal-buy-gdax-buy.timer, /etc/systemd/system/optimal-buy-gdax-deposit.service, and /etc/systemd/system/optimal-buy-gdax-deposit.timer to your liking. Make sure you:

    • Change the BTC, ETH, and LTC deposit addresses to deposit the coins into your wallet (use a Ledger or TREZOR)
    • Pop in the correct API keys
    • Check the deposit amount (start with something small, like $150, to make sure it actually works first)
    • Check the timer dates (it would be sensible to change the hh:mm so your script doesn't run the same time as everyone else's), make sure the deposit timer fires according to your deposit schedule (keeping in mind that ACH takes 2-5 business days to clear, typically)
    • Consider including your external balances in order to calculate the weights including those balances
  8. Enable the systemd units:

     $ sudo systemctl enable optimal-buy-gdax-buy.service
     $ sudo systemctl enable optimal-buy-gdax-buy.timer
     $ sudo systemctl enable optimal-buy-gdax-deposit.service
     $ sudo systemctl enable optimal-buy-gdax-deposit.timer
    
  9. Start the systemd timers: $ sudo systemctl start optimal-buy-gdax-buy.timer $ sudo systemctl start optimal-buy-gdax-deposit.timer

  10. Enjoy!

Configuration

usage: optimal-buy-gdax.py [-h] --mode MODE [--amount AMOUNT] --key KEY
                           --b64secret B64SECRET --passphrase PASSPHRASE
                           [--api-url API_URL]
                           [--payment-method-id PAYMENT_METHOD_ID]
                           [--btc-addr BTC_ADDR] [--eth-addr ETH_ADDR]
                           [--ltc-addr LTC_ADDR]
                           [--starting-discount STARTING_DISCOUNT]
                           [--discount-step DISCOUNT_STEP]
                           [--order-count ORDER_COUNT]
                           [--fiat-currency FIAT_CURRENCY]
                           [--withdrawal-amount WITHDRAWAL_AMOUNT]
                           [--btc-ext-balance BTC_EXT_BALANCE]
                           [--eth-ext-balance ETH_EXT_BALANCE]
                           [--ltc-ext-balance LTC_EXT_BALANCE]
                           [--db-engine DB_ENGINE]

Buy coins!

optional arguments:
  -h, --help            show this help message and exit
  --mode MODE           mode (deposit or buy)
  --amount AMOUNT       amount to deposit
  --key KEY             API key
  --b64secret B64SECRET
                        API secret
  --passphrase PASSPHRASE
                        API passphrase
  --api-url API_URL     API URL (default: https://api.gdax.com)
  --payment-method-id PAYMENT_METHOD_ID
                        Payment method ID for fiat deposits
  --btc-addr BTC_ADDR   BTC withdrawal address
  --eth-addr ETH_ADDR   ETH withdrawal address
  --ltc-addr LTC_ADDR   LTC withdrawal address
  --starting-discount STARTING_DISCOUNT
                        starting discount (default: 0.005)
  --discount-step DISCOUNT_STEP
                        discount step between orders (default: 0.01)
  --order-count ORDER_COUNT
                        number of orders (default: 5)
  --fiat-currency FIAT_CURRENCY
                        Fiat currency (default: USD)
  --withdrawal-amount WITHDRAWAL_AMOUNT
                        withdraw when fiat balancedrops below this amount
                        (default: 100)
  --btc-ext-balance BTC_EXT_BALANCE
                        BTC external balance
  --eth-ext-balance ETH_EXT_BALANCE
                        ETH external balance
  --ltc-ext-balance LTC_EXT_BALANCE
                        LTC external balance
  --db-engine DB_ENGINE
                        sqlalchemy DB engine (default:
                        sqlite:///state/gdax_history.db)

Details on the orders placed

By default, there are 5 orders placed (for each currency) in steps of 1%, starting at a 0.5% discount from the current price. To illustrate, if the current price was $100 (per LTC, let's say), and you had $100 to buy, the orders would look like this:

Order Size Price
1 0.2010 LTC $99.5
2 0.2030 LTC $98.5
3 0.2051 LTC $97.5
4 0.2072 LTC $96.5
5 0.2094 LTC $95.5

Furthermore, the amount of each currency to buy will be based on the current market cap weighting of each coin. For example, at the time of writing the weights are:

Coin Market Cap (USD) Weight
BTC $195,824,365,435 0.791
ETH $46,080,472,372 0.186
LTC $5,592,776,540 0.023

So if your USD account had $1000 to invest, the amount invested in each would become:

Coin Weight Amount Invested
BTC 0.791 $791
ETH 0.186 $186
LTC 0.023 $23

Caveats/limitations

  • If you try to trade manually or using some other bot at the same time, you're probably going to have a bad time
  • You might have a few dollars (<$100) sitting in your account at all times, even when all orders have been filled because it's not always possible to fill all orders
  • It makes a best effort with minimal complexity to invest all of your fiat, but it may not be possible to fill all orders right away
  • It may take a few days for the market to drop enough for the buys to fill
  • If the market experiences a significant bull run, your orders won't be filled, but it will reset every 24h (using the default buy timer)

Tip jar

If you got some value out of this, please send some kudos my way:

  • BTC: 3EEAE1oKEMnmHGU5Qxibv9mBQyNnes8j8N
  • LTC: 3MxmLzTf4sPsFBGYUnX9MMMbTMeaUSox46

About

Scheduled buying of BTC, ETH, and LTC from GDAX optimally!

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%