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

Working With Parameters

Estimated reading: 3 minutes 138 views

Summary: How to add and work with dynamic params in your trading strategy.

About

Parameters are one key feature that turns this from a strong application to an incredibly powerful application and opens up the ability to use multi values.

Lets say you write a strategy to buy and sell based on simple moving average (sma) values. Let’s make it simple, if the low sma goes higher then the high sma then buy and sell if the low sma goes lower than the high sma.

In your code you can define low and high sma values or you can instead turn them into params. This means that anytime you run the sma strategy the application will ask you to provide a low and high sma, pretty convenient 🙂 .

Now imagine instead you provide 20 different low sma’s and 30 different high sma’s together at once and then hit enter to run and the application tests every variation automatically and gives the results to all of them!!! Now thats real power that the backtestjs application offers and is the basis of running with multi values.

How to setup parameters

There are 2 major setup points to use parameters.

1. Add the parameters when creating the strategy in the app. Read more on this in the Create Strategy In App documentation.

2. Call and use these params in the strategies code. in the below code you can see how we might use low and high sma with buy and sells.

Full Example


// Define imports
import { BTH } from "../infra/interfaces"
import { indicatorSMA } from "../indicators/moving-averages"
// SMA Strategy
export async function paramsSMA(bth: BTH) {
// Get low and high SMA from input when running the application
const lowSMAInput = bth.params.lowSMA
const highSMAInput = bth.params.highSMA
// Get last candles based on low and high SMA
const lowSMACandles = await bth.getCandles('close', 0, lowSMAInput) 
const highSMACandles = await bth.getCandles('close', 0, highSMAInput)
// Get low and high SMA
const lowSMA = await indicatorSMA(lowSMACandles, lowSMAInput, 1)
const highSMA = await indicatorSMA(highSMACandles, highSMAInput, 1)
// Buy if lowSMA SMA crosses over highSMA SMA
if (lowSMA > highSMA) [
await bth.buy()
]
// Sell if lowSMA SMA crosses under highSMA SMA
else {
await bth.sell()
}
}

In the example above we have 2 lines to get the params from the cli input:

  • bth.params.lowSMA
  • bth.params.highSMA

In this case when we created the strategy in the CLI we would have defined two params called “lowSMA” and “highSMA”

Leave a Comment

Share this Doc

Working With Parameters

Or copy link

CONTENTS