Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added momentum indicator #250

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/indicator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module.exports = function(d3) {
atr = require('./atr')(indicatorMixin, accessor.ohlc, sma),
circularbuffer = require('../util')().circularbuffer,
sroc_init = require('./sroc'),
vwap = require('./vwap')(indicatorMixin, accessor.ohlc);
vwap = require('./vwap')(indicatorMixin, accessor.ohlc),
momentum = require('./momentum')(indicatorMixin, accessor.ohlc);

return {
atr: atr,
Expand All @@ -28,7 +29,8 @@ module.exports = function(d3) {
williams: require('./williams')(indicatorMixin, accessor.ohlc, ema),
adx: require('./adx')(d3.max, indicatorMixin, accessor.ohlc, ema),
bollinger: require('./bollinger')(indicatorMixin, accessor.ohlc, sma),
vwap: vwap
vwap: vwap,
momentum: momentum
};
};

Expand Down
56 changes: 56 additions & 0 deletions src/indicator/momentum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

module.exports = function(indicatorMixin, accessor_ohlc) { // Injected dependencies
return function() { // Closure function
var p = {}, // Container for private, direct access mixed in variables
pastSamples,
currentIndex;

function indicator(data) {
indicator.init();
return data.map(momentum).filter(function(d) { return d.value !== null; });
}

function momentum(d, i) {
var currentValue = p.accessor(d),
value = indicator.mom(currentValue);
//console.log({ date: p.accessor.d(d), value: value }, i, currentValue);
return { date: p.accessor.d(d), value: value };
}

indicator.mom = function(currentValue) {
var value = null;
var pastValue;

if (currentIndex+1 <= p.period) {
pastSamples.push(currentValue);
}
else {
var accessIndex = (currentIndex - p.period) % p.period;
pastValue = pastSamples[accessIndex];
pastSamples[accessIndex] = currentValue;
}

if (!!pastValue) {
value = currentValue - pastValue;
// round to 2 decimal points: value = Math.round((value + 0.0001) * 100) / 100;
}

currentIndex++;
return value;
};

indicator.init = function() {
pastSamples = [];
currentIndex = 0;
return indicator;
};

// Mixin 'superclass' methods and variables
indicatorMixin(indicator, p)
.accessor(accessor_ohlc())
.period(12);

return indicator;
};
};
26 changes: 25 additions & 1 deletion test/spec/bundle/_fixtures/data/momentum.js
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
module.exports = [{ date: new Date(2014, 2, 5), momentum: -1.06 }];
module.exports = {
input: [
{ date: new Date("2020-05-01"), close: 44.34 }, // -5
{ date: new Date("2020-05-02"), close: 44.09 }, // -4
{ date: new Date("2020-05-03"), close: 44.15 }, // -3
{ date: new Date("2020-05-04"), close: 43.61 }, // -2
{ date: new Date("2020-05-05"), close: 44.33 }, // -1
{ date: new Date("2020-05-06"), close: 44.83 }, // 0
{ date: new Date("2020-05-07"), close: 43.59 }, // 1
{ date: new Date("2020-05-08"), close: 43.33 }, // 2
{ date: new Date("2020-05-09"), close: 42.01 }, // 3
{ date: new Date("2020-05-10"), close: 43.21 }, // 4
{ date: new Date("2020-05-11"), close: 43.99 }, // 5
{ date: new Date("2020-05-12"), close: 44.32 }, // 6
],
expected: [
{ date: new Date("2020-05-06"), value: 0.49 },
{ date: new Date("2020-05-07"), value: -0.50 },
{ date: new Date("2020-05-08"), value: -0.82},
{ date: new Date("2020-05-09"), value: -1.60},
{ date: new Date("2020-05-10"), value: -1.12},
{ date: new Date("2020-05-11"), value: -0.84},
{ date: new Date("2020-05-12"), value: 0.73},
]
};
36 changes: 36 additions & 0 deletions test/spec/bundle/indicator/momentumSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
techanModule('indicator/momentum', function(specBuilder) {
'use strict';

var techan = require('../../../../src/techan'),
data = require('./../_fixtures/data/momentum');

var actualInit = function() {
return techan.indicator.momentum;
};

specBuilder.require(require('../../../../src/indicator/momentum'), function(instanceBuilder) {
instanceBuilder.instance('actual', actualInit, function(scope) {
describe('And momentum is initialised with period(5)', function () {
var momentum;

beforeEach(function () {
momentum = scope.momentum.period(5); // reduce window to 5 day (against default 12)
});

it('Then on default invoke, momentum should calculate correct values', function() {
var mom = momentum(data.input);
expect(mom.length).toEqual(data.expected.length);

var accessor = techan.accessor.value();

mom.forEach(function(d, i) {
// round to 2 decimal digits for test comparation
accessor.v(d, Math.round((accessor.v(d) + 0.0001) * 100) / 100);
//console.log("rawValue/rounded", rawValue, roundedValue);
expect(d).toEqual(data.expected[i]);
});
});
});
});
});
});
8 changes: 8 additions & 0 deletions test/spec/common/techanSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ function techanSpec(techan) {
expect(techan.indicator.wilderma()).toBeDefined();
});

it('Then techan.indicator.momentum should be defined', function () {
expect(techan.indicator.momentum).toBeDefined();
});

it('Then techan.indicator.momentum can be constructed', function () {
expect(techan.indicator.momentum()).toBeDefined();
});

it('Then techan.plot should be defined', function () {
expect(techan.plot).toBeDefined();
});
Expand Down