How to Code a Crypto Trading bot with JavaScript & Coygo

Evan Francis
Coygo Blog
Published in
5 min readAug 17, 2022

If you’ve ever wanted to code your own trading bot you’re in luck! Today we’ll be coding a crypto trading bot with JavaScript & Coygo Forge that implements a basic market maker strategy.

Coygo Forge is a framework for coding custom crypto trading bots using a simple JavaScript API with a focus on real-time order book data & day trading strategies like arbitrage and scalping. Forge is the underlying engine powering the Coygo Bots crypto trading bot platform which includes a number of pre-built trading bot strategies that were created with Forge, including triangular arbitrage and grid trading.

What we’re building: Simple market maker strategy

We will be creating a simple market making strategy that attempts to profit off of the bid-ask spread:

  • Submits one limit buy order at or near the current ask rate.
  • Submits one limit sell order at or near the current bid rate.
  • When either order is filled a new order will be submitted at or near the new ask/bid rate.
  • If the ask/bid rate moves too far away from the existing open buy/sell order, the order(s) will be cancelled and re-submitted at or near the new ask/bid rate.

This exact strategy is available within Coygo Bots here: Simple market maker crypto trading bot strategy.

First we’ll create a new Strategy

The first thing we need to do is create a new Strategy on the “My Strategies” screen of Coygo Bots. Strategies define the behavior of trading bots within Coygo Bots and this new Strategy will contain our custom JavaScript code.

We’ll name it “Simple market maker” since that’s what we’re building today.

Once the Strategy is created we can click the “Forge — Edit this Strategy” button to launch Coygo Forge.

Coygo Forge overview

We’ve landed in Coygo Forge, the tool that we’ll be using to code our custom crypto trading bot today.

The Coygo Forge intefrace for coding custom Bot Strateagies
  • On the left we’ll see our Strategy’s JavaScript code which we can modify
  • On the right we’ll see JavaScript Guides with API documentation of everything your code can do
  • On the right we’ll also see Example Strategies which has complete working code examples for reference and inspiration

If you want to learn more please see our other blog post: Coygo Forge — Code Crypto Trading Bots With Javascript.

Coding intro: Access a real-time order book

When we create a new Strategy we’re given a basic starter template code that creates two inputs on the “Configure” screen to select an exchange and a trade pair to use, then accesses a real-time order book for that trade pair.

See the two times that coygo.input(options) was called? Those are used to automatically generate a user interface to configure your bot on the “Configure” screen. Here is what that looks like:

Notice that the last line of code uses coygo.log(messages,…). This lets us log any information to the Activity Log so that we can see it when the Bot is running.

Coding intro: Run your new Bot

With that basic starter template code we can try to run our new Bot on the “Run” screen.

Once the Bot has started and is running we will see the “Activity Log” on the left side of the screen. See this line in our code?

coygo.log(‘This is the order book!’, orderBook);

Now we can see that message in the Activity Log!

This allows us to see what our Bot is doing and debug when issues might occur.

Coding Part 1: Inputs & persisted data

Now we can begin coding our market maker strategy that we described above. First we’ll set up all of the configuration inputs and persisted data that we’ll need. (Remember you can see all of this in the video above as well!)

Coding Part 2: Determining when to buy, sell, or cancel

With all of our inputs and data now set up we’re ready to determine what the Strategy should do. The Strategy may need to do one of a few things:

  1. Submit a new buy or sell order if there is not currently one open
  2. Cancel a buy or sell order if the current ask/bid rate has moved too far away from our open order’s rate
  3. Wait for a cancelled order to finish being canceled

Coding Part 3: Submitting and canceling orders

Our Strategy has determined what to do next, now it’s time to actually submit or cancel orders when appropriate. We will calculate the order’s rate depending on the current ask/bid rate and we’ll make sure our order rate and amounts adhere to the exchanges rules for how many decimal places are allowed.

Run your new market maker Bot!

It’s time to try out our new Strategy! We can run the Bot using this Strategy on the “Run” screen. We’ll see it submit a buy and sell order as soon as the bot starts, and on the left side Activity Log we’ll see detailed information about what the Bot is doing.

Try Coygo for free today

If you’d like to try creating your own crypto trading bot you can try Coygo for free today! All subscription plans include a free trial. You can sign up at https://coygo.app.

Follow us on social media and subscribe to our blog to keep up to date with Coygo’s progress as we continue to develop the best tools for cryptocurrency and digital asset traders.

Facebook: https://www.facebook.com/CoygoHQ/

LinkedIn: https://www.linkedin.com/company/coygo

Twitter: https://twitter.com/CoygoHQ

--

--

Evan Francis
Coygo Blog

CEO and Co-founder of Coygo, Inc. and Lunafi, LLC, cryptocurrency enthusiast, software engineer, alpine skier