commit 2 refactor
This commit is contained in:
parent
588f666943
commit
32e43c9243
310 changed files with 33318 additions and 31189 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import { createRequire } from 'node:module';
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
import { IncrementalSMA, TechnicalIndicators } from '@stock-bot/engine';
|
||||
import { BacktestEngine, IncrementalSMA, TechnicalIndicators } from '@stock-bot/engine';
|
||||
|
||||
describe('generated native bindings', () => {
|
||||
it('ESM loads the generated artifact and exposes the documented constructor aliases', () => {
|
||||
|
|
@ -15,4 +15,27 @@ describe('generated native bindings', () => {
|
|||
expect(binding.TechnicalIndicators).toBe(TechnicalIndicators);
|
||||
expect(binding.IncrementalSMA).toBe(IncrementalSMA);
|
||||
});
|
||||
|
||||
it('MACD keeps full-length EMA series aligned without out-of-bounds access', () => {
|
||||
const indicators = new TechnicalIndicators();
|
||||
const prices = Array.from({ length: 80 }, (_, index) => 100 + index);
|
||||
const result = JSON.parse(indicators.calculateMacd(prices, 12, 26, 9)) as {
|
||||
macd: number[]; signal: number[]; histogram: number[];
|
||||
};
|
||||
const fast = indicators.calculateEma(prices, 12);
|
||||
const slow = indicators.calculateEma(prices, 26);
|
||||
expect(result.macd).toHaveLength(prices.length);
|
||||
expect(result.signal).toHaveLength(prices.length);
|
||||
for (let index = 0; index < prices.length; index++) {
|
||||
expect(result.macd[index]).toBeCloseTo(fast[index]! - slow[index]!, 10);
|
||||
expect(result.histogram[index]).toBeCloseTo(result.macd[index]! - result.signal[index]!, 10);
|
||||
}
|
||||
});
|
||||
|
||||
it('invalid SMA periods throw before entering the native event loop', () => {
|
||||
const engine = new BacktestEngine({ name: 'validation', symbols: ['TEST'], startDate: '2023-01-01T00:00:00Z', endDate: '2023-02-01T00:00:00Z', initialCapital: 100000, commission: 0, slippage: 0, dataFrequency: '1d' });
|
||||
expect(() => engine.addTypescriptStrategy('unsupported', 'unsupported', {}, () => '{}')).toThrow('TypeScript strategy callbacks are not supported');
|
||||
expect(() => engine.addNativeStrategy('sma_crossover', 'invalid', 'invalid', { fastPeriod: 0, slowPeriod: 20 })).toThrow('SMA periods must satisfy');
|
||||
expect(() => engine.addNativeStrategy('sma_crossover', 'invalid', 'invalid', { fastPeriod: 20, slowPeriod: 10 })).toThrow('SMA periods must satisfy');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue