Giter Site home page Giter Site logo

acmeyer / open-source-coinbase-index-fund Goto Github PK

View Code? Open in Web Editor NEW
15.0 5.0 8.0 77 KB

Create your own index fund on Coinbase Pro that replicates Coinbase's Index Fund.

License: MIT License

JavaScript 100.00%
coinbase index-fund gdax-api nodejs javascript coinbase-api cryptocurrencies cryptocurrency-exchanges tokens money

open-source-coinbase-index-fund's Introduction

Open Source Coinbase Index Fund

Create your own index fund on Coinbase Pro that replicates Coinbase's Index Fund.

Why

Coinbase released their Index Fund in March 2018. However, it is restricted to accredited investors only and has a high 2% annual management fee.

The goal of this repository is to give anyone the ability to have an index of all assets listed on Coinbase Pro with lower fees.

How to Use

Sandbox

Coinbase has a public sandbox for testing. By default, the app is set to use the Sandbox API so that you don't accidentally execute real trades.

All of the below instructions apply to the sandbox as well as real site. The only difference is the urls you use.

You can add funds using the web interface deposit and withdraw buttons as you would on the production web interface.

Sandbox URLs

Website: https://public.sandbox.pro.coinbase.com

API: https://api-public.sandbox.pro.coinbase.com

Live Site URLs

Website: https://pro.coinbase.com

API: https://api.pro.coinbase.com

Coinbase Pro

In order to use this code, you'll first need to have a Coinbase Pro account. Once you have an account, you'll need to create an API Key, check all permissions. Make note of the API key, secret, and passphrase, you'll need these and you will not be able to see the secret or passphrase after you create it initially.

You'll also need to make sure you have enough funds in your Coinbase Pro account to execute the trades.

One-time Usage vs Automatic Usage

There are two ways you can choose to use this repository. The first is to use it to make a one-time purchase of currencies on Coinbase Pro in the amount equal to their relative market cap sizes (see One-time Usage below). The second way is to use it as an automatic crypto index fund, much like if you were to buy into Coinbase's Index Fund (see Automatic Usage below).

One-time Usage

The one-time usage executes trades in your Coinbase Pro account with the fiat currency of your choice to buy an amount of each crypto currency available equal to their relative market cap sizes. Note that this will use all of the fiat currency you have in your account available.

Run on your own machine

First clone the repository git clone https://github.com/acmeyer/open-source-coinbase-index-fund.

Next, you have to set environment variables for COINBASE_API_URL, COINBASE_FIAT_CURRENCY, COINBASE_API_KEY, COINBASE_API_SECRET, and COINBASE_API_PASSPHRASE. The fiat currency environment variable should be the fiat currency you want to do trades in. The default is 'USD'. The api url is provided above for both the sandbox and live environments.

Finally, all you have to do to purchase an index fund of available currencies on Coinbase Pro, run yarn run fund inside the app's directory.

Warning: This will execute trades!

Note: it is assumed you followed the instructions above and your Coinbase Pro account has a sufficient balance in it to execute the trades. See above section Coinbase Pro if not.

If you would like to run this code automatically whenever your Coinbase Pro account has a high enough balance, you can deploy this code to a server and use a scheduler.

Deploy on a server

The easiest way to deploy this code to a server is by clicking the button below. Once you do that, you only have to set up the COINBASE_API_URL, COINBASE_FIAT_CURRENCY, COINBASE_API_KEY, COINBASE_API_SECRET, and COINBASE_API_PASSPHRASE environment variables and create a scheduler task that runs yarn run fund.

Once the server and scheduler are set, it will automatically make trades whenever your Coinbase Pro account has sufficient funds in the fiat currency you set the COINBASE_FIAT_CURRENCY to.

Deploy

You can also deploy this code to other servers. The setup is very similar.

Automatic Usage

Use this set up if you would like to convert your Coinbase Pro account into an Index Fund account. This set up a server to automatically manage your Coinbase Pro account to replicate Coinbase's Index Fund.

Note: This set up will rebalance ALL currencies in your Coinbase Pro account to match the Index's weights, even if you already have different amounts of currencies in your account before running this server. Only use this set up if you don't care that the code will make trades to rebalance your account.

Tip: If you would like to keep the current amount of each currency but still want to also have an index of all currencies on Coinbase, transfer your current amounts into their respective wallets to your regular Coinbase account. Then anything in your Coinbase Pro account will be the Index Fund and anything in your regular Coinbase wallets won't be affected.

The goal is to add the ability to manage an individual Coinbase Pro account so that it can have an Index Fund and still execute other trades. If you would like the ability to do this, please submit a pull-request!

