npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tradalize/core

v0.5.1

Published

Backtesting framework

Downloads

156

Readme

@tradalize/core

@tradalize/core is a JavaScript library designed to facilitate algorithmic trading backtesting. The library provides a set of functions and classes allowing users to efficiently run backtests for their trading algorithms.

Getting Started

Installation

Install the package using npm:

npm install @tradalize/core

Usage

The main entry point in your future backrest is a Mainframe class

import { Mainframe } from "@tradalize/core";

To run properly this class requires 2 parameters to be provided in its constructor - Datafeed and Strategy. These are essential concepts.

Datafeed

Datafeed is a class, that will provide candlestick data to your Strategy. You can implement your own Datafeed (to provide data from your local DB or even from a file) or use some of the already implemented classes in this library (e.g. BinanceFuturesDatafeed)

import { BinanceFuturesDatafeed } from "@tradalize/core";

To implement your custom Datafeed you need to extend the abstract class and implement only 1 method and 2 properties

import { Datafeed } from "@tradalize/core";

class MyCustomDatafeed extends Datafeed {
  // A symbol/ticker to load
  symbol: string;

  // A Timeframe wo work with (1d, 1h)
  timeframe: Timeframe;

  public async loadNextChunk(): Promise<Candle[]> {
    // Here write your logic for loading new data
  }
}

That's it. After that, this Datafeed will emulate the market behavior and provide a new Candle one by one to your strategy. When it reaches the end of the list, it will try to load a new chunk of data by calling the loadNextChunk method. If this method returns an empty array, backtesting will stop. So you can control on which part of your data you want to run your backtests.

Broker

Before proceeding with Strategy we have to discuss Broker because they are tight together. During your backtests your strategy will execute orders. You can decide just to run your backtest in memory and see the result or save your trades to some persistent storage (DB, spreadsheet, whatever...) for further analysis. That is what Broker is responsible for.

Same as all entities mentioned in this guide, you simply need to import and extend an abstract class, or you can use some predefined Brokers.

import { Broker } from "@tradalize/core";

class MyBroker extends Broker {
  public openPosition(payload: OpenPositionPayload): void | Promise<void> {
    // Implement your logic for the position opening here
    // See the payload type in the type-hint
  }

  public closePosition(payload: ClosePositionPayload): void | Promise<void> {
    // Implement your logic for position closing here
    // See the payload type in the type-hint
  }
}

Strategy

Strategy is the most valuable part of any backtest. Here you gone implement your logic of making trading decision.

import { Strategy, POSITION_DIRECTION } from "@tradalize/core";

class MyStrategy extends Strategy {
  public update(candle: Candle): void | Promise<void> {
    // update - is the only method you need to implement to make your strategy work
    // this method will receive a new candle one by one until they ends

    // Here's a sample strategy
    const dayOfTheWeek = new Date(candle.openTime).getDay();

    // Some trading condition
    if (dayOfTheWeek === 0) {
      // To open a trade you need to set this internal property to the position direction you need
      // Trade will be executed on the next candle open
      this.openOnNext = POSITION_DIRECTION.Long;
    }

    if (dayOfTheWeek === 5) {
      // Same for the cancelation of the position, but here you just have to use the boolean value
      this.closeOnNext = true;
    }
  }
}

Mainframe

And finally - let's run our strategy

import { Mainframe } from "@tradalize/core";

const broker = new MyBroker();
const strategy = new MyStrategy(broker);

const mf = new Mainframe(datafeed, strategy);

await mf.backtest();

That's it :)

Afterwords

This is only essential functionality. This guide does not cover how to work with analysis, analyze your trades, and strategy performance. It will be added soon.

As I said before - this is only a library. But I will add a starter app template soon to provide more "framework-ish" developer experience for the backtesting