{"id":15743899,"url":"https://github.com/doowb/simple-moving-avg","last_synced_at":"2025-03-13T11:30:36.152Z","repository":{"id":57360469,"uuid":"72679475","full_name":"doowb/simple-moving-avg","owner":"doowb","description":"Calculate the simple moving average for an array.","archived":false,"fork":false,"pushed_at":"2016-11-02T20:41:11.000Z","size":13,"stargazers_count":5,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-05-01T22:10:44.132Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/doowb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"contributing.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-02T20:40:46.000Z","updated_at":"2019-02-19T09:34:53.000Z","dependencies_parsed_at":"2022-09-06T22:23:17.550Z","dependency_job_id":null,"html_url":"https://github.com/doowb/simple-moving-avg","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doowb%2Fsimple-moving-avg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doowb%2Fsimple-moving-avg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doowb%2Fsimple-moving-avg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doowb%2Fsimple-moving-avg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/doowb","download_url":"https://codeload.github.com/doowb/simple-moving-avg/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221362135,"owners_count":16805357,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-04T03:21:56.994Z","updated_at":"2024-10-24T23:40:34.517Z","avatar_url":"https://github.com/doowb.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# simple-moving-avg [![NPM version](https://img.shields.io/npm/v/simple-moving-avg.svg?style=flat)](https://www.npmjs.com/package/simple-moving-avg) [![NPM downloads](https://img.shields.io/npm/dm/simple-moving-avg.svg?style=flat)](https://npmjs.org/package/simple-moving-avg) [![Linux Build Status](https://img.shields.io/travis/doowb/simple-moving-avg.svg?style=flat\u0026label=Travis)](https://travis-ci.org/doowb/simple-moving-avg) [![Windows Build Status](https://img.shields.io/appveyor/ci/doowb/simple-moving-avg.svg?style=flat\u0026label=AppVeyor)](https://ci.appveyor.com/project/doowb/simple-moving-avg)\n\n\u003e Calculate the simple moving average for an array.\n\n## Install\n\nInstall with [npm](https://www.npmjs.com/):\n\n```sh\n$ npm install --save simple-moving-avg\n```\n\n## Usage\n\n```js\nvar sma = require('simple-moving-avg');\n```\n\n## API\n\n### [sma](index.js#L25)\n\nCalculate the simple moving average of a given array and using a subset of `n` elements. Note that elements that cannot be calculated because of not enough items in the subset will be set to 0\n\n**Example**\n\n```js\nvar arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];\nconsole.log(sma(arr, 3));\n//=\u003e [ 0, 0, 2, 3, 4, 5, 6, 7, 8, 9 ]\n\n// center the mean by passing `true` as the third argument\nconsole.log(sma(arr, 3, true));\n//=\u003e [ 0, 2, 3, 4, 5, 6, 7, 8, 9, 0 ]\n```\n\n**Params**\n\n* `arr` **{Array}**: Array containing full set of items to calcualte\n* `n` **{Number}**: Number of items in each subset to use\n* `center` **{Boolean}**: Center around the item in the array. Defaults to `false`. See [center](#center) for more details.\n* `returns` **{Array}**: Array containing averaged values\n\n### [center](index.js#L66)\n\nCalculate the simple moving average of a given array and using a subset of `n` elements. The subset is calculated around the current index using `{i - ceil(n/2) .. i + ceil(n/2)}`. This requires that `n` is odd to have a balanced number of items on either side of `i`. Note that elements that cannot be calculated because of not enough items in the subset will be set to 0\n\n**Example**\n\n```js\nconsole.log(sma.center(arr, 3));\n//=\u003e [ 0, 2, 3, 4, 5, 6, 7, 8, 9, 0 ]\n```\n\n**Params**\n\n* `arr` **{Array}**: Array containing full set of items to calcualte\n* `n` **{Number}**: Number of items in each subset to use\n* `returns` **{Array}**: Array containing averaged values\n\n### [calc](index.js#L113)\n\nThe calculation function used for calculating the next simple moving average in a series of simple moving averages. This is used in the [sma](#sma) and [center](#center) functions after calculating the average of the first subset. This reduces the amount of times that the total array needs to be sliced and also allows for using the calculation function outside of this library when streaming an array of values.\n\n**Example**\n\n```js\nvar prevAvg = avg([1, 2, 3]); // fake subset array with 3 values\nvar currentVal = 4; // current value (the value that would be next in the main array)\nvar firstVal = 1; // first value from the subset used to calculate the previous avg;\n\n// create a calc function that will calculate the next average using a subset size of 3\nvar calcFn = sma.calc(3);\nvar currentAvg = calcFn(prevAvg, currentVal, firstVal);\nconsole.log(currentAvg);\n//=\u003e 3\n```\n\n**Params**\n\n* `n` **{Number}**: Size of the subsets\n* `precision` **{Number}**: Number of decimal places to use. Defaults to 0\n* `returns` **{Number}**: Average from the calculation.\n\n## About\n\n### Related projects\n\n[array-avg](https://www.npmjs.com/package/array-avg): Calculate the average of an array of numbers. | [homepage](https://github.com/doowb/array-avg \"Calculate the average of an array of numbers.\")\n\n### Contributing\n\nPull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).\n\nPlease read the [contributing guide](contributing.md) for avice on opening issues, pull requests, and coding standards.\n\n### Building docs\n\n_(This document was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme) (a [verb](https://github.com/verbose/verb) generator), please don't edit the readme directly. Any changes to the readme must be made in [.verb.md](.verb.md).)_\n\nTo generate the readme and API documentation with [verb](https://github.com/verbose/verb):\n\n```sh\n$ npm install -g verb verb-generate-readme \u0026\u0026 verb\n```\n\n### Running tests\n\nInstall dev dependencies:\n\n```sh\n$ npm install -d \u0026\u0026 npm test\n```\n\n### Author\n\n**Brian Woodward**\n\n* [github/doowb](https://github.com/doowb)\n* [twitter/doowb](http://twitter.com/doowb)\n\n### License\n\nCopyright © 2016, [Brian Woodward](https://github.com/doowb).\nReleased under the [MIT license](LICENSE).\n\n***\n\n_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.2.0, on November 02, 2016._","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdoowb%2Fsimple-moving-avg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdoowb%2Fsimple-moving-avg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdoowb%2Fsimple-moving-avg/lists"}