Run on your own machine

First clone the repository git clone https://github.com/acmeyer/open-source-coinbase-index-fund.

Next, you have to set environment variables for COINBASE_API_URL, COINBASE_FIAT_CURRENCY, COINBASE_API_KEY, COINBASE_API_SECRET, and COINBASE_API_PASSPHRASE. The fiat currency environment variable should be the fiat currency you want to do trades in. The default is 'USD'. The api url is provided above for both the sandbox and live environments.

The code uses Parse Server to manage the server and Parse Dashboard for a server dashboard. This repository has defaults for each's set up but you'll likely want to update the environment variables in src/env.js to match your own machine's configuration.

Once you have set up the environment variables, you can run the server using the command yarn run start inside the app's directory. If you are using a local version of mongodb, make sure that is running before you try and run the server.

In order to actually execute any trades, you have to either manually run the background job called update_index by running yarn run updateIndex inside the app's directory, or by clicking the Run Now button in the Parse Dashboard under Jobs.

Note: Running this code will execute trades!

To manage your Coinbase Pro account automatically, however, you'll need to set this up as a scheduled job. You can do this on your own machine but it's much easier to have a server manage it for you.

Deploy on a server

The easiest way to deploy this code to a server is by clicking the button below. Once you do that, you have to set up the all the environment variables found in Heroku's Config Vars section under the app's Settings tab.

Next, create a scheduler task that runs yarn run updateIndex. If you would like to match Coinbase's Index Fund exactly, then schedule this task to run daily at 12:00am UTC (5:00pm PT).

Once the server and scheduler are set, it will automatically check the current currency weights on Coinbase and rebalance your Coinbase Pro account to match, if necessary. See the below section on Methodology to understand when these rebalances will happen.

Finally, in order to keep your orders up to date with Coinbase, add another scheduler task that runs yarn run updateOrders. You can set this to run at whatever frequency you desire. This task just keeps the orders in your database in sync with Coinbase.

Note: On first set up, the database will have no currencies already in it. That means the first time the update_index background job is run, it will rebalance your portfolio. This may be desirable if you want your account to be updated as soon as you set this server up, but if you don't want the rebalancing to happen right away and instead wait until either a new currency is added to Coinbase's Index Fund or January 1st of the next year, then you will need to seed your database with the current currencies before the background job runs. This can be easily done by running yarn run seed in the app's console.

Deploy

You can also deploy this code to other servers. The setup is very similar.

Methodology

This code is meant to replicate Coinbase's Index Fund. You can read more about their methodology here: https://am.coinbase.com/documents/cbi-methodology.pdf.

Currently, the asset weights are pulled from https://index-am.coinbase.com//v1/cbi/composition.json to match Coinbase's weights. In the future, the goal is to do this all automatically with an official calculation, that uses the same formula Coinbase does. That way, if Coinbase ever changes the url or otherwise removes the ability to get this information, the code can still work properly.

The two main takeaways are that the Index Fund gets rebalance when one of two things happens:

  1. 5 days after a new currency is added to Coinbase Pro (at 5:00pm PT)
  2. The 1st day of a new year (at 5:00pm PT)

If you run this repository on a server and schedule the background job update_index to run daily at 5:00pm PT (12:00am UTC) then it will automatically rebalance your Coinbase Pro account as outlined in the Methodology above.

License

Released under the MIT License. See LICENSE or http://opensource.org/licenses/MIT for more information.

Contributing

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
  • Fork the project
  • Start a feature/bugfix branch
  • Commit and push until you are happy with your contribution
  • Create a Pull-Request on the master branch for your feature/fix

Todos

  • Add a method to get weights rather than rely on scraping
  • Automatically rebalance account, annually and any time new asset is added to GDAX
  • Add option to use different types of orders to avoid trading fees
  • Add tests
  • Better error handling
  • Add background job to update executed orders on Coinbase Pro to keep data up to date
  • Separate index fund vs rest of Coinbase Pro account
  • Add frontend UI for viewing Index Fund's performance
  • Add an official calculation of Methodology rather than pulling weights from Coinbase

Disclaimer

This repository is for information purposes only. Use the code at your own risk. You may lose money if you use this code. Trading crypto currencies for real money is extremely risky and likely to result in disappointment. You bear sole responsibility for anything that happens using this code.

Support

To support the development of this project, you can make donations to the below addresses:

BTC: 3FEUPGgdgmzu7w4GkW7vnBwRWqdyCsy53v

BCH: qqkw35llyty9t8gd2aafg6j946xjpaqdkc8adx8q04

