Skip to content

Commit

Permalink
refactor: move indicators to libs (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
zccz14 authored Oct 28, 2023
1 parent f308061 commit 5214cf9
Show file tree
Hide file tree
Showing 23 changed files with 143 additions and 22 deletions.
18 changes: 0 additions & 18 deletions @hooks/index.ts

This file was deleted.

19 changes: 18 additions & 1 deletion @libs/index.ts
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";
2 changes: 1 addition & 1 deletion @hooks/indicators/ATR.ts → @libs/indicators/ATR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ export const useATR = (
ATR.tags.display = "line";
ATR.tags.chart = "new";
}, []);
return ATR;
return { ATR, TR };
};
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.
122 changes: 122 additions & 0 deletions @libs/indicators/ZIGZAG.ts
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];
}
};
2 changes: 1 addition & 1 deletion @models/super-trend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useATR, useSeriesMap } from "@libs";
export default () => {
const { product_id, close, open, high, low } = useParamOHLC("SomeKey");
const A = useParamNumber("A", 2);
const atr = useATR(high, low, close, 14);
const { ATR: atr } = useATR(high, low, close, 14);

const idx = close.length - 2;
const pL = useSinglePosition(product_id, PositionVariant.LONG);
Expand Down
2 changes: 1 addition & 1 deletion @models/turtle-long.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default () => {
const LL = useMIN(low, N);

const pL = useSinglePosition(product_id, PositionVariant.LONG);
const atr = useATR(high, low, close, 14);
const { ATR: atr } = useATR(high, low, close, 14);
const price_break = useRef(0);
useEffect(() => {
const idx = close.length - 2;
Expand Down

0 comments on commit 5214cf9

Please sign in to comment.