With BacktestJS you can

Easily download candle data from binance and export to a csv.

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

Access Order Book

Estimated reading: 3 minutes 180 views

Summary: How to access the order book data object in your strategy.

About

The order book is a key object in BacktestJS that contains all the buy / sell info as well as asset info. This object can be used to get data such as if your bought into a short or current trading amount as well as make updates such as changing the stop loss amount.


Order Book Keys

Key Type Value Info
bought
Boolean
Represents if you are currently bought into anything
boughtLong
Boolean
Represents if you are currently bought into a long
boughtShort
Boolean
Represents if you are currently bought into a short
baseAmount
int
The current base amount you have (in btcusdt this would be the bitcoin amount)
quoteAmount
int
The current quote amount you have in the wallet (usually dollar amount)
stopLoss
int
Amount the stop loss is set to (0 means no stop loss)
takeProfit
int
Amount the take profit is set to (0 means no take profit)
preBoughtQuoteAmount
int
The quote amount (usually dollar) that you had before you bought into anything and is updated once all is sold.

Examples

Example 1


// Update the stop loss to the current candle close
bth.orderBook.stopLoss = bth.currentCandle.close

Example 2


// Buy periodically but only 25% at a time
// Buy first time with 25% of the total
await buy({amount: bth.orderBook.quoteAmount * .25})
// Buy second time with 25% of the total but the total has 
// changed because of the first buy so we will use
// preBoughtQuoteAmount instead of quoteAmount
await buy({amount: bth.orderBook.preBoughtQuoteAmount * .25})


Example 3


// Only buy if not already bought in
if (bth.orderBook.bought === false) {
await buy()
}
// Only buy in not already bought into a long
if (bth.orderBook.boughtLong === false) {
await buy()
}
// Only buy in not already bought into a short
if (bth.orderBook.boughtShort === false) {
await buy()
}


Full Example


// MANDATORY IMPORT
import { BTH } from "../infra/interfaces"
// Check if higher than last candle 
export async function checkLastCandle(bth: BTH) {
// Get last 2 candle closes
const candles = bth.getCandles('close', 0, 2);
// Buy 25% of total if current candle close is more than last candle 
if (candles[0] > candles[1]) {
// Check if first time buying and set stop loss to current candles close
if (bth.orderBook.bought === false) {
await buy({amount: "25%", stopLoss: bth.currentCandle.close})
}
// If candle is higher again then buy with 25% 
// of beginning total and update the stop loss
else {
await buy({amount: bth.orderBook.preBoughtQuoteAmount * .25})
bth.orderBook.stopLoss = bth.currentCandle.low
}
}
// Sell if current candle is less than last candle
else {
await sell()
}
}

In the example above we buy with 25% of our total dollar amount if the current candle is higher then last candle and make the stop loss the current candles close.

For example if we have $1000 we will buy with $250 and set the stop loss to the candles low price.

If bitcoin goes up again next candle and didnt hit the stop loss then we will buy with another $250 by doing bth.orderBook.preBoughtQuoteAmount * .25. If we were to again do bth.orderBook.quoteAmount * .25 then we would get a number higher than 250 that doesn’t reflect the 25% jump on each buy properly.

If we do eventually buy 4 times (25% of total each buy) then when we try to buy a fifth time the buy will be ignored as you have no more to buy in with.

Leave a Comment

Share this Doc

Access Order Book

Or copy link

CONTENTS