Coder Social home page Coder Social logo

brndnmtthws / optimal-buy-cbpro Goto Github PK

View Code? Open in Web Editor NEW
335.0 38.0 69.0 25.95 MB

Scheduled buying of BTC, ETH, and LTC from Coinbase Pro, optimally!

License: The Unlicense

Python 99.51% Dockerfile 0.49%
bitcoin docker litecoin btc ltc ethereum gdax eth cryptocurrency cryptocurrency-trading-bot

optimal-buy-cbpro's Introduction

optimal-buy-cbpro (formerly optimal-buy-gdax)

Build Status Maintainability Test Coverage PyPI version

Scheduled buying of BTC, ETH, and LTC from Coinbase Pro (formerly GDAX) optimally!

crypto

What is this?

This is a Python script you can use to automatically buy Bitcoin, Ethereum, Litecoin, and more using the Coinbase Pro API. By default, 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 (assuming default values):

  1. Check current balances of fiat (USD by default), BTC, ETH, and LTC
  2. If the fiat balance is above $25, 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 $25 (or whatever you specify), withdraw coins to desired addresses

In effect, this script mimmicks the behaviour of a market cap weighted index fund, but without the fees. It also only supports the coins that trade on Coinbase Pro (because that's the only exchange that has an API for ACH deposits AFAIK).

You can also use the same script to schedule deposits from your bank account periodically, such as when you're paid. The parameters may be configured to suit your preferences, such as which coins to buy, external balances, discount values, number of steps, etc.

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

A note on the default parameters: it's likely you'll want to change --starting-discount, --discount-step, or --order-count. The more spread out the orders are (i.e., difference between the current price and the lowest priced order), the longer they will take to fill (if they fill), and the closer the orders are, the more likely you are to miss out on bigger price drops. You should consider your appetite for risk and how much you want to optimize for catching those dips vs. not missing out on gains. There is no magic here. My personal advice is to stick somewhat close to the defaults, and try to continuously deposit a little more fiat every week to spread the risk but also catch some dips.

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 type of funny business.

How do I use it?

The package itself can be used as a Docker container, or by installing the pip package with pip install optimal-buy-cbpro. Using the Docker container is recommended to avoid Python environment issues. Instructions for running with Docker and systemd are as follows:

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

  2. Set up a Coinbase Pro account, and link your bank account

  3. Create a Coinbase Pro API key with view, trade, manage, transfer, and bypass-2fa permissions

  4. Determine the payment_method_id value by using the Coinbase Pro 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 systemd/optimal-buy-cbpro-*.{service,timer} /etc/systemd/system
    
  7. Edit /etc/systemd/system/optimal-buy-cbpro-buy.service, /etc/systemd/system/optimal-buy-cbpro-buy.timer, /etc/systemd/system/optimal-buy-cbpro-deposit.service, and /etc/systemd/system/optimal-buy-cbpro-deposit.timer to your liking. Make sure you:

    • Change the BTC, ETH, and LTC withdrawal 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 specifying your external balances in order to accurately calculate the weights and amounts to purchase
  8. Enable the systemd units:

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

    $ sudo systemctl start optimal-buy-cbpro-buy.timer
    $ sudo systemctl start optimal-buy-cbpro-deposit.timer
    
  10. Enjoy!

Configuration

usage: optimal-buy-cbpro [-h] --mode MODE [--amount AMOUNT] --key KEY
                        --b64secret B64SECRET --passphrase PASSPHRASE
                        [--api-url API_URL]
                        [--payment-method-id PAYMENT_METHOD_ID]
                        [--starting-discount STARTING_DISCOUNT]
                        [--discount-step DISCOUNT_STEP]
                        [--order-count ORDER_COUNT]
                        [--fiat-currency FIAT_CURRENCY]
                        [--withdrawal-amount WITHDRAWAL_AMOUNT]
                        [--db-engine DB_ENGINE] [--max-retries MAX_RETRIES]
                        [--coins COINS] [--base-fee BASE_FEE]

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.pro.coinbase.com)
  --payment-method-id PAYMENT_METHOD_ID
                        Payment method ID for fiat deposits
  --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 balance drops below this amount
                        (default: 25)
  --db-engine DB_ENGINE
                        SQLAlchemy DB engine (default:
                        sqlite:///cbpro_history.db)
  --max-retries MAX_RETRIES
                        Maximum number of times to retry if there are any
                        failures, such as API issues (default: 3)
  --coins COINS         Coins to trade, minimum trade size, withdrawal
                        addresses and external balances. Accepts a JSON
                        string.
  --base-fee BASE_FEE   Default base fee to subtract from overall balance.

Default coins are as follows:
    {
      "BTC":{
        "name":"Bitcoin",
        "withdrawal_address":null,
        "external_balance":0
      },
      "ETH":{
        "name":"Ethereum",
        "withdrawal_address":null,
        "external_balance":0
      },
      "LTC":{
        "name":"Litecoin",
        "withdrawal_address":null,
        "external_balance":0
      }
    }

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, the amount purchased of each would become:

Coin Weight Amount Purchased
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 (<$25, you can change this with --withdrawal-amount) sitting in your account at all times, even when all orders have been filled because it's not always possible to fill all orders and there may be small rounding errors (on the order of cents)
  • It makes a best effort with minimal complexity to use 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)

