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

highs-and-lows

v0.0.1146

Published

Extract indices of high/low points in a 1D array with a moving window

Downloads

6

Readme

highs-and-lows

Extract indices of high/low points in a 1D array with a moving window

Experimental, use at your own risk

Installation

npm i highs-and-lows

Usage

var hl = require('highs-and-lows');
var prices = [0,1,2,3,4,5,4,3,2,1,0,1,2,3,4,4,4,3,2,1,0,1,2,3,4,5]

var windowSize = 5;
var doFilterRuns = true; //remove runs -- keep the highest highs and lowest lows in each run

//highsAndLows(values, numRowsToLook = 30, doFilterRuns= true, doEnhance=false, candleMode=false)
var doEnhance = true; //if true, add extra value at the end indicating "higher/lower/equal high", etc
var candleMode = false; //if true, assume it's a list of ohlcv candles like [{open high low close volume},...] and use the high/low values for the high/low calculations
var res = hl.highsAndLows(prices, windowSize, doFilterRuns, doEnhance, candleMode);
console.log(res);

// format [ 
//    [ index, high|low, (higher|lower|equal)? high|low ], 
// ...]

//[
//  [ 5, 'high', 'high' ], //first high+low is not marked as lower/higher/equal
//  [ 10, 'low', 'low' ],
//  [ 14, 'high', 'lower high' ],
//  [ 20, 'low', 'equal low' ],
//  [ 25, 'high', 'higher high' ]
//]


var bidAskSpread = 0.01; //1% spread
var profits = hl.buySellProfitsForWindowSize(prices, windowSize, bidAskSpread);

console.log(profits);

//{
//  profit: 8.955,
//  totalGains: 9,
//  totalLosses: 0,
//  maxMoney: 8.955,
//  minMoney: 0,
//  minMoneyAfterSell: 3.98,
//  riskReward: Infinity
//}

//get highs and lows for window size, then 
// [with "buys" at lows and "sells" at highs],
//  calculate "profit" for the given series of prices
//   -buys first low "for free" then sells first high
//   -always filters out runs  
//   -always starts with buy and ends with sell
//   -discards start/end indices otherwise
//   -bidAskSpread is expressed as fraction of price, 0...1 
//   -buys get prices increased by spread/2, sells get price decreased by spread/2
//.buySellProfitsForWindowSize(prices, windowSize, bidAskSpread=0)

//like above but takes raw list of buys/sells produced by buysAndSells function
//.buySellProfitsForIndices(buySellIndices,pricesList,bidAskSpread=0)

//get candle-by-candle "score" for if this candle is a local high or low [high/low oracle indicator for training etc], candle format [time, o, h, l, c, v]
//.calculateBuySellMaskOracleIndicator(candles, windowSize, doNormalize=true) //doNormalize reduces everything to 0...1 where 1 corresponds to windowSize
// // suppose the "buy" indices are [1,3] and the "sells" are [0,1]
// {
//     buyMask,  //[0,1,0,1,0]
//     sellMask, //[1,1,0,0,0]
//     buyMaskDistToNextOne, //[1,0,1,windowSize].map(r=>r/windowSize)
//     sellMaskDistToNextOne //[0,0,windowSize,windowSize,windowSize].map(r=>r/windowSize)
// }

stonks