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

walk-forward

v0.1.0

Published

Walk Forward is a JavaScript framework that lets you backtest your trading strategies (stocks, futures, forex, crypto currencies, etc.)

Downloads

3

Readme

Walk Forward

Walk Forward is a JavaScript framework that lets you backtest your trading strategies (stocks, futures, forex, crypto currencies, …).

Features

  • Uses re-usable algorithms (signal generation, position sizing, risk management etc.) that can be freely stacked together.
  • Let's you plug in any data source you like – CSVs, web services, a broker's API etc. (only supports CSVs out of the box for now)
  • Handles any instrument you wish to backtest: Futures, Stocks, Crypto Currencies, Forex etc.
  • Produces standardized CSV output that can be read and displayed by any CSV-capable application (Google Spreadsheets, Microsoft Excel; a matching frontend is on the roadmap)
  • Parameter optimization (with log scales)
  • Written in a flexbile, heavily used and steadily improving language (yes, JavaScript), using current developments (ES6 modules, async/await)
  • Used and tested (unit/integration tests) on the server side, but should also work in the frontend
  • Open source (ISC license)
  • Uses tulip's indicators

Example Code

Prerequisites

  1. You need Node.js and NPM.
  2. Install WalkForward: npm i -S walk-forward
  3. WalkForward uses ES2015 modules (it's the future). In order to use them in current Node.js versions (below 10), we have to transform them to CommonJS modules. We use babel-register to do so: npm i -S babel-register

Your Code

Instruments (TransformableDataSeries)

The foundation of WalkForward are TransformableDataSeries. They're used for instruments, positions and accounts (cash/positions etc.).

Indicators

Algorithms

WalkForward simulates the strategy you provide based on the data you provide and outputs the corresponding statistics (basically gains and losses). The strategy is called whenever an instrument closes and is expected to return orders. Orders tell WalkForward what, when and how much to buy. A strategy can be split up into different parts (algorithms):

  1. Trading signals: Should we buy now?
  2. Position Sizing: If we buy, how much should we buy?
  3. Risk management: Make sure we don't bet everything on one horse.

As those algorithms can by their nature be used in different strategies, WalkForward makes them re-usable and pluggable.

A basic algorithm looks like this:

import { SMA } from 'walk-forward-indicators';

/**
 * Buys 
 */
class SMAStrategy {

	/**
	 * onNewInstrument is called by WalkForward whenever a new instrument is discovered in the
	 * data you provide.
	 */
	onNewInstrument(instrument) {
		// instrument.addTransformer returns a Symbol() that can be used to access the transformer's
		// values when the backtest is running (see onClose method)
		this.fastSMA = instrument.addTransformer(new SMA(5));
		this.slowSMA = instrument.addTransformer(new SMA(10));
	}

	/**
	 * onClose is called by WalkForward whenever data (open/close etc.) was received for an 
	 * instrument. 
	 * It is expected to return an array of orders; every order is an object with two properties:
	 * - size (number) 
	 * - instrument (instrument)
	 * onClose is called with the following arguments: 
	 * - orders (current orders)
	 * - instrument (the instrument which received the data and the close event was fired on)
	 * - instruments (an array of all instruments)
	 * - accounts (the current account)
	 */
	onClose(orders, instrument) {
		if (
			// Current fastSMA is above current slowSMA
			instrument.head().get(this.fastSMA) > instrument.head().get(this.slowSMA) &&
			// Previous fastSMA was below or equal to previous slowSMA; this makes sure that 
			// we only buy on cross overs.
			instrument.back(1).get(this.fastSMA) <= instrument.back(1).get(this.slowSMA)
		) {
			// [...orders] clones the orders array (as you should not modify parameters passed
			// to your functions). It is then extended with the current order.
			// We use a default size of 1 which will later be modified by the position sizing
			// algorithm
			return [...orders, { instrument: instrument, size: 1 }]
		}
		// Default: Just return the existing orders. If we returned an empty array, all existing
		// orders would be canceled.
		return orders;
	}
}

Backtest

In order to run a backtest,

  1. instantiate a new Backtest()
  2. provide a data source (e.g. some CSV files)
  3. write an algorithm that creates the buy signals and another algorithm that

Documentation

Logging

To enable logs, set the log level and components through your environment:

export DEBUG=WalkForward:*
export "DEBUG_LEVELS=debug, info, warn, error"

Algos and stacks

Algos

Backtest

backtest.run()

Transformers

Instruments, results

Instruments

Roadmap

  • [x] Parameter optimization
  • [ ] Configure spread, commission and slippage depending on an order
  • [x] Output relevant display data as JSON (for formatted output in frontend)
  • [x] Interactive web frontend
  • [ ] Instrument configuration (type of instrument, whole sizes, margins)
  • [ ] Margin handling
  • [ ] More optimization techniques (in addition to current log, e.g. linear, other log bases, integers only instead of floats)
  • [x] Relevant ouptut in form of tables (orders, positions in addition to charts)
  • [ ] Better charts for parameter optimization (2D/3D visualization)
  • [ ] Currency handling
  • [ ] Walk forward optimization
  • [ ] More data sources out of the box - Better docs, always better docs

Inspiration

koa's Middleware, bt's reusable algos, glulp 4's chainable serial/parallel functions.