feat: 添加布林带宽度计算功能及相关配置项,优化趋势引擎以支持布林带宽度过滤

This commit is contained in:
discountry
2025-09-27 22:29:56 +08:00
parent 151130dfc4
commit d392f54f21
6 changed files with 88 additions and 4 deletions
+14 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { getPosition, getSMA } from "../src/utils/strategy";
import { computeBollingerBandwidth, getPosition, getSMA } from "../src/utils/strategy";
import type { AsterAccountSnapshot, AsterKline } from "../src/exchanges/types";
const mockSnapshot = (positions: Array<{ symbol: string; amt: number; entry: number; pnl: number }> = []): AsterAccountSnapshot => ({
@@ -55,4 +55,17 @@ describe("strategy utils", () => {
const data = mockKlines(Array.from({ length: 30 }, (_, i) => i + 1));
expect(getSMA(data, 30)).toBe(15.5);
});
it("returns null Bollinger bandwidth when data insufficient", () => {
const klines = mockKlines([100, 101, 102]);
expect(computeBollingerBandwidth(klines, 20, 2)).toBeNull();
});
it("computes Bollinger bandwidth ratio", () => {
const closes = [...Array(19).fill(100), 110];
const klines = mockKlines(closes);
const bandwidth = computeBollingerBandwidth(klines, 20, 2);
expect(bandwidth).not.toBeNull();
expect(bandwidth ?? 0).toBeCloseTo(0.0867443, 5);
});
});