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

pivotrade

v1.0.2

Published

Pivotrade is a Javascript backtesting framework for testing trading strategies on historical price data.

Downloads

7

Readme

Pivotrade

Pivotrade is a Javascript backtesting framework for testing trading strategies on historical price data.

Pivotrades requires only a callback function and will handle historical price data, performance monitoring, and state management, giving you the freedom to focus more on strategy and less on infrastructure.

Documentation

Feel free to visit the documentation

Getting Started

Download from NPM

https://www.npmjs.com/package/pivotrade

> npm i pivotrade

Here is an example implementation of a classic Simple Moving Average (SMA) crossover strategy trading on the S&P ETF. A strategy that involves:

  • Buying N shares of a security when it's 20 day moving average crosses above its 50 day moving average. Set a stop loss at 10% below it's long entry price.

  • Selling N shares of a security when its 20 day average falls below the 50 day average.

// node example/sma_cross.js

const Session = require("pivotrade").Session;
const Algorithm = require("pivotrade").Algorithm;
const SMA = Algorithm.SMA;

const session = new Session({
  name: "SMA Crossover",
  symbol: "SPY",
  capital: 100000,
  start_date: "2006-01-01",
  end_date: "2010-01-01",
  indicators: {
    SMA50: new SMA(50),
    SMA20: new SMA(20)
  }
});

session.backtest((price, account, indicators) => {
  let SMA20 = indicators.SMA20;
  let SMA50 = indicators.SMA50;

  let cur_price = price.close;
  if (account.positions.length === 0) {
    if (SMA20 > SMA50) {
      let num_shares = Math.floor(account.capital / cur_price);
      let stop_loss = 0.90 * cur_price;
      session.buy({ limit: cur_price, quantity: num_shares, stop_loss: stop_loss});
    }
  }

  if (account.positions.length === 1) {
    let position = account.positions[0];
    if (SMA20 < SMA50) {
      session.sell({ id: position.id, limit: cur_price, quantity: position.quantity });
    }
  }
});