DEV Community

Vladyslav Tarasenko
Vladyslav Tarasenko

Posted on

Build and Test Trading Strategies in Seconds with trading-cycle ๐Ÿง ๐Ÿ“ˆ

Have you ever wanted a flexible, zero-bloat engine to build, test, and iterate on trading strategies โ€” without digging through mountains of boilerplate code?

Let me introduce you to trading-cycle โ€” a modular, blazing-fast core for backtesting trading logic in JavaScript/TypeScript.

Designed to be lightweight, extensible, and unapologetically developer-friendly.

No magic. No assumptions. Just powerful building blocks.


Why I Built This

Most open-source trading frameworks are either:

  • bloated and hard to extend, or
  • too minimal, lacking support for real-world conditions.

trading-cycle is my take on solving that. It provides:

  • A powerful event-driven pipeline to simulate ticks.
  • A core + handlers architecture for full customization.
  • Built-in presets so you can start in seconds.

And yes, it's written in TypeScript, typed from top to bottom.


โš™๏ธ Installation

npm install trading-cycle
Enter fullscreen mode Exit fullscreen mode

This gives you:

  • The full version, including indicators like Renko, logic handlers, candles, etc.
  • The light version, for when you want just the core.

๐Ÿš€ Quick Start

import {
  TradingCycle,
  defaultPreset,
  handlers,
} from 'trading-cycle/full';

const cycle = new TradingCycle(handlers, defaultPreset);

// your data: ticks from CSV or real-time
ticks.forEach(tick => cycle.execute(tick));

console.log(cycle.state); // inspect the result
Enter fullscreen mode Exit fullscreen mode

Want just the bare bones?

import { TradingCycle } from 'trading-cycle/light';
Enter fullscreen mode Exit fullscreen mode

๐Ÿงฉ Plug-and-Play Handlers

Built-in modules you can use out of the box:

Candles,
PositiveValues,
NegativeValues,
Renko,
TimeRenko,
TestLogic,
PositiveTimeLength,
NegativeTimeLength,
FakeTrader,
LogCandle,
RenkoCounter
Enter fullscreen mode Exit fullscreen mode

Customize the flow using presets or your own HandlerConfig[].


๐Ÿ’ก Writing Your Own Handler

You can write a handler in ~10 lines. Here's the idea:

class MyHandler extends AbstractHandler {
  prev = 0;

  doExecute() {
    if (this.v.close < this.prev) {
      this.prev = this.v.close;
      return this.v;
    }
    this.prev = this.v.close;
    return undefined;
  }
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“„ CSV-Based Backtests

const cycle = new TradingCycle(handlers, defaultPreset);

// Load and parse your CSV into ticks
for (const tick of ticks) {
  cycle.execute(tick);
}

console.log(cycle.state); // final results
Enter fullscreen mode Exit fullscreen mode

Use your own indicators, logic, trader bots โ€” or combine with built-ins.


๐Ÿงช CI/CD + Code Coverage

All PRs go through:

  • yarn lint
  • jest unit tests
  • Code coverage via Codecov
  • Node.js 18 on GitHub Actions
- name: Run tests
  run: yarn test --coverage

- name: Upload to Codecov
  uses: codecov/codecov-action@v3
Enter fullscreen mode Exit fullscreen mode

Fully automated. No manual steps.


๐Ÿ“ฆ Bundle Size

trading-cycle is lean:

Version Minified Gzipped
Full ~4.1kb ~1.7kb
Light Even smaller โšก๏ธ

๐Ÿ” API Overview

new TradingCycle(handlers, config);
cycle.execute(tick);
cycle.state // final outputs
Enter fullscreen mode Exit fullscreen mode

Handlers can:

  • read inputs (this.v)
  • track internal state (this._s)
  • emit values when ready

๐Ÿ‘จโ€๐Ÿ’ป Made with โค๏ธ by @vladtarrow

MIT licensed. Contributions welcome. PRs and GitHub stars fuel this project.


๐Ÿ‘‰ Try it now

npm install trading-cycle
Enter fullscreen mode Exit fullscreen mode

And start simulating strategies without friction.

Have questions? Want to show off your own handlers? Drop a comment below or open an issue ๐Ÿ’ฌ

Top comments (0)