With BacktestJS you can

Get power by backtesting strategies with multi values

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

Using Indicators

Estimated reading: 3 minutes 359 views

Summary: How and where to call indicators / make your own indicators.

About

While using Indicators is not mandatory, many strategies rely on one to many indicators to decide when to buy / sell an asset.

The application of course does not come with every indicator but does use a third party package called tulind that comes with most of the basics and a few more advanced indicators. It is highly suggested to use the link to tulind and review the indicators that are available and what params are needed / how to use them.

Example

While you can code your own indicator inside the strategy itself its a much better practice to make a helper file with exported functions inside the indicators folder located at /src/indicators that your strategy or many strategies can use.

An example included in BacktestJS is the file called moving-averages.ts in which some moving averages are written using the tulind package. Below is an example and explanation.


const tulind = require('tulind')
// Get SMA
export async function indicatorSMA(candles: number[], length: number, limit?: number) {
// Call SMA function from tulind with the candles and desired sma length
const tulindReturn = await tulind.indicators.sma.indicator([candles], [length])
// Tulind can return a few things, in this case 
// the SMA data is in the first item in its return
const sma = tulindReturn[0]
// Limit means how much of the SMA data to return
if (limit !== undefined) {
// If you requested more SMA's then were 
// generated return all that you have
if (limit >= sma.length) return sma
// Return the most recent SMA 
if (limit === 1) return sma[sma.length - 1]
// Return only a specific amount of SMA's
else return sma.slice(sma.length - limit)
}
// Return the data
return sma
}


Code Explanation

Let’s explain line by line here.

  • Line 2: We declare and export a function called indicatorSMA that has 3 parameters in which 2 are mandatory
  • Line 4: We call the SMA indicator from tulind passing the candles and sma amount.
  • Line 7: The [0] is because tulind can return a few things in an array but only the first element contains all the needed SMA data.
  • Line 10 and later is regarding the limit. The limit is not a mandatory param but limits how many SMA’s to return.
  • Lets explain limit deeper. Say you pass 20 candles and ask for a SMA of 10. In this case you only needed to pass 10 candles worth of data but you sent 20, so tulind will return 10 SMA’s which are the last 10 candles SMA’s. Lets say you pass 10 candles and ask for a 10 SMA then tulind will only return one SMA as you passed the exact amount of needed candles, if instead you send 9 candles then you of course will get an error as you cant get a 10 SMA with 9 candles.

Leave a Comment

Share this Doc

Using Indicators

Or copy link

CONTENTS