With BacktestJS you can

Get power by backtesting strategies with multi values

leafleafleafDocy banner shape 01Docy banner shape 02Man illustrationFlower illustration
Trading Strategy Examples

SMA With Params

Estimated reading: 2 minutes 138 views

Summary: How to code a simple sma trading strategy with paramaters.

About

This example will be very similar to the last example but instead of hard coding a low and high sma we will instead use parameters so we can put any low and high sma when we go to run the trading strategy.

Lets start by creating the trading strategy within the app, for more info on this refer to create trading strategy in app

Trading Strategies Create Trading Strategy

Now that the trading strategy is created lets get down to the code.  As you can see in lines 10 and 11 we call lowSMA and highSMA by using bth.params.someDefinedParamName.  Other than this the code is pretty much the same from the last example except we filled in all the hard coded 3 and 90 parts with the params coming from the cli.

				
					// Must Define Params When Creating Strategy: lowSMA, highSMA

// Define imports
import { BTH } from "../infra/interfaces"
import { indicatorSMA } from "../indicators/moving-averages"

// SMA Strategy With Params
export async function sma(bth: BTH) {
    // Get low and high SMA from input
    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 crosses over highSMA
    if (lowSMA > highSMA) {
        await bth.buy()
    }

    // Sell if lowSMA crosses under highSMA
    else {
        await bth.sell()
    }
}
				
			

When we go to run the strategy it will look like this, as you can see we can choose any param we want when it comes to run the trading strategy!!  As a bonus you can even put multiple lowSMA and multiple highSMA (seperated by a comma) and get the results of all the combinations 🤯.

Run Trading Strategy Multi Value

Leave a Comment