feat: Chandelier-Trailing-Stop

This commit is contained in:
2026-06-09 20:22:46 +00:00
parent 8a5a6f4eb1
commit b4388c959c
2 changed files with 33 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
import { expect, test } from 'bun:test';
import { updateChandelier } from './chandelier';
test('Stop steigt mit neuem Hoch', () => {
// hh 100 → 110, ATR 2, Mult 3 → Stop 1106 = 104
const r = updateChandelier({ highestHigh: 100, stop: 94 }, 110, 2, 3);
expect(r.highestHigh).toBe(110);
expect(r.stop).toBe(104);
});
test('Stop fällt NIE — auch wenn ATR explodiert', () => {
// hh bleibt 110, ATR springt auf 10 → Kandidat 80, aber Stop bleibt 104
const r = updateChandelier({ highestHigh: 110, stop: 104 }, 105, 10, 3);
expect(r.highestHigh).toBe(110);
expect(r.stop).toBe(104);
});
test('NaN-ATR lässt Stop unverändert', () => {
const r = updateChandelier({ highestHigh: 110, stop: 104 }, 120, NaN, 3);
expect(r.highestHigh).toBe(120);
expect(r.stop).toBe(104);
});

View File

@@ -0,0 +1,11 @@
export interface TrailState {
highestHigh: number;
stop: number;
}
/** Chandelier-Stop: hh mult×ATR, wandert nur aufwärts. Aufruf pro abgeschlossener 4h-Candle. */
export function updateChandelier(state: TrailState, barHigh: number, atrValue: number, mult: number): TrailState {
const highestHigh = Math.max(state.highestHigh, barHigh);
const candidate = Number.isNaN(atrValue) ? -Infinity : highestHigh - mult * atrValue;
return { highestHigh, stop: Math.max(state.stop, candidate) };
}