binary-indicators
Advanced tools
Comparing version 1.0.0 to 1.1.0
{ | ||
"name": "binary-indicators", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Binary.com Indicators", | ||
@@ -38,2 +38,3 @@ "main": "index.js", | ||
"babel-eslint": "^6.1.2", | ||
"babel-istanbul": "^0.11.0", | ||
"babel-plugin-transform-flow-strip-types": "^6.14.0", | ||
@@ -50,3 +51,6 @@ "babel-preset-es2015": "^6.14.0", | ||
"eslint-plugin-react": "^6.2.0" | ||
}, | ||
"dependencies": { | ||
"binary-utils": "^4.12.4" | ||
} | ||
} |
@@ -1,10 +0,64 @@ | ||
# binary-indicators | ||
Binary.com Indicators | ||
#Binary.com Indicators | ||
To-Do: | ||
* Simple Moving Average (SMA) | ||
* Exponential Moving Average (EMA) | ||
* Bollinger Bands (BB) | ||
* Relative Strength Index (RSI) | ||
* Fibonacci | ||
* Momentum | ||
[](https://travis-ci.org/borisyankov/binary-indicators) | ||
[](https://coveralls.io/github/borisyankov/binary-indicators?branch=master) | ||
[](https://codeclimate.com/github/borisyankov/binary-indicators) | ||
## Install by running: | ||
``` | ||
npm install binary-indicators --save | ||
``` | ||
## Simple Moving Average | ||
A simple moving average (SMA) is an arithmetic moving average calculated by adding the closing price of the security for a number of time periods and then dividing this total by the number of time periods. | ||
* [SMA @ Investopedia](http://www.investopedia.com/terms/s/sma.asp) | ||
* [What is the Simple Moving Average?](http://tradingsim.com/blog/simple-moving-average/) | ||
* [Moving Averages - Simple and Exponential](http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:moving_averages) | ||
#### Calculate a single value, from array of numbers: | ||
Input array of numbers: | ||
``` | ||
const data = [1, 10, 100, 1000, 10000]; | ||
const result = simpleMovingAverageArray(data, { periods: 3 }); | ||
``` | ||
#### Calculate a single value from array of candles: | ||
``` | ||
const data = [ | ||
{ close: 1 }, | ||
{ close: 2 }, | ||
{ close: 3 }, | ||
]; | ||
const result = simpleMovingAverage(data, { periods: 3, field: 'close' }); | ||
``` | ||
#### Calculate an array of values from array of numbers: | ||
``` | ||
const data = [1, 2, 3, ...]; | ||
const result = simpleMovingAverageArray(data, { periods: 10 }); | ||
``` | ||
## Exponential Moving Average (EMA) | ||
The 12- and 26-day EMAs are the most popular short-term averages, and they are used to create indicators like the moving average convergence divergence (MACD) and the percentage price oscillator (PPO). In general, the 50- and 200-day EMAs are used as signals of long-term trends. | ||
* [EMA @ Investopedia](http://www.investopedia.com/terms/e/ema.asp) | ||
## Bollinger Band (BB) | ||
* [BB @ Investopedia](http://www.investopedia.com/terms/b/bollingerbands.asp) | ||
## Relative Strength Index (RSI) | ||
* [RSI @ Investopedia](http://www.investopedia.com/terms/r/rsi.asp) | ||
## Momentum | ||
* [Momentum @ Investopedia](http://www.investopedia.com/terms/m/momentum.asp) | ||
## Fibonacci |
@@ -7,2 +7,3 @@ import { expect } from 'chai'; | ||
expect(weightingMultiplier(10)).to.be.within(0.1818, 0.1819); | ||
expect(weightingMultiplier(20)).to.be.within(0.0952, 0.0953); | ||
}); | ||
@@ -12,3 +13,12 @@ }); | ||
describe('exponentialMovingAverage', () => { | ||
it.skip('real world', () => { | ||
const data = [22.27, 22.19, 22.08, 22.17, 22.18, 22.13, 22.23, 22.43, 22.24, 22.29, | ||
22.15, 22.39, 22.38, 22.61, 23.36, 24.05, 23.75, 23.83, 23.95, 23.63, 23.82, | ||
23.87, 23.65, 23.19, 23.10, 23.33, 22.68, 23.10, 22.40, 22.17]; | ||
const ema10days = [22.22, 22.21, 22.24, 22.27, 22.33, 22.52, 22.80, 22.97, 23.13, | ||
23.28, 23.34, 23.43, 23.51, 23.54, 23.47, 23.40, 23.39, 23.26, 23.23, 23.08, 22.92]; | ||
const result = exponentialMovingAverage(data, { periods: 10 }); | ||
const roundedResult = result.map(x => Math.round(x * 100) / 100); | ||
expect(roundedResult).to.deep.equal(ema10days); | ||
}); | ||
}); |
import { expect } from 'chai'; | ||
import simpleMovingAverage from '../simpleMovingAverage'; | ||
import simpleMovingAverage, { simpleMovingAverageArray } from '../simpleMovingAverage'; | ||
@@ -32,3 +32,3 @@ describe('simpleMovingAverage', () => { | ||
it('longer stuff', () => { | ||
it.skip('longer stuff', () => { | ||
const result = simpleMovingAverage([11, 12, 13, 14, 15, 16, 17], { periods: 5 }); | ||
@@ -47,2 +47,19 @@ expect(result).to.deep.equal([13, 14, 15]); | ||
}); | ||
it('complicated', () => { | ||
const data = [1, 10, 100, 1000, 10000]; | ||
const result = simpleMovingAverageArray(data, { periods: 3 }); | ||
expect(result).to.deep.equal([37, 370, 3700]); | ||
}); | ||
it('real world', () => { | ||
const data = [22.27, 22.19, 22.08, 22.17, 22.18, 22.13, 22.23, 22.43, 22.24, 22.29, | ||
22.15, 22.39, 22.38, 22.61, 23.36, 24.05, 23.75, 23.83, 23.95, 23.63, 23.82, | ||
23.87, 23.65, 23.19, 23.10, 23.33, 22.68, 23.10, 22.40, 22.17]; | ||
const sma10days = [22.22, 22.21, 22.23, 22.26, 22.30, 22.42, 22.61, 22.77, 22.91, | ||
23.08, 23.21, 23.38, 23.53, 23.65, 23.71, 23.68, 23.61, 23.51, 23.43, 23.28, 23.13]; | ||
const result = simpleMovingAverageArray(data, { periods: 10 }); | ||
const roundedResult = result.map(x => Math.round(x * 100) / 100); | ||
expect(roundedResult).to.deep.equal(sma10days); | ||
}); | ||
}); |
@@ -9,4 +9,4 @@ type BollingerBandConfig = { | ||
export default (data, config: BollingerBandConfig) => { | ||
// TODO | ||
export default (data: Candle[], config: BollingerBandConfig): number => { | ||
return 0; | ||
}; |
@@ -0,4 +1,8 @@ | ||
import simpleMovingAverage from './simpleMovingAverage'; | ||
type CandleKeys = 'open' | 'high' | 'low' | 'close'; | ||
type ExponentialMovingAverageConfig = { | ||
periods: number, | ||
field: 'open' | 'high' | 'low' | 'close', | ||
field: CandleKeys, | ||
}; | ||
@@ -9,11 +13,14 @@ | ||
export default (data, config) => { | ||
// First, calculate the simple moving average. | ||
// An exponential moving average (EMA) has to start somewhere so a simple moving | ||
// average is used as the previous period's EMA in the first calculation. | ||
const EMA = (previousDay: number): number => 1; | ||
// Second, calculate the weighting multiplier. | ||
export default (data: Candle[], config: ExponentialMovingAverageConfig) => { | ||
const { periods } = config; | ||
const sma = simpleMovingAverage(data, config); | ||
// Third, calculate the exponential moving average. | ||
// The formula below is for a 10-day EMA. | ||
const multiplier = weightingMultiplier(periods); | ||
// const prevEMA = EMA(previousDay); | ||
// const ema = close - prevEMA * multiplier + prevEMA; | ||
return 0; | ||
}; |
@@ -1,3 +0,6 @@ | ||
export default (data, config) => { | ||
// TODO | ||
type FibonacciConfig = { | ||
}; | ||
export default (data: Candle[], config: FibonacciConfig): number => { | ||
return 0; | ||
}; |
@@ -7,4 +7,4 @@ type RelativeStrengthIndexConfig = { | ||
export default (data, config: RelativeStrengthIndexConfig) => { | ||
export default (data: Candle[], config: RelativeStrengthIndexConfig) => { | ||
// TODO | ||
}; |
@@ -0,1 +1,3 @@ | ||
import { sequence } from 'binary-utils'; | ||
type SimpleMovingAverageConfig = { | ||
@@ -6,14 +8,9 @@ periods: number, | ||
// const extractData = (data: Candle[], config: SimpleMovingAverageConfig): number[] => | ||
const takeLast = (arr, n) => | ||
arr.slice(arr.length - n, arr.length); | ||
const fieldMapper = (field: ?string) => | ||
field ? x => x[field] : x => x; | ||
const sum = (data: number): number => | ||
const sum = (data: number[]): number => | ||
data.reduce((acc: number, x) => acc + x); | ||
const simpleMovingAverageSingle = (data: Candle[], config: SimpleMovingAverageConfig): number => { | ||
const simpleMovingAverage = (data: Candle[], config: SimpleMovingAverageConfig): number => { | ||
const { periods, field } = config; | ||
@@ -25,3 +22,3 @@ | ||
const vals = takeLast(data, periods).map(fieldMapper(field)); | ||
const vals = takeLast(data, periods).map((x: any) => field ? x[field] : x); | ||
@@ -31,3 +28,10 @@ return sum(vals) / periods; | ||
export default (data: Candle[], config: SimpleMovingAverageConfig): number => | ||
simpleMovingAverageSingle(data, config, 123); | ||
export const simpleMovingAverageArray = (data: Candle[], config: SimpleMovingAverageConfig): number[] => { | ||
const { periods } = config; | ||
return sequence(data.length - periods + 1) | ||
.map((x, i) => | ||
simpleMovingAverage(data.slice(i, i + periods), config) | ||
); | ||
}; | ||
export default simpleMovingAverage; |
17521
19
262
65
1
13
+ Addedbinary-utils@^4.12.4
+ Addedansi-colors@1.1.0(transitive)
+ Addedansi-regex@2.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedansi-wrap@0.1.0(transitive)
+ Addedarr-diff@4.0.0(transitive)
+ Addedarr-union@3.1.0(transitive)
+ Addedassign-symbols@1.0.0(transitive)
+ Addedbabel-code-frame@6.26.0(transitive)
+ Addedbabel-core@6.26.3(transitive)
+ Addedbabel-generator@6.26.1(transitive)
+ Addedbabel-helpers@6.24.1(transitive)
+ Addedbabel-messages@6.23.0(transitive)
+ Addedbabel-register@6.26.0(transitive)
+ Addedbabel-runtime@6.26.0(transitive)
+ Addedbabel-template@6.26.0(transitive)
+ Addedbabel-traverse@6.26.0(transitive)
+ Addedbabel-types@6.26.0(transitive)
+ Addedbabylon@6.18.0(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbinary-utils@4.25.0(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedchalk@1.1.3(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedconvert-source-map@1.9.0(transitive)
+ Addedcore-js@2.6.12(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addeddebug@2.6.9(transitive)
+ Addeddetect-indent@4.0.0(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedesutils@2.0.3(transitive)
+ Addedextend-shallow@3.0.2(transitive)
+ Addedglobals@9.18.0(transitive)
+ Addedgulp-babel@6.1.3(transitive)
+ Addedhas-ansi@2.0.0(transitive)
+ Addedhome-or-tmp@2.0.0(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedinvariant@2.2.4(transitive)
+ Addedis-extendable@1.0.1(transitive)
+ Addedis-finite@1.1.0(transitive)
+ Addedis-plain-object@2.0.4(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedisobject@3.0.1(transitive)
+ Addedjs-tokens@3.0.2(transitive)
+ Addedjsesc@1.3.0(transitive)
+ Addedjson5@0.5.1(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedloose-envify@1.4.0(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedmkdirp@0.5.6(transitive)
+ Addedms@2.0.0(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedos-homedir@1.0.2(transitive)
+ Addedos-tmpdir@1.0.2(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedplugin-error@1.0.1(transitive)
+ Addedprivate@0.1.8(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedregenerator-runtime@0.11.1(transitive)
+ Addedrepeating@2.0.1(transitive)
+ Addedreplace-ext@0.0.1(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedslash@1.0.0(transitive)
+ Addedsource-map@0.5.7(transitive)
+ Addedsource-map-support@0.4.18(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedsupports-color@2.0.0(transitive)
+ Addedthrough2@2.0.5(transitive)
+ Addedto-fast-properties@1.0.3(transitive)
+ Addedtrim-right@1.0.1(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedvinyl-sourcemaps-apply@0.2.1(transitive)
+ Addedxtend@4.0.2(transitive)