With BacktestJS you can

Run trading strategies completely free and open source

leaf leaf leaf Docy banner shape 01 Docy banner shape 02 Man illustration Flower illustration
Write Trading Strategy

Working With Candles

Estimated reading: 4 minutes 230 views

Summary: How to retrieve candle data in your strategy.

About

To perform any strategy you must know some form of candle data. To get candle data you can use the historical candle data section to download data directly from binance or to import candle data from a csv.

If the candle data is retrieved through binance you have many types of candle data that can be accessed, if imported then you have some mandatory data that can be accessed and other data is optional as it might not have been included in the csv.

Available Candle Data

All the “mandatory” data is included in downloaded binance data and data imported from a csv file.

All of the “not mandatory” data is included in data downloaded from binance and MIGHT be included in csv data.

Name Type Mandatory
open
int
true
high
int
true
low
int
true
close
int
true
closeTime
date
true
openTime
date
false
volume
int
false
assetVolume
int
false
numberOfTrades
int
false


How To Access The Data

As we know we have a helper called bth (backtest helper). This helper contains variables, objects and functions. There is 1 variable and function related to candle data.

You can always access the current candles params with 1 variable. To access the current open price we will do something like below:


const currentPrice = bth.currentCandle.open

To get past candle data we will use a powerful function called getCandles() which will return an array of numbers or an array of objects.

getCandles() Function

Data Type Type Example Mandatory
Candle Data
string
open
assetVolume
all
True
Start
int
0
True
End
int
7
False

Now that we know the params the getCandles() needs lets go more into the params.


Candle Data – Refer to Available Candle Data above to see all the available types to use. The “all” keyword is special and will return an array of objects instead of array of numbers like all other calls.


Start – Refers to which candle start to get. 0 represents now while 7 would represent 7 candles ago.


End – Refers to which candle to end. If nothing is put then the return will be an array with one value (which value represents now).


*** While this can seem confusing at first, refer to examples below as it might be simpler than you think 🙂


Examples

Lets run over some examples of calls and what they would return.

Example 1


const candles = bth.getCandles('close', 0, 7);
console.log(candles)
// Returns the most current 7 candles 
// close data in an array of numbers.
// ex: [2,4,3,5,4,6,3]
// Log close from 4 candles ago
console.log(candles[4])

Example 2


const candles = bth.getCandles('all', 0, 2);
console.log(candles)
// Returns the last 2 candles data in an array of objects 
// with all data it can get (open, high, low, close...)
// [{
//   open: 2,
//   high: 3
//   low: 1,
//   close: 2.5,
//   assetVolume: 2
// },{
//   open: 2.5,
//   high: 4
//   low: 2,
//   close: 3,
//   assetVolume: 2.3
// }]
// Log asset volume from the last candle
console.log(candles[1].assetVolume)

Example 3


const candles = bth.getCandles('open', 7, 14);
console.log(candles)
// Returns all the open candle prices from 1
// week before to two weeks before
// ex: [2,4,3,5,4,6,3]
// Log open from 7 candles ago
console.log(candles[0])
// Log open from 14 candles ago
console.log(candles[candles.length - 1])

Example 4


const candles = bth.getCandles('volume', 1);
console.log(candles)
// Returns an array of one value which is the current volume amount.
// [25]
// Log current candle volume
console.log(candles[0])
// Same can be achieved with
console.log(bth.currentCandle.volume)


Full Example


// MANDATORY IMPORT 
import { BTH } from "../infra/interfaces"
// Check if higher than last week
export async function checkIfHigherThanLastWeek(bth: BTH) {
// Get 7 last candles
const candles = bth.getCandles('close', 0, 7);
// Check if current candle close is more than a week ago
if (candles[0] > candles[candles.length - 1]) {
console.log("Current candle close is higher then a week ago")
}
// We can achieve the same thing with the following code
if (bth.currentCandle.close > candles[6]) {
console.log("Current candle close is higher then a week ago")
}
}

Leave a Comment

Share this Doc

Working With Candles

Or copy link

CONTENTS