Skip to content

Commit

Permalink
feat(indicator): adding algorithm indicator
Browse files Browse the repository at this point in the history
Used for MA crossover.
  • Loading branch information
markmcdowell committed Aug 29, 2020
1 parent a967a18 commit 206c6a2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
53 changes: 53 additions & 0 deletions packages/indicators/src/indicator/algorithm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { identity, merge, slidingWindow } from "@react-financial-charts/core";

export interface Algorithm {
(data: any[]): any;
accumulator(): any;
accumulator(newAccumulator: any): Algorithm;
windowSize(): number;
windowSize(windowSize: number): Algorithm;
merge(): any;
merge(newMerge: any): Algorithm;
}

export default function () {
let windowSize = 1;
let accumulator = identity;
let mergeAs = identity;

function algorithm(data: any[]) {
const defaultAlgorithm = slidingWindow().windowSize(windowSize).accumulator(accumulator);

const calculator = merge().algorithm(defaultAlgorithm).merge(mergeAs);

const newData = calculator(data);

return newData;
}

algorithm.accumulator = (newAccumulator?: any) => {
if (newAccumulator === undefined) {
return accumulator;
}
accumulator = newAccumulator;
return algorithm;
};

algorithm.windowSize = (newWindowSize?: number) => {
if (newWindowSize === undefined) {
return windowSize;
}
windowSize = newWindowSize;
return algorithm;
};

algorithm.merge = (newMerge?: any) => {
if (newMerge === undefined) {
return mergeAs;
}
mergeAs = newMerge;
return algorithm;
};

return algorithm as Algorithm;
}
1 change: 1 addition & 0 deletions packages/indicators/src/indicator/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as algo } from "./algorithm";
export { default as ema } from "./ema";
export { default as sma } from "./sma";
export { default as wma } from "./wma";
Expand Down

0 comments on commit 206c6a2

Please sign in to comment.