Intelligence, Advanced Charting & Trading Platform
test(paper): cover bracket exits trailing reversals fees timestamps and fail-closed input
davoodmehraban89 committed
Oct 14, 2026 at 22:39 UTC
1b14f17678d1b8f4bdb6c58342c1111f73a64f98
1 file changed
+106
packages/market-domain/test/paper-position.test.mjs
new
+106
@@ -0,0 +1,106 @@
1
+import test from 'node:test';
2
+import assert from 'node:assert/strict';
3
+import {
4
+ openPaperPosition, markPaperPosition, closePaperPosition, paperRiskSummary
5
+} from '../src/paper-position.mjs';
6
+
7
+const at = (s) => '2026-10-14T10:00:' + String(s).padStart(2, '0') + 'Z';
8
+const config = {symbol:'XAU_USD',side:'LONG',entryPrice:100,capital:1000,
9
+ stopLossPercent:1.2,takeProfitPercent:2,trailingStopPercent:null,at:at(0)};
10
+const open = (overrides={}) => openPaperPosition({...config,...overrides});
11
+const step = (p,price,second) => markPaperPosition(p,{price,at:at(second)});
12
+
13
+test('opens a quote-unit paper-only long with 1.2% SL and 2% TP',()=>{
14
+ const p=open();
15
+ assert.equal(p.status,'OPEN');
16
+ assert.equal(p.stopPrice,98.8);
17
+ assert.equal(p.takeProfitPrice,102);
18
+ assert.equal(p.quantity,10);
19
+ assert.equal(p.events[0].source,'MANUAL_SCENARIO');
20
+ assert.equal(paperRiskSummary(p).netPnL,0);
21
+});
22
+test('long stop closes once and computes observed price, not guaranteed threshold',()=>{
23
+ const p=step(open(),98,1);
24
+ assert.equal(p.status,'CLOSED');assert.equal(p.exit.reason,'STOP');
25
+ assert.equal(p.exit.observedPrice,98);assert.equal(p.realizedPnL,-20);
26
+ assert.throws(()=>step(p,99,2),/OPEN/);
27
+});
28
+test('long take-profit closes and stop is no longer active',()=>{
29
+ const p=step(open(),102,1);
30
+ assert.equal(p.exit.reason,'TAKE_PROFIT');
31
+ assert.equal(p.realizedPnL,20);
32
+ assert.equal(p.events.filter(e=>e.type==='CLOSE').length,1);
33
+});
34
+test('trailing stop ratchets upward and protects profit after favorable move',()=>{
35
+ let p=open({trailingStopPercent:1});
36
+ p=step(p,101.5,1);
37
+ assert.equal(p.stopPrice,100.485);
38
+ p=step(p,102,2);
39
+ assert.equal(p.status,'CLOSED');
40
+ assert.equal(p.exit.reason,'TAKE_PROFIT');
41
+});
42
+test('trailing stop locks in gains before static take profit',()=>{
43
+ let p=open({takeProfitPercent:10,trailingStopPercent:1});
44
+ p=step(p,104,1);assert.equal(p.stopPrice,102.96);
45
+ p=step(p,103,2);assert.equal(p.status,'OPEN');assert.equal(p.stopPrice,102.96);
46
+ p=step(p,102.9,3);assert.equal(p.exit.reason,'STOP');
47
+ assert.ok(p.realizedPnL>0);
48
+});
49
+test('short stop and take profit directions are reversed',()=>{
50
+ const short=open({side:'SHORT'});
51
+ assert.equal(short.stopPrice,101.2);
52
+ assert.equal(short.takeProfitPrice,98);
53
+ assert.equal(step(short,101.5,1).exit.reason,'STOP');
54
+ assert.equal(step(short,97.5,1).exit.reason,'TAKE_PROFIT');
55
+});
56
+test('short trailing stop follows favorable lower mark',()=>{
57
+ let p=open({side:'SHORT',takeProfitPercent:10,trailingStopPercent:1});
58
+ p=step(p,95,1);assert.equal(p.stopPrice,95.95);
59
+ p=step(p,96,2);assert.equal(p.exit.reason,'STOP');
60
+ assert.ok(p.realizedPnL>0);
61
+});
62
+test('commission and conservative slippage reduce hypothetical P&L',()=>{
63
+ const p=step(open({commissionBps:10,slippageBps:20}),102,1);
64
+ assert.equal(p.status,'CLOSED');
65
+ assert.equal(p.exit.fillPrice,101.796);
66
+ assert.ok(p.realizedPnL<20);
67
+ assert.ok(p.exit.fees>0);
68
+});
69
+test('negative, NaN, unsupported sides and excessive risk are rejected',()=>{
70
+ for(const capital of [-1,0,NaN,Infinity])assert.throws(()=>open({capital}),/positive/);
71
+ assert.throws(()=>open({side:'BUY'}),/side/);
72
+ assert.throws(()=>open({stopLossPercent:100}),/below/);
73
+ assert.throws(()=>open({commissionBps:501}),/basis points/);
74
+ assert.throws(()=>open({symbol:'EUR/USD;DROP'}),/canonical symbol/);
75
+});
76
+test('out-of-order or duplicate scenario timestamps are rejected',()=>{
77
+ const p=step(open(),100.5,1);
78
+ assert.throws(()=>step(p,101,1),/strictly increase/);
79
+ assert.throws(()=>step(p,101,0),/strictly increase/);
80
+});
81
+test('manual close requires an open position and a later timestamp',()=>{
82
+ const initial=open();
83
+ assert.throws(()=>closePaperPosition(initial,{price:100,at:at(0)}),/increase/);
84
+ const p=closePaperPosition(initial,{price:101,at:at(1)});
85
+ assert.equal(p.exit.reason,'MANUAL');
86
+ assert.equal(p.exit.observedPrice,101);
87
+ assert.throws(()=>closePaperPosition(p,{price:100,at:at(2)}),/OPEN/);
88
+});
89
+test('a no-trailing position does not adjust its fixed stop',()=>{
90
+ let p=open();
91
+ p=step(p,101,1);
92
+ assert.equal(p.stopPrice,98.8);
93
+});
94
+test('summary reports signed percentage return and audit event count',()=>{
95
+ let p=open({takeProfitPercent:20});
96
+ p=step(p,99,1);
97
+ const s=paperRiskSummary(p);
98
+ assert.equal(s.netPnLPct,-10);
99
+ assert.equal(s.eventCount,2);
100
+ assert.equal(s.status,'OPEN');
101
+});
102
+test('no network or brokerage dependencies exist in the pure simulation module',()=>{
103
+ const p=open();
104
+ assert.equal(p.kind,'PAPER_POSITION');
105
+ assert.equal(p.events[0].source,'MANUAL_SCENARIO');
106
+});