Algorithmic Trading with Node.js on GDAX: A Developer's Guide

·

Algorithmic trading (algo-trading, black-box trading, etc.) offers an exciting opportunity for developers to engage with financial markets, enhance their coding skills, and potentially grow their wealth. While traditional stock markets are heavily regulated, cryptocurrency trading provides a more accessible entry point. Platforms like GDAX (now Coinbase Pro) offer robust APIs for building and testing trading algorithms.

Why Start with Cryptocurrency Trading?

GDAX provides well-documented APIs and a native Node.js client, making it an excellent platform for beginners and experienced traders alike.


Getting Started with Node.js and GDAX

Prerequisites

Step 1: Set Up Your Project

Initialize your trading project with the following commands:

mkdir trading
cd trading
touch index.js
npm init

Step 2: Install GDAX-Node Library

Add the official GDAX Node.js client to your project:

npm install gdax

Working with Public APIs

Start by exploring GDAX's public APIs, which don’t require authentication.

Fetch Available Products

const GDAX = require('gdax');
const publicClient = new GDAX.PublicClient();

publicClient.getProducts((error, response, data) => {
  if (error) return console.error(error);
  console.log(data);
});

Output: A list of tradable products (e.g., BTC-USD, ETH-USD) with details like min/max order sizes and price increments.

Fetch Supported Currencies

publicClient.getCurrencies((error, response, data) => {
  if (error) return console.error(error);
  console.log(data);
});

Output: A list of supported cryptocurrencies (e.g., BTC, ETH) and fiat currencies (e.g., USD, EUR).


Authenticating with Your GDAX Account

To access account-specific data (e.g., balances, orders), generate an API key in your GDAX settings. For testing, grant READ permissions only.

Initialize Authenticated Client

const apiKey = 'yourAPIkey';
const base64secret = 'yourBase64Secret';
const passPhrase = 'yourPassPhrase';
const apiURI = 'https://api.gdax.com';

const authenticatedClient = new GDAX.AuthenticatedClient(
  apiKey,
  base64secret,
  passPhrase,
  apiURI
);

Fetch Account Balances

authenticatedClient.getAccounts((error, response, data) => {
  if (error) return console.error(error);
  console.log(data);
});

Output: A list of your GDAX accounts with balances (e.g., ETH, USD).


Real-Time Price Tracking with WebSockets

Polling for prices is discouraged due to rate limits. Instead, use WebSocketClient to stream live data.

Subscribe to ETH-USD Ticker

const ETH_USD = 'ETH-USD';
const websocket = new GDAX.WebsocketClient([ETH_USD]);

websocket.on('message', (data) => {
  if (data.type === 'done' && data.reason === 'filled') {
    console.log(`Latest ETH price: ${data.price}`);
  }
});

Output: Real-time updates for filled orders, reflecting the latest market price.


Placing Buy/Sell Orders

Buy Order Example

function placeBuyOrder() {
  const buyParams = {
    price: '300.00',  // Price per unit
    size: '0.1',      // Amount of ETH
    product_id: ETH_USD,
  };

  authenticatedClient.buy(buyParams, (error, response, data) => {
    if (error) return console.error(error);
    console.log(`Order placed: ${data.id}`);
  });
}

Sell Order Example

function placeSellOrder() {
  const sellParams = {
    price: '310.00',
    size: '0.1',
    product_id: ETH_USD,
  };

  authenticatedClient.sell(sellParams, (error, response, data) => {
    if (error) return console.error(error);
    console.log(`Order placed: ${data.id}`);
  });
}

Checking Order Status

Fetch All Open Orders

authenticatedClient.getOrders((error, response, data) => {
  if (error) return console.error(error);
  console.log(data);
});

Check Individual Order

const orderId = 'your-order-id';
authenticatedClient.getOrder(orderId, (error, response, data) => {
  if (error) return console.error(error);
  console.log(data);
});

FAQs

1. Is GDAX free to use?

Yes, GDAX charges no fees for API usage, but standard trading fees apply for executed orders.

2. How do I handle API rate limits?

GDAX enforces rate limits (e.g., 3 requests per second). Use WebSockets for real-time data to avoid throttling.

3. Can I backtest trading strategies?

GDAX doesn’t provide historical data via API. Use third-party tools like CCXT for backtesting.

4. What’s the minimum order size?

Varies by product (e.g., 0.001 BTC for BTC-USD). Check getProducts() for details.

5. How secure is my API key?

Never share your base64secret or passPhrase. Use read-only keys for testing.

6. Can I automate trades 24/7?

Yes, but ensure your Node.js script runs on a stable server (e.g., AWS EC2).


Final Thoughts

Building an algorithmic trading platform requires patience, testing, and risk management. Start with small amounts to test your strategies, and gradually scale up.

👉 Explore advanced strategies with GDAX

👉 Learn more about Node.js and financial APIs

Happy coding, and may the markets be ever in your favor!