Contents

Automated crypto trading bot implementation 5 (with.Upbit Open API)

   Nov 20, 2023     3 min read

This post is about “Implementing an automated cryptocurrency trading bot 5 (series)”.

Nowadays, the value of cryptocurrencies is steadily increasing.

In a bull market, it is necessary to trade according to your own trading technique.

In this series, we’re going to create a trading bot that will automatically trade according to the logic written in the code without you having to keep an eye on it.

In the previous article, we learned what Bollinger Bands are and implemented this technical indicator directly in code. In this article, we will provide a step-by-step guide to the written code.

(Reference: https://github.com/yeonuk44/Trading-Bot)

Install required modules

npm install request
npm install uuid
npm install crypto
npm install jsonwebtoken
npm install querystring
npm install dotenv

Set API key and secret

To use the Upbit Open API, get an API key and secret from the Upbit Developer Center and save it in an .env file.

UPBIT_OPEN_API_ACCESS_KEY=your_access_key
UPBIT_OPEN_API_SECRET_KEY=your_secret_key
upbit_open_api_server_url=https://api.upbit.com

Load required files and modules

const request = require("request");
const uuidv4 = require("uuid/v4");
const crypto = require("crypto");
const sign = require("jsonwebtoken").sign;
const queryEncode = require("querystring").encode;
const dotenv = require("dotenv");
const accountsInfo = require("./apis/assets");
const orderCryptocurrency = require("./apis/order");
const getCandlesInfo = require("./apis/ticker");

dotenv.config();

Implement the necessary functions

findKRW and findBTC: Functions to find the indices of KRW and BTC.

fetchData: Main function to collect data and execute the trading strategy

Please refer to the attached code repository in the outline of this article.

async function findKRW() {
  // ...
}

async function findBTC() {
  // ...
}

async function fetchData() {
  try {
    // ...
  } catch (error) {
    console.error(error);
  }
}

fetchData();

Make periodic API calls and execute the trading strategy

const minuteInterval = setInterval(async () => {
  // ...
}, 60000);

Implementing a trading strategy

This example uses a simple trading strategy using Bollinger Bands.

if (minuteCandlePrice <= lowerBand * 1.05) {
  // Buy order logic
} else if (minuteCandlePrice >= upperBand * 0.95) {
  // Sell order logic
}

If you’ve been following my series on automated cryptocurrency trading bots, you’ll know which technical indicators I’ve utilized.

Complementary and additional tasks

To extend this project, you can consider the following tasks

Optimize the strategy: The current strategy was implemented simply, but you can implement more complex strategies to maximize your profits.

Finalizing

With these steps, you should be able to develop an automated trading bot while increasing your understanding of JavaScript and cryptocurrency trading.

If you have any gaps, feel free to contact us by email and we will get back to you.