With BacktestJS you can

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

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

Create Structure

Estimated reading: 2 minutes 335 views

Summary: Create the basic skeleton structure that every strategy needs.

About

There are a few MANDATORY structure rules that must be followed in order to create a strategy.  Its important to know this skeleton format and rules as there are only a few and are key to writing a successful strategy


Typescript vs Javascript

I highly advise to use TS instead of JS.  If you really prefer JS instead just run the command “npm run makejs” or “npm build” to create the needed files.  Upon running a new folder called “dist” will be made with all the converted TS code into JS.

If you dont know what typescript is do a quick search as its just a wrapper of JS and for the most part your normal JS code should work. 


Skeleton Code Example

Below we can see some skeleton code that must be used to make any strategy as well as explanations of the rules to follow.

Typescript

				
					// MANDATORY IMPORT 
import { BTH } from "../infra/interfaces"

// THE FUNCTION NAME MUST MATCH THE FILE NAME
export async function sma(bth: BTH) {
    // bth IS A MANDATORY IMPORT AND HOLDS USEFUL FUNCTIONS / OBJECTS 
}
				
			

Javascript

				
					// THE FUNCTION NAME MUST MATCH THE FILE NAME
export async function sma(bth) {
    // bth IS A MANDATORY IMPORT AND HOLDS USEFUL FUNCTIONS / OBJECTS 
}
				
			


Rules

  • The file must exist in src/strategies (for TS) if JS then it must be in /dist/strategies/
  • The function name must match the file name (in this case its “sma”).
  • You must import the bth (backtest helpers) interface.
  • bth with type of BTH is the only param and is mandatory.


bth Helpers

currentCandle – Object that includes the current candle data, see more info in Working With Candles documentation

getCandles() – Function to get past candle data, see more info in Working With Candles documentation

buy() / sell() – Functions that are used to buy / sell.  There are many params that can be used, see more in Perform Buy Sell documentation.

orderBook – Holds all the info regarding asset amounts and if bought in what type such as short and how much etc… more data can be found in the Access Order Book documentation

Params – Gives access to user defined params when running the app, see more info in Working With Parameters documentation.

Leave a Comment