optimal-buy-cbpro's People

Contributors

brndnmtthws avatar coulterj avatar dependabot-preview[bot] avatar hazelnusse avatar spiveym avatar tomhoover avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

optimal-buy-cbpro's Issues

Coin address

In the instructions it says we need to copy the files over and edit them to our preferences. I don't see where to edit the coin address like there were on previous releases. Is the option still '--btc-addr' ?

Service script appears to pull the docker image on execution

https://github.com/brndnmtthws/optimal-buy-gdax/blob/f00471303d346fa91011a5c0b67a19ef8acc2575/optimal-buy-gdax-buy.service#L11

I could be wrong, but it seems like this line will pull a fresh docker image (including the core python script) fresh from docker hub. If it's possible to update v1.0.3 in place on docker hub, then this would allow arbitrary python code to be executed using folks GDAX keys. Seems like a bad thing.

Can this be updated to build and use a local version of the docker image?

Unable to open database file?

Just upgraded from the version I was using a week ago. Now I get an unable to open database file error. I am not providing a --db-engine argument and I'm just using the default of
sqlite:///state/gdax_history.db. Do I need to initialize the db somehow?

BTW, I'm not using docker either, just running the script by hand passing arguments to it to test it out.

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 2147, in _wrap_pool_connect
    return fn()
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 387, in connect
    return _ConnectionFairy._checkout(self)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 766, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 516, in checkout
    rec = pool._do_get()
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 1229, in _do_get
    return self._create_connection()
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 333, in _create_connection
    return _ConnectionRecord(self)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 461, in __init__
    self.__connect(first_connect_check=True)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 651, in __connect
    connection = pool._invoke_creator(self)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/strategies.py", line 105, in connect
    return dialect.connect(*cargs, **cparams)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/default.py", line 393, in connect
    return self.dbapi.connect(*cargs, **cparams)
