JSPM

  • Created
  • Published
  • Downloads 104
  • Score
    100M100P100Q65650F
  • License MIT

API for backtesting trading strategies in JavaScript and TypeScript.

Package Exports

  • grademark

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (grademark) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

grademark

API for backtesting financial trading strategies in JavaScript and TypeScript.

WORK IN PROGRESS: The API is fairly stable, but there are features yet to be implemented.

This API builds on Data-Forge and is best used from Data-Forge Notebook (making it easy to plot charts and visualize).

To learn more about working with data in JavaScript please read my book Data Wrangling with JavaScript.

For news and updates see my blog The Data Wrangler.

Example output

From the Grademark first example here's some example output.

Analysis of a sequence of trades looks like this:

Analysis of trades screenshot

Here's a chart that visualizes the equity curve for the example strategy:

Equity curve

Here's another chart, this one is a visualization of the drawdown for the example strategy:

Drawdown

Pre-requisites

  • Make sure your data is sorted in forward chronological order.

Features

  • Define a trading strategy with entry and exit rules.
  • Backtest a trading strategy on a single financial instrument.
  • Apply custom indicators to your input data series.
  • Specify lookback period.
  • Built-in intrabar stop loss.
  • Compute and plot equity curve and drawdown charts.
  • Throughly covered by automated tests.

Coming soon

  • Calculation of risk and rmultiples.
  • Intrabar profit target.
  • Intrabar trailing stop loss.
  • Plot a chart of trailing stop loss.
  • Conditional buy on price level (intrabar).
  • Parameters.
  • Optimization based on permutations of parameters.
  • Monte Carlo simulation.
  • Walk-forward backtesting.
  • Plot a chart of risk over time.

Maybe coming later

  • Support for precise decimal numbers.

Not coming soon

Due to this being a simple API there is no support (at least not yet) for:

  • Fees.
  • Slippage.
  • Position sizing.
  • Testing multiple instruments / portfolio simulation / ranking instruments.
  • Short selling.

Complete examples

For a ready to go example please see the repo grademark-first-example.

Usage

Instructions here are for JavaScript, but this library is written in TypeScript and so it can also be used from TypeScript.

Installation

npm install --save grademark

Import modules

const dataForge = require('data-forge');
require('data-forge-indicators'); // For the moving average indicator.
require('data-forge-plot'); // For rendering charts.
const { backtest, analyze, computeEquityCurve, computeDrawdown } = require('grademark');

Load your data

Use Data-Forge to load and prep your data, make sure your data is sorted in forward chronological order.

This example loads a CSV file, but feel free to load your data from REST API, database or wherever you want!

let inputSeries = dataForge.readFileSync("STW.csv")
    .parseCSV()
    .parseDates("date", "DD/MM/YYYY")
    .parseFloats(["open", "high", "low", "close", "volume"])
    .setIndex("date") // Index so we can later merge on date.
    .renameSeries({ date: "time" });

The example data file is available in the example repo.

Add indicators

Add whatever indicators and signals you want to your data.

const movingAverage = inputSeries
    .deflate(bar => bar.close)          // Extract closing price series.
    .sma(30);                           // 30 day moving average.

inputSeries = inputSeries
    .withSeries("sma", movingAverage)   // Integrate moving average into data, indexed on date.
    .skip(30)                           // Skip blank sma entries.

Create a strategy

This is a very simple and very naive mean reversion strategy:

const strategy = {
    entryRule: (enterPosition, bar, lookback) => {
        if (bar.close < bar.sma) { // Buy when price is below average.
            enterPosition();
        }
    },

    exitRule: (exitPosition, position, bar, lookback) => {
        if (bar.close > bar.sma) {
            exitPosition(); // Sell when price is above average.
        }
    },

    stopLoss: (entryPrice, latestBar, lookback) => { // Optional intrabar stop loss.
        return entryPrice * (5/100); // Stop out on 5% loss from entry price.
    },
};

Running a backtest

Backtest your strategy, then compute and print metrics:

const trades = backtest(strategy, inputSeries)
console.log("Made " + trades.count() + " trades!");

const startingCapital = 10000;
const analysis = analyze(startingCapital, trades);
console.log(analysis);

Visualizing the results

Use Data-Forge Plot to visualize the equity curve and drawdown chart from your trading strategy:

computeEquityCurve(trades)
    .plot()
    .renderImage("output/my-equity-curve.png");

computeDrawdown(trades)
    .plot()
    .renderImage("output/my-drawdown.png");

Advanced backtesting

We are only just getting started here and in future notebooks, videos and blog posts we'll explore some of the more advanced aspects of backtesting including:

  • Market filters
  • Ranking and portfolio simulation
  • Position sizing
  • Optimization
  • Walk-forward testing
  • Monte-carlo testing
  • Comparing systems
  • Eliminating bias

Follow my blog and YouTube channel to keep up.

Resources