Intelligence, Advanced Charting & Trading Platform
feature/paper-trading-risk-controls-2026-10-14
diagnostics/chartnova-blank-screen-2026-10-15
diagnostics/chartnova-stage2-v27-preview-2026-10-15
docs/chartnova-stage1-reconcile-manifest-2026-10-15
feature/chartnova-ios-widget
feature/chartnova-light-catalog-lazy-series-2026-10-15
feature/chartnova-market-terminal
feature/chartnova-multimarket-chart-ws-foundation
feature/chartnova-neon-direct-history
feature/chartnova-neon-history
feature/paper-trading-risk-controls-2026-10-14
fix/alpha-vantage-live-payload
fix/chartnova-progressive-loading-2026-10-15
fix/live-proof-runner-cwd-2026-10-14
foundation/phase-0
foundation/postgres-core
main
ops/chartnova-emergency-rollback-2026-10-15
phase-1/candle-builder
phase-1/data-proof-artifact
phase-1/data-proof-metrics
phase-1/data-proof-report
phase-1/external-gates-state
phase-1/gold-api-fallback
phase-1/ingestion-pipeline
phase-1/live-proof-runner
phase-1/market-calendar
phase-1/market-domain
phase-1/neon-live-proof
phase-1/persistence-contract
phase-1/postgres-ci
phase-1/provider-adapters
phase-1/provider-recovery
phase-1/quality-propagation
phase-1/spike-detection
phase-1/xau-provider-adapter
risk/chartnova-alert-runtime-atomic-2026-10-22
risk/chartnova-atomic-outbox-2026-10-22
risk/chartnova-seven-track-safety-2026-10-21
safety/p0-triage-2026-10-13
| 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,-1); |
| 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 | }); |