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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@kendelchopp/alpaca-js-backtesting

v1.1.3

Published

This package is in no way affiliated with Alpaca

Downloads

10

Readme

@kendelchopp/alpaca-js-backtesting

Disclaimer

This package is in no way affiliated with Alpaca

Getting Started

Installing the package

$ npm install @kendelchopp/alpaca-js-backtesting

Writing the code

  1. Initialize your Alpaca object with the keys
const Alpaca = require('@alpacahq/alpaca-trade-api');

const alpaca = new Alpaca({
  keyId: process.env.API_KEY,
  secretKey: process.env.SECRET_API_KEY,
  paper: true,
  usePolygon: false
});
  1. Initialize your Backtest object

Note: When passing in your alpaca object, you are giving this package the ability to use your keys to make API requests. This package will make API requests for the market data so that it can run the simulation. Be careful when sending out your alpaca object.

const Backtest = require('@kendelchopp/alpaca-js-backtesting');

const backtest = new Backtest({
  alpaca,
  startDate: new Date(2020, 0, 1),
  endDate: new Date(2020, 1, 1)
});
  1. Run your algorithm replacing Alpaca/websockets accordingly.

For example (commented out lines are the old lines to be replaced):

// const client = alpaca.data_ws;
const client = backtest.data_ws;

// alpaca.createOrder({...})
backtest.createOrder({...})
  1. Add a disconnect handler to see how your algorithm performed
client.onDisconnect(() => {
  console.log(backtest.getStats());
});

Alpaca Compatibility

This table lists the version of @kendelchopp/alpaca-js-backtesting with the version of alpaca-trade-api-js it was tested with. Using other versions may require some syntax changes.

| @kendelchopp/alpaca-js-backtesting | @alpacahq/alpaca-trade-api-js | | ---------------------------------- | ----------------------------- | | 1.0.0 | 1.4.1 | | 1.1.0 | 1.4.1 | | 1.1.1 | 1.4.1 | | 1.1.2 | 1.4.1 | | 1.1.3 | 1.4.1 |

Relevant Functions

Backtest#createOrder

Creates an order using the same parameters as Alpaca#createOrder

Example

backtest.createOrder({
  symbol: 'SPY',
  qty: 300,
  side: 'sell',
  type: 'market',
  time_in_force: 'day'
})

Backtest#getStats

Gets the stats for the portfolio including starting and ending value and ROI

Example

console.log(backtest.getStats());

Websocket#connect

Simulates connecting to the API, loads the data, and runs the simulation

Example

client.connect();

Websocket#onConnect

Runs a function when establishing the connection. When backtesting, you will usually just use this to subscribe to channels

Example

client.onConnect(() => {
  client.subscribe(['alpacadatav1/AM.SPY']);
});

Websocket#onDisconnect

Runs a function when the simulation is completed. You can print out the stats here.

Example

client.onDisconnect(() => {
  console.log(backtest.getStats());
});

Websocket#onStockAggMin

This runs your function when a simulated minute occurrs and returns the relevant data. This is where your trading will likely take place. Subject will look something like AM.SPY and data will look like a standard bar data.

Example

client.onStockAggMin((subject, data) => {
  if (data.closePrice < 500) {
    backtest.createOrder({
      symbol: 'SPY',
      qty: 300,
      side: 'buy',
      type: 'market',
      time_in_force: 'day'
    });

    console.log('Bought SPY');
  }
});

Websocket#subscribe

Subscribes to the channels listed. Currently, only aggregate minute channels are supported.

Example

client.onConnect(() => {
  client.subscribe(['alpacadatav1/AM.SPY', 'alpacadatav1/AM.AAPL']);
});