ETH: 0x93fd6895a4756B6233CAa3B491594d0EB040D189

ZEC: t1Mr3updMscxiw1r9ob726W5VucieVknEcC

open-source-coinbase-index-fund's People

Contributors

acmeyer avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar

open-source-coinbase-index-fund's Issues

Add frontend UI for viewing Index Fund's performance

Since the app makes it easy to host the Index Fund on a server and uses Parse Server on the backend, it would be nice to set up a frontend UI that could be accessible on the web to view the Index Fund's performance. This would likely require some backend additions as well like the ability to pull relevant information like orders executed and current/past market caps in order to calculate the Index Fund's return.

Some ideas for what the UI might be like:

  • chart showing returns over time, filterable between 1d, 1w, 1m, 1y, and all-time
  • table showing returns over time
  • downloadable csv file of trades and returns

Better error handling

The app does a poor job of error handling currently. The goal of this ticket would be to add better error handling so that:

  1. it is clear to the user what exactly went wrong
  2. had common sense fallbacks when things do go wrong

Take into account crypto held in other wallets?

I'm not entirely comfortable holding my entire portfolio in Coinbase Pro for obvious reasons. However if I withdraw it to a private wallet, this application will no longer take that into consideration.

It'd be great if it could somehow track currency taken off Coinbase Pro and just manage the portfolio with the left-over currency. Shrimpy does something like this but they don't yet support Coinbase Pro as far as I know.

Add tests

Since this repository could deal with real money, it would be ideal to have full test coverage.

Coinbase Pro has Sandbox APIs that would be ideal in creating tests. It would also be good to create tests that didn't rely on Coinbase's APIs either.

In an ideal world, some CI would be set up to.

Separate Index Fund vs rest of Coinbase Pro account

At the moment this app is all or nothing. If you want to have it automatically manage an Index Fund for you then it will ignore any existing amounts and rebalance the entire portfolio to be an index of the existing currencies.

Some people may not desire this. They may want to have an index of currencies but also be able to execute trades that aren't affected by an index fund's rebalancing.

Since there is currently no way to separate wallets/accounts on Coinbase Pro for an individual's account, the app will have to manage this.

The way this might be done is to allow a user to indicate what portions of each currency account is not dedicated to the index fund. Then you each time a rebalance was executed, it would have to refer to this allocation and act appropriately.

Add background job to update executed orders on Coinbase Pro to keep data up to date

If running a server, this app will save all executed orders (both those that succeed and those that fail). This is helpful for record keeping but also puts it in a position to add new features down the line. One of those features is keeping tabs of orders that are executed on Coinbase Pro through the app.

This can be done using Coinbase Pro's Order API. It would likely be best to do it as a background job that can be executed on a schedule.

It may be best to also stop pinging for an order's information once its settled property is true.

Add an official calculation of Methodology rather than pulling weights from Coinbase

Currently, the weights for the Index Fund are being pulled from https://index-am.coinbase.com//v2/indices/cbi/composition.json to match Coinbase's weights.

The goal here is to do this all automatically with an official calculation, that uses the same formula Coinbase does. That way, if Coinbase ever changes the url or otherwise removes the ability to get this information, the code can still work properly.

Their methodology can be found here.

Add option to use different types of orders to avoid trading fees

What

Since the whole point of this repository is to create an open and lower cost version of Coinbase's Index Fund, there should be a way to better trading fees that take place whenever an account is rebalanced.

Base on Coinbase Pro's Fee Structure, taker fees are 0.30%. Trades are considered takers based on whether or not the trade matches an existing order immediately.

In order to avoid this fee, you have to place maker trades (orders that aren't immediately matched but are later by another trade). Without having tested this, I would guess that you could be somewhat clever in your trades and place them just outside of the market order amount to avoid the associated fees. It is possible that Coinbase has mechanisms to prevent this or that other traders have a similar idea and therefore is not possible or rare to pull off. But worth investigating.

How

The feature should work as follows:

You should be able to set an environment variable and/or pass a variable to the background job update_index that indicates whether or not you would like to minimize fees. If it is set to true, then instead of placing market orders, the trade would place a limit order.

Depending on which side the order is on (sell vs buy) the limit order might take into account the current market ask price of a currency and place a limit order just above or below this price for the currency.

Considerations

It is possible that an order placed like this is never filled or takes awhile to fill. Because of this, it will be necessary to figure out how to deal with rebalancing the full portfolio when there are a lot of pending or non-executed orders. This probably will have to then tie in with the feature of pinging a Coinbase Pro account to get the latest information on all orders placed via this app.

Resources

Coinbase Pro's API

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.