Contents

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

   Nov 18, 2023     4 min read

This post is about ā€œImplementing an automated cryptocurrency trading bot 3 (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, Iā€™m going to create a trading bot that automatically executes trades based on the logic written in the code without me having to keep an eye on it.

In the previous article, we performed the trades manually, so in this article, weā€™ll be fetching candle information and getting values to implement investment techniques such as Bollinger Bands.(Reference: https://github.com/yeonuk44/Trading-Bot)

Get Candlestick Information API

Upbitā€™s Candlestick Data API allows you to get candlestick data for a specific market. This can be used for trend analysis, technical analysis, and many other purposes.

My code

getDailyCandlesInfo function

function getDailyCandlesInfo() {
  // Set the API request options
  const options = {
    method: "GET",
    url: "https://api.upbit.com/v1/candles/days?count=30&market=KRW-BTC",
    headers: { accept: "application/json" },
  };

  // Process the API request and response
  return new Promise((resolve, reject) => {
    request(options, (error, response, body) => {
      if (error) {
        reject(error);
      } else {
        try {
          // Parse the JSON
          const responseBody = JSON.parse(body);

          // Extract only the candle's trade prices
          const tradePrices = responseBody.map((candle) => candle.trade_price);

          // Calculate the value using technical analysis (Bollinger Bands)
          const dailyBBValue = technicalBollingerBand.bb(tradePrices);

          // return the result
          resolve(dailyBBValue);
        } catch (parseError) {
          reject(parseError);
        }
      }
    });
  });
}

The above code requests and receives the daily candle information of the KRW-BTC market through QS, parses it into JSON format, and extracts only the closing price of the desired candle.

We inserted it into the Bollinger Bands function that will be used in the next article to calculate the technical analysis basis for the trade.

Here is the code to get the candle information per minute for trading.

getMinuteCandleInfo function

function getMinuteCandleInfo() {
  // Set the API request options
  const options = {
    method: "GET",
    url: "https://api.upbit.com/v1/candles/minutes/1?market=KRW-BTC&count=1",
    headers: { accept: "application/json" },
  };

  // Process the API request and response
  return new Promise((resolve, reject) => {
    request(options, (error, response, body) => {
      if (error) {
        reject(error);
      } else {
        try {
          // Parse the JSON
          const responseBody = JSON.parse(body);

          // Extract only the candle's trade prices
          const tradePrices = responseBody.map((candle) => candle.trade_price);

          // return the result
          resolve(tradePrices);
        } catch (parseError) {
          reject(parseError);
        }
      }
    });
  });
}

Weā€™ve also imported an example that uses the code written above.

Example of using

const { getDailyCandlesInfo, getMinuteCandleInfo } = require("./path/to/api");

// Get daily candle information
getDailyCandlesInfo()
  .then((dailyBBValue) => {
    console.log("Daily Bollinger Bands value:", dailyBBValue);
  })
  .catch((error) => {
    console.error("Failed to retrieve daily candle information:", error);
  });

// Get minute candle information
getMinuteCandleInfo()
  .then((tradePrices) => {
    console.log("Minute candle trade prices:", tradePrices);
  })
  .catch((error) => {
    console.error("Failed to retrieve 1-minute candle information:", error);
  });

Wrapping up

Utilizing Upbitā€™s candlestick data retrieval API can help you identify market trends and perform technical analysis.

You can use the functions above to get candlestick information, analyze it as needed, and use it to make investment decisions.

In the next article, we will look at implementing technical analysis indicators such as Bollinger Bands through algorithms, and furthermore, implementing a trading bot that applies investment techniques through periodic API calls.

Thanks for reading.