sqlite3.OperationalError: unable to open database file

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/myusername/crypto/optimal-buy-gdax-mine/optimal-buy-gdax.py", line 65, in <module>
    db_session = get_session(args.db_engine)
  File "/home/myusername/crypto/optimal-buy-gdax-mine/history.py", line 46, in get_session
    Base.metadata.create_all(engine)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/schema.py", line 3949, in create_all
    tables=tables)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 1928, in _run_visitor
    with self._optional_conn_ctx_manager(connection) as conn:
  File "/usr/lib/python3.5/contextlib.py", line 59, in __enter__
    return next(self.gen)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 1921, in _optional_conn_ctx_manager
    with self.contextual_connect() as conn:
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 2112, in contextual_connect
    self._wrap_pool_connect(self.pool.connect, None),
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 2151, in _wrap_pool_connect
    e, dialect, self)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 1465, in _handle_dbapi_exception_noconnection
    exc_info
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/util/compat.py", line 186, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 2147, in _wrap_pool_connect
    return fn()
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 387, in connect
    return _ConnectionFairy._checkout(self)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 766, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 516, in checkout
    rec = pool._do_get()
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 1229, in _do_get
    return self._create_connection()
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 333, in _create_connection
    return _ConnectionRecord(self)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 461, in __init__
    self.__connect(first_connect_check=True)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 651, in __connect
    connection = pool._invoke_creator(self)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/strategies.py", line 105, in connect
    return dialect.connect(*cargs, **cparams)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/default.py", line 393, in connect
    return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file

Not withdrawing

I have not been watching when it stopped working, but it looks like the script is no longer moving my coins to my external wallets. Funds in BTC are over .05 Could this be part of the move to Coinbase Pro?

Anyway looking at the logs, and I don't see any obvious errors.

Trouble with copying files over

Hello,

I am having trouble with transferring files to the virtual instance (hosted through Digital Ocean).
While connected to the remote host via ssh I execute this line: sudo cp optimal-buy-gdax-*.{service,timer} /etc/systemd/system

And I receive this error:
cp: cannot stat 'optimal-buy-gdax-*.timer': No such file or directory

While still connected to the remote host, I then tried to use SCP: scp /directory/optimal-buy-gdax-master/optimal-buy-gdax-.*{service,timer} /etc/systemd/system

But received this error:
cp: cannot stat '/directory/optimal-buy-gdax-master/optimal-buy-gdax-.*service': No such file or directory

I am still unfamiliar with remote machines, so excuse me for the noob question!

Thanks!

working example of coins json string

Do you have any working examples of coins parameter in a JSON string..

I tried things following variations but all of them threw some kind of error.

The following threw "json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)"

--coins='{"BTC":{"name":"Bitcoin","withdrawal_address":null,"external_balance":0},"ETH":{"name":"Ethereum","withdrawal_address":null,"external_balance":0},"LTC":{"name":"Litecoin","withdrawal_address":null,"external_balance":0}}'

and the this one threw "TypeError: string indices must be integers"
"""{"BTC":{"name":"Bitcoin","withdrawal_address":null,"external_balance":0},"ETH":{"name":"Ethereum","withdrawal_address":null,"external_balance":0},"LTC":{"name":"Litecoin","withdrawal_address":null,"external_balance":0}}"""

Missing requirements.txt

The Dockerfile reads from missing file requirements.txt. It was in the initial commit but was removed later on.

It'd be great to add instructions on how to build the docker. The systemd files also rely on Docker and it'd be useful to provide non-docker versions of these files.

Coinbase fee calculation bug

Coinbase has an apparent bug in their API where fees are not included in limit orders. This results in the last order getting rejected.

Should I implement a workaround? Should I wait for a fix?

I tweeted at @coinbasepro but they did not respond/acknowledge.

Question on withdrawl amount

I have a question on the withdrawal amount. I am sticking to the default of $25. Ether seems to have done fine, but for BTC, it didn't want to transfer. It has $68.84 USD worth of BTC, Shouldn't it have transferred?

--
Jun 10 00:42:20 docker[13572]: fiat_balances={'BTC': 68.8452891525, 'USD': 0.0585899719, 'ETH': 31.6041170672}
Jun 10 00:42:20 docker[13572]: only 0.0585899719 USD fiat balance remaining, withdrawing coins without buying
Jun 10 00:42:20 docker[13572]: BTC balance only 0.0094354500000000, not withdrawing
Jun 10 00:42:20 docker[13572]: withdrawing 0.05522878 ETH to EtherAddress

different coins than default

Not sure this a code issue but the documentation isn't clear how to enter different coins besides the defaults. Nice bot it did work for me though I just dont trade btc.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.