82 lines
2.3 KiB
TypeScript
82 lines
2.3 KiB
TypeScript
|
|
import { describe, expect, it } from 'vitest';
|
|||
|
|
|
|||
|
|
import type { PredictionData } from './types';
|
|||
|
|
import { entryText, isRangeEntry, predictionMath, riskRewardOf } from './utils';
|
|||
|
|
|
|||
|
|
const exact: PredictionData = {
|
|||
|
|
symbol: 'XAUUSD',
|
|||
|
|
direction: 'long',
|
|||
|
|
entryPrice: 100,
|
|||
|
|
targetPrices: [120],
|
|||
|
|
stopLoss: 90,
|
|||
|
|
horizon: '1w',
|
|||
|
|
};
|
|||
|
|
const range: PredictionData = {
|
|||
|
|
...exact,
|
|||
|
|
entryPrice: 68000,
|
|||
|
|
entryLow: 67500,
|
|||
|
|
entryHigh: 68500,
|
|||
|
|
targetPrices: [70000],
|
|||
|
|
stopLoss: 67000,
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
describe('riskRewardOf', () => {
|
|||
|
|
it('has one answer for an exact entry', () => {
|
|||
|
|
expect(riskRewardOf(exact)).toEqual({ worst: 2, best: 2 });
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
it('works a long range out at both ends, the lower ratio first', () => {
|
|||
|
|
// From the top: 1500 of reward for 1500 of risk. From the bottom: 2500 for 500.
|
|||
|
|
expect(riskRewardOf(range)).toEqual({ worst: 1, best: 5 });
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
it('mirrors for a short', () => {
|
|||
|
|
const short: PredictionData = {
|
|||
|
|
...exact,
|
|||
|
|
direction: 'short',
|
|||
|
|
entryPrice: 100,
|
|||
|
|
entryLow: 98,
|
|||
|
|
entryHigh: 102,
|
|||
|
|
targetPrices: [90],
|
|||
|
|
stopLoss: 110,
|
|||
|
|
};
|
|||
|
|
expect(riskRewardOf(short)).toEqual({ worst: 8 / 12, best: 12 / 8 });
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
it('reads the first target, not the final one', () => {
|
|||
|
|
expect(riskRewardOf({ ...exact, targetPrices: [110, 200] })).toEqual({
|
|||
|
|
worst: 1,
|
|||
|
|
best: 1,
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
it.each([
|
|||
|
|
['no stop', { stopLoss: null }],
|
|||
|
|
['no target', { targetPrices: [] }],
|
|||
|
|
['a stop on the wrong side', { stopLoss: 105 }],
|
|||
|
|
['a stop on the entry', { stopLoss: 100 }],
|
|||
|
|
['a target on the wrong side', { targetPrices: [95] }],
|
|||
|
|
])('is null with %s', (_name, over) => {
|
|||
|
|
expect(riskRewardOf({ ...exact, ...over })).toBeNull();
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
describe('the entry', () => {
|
|||
|
|
it('knows a range only when both ends are there', () => {
|
|||
|
|
expect(isRangeEntry(range)).toBe(true);
|
|||
|
|
expect(isRangeEntry(exact)).toBe(false);
|
|||
|
|
expect(isRangeEntry({ entryLow: 1, entryHigh: null })).toBe(false);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
it('writes both ends of a range, low first', () => {
|
|||
|
|
expect(entryText(range)).toBe('67٬500 – 68٬500');
|
|||
|
|
expect(entryText(exact)).toBe('100');
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
it('measures every distance from the middle of a range', () => {
|
|||
|
|
const math = predictionMath(range);
|
|||
|
|
expect(math.targetsFav[0]).toBeCloseTo((2000 / 68000) * 100);
|
|||
|
|
expect(math.risk).toBeCloseTo((-1000 / 68000) * 100);
|
|||
|
|
});
|
|||
|
|
});
|