-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move indicators to libs (#22)
- Loading branch information
Showing
23 changed files
with
143 additions
and
22 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,20 @@ | ||
export * from "@hooks"; | ||
export * from "./constants/products"; | ||
export * from "./indicators/ATR"; | ||
export * from "./indicators/BOLL"; | ||
export * from "./indicators/CCI"; | ||
export * from "./indicators/EMA"; | ||
export * from "./indicators/HP-Filter"; | ||
export * from "./indicators/HV"; | ||
export * from "./indicators/KDJ"; | ||
export * from "./indicators/MACD"; | ||
export * from "./indicators/MAX"; | ||
export * from "./indicators/MIN"; | ||
export * from "./indicators/PROD"; | ||
export * from "./indicators/RANGE"; | ||
export * from "./indicators/RANK"; | ||
export * from "./indicators/RSI"; | ||
export * from "./indicators/SAR"; | ||
export * from "./indicators/SMA"; | ||
export * from "./indicators/STD"; | ||
export * from "./indicators/SUM"; | ||
export * from "./utils"; |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,122 @@ | ||
import { useMAX, useMIN } from "@libs"; | ||
|
||
const LOW_TYPE = 0; | ||
const HIGH_TYPE = 1; | ||
|
||
export const useZigZag = (high: Series, low: Series, period: number) => { | ||
const lastHighPeak = useSeries("lastHighPeak", high, { display: "line" }); | ||
// add second paramenter {"type": "Line", "panel": "main"} to plot in the graph, e.g. | ||
// const lastHighPeak = useOutputSeries("lastHighPeak", {"type": "Line", "panel": "main"}); | ||
const lastLowPeak = useSeries("lastLowPeak", high); | ||
const zigzagValue = useSeries("zigzagValue", high); | ||
|
||
const currentZigzagValue = useSeries("currentZigzagValue", high); | ||
const lastZigzagValue = useSeries("lastZigzagValue", high); | ||
const secondLastZigzagValue = useSeries("secondLastZigzagValue", high); | ||
|
||
const sourceMax = useMAX(high, period); | ||
const sourceMin = useMIN(low, period); | ||
|
||
const lastHigh = useRef(0); | ||
const lastLow = useRef(0); | ||
const lastHighIdx = useRef(0); | ||
const lastLowIdx = useRef(0); | ||
const zigzagValueList = useRef<number[]>([]); | ||
|
||
useEffect(() => { | ||
for ( | ||
let i = Math.max(0, lastLowPeak.length) - 1; | ||
i < high.length - 1; | ||
i++ | ||
) { | ||
if (i <= 0) { | ||
lastHighPeak[0] = high[0]; | ||
lastLowPeak[0] = low[0]; | ||
lastHighIdx.current = 0; | ||
lastLowIdx.current = 0; | ||
lastHigh.current = high[0]; | ||
lastLow.current = low[0]; | ||
zigzagValue[0] = NaN; | ||
zigzagValueList.current = []; | ||
} else { | ||
lastLowPeak[i] = lastLowPeak[i - 1]; | ||
lastHighPeak[i] = lastHighPeak[i - 1]; | ||
zigzagValue[i] = NaN; | ||
|
||
if (isPeriodHighest(i)) { | ||
if ( | ||
getLastPeakType() == LOW_TYPE || | ||
(getLastPeakType() == HIGH_TYPE && high[i] > lastHigh.current) | ||
) { | ||
lastHighPeak[i] = high[i]; | ||
|
||
zigzagValue[i] = high[i]; | ||
if (getLastPeakType() == HIGH_TYPE) { | ||
zigzagValue[lastHighIdx.current] = NaN; | ||
zigzagValueList.current.pop(); | ||
} | ||
|
||
zigzagValueList.current.push(high[i]); | ||
lastHighIdx.current = i; | ||
lastHigh.current = high[i]; | ||
} | ||
} else if (isPeriodLowest(i)) { | ||
if ( | ||
getLastPeakType() == HIGH_TYPE || | ||
(getLastPeakType() == LOW_TYPE && low[i] < lastLow.current) | ||
) { | ||
lastLowPeak[i] = low[i]; | ||
|
||
zigzagValue[i] = low[i]; | ||
if (getLastPeakType() == LOW_TYPE) { | ||
zigzagValue[lastLowIdx.current] = NaN; | ||
zigzagValueList.current.pop(); | ||
} | ||
|
||
zigzagValueList.current.push(low[i]); | ||
lastLowIdx.current = i; | ||
lastLow.current = low[i]; | ||
} | ||
} | ||
} | ||
|
||
const zigzagLength = zigzagValueList.current.length; | ||
if (zigzagLength == 0) { | ||
currentZigzagValue[i] = NaN; | ||
lastZigzagValue[i] = NaN; | ||
secondLastZigzagValue[i] = NaN; | ||
} else if (zigzagLength == 1) { | ||
currentZigzagValue[i] = zigzagValueList.current[zigzagLength - 1]; | ||
lastZigzagValue[i] = NaN; | ||
secondLastZigzagValue[i] = NaN; | ||
} else if (zigzagLength == 2) { | ||
currentZigzagValue[i] = zigzagValueList.current[zigzagLength - 1]; | ||
lastZigzagValue[i] = zigzagValueList.current[zigzagLength - 2]; | ||
secondLastZigzagValue[i] = NaN; | ||
} else { | ||
currentZigzagValue[i] = zigzagValueList.current[zigzagLength - 1]; | ||
lastZigzagValue[i] = zigzagValueList.current[zigzagLength - 2]; | ||
secondLastZigzagValue[i] = zigzagValueList.current[zigzagLength - 3]; | ||
} | ||
} | ||
if (high.length >= 1) { | ||
lastHighPeak[high.length - 1] = NaN; | ||
lastLowPeak[high.length - 1] = NaN; | ||
currentZigzagValue[high.length - 1] = NaN; | ||
lastZigzagValue[high.length - 1] = NaN; | ||
secondLastZigzagValue[high.length - 1] = NaN; | ||
} | ||
}); | ||
|
||
function getLastPeakType() { | ||
return lastHighIdx.current >= lastLowIdx.current ? HIGH_TYPE : LOW_TYPE; | ||
} | ||
|
||
function isPeriodHighest(curIndex: number) { | ||
return high[curIndex] == sourceMax[curIndex]; | ||
} | ||
|
||
function isPeriodLowest(curIndex: number) { | ||
return low[curIndex] == sourceMin[curIndex]; | ||
} | ||
}; |
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
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