forked from react-financial/react-financial-charts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(indicator): adding algorithm indicator
Used for MA crossover.
- Loading branch information
1 parent
a967a18
commit 206c6a2
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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; | ||
} |
This file contains 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