Intelligence, Advanced Charting & Trading Platform
feat(ui): add paper-only position risk controls and manual scenario event timeline
davoodmehraban89 committed
Oct 14, 2026 at 22:40 UTC
0550f765a18bd3bdb90282d2a2476a7501f3828f
1 file changed
+167
apps/market-terminal/src/PaperTradingPanel.jsx
new
+167
@@ -0,0 +1,167 @@
1
+import React, { useState } from 'react';
2
+import {
3
+ openPaperPosition, markPaperPosition, closePaperPosition, paperRiskSummary
4
+} from '../../../packages/market-domain/src/paper-position.mjs';
5
+
6
+const format = (x) => Number.isFinite(x) ?
7
+ new Intl.NumberFormat('fa-IR', { maximumFractionDigits: 5 }).format(x) : '—';
8
+const nextTimestamp = (prev) => {
9
+ const previous = prev ? Date.parse(prev) : 0;
10
+ return new Date(Math.max(Date.now(), previous + 1)).toISOString();
11
+};
12
+
13
+export default function PaperTradingPanel({ symbol, onPositionChange }) {
14
+ const [position, setPosition] = useState(null);
15
+ const [side, setSide] = useState('LONG');
16
+ const [entry, setEntry] = useState('');
17
+ const [capital, setCapital] = useState('1000');
18
+ const [stopLoss, setStopLoss] = useState('1.2');
19
+ const [takeProfit, setTakeProfit] = useState('2');
20
+ const [trailing, setTrailing] = useState('1');
21
+ const [useTrailing, setUseTrailing] = useState(true);
22
+ const [commission, setCommission] = useState('0');
23
+ const [slippage, setSlippage] = useState('0');
24
+ const [nextPrice, setNextPrice] = useState('');
25
+ const [error, setError] = useState('');
26
+
27
+ function commit(next) {
28
+ setPosition(next);
29
+ onPositionChange(next);
30
+ setError('');
31
+ }
32
+ function open() {
33
+ try {
34
+ const p = openPaperPosition({
35
+ symbol, side, entryPrice: entry, capital,
36
+ stopLossPercent: stopLoss, takeProfitPercent: takeProfit,
37
+ trailingStopPercent: useTrailing ? trailing : null,
38
+ commissionBps: commission, slippageBps: slippage,
39
+ at: nextTimestamp()
40
+ });
41
+ setNextPrice(String(p.entryPrice));
42
+ commit(p);
43
+ } catch (e) { setError(e.message); }
44
+ }
45
+ function evaluate() {
46
+ try {
47
+ if (!position || position.status !== 'OPEN') return;
48
+ commit(markPaperPosition(position, {
49
+ price: nextPrice, at: nextTimestamp(position.lastAt)
50
+ }));
51
+ } catch (e) { setError(e.message); }
52
+ }
53
+ function exitManual() {
54
+ try {
55
+ if (!position || position.status !== 'OPEN') return;
56
+ commit(closePaperPosition(position, {
57
+ price: nextPrice, at: nextTimestamp(position.lastAt)
58
+ }));
59
+ } catch (e) { setError(e.message); }
60
+ }
61
+ function restart() {
62
+ setPosition(null);
63
+ onPositionChange(null);
64
+ setNextPrice('');
65
+ setError('');
66
+ }
67
+ const report = position ? paperRiskSummary(position) : null;
68
+ const fieldsDisabled = Boolean(position);
69
+ return (
70
+ <section className="paper-trading" aria-label="مدیریت موقعیت آزمایشی بدون معامله واقعی">
71
+ <div className="paper-title">
72
+ <h3>مدیریت معامله آزمایشی</h3>
73
+ <span className="paper-badge">PAPER ONLY · بدون سفارش واقعی</span>
74
+ </div>
75
+ <p className="paper-disclosure">
76
+ این پنل فقط سناریوهای عددی را شبیهسازی میکند؛ هیچ سفارشی به صرافی یا کارگزاری ارسال نمیشود.
77
+ قیمتها را خودت وارد میکنی. نرخ مرجع ECB و داده تاریخی، قیمت قابلاجرای معامله نیستند.
78
+ </p>
79
+ <div className="paper-grid">
80
+ <label>جهت فرضی معامله
81
+ <select value={side} disabled={fieldsDisabled} onChange={e => setSide(e.target.value)}>
82
+ <option value="LONG">خرید / Long</option>
83
+ <option value="SHORT">فروش / Short (شبیهسازی مفهومی)</option>
84
+ </select>
85
+ </label>
86
+ <label>قیمت ورود فرضی ({symbol})
87
+ <input inputMode="decimal" type="number" min="0" step="any" value={entry}
88
+ disabled={fieldsDisabled} onChange={e => setEntry(e.target.value)} placeholder="دستی وارد کنید" />
89
+ </label>
90
+ <label>سرمایه فرضی (واحد ارز مظنه)
91
+ <input inputMode="decimal" type="number" min="0" step="any" value={capital}
92
+ disabled={fieldsDisabled} onChange={e => setCapital(e.target.value)} />
93
+ </label>
94
+ <label>حد ضرر (%)
95
+ <input inputMode="decimal" type="number" min="0" max="99" step="any" value={stopLoss}
96
+ disabled={fieldsDisabled} onChange={e => setStopLoss(e.target.value)} />
97
+ </label>
98
+ <label>حد سود (%)
99
+ <input inputMode="decimal" type="number" min="0" max="99" step="any" value={takeProfit}
100
+ disabled={fieldsDisabled} onChange={e => setTakeProfit(e.target.value)} />
101
+ </label>
102
+ <label className="paper-switch"><input type="checkbox" checked={useTrailing}
103
+ disabled={fieldsDisabled} onChange={e => setUseTrailing(e.target.checked)} />
104
+ حد ضرر متحرک
105
+ </label>
106
+ {useTrailing && <label>فاصله Trailing (%)
107
+ <input inputMode="decimal" type="number" min="0" max="99" step="any" value={trailing}
108
+ disabled={fieldsDisabled} onChange={e => setTrailing(e.target.value)} />
109
+ </label>}
110
+ <label>کارمزد فرضی (bps)
111
+ <input inputMode="decimal" type="number" min="0" max="500" step="any"
112
+ value={commission} disabled={fieldsDisabled} onChange={e => setCommission(e.target.value)} />
113
+ </label>
114
+ <label>لغزش فرضی (bps)
115
+ <input inputMode="decimal" type="number" min="0" max="500" step="any"
116
+ value={slippage} disabled={fieldsDisabled} onChange={e => setSlippage(e.target.value)} />
117
+ </label>
118
+ </div>
119
+ {!position ? (
120
+ <button type="button" className="paper-action" onClick={open}>ساخت موقعیت آزمایشی</button>
121
+ ) : (
122
+ <>
123
+ <div className="paper-metrics" aria-live="polite">
124
+ <div><span>وضعیت</span><strong>{position.status === 'OPEN' ? 'باز (فرضی)' : 'بسته (فرضی)'}</strong></div>
125
+ <div><span>ورود</span><strong>{format(report.entryPrice)}</strong></div>
126
+ <div><span>آخرین سناریو</span><strong>{format(report.markPrice)}</strong></div>
127
+ <div><span>حد ضرر مؤثر</span><strong>{format(report.stopPrice)}</strong></div>
128
+ <div><span>حد سود</span><strong>{format(report.takeProfitPrice)}</strong></div>
129
+ <div><span>سود / زیان فرضی، پس از هزینه</span>
130
+ <strong className={report.netPnL >= 0 ? 'paper-positive' : 'paper-negative'}>
131
+ {format(report.netPnL)} ({format(report.netPnLPct)}٪)
132
+ </strong></div>
133
+ </div>
134
+ {position.status === 'OPEN' && (
135
+ <div className="paper-manual">
136
+ <label>قیمت سناریوی بعدی (دستی)
137
+ <input inputMode="decimal" type="number" min="0" step="any"
138
+ value={nextPrice} onChange={e => setNextPrice(e.target.value)} />
139
+ </label>
140
+ <div className="paper-buttons">
141
+ <button type="button" className="paper-action" onClick={evaluate}>ثبت قیمت و ارزیابی حدود</button>
142
+ <button type="button" className="paper-plain" onClick={exitManual}>بستن فرضی موقعیت</button>
143
+ </div>
144
+ </div>
145
+ )}
146
+ {position.exit && (
147
+ <p className="paper-outcome" role="status">
148
+ موقعیت فرضی بسته شد: {position.exit.reason === 'STOP' ? 'حد ضرر / متحرک' :
149
+ position.exit.reason === 'TAKE_PROFIT' ? 'حد سود' : 'بستن دستی'}.
150
+ قیمت مدلشده خروج: {format(position.exit.fillPrice)}؛ این قیمت تضمین اجرای واقعی نیست.
151
+ </p>
152
+ )}
153
+ <details className="paper-events">
154
+ <summary>تاریخچه رویدادهای سناریو ({position.events.length})</summary>
155
+ <ol>{position.events.map((event, i) => (
156
+ <li key={i}><span>{event.type}</span> — {format(event.price ?? event.observedPrice)}
157
+ <small dir="ltr">{event.at}</small></li>
158
+ ))}</ol>
159
+ </details>
160
+ <button type="button" className="paper-reset" onClick={restart}>سناریوی جدید</button>
161
+ </>
162
+ )}
163
+ {error && <p role="alert" className="paper-error">{error}</p>}
164
+ <p className="paper-footnote">این شبیهسازی پس از بارگذاری مجدد صفحه پاک میشود؛ هنوز ذخیرهسازی حساب، اجرای سفارش، اتصال قیمت قابل معامله و پایش سروری ندارد.</p>
165
+ </section>
166
+ );
167
+}