Dollar Cost Averaging Bot for Binance and Coinbase Pro

Alex Ford
5 min readJul 29, 2021
Dollar Cost Average that Crypto — Image from https://medium.com/coinmonks/dollar-cost-averaging-dca-how-profitable-is-this-strategy-really-b4beed886f75

I’ve been dabbling in Crypto for about a year now, I’m by no means an expert and I don’t have a better strategy than buy and HODL really.

With my long term strategy in mind, hoping that crypto is the technology to take over the world, I needed to find a good way to accumulate with my available funds in an efficient way — what better way than Dollar Cost Averaging.

Dollar Cost Averaging (DCA)

If you don’t know what DCA is, you can give it a google and find loads of articles about it. Here is a quick summary —

Dollar-cost averaging (DCA) is an investment strategy in which an investor divides up the total amount to be invested across periodic purchases of a target asset in an effort to reduce the impact of volatility on the overall purchase. The purchases occur regardless of the asset’s price and at regular intervals. In effect, this strategy removes much of the detailed work of attempting to time the market in order to make purchases of equities at the best prices. Dollar-cost averaging is also known as the constant dollar plan.

Coinbase will do this for you, but the fees are quite expensive so it eats away at the long term gains, especially if the interval is very short.

Low Fees Where?

The two places that I know that have low fees are Binance and Coinbase Pro, both of these have no DCA functionality so let’s do it ourselves.

Setting Up

Depending on which platform you choose, you will need to generate some API keys. You can google how to do this for the respective platform but usually they’re under the profile tab, labelled something like “API Management”.

If you’re looking to use Coinbase or Binance, consider using my referral links:

Coinbase: https://www.coinbase.com/join/ford_s4w

Binance: https://accounts.binance.com/en/register?ref=UKHSELJH

Once you have these, you should get a few keys that we will use to let the exchange know that you are the one making these orders/requests.

These will look like long strings of random numbers/letters. Something like this “5ae9b7f211e23aac3df5f2b8f3b8eada”

AWS Lambda

I didn’t really want to have a server that just had this running and have to pay for that, but I’ve used AWS’s Lambda service before (Serverless Compute) and it seemed great for this.

I wrote the code as a standalone program, it will run just fine if you run it on your own machine, there are some extra steps to get it running on AWS Lambda which I will run through here.

Using the code in the github repo as reference https://github.com/WhatATragedy/DCA_Crypto_Bot, let’s get this lambda’d.

Lambda Prep

The DCA bot uses some dependencies, namely the Coinbase Pro API Library or Binance API Library. Lambda won’t have these natively so we need to add a “layer”. You can think of layers as a way for us to load extra bits before our program runs in the cloud.

Depending on what you use, Coinbase or Binance will change this but I will run through Coinbase here. We are going to download the extra coinbase bits and get them ready for lambda.

What this snippit is doing, is making a proxy python dependency tree, installing the Coinbase Python Library (cbpro) into it, we then zip is up to a maximum recursion of 9. You should now see a file called “cbpro-layer.zip” in your aws-layers directory.

Uploading to AWS Lambda

Login to your AWS account and head over the “Lambda” service. You should see these options on the left.

Lambda service options

We’ve just created a layer, so head over to Layers.

At the top right, you want to “Create layer”.

Give it a descriptive name/description and upload the zip file we just made. Also add some python runtimes so lambda knows what programming language this layer should be used with. Then click create.

Head back over to the lambda functions page and on the top right, click “Create function”.

You want to make something that looks like this — make sure to pick python3.8+ as the runtime.

You should see once function, “lambda_handler” we are going to grab the code from github and make it work with that. When the lambda get’s run, that handler is the first function it calls. So something like this works -

Code

You should now see something like this

Almost there…

So now we just need to make sure the context around this code is okay. We haven’t added our API keys or the layer we uploaded.

API Keys

Those keys we generated earlier still have to be used to tell Coinbase which account wants to create the buy order. If you head over to the “configuration” tab in your lambda function and go to “environment variables”. You want to edit your variables to look like this, with your keys.

Layers

Next, let’s add that Coinbase layer. Head back to your “Code” tab and scroll down to the bottom till you see “Layers”. Click “Add a layer” and pick the coinbase layer you created. It should now look like this.

Intervals

All that is left to do, is make this run at your specified intervals.

At the top of the lambda function there is a “Funcation Overview”. Click “Add trigger” and choose “EventBridge (CloudWatch Events)”. You want to create a new rule that looks something like this, changing the rate as you see fit.

All done

That should be everything — you can edit the crypto and amount in the “lambda_handler” and click deploy. If you want you can click test to double check it works.

Make sure to keep your Coinbase or Binance account topped up with money and this will handle the rest for you!

Thanks,

Alex

--

--