Skip to content

Commit

Permalink
feat: add indicators (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
zccz14 authored Oct 28, 2023
1 parent 5214cf9 commit 7ef953d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
3 changes: 3 additions & 0 deletions @libs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from "./indicators/KDJ";
export * from "./indicators/MACD";
export * from "./indicators/MAX";
export * from "./indicators/MIN";
export * from "./indicators/NVI";
export * from "./indicators/PROD";
export * from "./indicators/RANGE";
export * from "./indicators/RANK";
Expand All @@ -17,4 +18,6 @@ export * from "./indicators/SAR";
export * from "./indicators/SMA";
export * from "./indicators/STD";
export * from "./indicators/SUM";
export * from "./indicators/TD";
export * from "./indicators/WMA";
export * from "./utils";
24 changes: 24 additions & 0 deletions @libs/indicators/NVI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useEMA, useSeriesMap } from "@libs";

export const useNVI = (volume: Series, close: Series, period: number = 72) => {
const NVI = useSeriesMap(
`NVI`,
close,
{ display: "line", chart: "new" },
(i, s) =>
i === 0
? 100
: s[i - 1] *
(volume[i] < volume[i - 1]
? Math.abs(
// abs 是对负数价格的特殊处理
close[i] / close[i - 1]
)
: 1)
);
const MA_NVI = useEMA(NVI, period);
useEffect(() => {
MA_NVI.name = "MA_NVI";
}, []);
return { NVI: NVI, MA_NVI: MA_NVI };
};
21 changes: 21 additions & 0 deletions @libs/indicators/TD.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// TD 序列指标
export const useTD = (source: Series) => {
const TD = useSeries("Output", source, {
display: "hist",
chart: "new",
});

useEffect(() => {
const i = source.length - 1;
if (i < 0) return;
TD[i] = 0;
if (source[i] > source[i - 4]) {
TD[i] = Math.max(0, TD[i - 1] ?? 0) + 1;
}
if (source[i] < source[i - 4]) {
TD[i] = Math.min(0, TD[i - 1] ?? 0) - 1;
}
});

return { TD };
};
22 changes: 22 additions & 0 deletions @libs/indicators/WMA.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useSUM, useSeriesMap } from "@libs";

/**
* 加权移动平均线 WMA
*/
export const useWMA = (source: Series, weight: Series, period: number) => {
const prod = useSeriesMap("Prod", source, {}, (i) => source[i] * weight[i]);

const sumOfProd = useSUM(prod, period);
const sumOfWeight = useSUM(weight, period);

const WMA = useSeriesMap(
`WMA(${period})`,
source,
{
display: "line",
},
(i) => sumOfProd[i] / sumOfWeight[i]
);

return { WMA };
};

0 comments on commit 7ef953d

Please sign in to comment.