Contents

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

   Nov 19, 2023     2 min read

This post is about “Implementing an automated cryptocurrency trading bot 4 (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 will create a trading bot that will automatically execute trades based on the logic written in the code without me having to keep an eye on it.

In the previous article, we learned how to get candle information and get values. In this article, we will learn what Bollinger Bands are and implement this technical indicator in code.

Introduction to Bollinger Bands

Bollinger Bands is a technical analysis tool developed by John Bollinger and is a popular tool used to identify trends, volatility, and potential reversal points in financial markets.

It consists of three bands, the middle band is a simple moving average and the upper and lower bands are calculated based on the standard deviation from the middle band.

Understanding Bollinger Bands

  1. the middle band
    • The middle band is a simple moving average (SMA) and represents the average price over a certain period of time.
  2. upper and lower bands
    • Upper Band: Calculated as the middle band plus twice the standard deviation.
    • Bottom band: Calculated as the middle band minus twice the standard deviation.
    • These bands provide a dynamic range centered on the middle band, expanding during periods of high volatility and contracting during periods of low volatility.

Implementing technical indicators on charts in code

function bb(tradePrices) {
  const period = 20;
  const movingAverages = [];

  // calculate moving averages
  for (let i = 0; i <= tradePrices.length - period; i++) {
    const average =
      tradePrices.slice(i, i + period).reduce((sum, price) => sum + price, 0) /
      period;
    movingAverages.push(average);
  }

  // calculate the standard deviation
  const standardDeviation = Math.sqrt(
    movingAverages.reduce(
      (sum, avg) => sum + Math.pow(avg - movingAverages[0], 2),
      0
    ) / period
  );

  // Compute the Bollinger bands
  const upperBand = movingAverages[0] + 2 * standardDeviation;
  const middleBand = movingAverages[0];
  const lowerBand = movingAverages[0] - 2 * standardDeviation;

  // Save the result as an object
  const bollingerBands = {
    "Upper bands": upperBand,
    "Middle Band": middleBand,
    "lower band": lowerBand,
  };

  return bollingerBands;
}

module.exports = {
  bb,
};

In this implementation:

The movingAverages array stores the calculated moving averages.

The standard deviation is calculated based on the moving averages.

The upper and lower bands are determined using the standard deviation.