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

Add smoothing option to Stochastic #230

Open
wants to merge 2 commits 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
49 changes: 31 additions & 18 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions src/momentum/Stochastic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class Stochastic extends Indicator {
let closes = input.close;
let period = input.period;
let signalPeriod = input.signalPeriod;
let smoothing = input.smoothing;
let format = this.format;
if(!((lows.length === highs.length) && (highs.length === closes.length) )){
throw ('Inputs(low,high, close) not of equal size');
Expand All @@ -45,6 +46,14 @@ export class Stochastic extends Indicator {
let index = 1;
let pastHighPeriods = new LinkedList(period, true, false);
let pastLowPeriods = new LinkedList(period, false, true);
let kSma
if (smoothing > 1) {
kSma = new SMA({
period: smoothing,
values: [],
format: (v) => { return v; }
});
}
let dSma = new SMA({
period : signalPeriod,
values : [],
Expand All @@ -62,7 +71,11 @@ export class Stochastic extends Indicator {
}
let periodLow = pastLowPeriods.periodLow;
k = (tick.close - periodLow) / (pastHighPeriods.periodHigh - periodLow) * 100;
k = isNaN(k) ? 0 : k; //This happens when the close, high and low are same for the entire period; Bug fix for
k = isNaN(k) ? 0 : k; //This happens when the close, high and low are same for the entire period; Bug fix for
if (smoothing > 1) {
k = kSma.nextValue(k);
k = isNaN(k) ? 0 : k;
}
d = dSma.nextValue(k);
tick = yield {
k : format(k),
Expand Down Expand Up @@ -102,4 +115,4 @@ export function stochastic(input:StochasticInput):StochasticOutput[] {
}
Indicator.reverseInputs(input);
return result;
};
};