Last active
August 10, 2019 15:05
-
-
Save andrIvash/5fcb13f37a0913700d049f0cdbf25aac to your computer and use it in GitHub Desktop.
stocks indicators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 1)The minimum price for the same period(extremum) is subtracted from the maximum price for a certain period(extremum). | |
| 2)The closing price for the previous period(ma) is subtracted from the absolute value of the maximum price(extremum). | |
| 3)The closing price for the previous period(ma) is subtracted from the absolute value of the minimum price(extremum). | |
| 4)Calculate the arithmetic mean rate(ATR) | |
| */ | |
| import sma from './sma_indicator.js'; | |
| import findExtremum from './findExtremum.js'; | |
| function atr(arr, gap = 2) { | |
| const extremums = findExtremum(arr, gap); | |
| const maCLose = sma(arr, gap, 'close'); | |
| const atr = ((Math.abs(extremums.max - extremums.min) + | |
| Math.abs(extremums.max - maCLose) + | |
| Math.abs(extremums.min - maCLose)) / 3).toFixed(1); | |
| return atr; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| findExtremum find min and max in the bars row, where length = gap | |
| @param {number} [gap=9] Number - quantity of the first type bars | |
| **/ | |
| function findExtremum (arr, gap = 9) { | |
| let result = []; | |
| try { | |
| if (!Array.isArray(arr) || arr.length === 0) throw 'not empty Array expected as first argument'; | |
| if (gap > arr.lenght || gap < 0) gap = arr.length; | |
| result = arr.slice(0, gap).reduce((prev, cur) => { | |
| if(!cur.hasOwnProperty('max') && cur.hasOwnProperty('min')) throw 'min or max properties expected'; | |
| if(cur.max < 0 || cur.min < 0) throw 'min or max wrong value'; | |
| if (cur.max > prev.max) prev.max = cur.max; | |
| if (cur.min < prev.min) prev.min = cur.min; | |
| return prev; | |
| },{max: 0, min: 0}); | |
| } | |
| catch(err) { | |
| console.error(err); | |
| } | |
| return result; | |
| } | |
| const arr = [ | |
| {min: 1, max:11, close: 11}, | |
| {min: 2, max:12, close: 12}, | |
| {min: 3, max:13, close: 13}, | |
| {min: 4, max:14, close: 14}, | |
| {min: 5, max:15, close: 15}, | |
| {min: 2, max:23, close: 16}, | |
| {min: 4, max:11, close: 17}, | |
| {min: 0, max:0, close: 18}, | |
| {min: 0, max:5, close: 19}, | |
| ]; | |
| console.log(findExtremum(arr)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Simple Moving Average (SMA) | |
| function sma(arr, gap = 2, type = 'close') { | |
| if (!Array.isArray(arr) || arr.length === 0) throw new Error ('Array expected as first argument'); | |
| const result = []; | |
| arr.forEach((elem, i) => { | |
| let subarr = arr.slice(i, gap + i); | |
| if (subarr.length < gap) return; // erase incomplite data | |
| result.push(subarr.reduce((prev, cur) => { | |
| return cur[type] ? prev + cur[type]: null; // if obj doesn't have property - just null returned | |
| }, 0) / gap); | |
| }); | |
| return result; | |
| } | |
| const arr = [ | |
| {close: 11}, | |
| {close: 12}, | |
| {close: 13}, | |
| {close: 14}, | |
| {close: 15}, | |
| {close: 16}, | |
| {close: 17}, | |
| {close: 18}, | |
| {close: 19}, | |
| ]; | |
| console.log(sma(arr, 5, 'close')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment