62cd868f79
以损定仓/固定张数分栏下单、限价市价、持仓仅读柜台;DEPLOY 补充 SimNow 与 vnpy 安装说明。 Co-authored-by: Cursor <cursoragent@cursor.com>
334 lines
15 KiB
JavaScript
334 lines
15 KiB
JavaScript
(function () {
|
|
var sizingMode = window.TRADE_SIZING_MODE || 'risk';
|
|
var list = document.getElementById('position-live-list');
|
|
var recommendList = document.getElementById('recommend-list');
|
|
var symInput = document.getElementById('trade-symbol');
|
|
var dirSelect = document.getElementById('trade-direction');
|
|
var lotsInput = document.getElementById('trade-lots');
|
|
var lotsCalc = document.getElementById('trade-lots-calc');
|
|
var priceInput = document.getElementById('trade-price');
|
|
var slInput = document.getElementById('trade-sl');
|
|
var tpInput = document.getElementById('trade-tp');
|
|
var marketHint = document.getElementById('market-hint');
|
|
var metricsHint = document.getElementById('trade-metrics-hint');
|
|
var pollTimer = null;
|
|
var recommendSource = null;
|
|
var quoteTimer = null;
|
|
var lastQuotePrice = null;
|
|
var priceType = 'limit';
|
|
|
|
function runWhenReady(fn) {
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', fn);
|
|
} else {
|
|
fn();
|
|
}
|
|
}
|
|
|
|
function fmtNum(v, digits) {
|
|
if (v === null || v === undefined) return '--';
|
|
return Number(v).toFixed(digits === undefined ? 2 : digits);
|
|
}
|
|
|
|
function selectedSymbol() {
|
|
return (symInput && symInput.value || '').trim();
|
|
}
|
|
|
|
function isRiskMode() {
|
|
return sizingMode === 'risk';
|
|
}
|
|
|
|
function effectiveLots() {
|
|
if (isRiskMode()) {
|
|
var v = parseInt(lotsCalc && lotsCalc.value, 10);
|
|
return v > 0 ? v : 0;
|
|
}
|
|
return parseInt(lotsInput && lotsInput.value, 10) || 1;
|
|
}
|
|
|
|
function entryPrice() {
|
|
if (priceType === 'market') return lastQuotePrice;
|
|
return parseFloat(priceInput && priceInput.value) || 0;
|
|
}
|
|
|
|
function setPriceType(type) {
|
|
priceType = type === 'market' ? 'market' : 'limit';
|
|
document.querySelectorAll('.price-tab').forEach(function (btn) {
|
|
btn.classList.toggle('active', btn.getAttribute('data-type') === priceType);
|
|
});
|
|
if (priceInput) {
|
|
priceInput.disabled = priceType === 'market';
|
|
if (priceType === 'market' && lastQuotePrice) priceInput.value = lastQuotePrice;
|
|
}
|
|
if (marketHint) marketHint.hidden = priceType !== 'market';
|
|
}
|
|
|
|
function refreshQuote() {
|
|
var sym = selectedSymbol();
|
|
var lots = isRiskMode() ? (effectiveLots() || 1) : (lotsInput ? lotsInput.value : '1');
|
|
if (!sym) return;
|
|
fetch('/api/trade/quote?symbol=' + encodeURIComponent(sym) + '&lots=' + encodeURIComponent(lots))
|
|
.then(function (r) { return r.json(); })
|
|
.then(function (data) {
|
|
if (!data.ok) return;
|
|
lastQuotePrice = data.price;
|
|
if (priceType === 'market' && priceInput && data.price) {
|
|
priceInput.value = data.price;
|
|
} else if (priceInput && !priceInput.dataset.manual && data.price) {
|
|
priceInput.value = data.price;
|
|
}
|
|
if (metricsHint && data.metrics) {
|
|
var m = data.metrics;
|
|
metricsHint.innerHTML =
|
|
'<strong>' + (data.name || sym) + '</strong> 精度 ' + m.price_precision +
|
|
' 位 · 每跳 <strong class="text-accent">' + m.tick_value_total + '</strong> 元(' + lots + ' 手)';
|
|
}
|
|
}).catch(function () {});
|
|
}
|
|
|
|
function scheduleQuote() {
|
|
clearTimeout(quoteTimer);
|
|
quoteTimer = setTimeout(refreshQuote, 400);
|
|
}
|
|
|
|
function calcLotsPreview() {
|
|
var sym = selectedSymbol();
|
|
var entry = entryPrice() || parseFloat(priceInput && priceInput.value) || 0;
|
|
var sl = parseFloat(slInput && slInput.value) || 0;
|
|
if (!sym || !entry || !sl) {
|
|
alert('请填写品种、入场价与止损');
|
|
return;
|
|
}
|
|
fetch('/api/trade/preview', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
symbol: sym,
|
|
direction: dirSelect ? dirSelect.value : 'long',
|
|
entry: entry,
|
|
price: entry,
|
|
stop_loss: sl,
|
|
take_profit: parseFloat(tpInput && tpInput.value) || 0
|
|
})
|
|
}).then(function (r) { return r.json(); }).then(function (data) {
|
|
if (!data.ok) { alert(data.error || '计算失败'); return; }
|
|
if (lotsCalc) lotsCalc.value = data.lots;
|
|
scheduleQuote();
|
|
});
|
|
}
|
|
|
|
function postOrder(offset) {
|
|
var sym = selectedSymbol();
|
|
if (!sym) { alert('请选择品种'); return; }
|
|
var direction = dirSelect ? dirSelect.value : 'long';
|
|
var price = entryPrice();
|
|
if (!price || price <= 0) {
|
|
alert('无法获取有效价格,请先填写或刷新行情');
|
|
return;
|
|
}
|
|
var lots = effectiveLots();
|
|
if (offset === 'open') {
|
|
if (isRiskMode() && lots <= 0) {
|
|
alert('请先点击「计算」得到手数');
|
|
return;
|
|
}
|
|
if (!isRiskMode() && lots <= 0) {
|
|
alert('请填写手数');
|
|
return;
|
|
}
|
|
} else {
|
|
lots = parseInt(lotsInput && lotsInput.value, 10) || 1;
|
|
}
|
|
var body = {
|
|
symbol: sym,
|
|
offset: offset,
|
|
direction: direction,
|
|
lots: lots,
|
|
price: price,
|
|
order_type: priceType,
|
|
stop_loss: slInput && slInput.value ? parseFloat(slInput.value) : null,
|
|
take_profit: tpInput && tpInput.value ? parseFloat(tpInput.value) : null
|
|
};
|
|
fetch('/api/trade/order', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(body)
|
|
}).then(function (r) { return r.json(); }).then(function (data) {
|
|
if (!data.ok) { alert(data.error || '下单失败'); return; }
|
|
alert((offset === 'open' ? '开仓' : '平仓') + '已提交 ' + (data.lots || lots) + ' 手');
|
|
pollPositions();
|
|
refreshQuote();
|
|
});
|
|
}
|
|
|
|
function buildPosCard(row) {
|
|
var pnlClass = row.float_pnl > 0 ? 'pnl-pos' : (row.float_pnl < 0 ? 'pnl-neg' : '');
|
|
var pnlText = row.float_pnl != null ? ((row.float_pnl >= 0 ? '+' : '') + fmtNum(row.float_pnl) + ' 元') : '--';
|
|
var dirBadge = row.direction_label || (row.direction === 'long' ? '做多' : '做空');
|
|
var closeBtn = row.can_close ?
|
|
'<button type="button" class="btn-del pos-del" data-close=\'' + JSON.stringify({
|
|
source: row.source, symbol_code: row.symbol_code, direction: row.direction,
|
|
lots: row.lots, mark_price: row.mark_price, monitor_id: row.monitor_id || null
|
|
}) + '\'>平仓</button>' : '';
|
|
return (
|
|
'<div class="pos-card">' +
|
|
'<div class="pos-card-head"><div><div class="title">' + row.symbol + ' <span class="badge dir">' + dirBadge + '</span></div>' +
|
|
'<div class="text-muted" style="font-size:.72rem">' + (row.symbol_code || '') + '</div></div>' + closeBtn + '</div>' +
|
|
'<div class="pos-card-meta">来源 <strong>' + (row.source_label || 'CTP') + '</strong> · 柜台浮盈</div>' +
|
|
'<div class="pos-metrics">' +
|
|
'<div class="cell"><label>持仓均价</label><div>' + fmtNum(row.entry_price) + '</div></div>' +
|
|
'<div class="cell"><label>止损</label><div>' + (row.stop_loss != null ? fmtNum(row.stop_loss) : '--') + '</div></div>' +
|
|
'<div class="cell"><label>止盈</label><div>' + (row.take_profit != null ? fmtNum(row.take_profit) : '--') + '</div></div>' +
|
|
'<div class="cell ' + pnlClass + '"><label>浮盈亏</label><div>' + pnlText + '</div></div>' +
|
|
'</div><div class="pos-footer"><span>' + row.lots + ' 手</span></div></div>'
|
|
);
|
|
}
|
|
|
|
function closePosition(payload) {
|
|
function doClose(price) {
|
|
if (!price || price <= 0) { alert('无法获取现价'); return; }
|
|
if (!confirm('确认平仓 ' + payload.lots + ' 手?')) return;
|
|
fetch('/api/trading/close', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(Object.assign({}, payload, { price: price }))
|
|
}).then(function (r) { return r.json(); }).then(function (d) {
|
|
if (!d.ok) { alert(d.error || '平仓失败'); return; }
|
|
pollPositions();
|
|
});
|
|
}
|
|
if (payload.mark_price > 0) {
|
|
doClose(payload.mark_price);
|
|
return;
|
|
}
|
|
fetch('/api/trade/quote?symbol=' + encodeURIComponent(payload.symbol_code) + '&lots=' + payload.lots)
|
|
.then(function (r) { return r.json(); })
|
|
.then(function (d) { doClose(d.price); });
|
|
}
|
|
|
|
function pollPositions() {
|
|
if (!list) return;
|
|
fetch('/api/trading/live')
|
|
.then(function (r) {
|
|
if (!r.ok) throw new Error('HTTP ' + r.status);
|
|
return r.json();
|
|
})
|
|
.then(function (data) {
|
|
var cap = document.getElementById('cap-display');
|
|
if (cap && data.capital != null) cap.textContent = Number(data.capital).toFixed(2);
|
|
var ctpBadge = document.getElementById('ctp-badge');
|
|
if (ctpBadge && data.ctp_status) {
|
|
ctpBadge.textContent = data.ctp_status.connected ? 'CTP 已连接' : 'CTP 未连接';
|
|
ctpBadge.className = 'badge ' + (data.ctp_status.connected ? 'profit' : 'planned');
|
|
}
|
|
var rows = data.rows || [];
|
|
if (!data.ctp_status || !data.ctp_status.connected) {
|
|
list.innerHTML = '<div class="empty-hint">请先连接 CTP,持仓将显示柜台实际数据。</div>';
|
|
return;
|
|
}
|
|
if (!rows.length) {
|
|
list.innerHTML = '<div class="empty-hint">柜台暂无持仓。</div>';
|
|
return;
|
|
}
|
|
list.innerHTML = rows.map(buildPosCard).join('');
|
|
list.querySelectorAll('[data-close]').forEach(function (btn) {
|
|
btn.addEventListener('click', function () {
|
|
closePosition(JSON.parse(btn.getAttribute('data-close')));
|
|
});
|
|
});
|
|
})
|
|
.catch(function () {
|
|
if (list.innerHTML.indexOf('pos-card') < 0) {
|
|
list.innerHTML = '<div class="empty-hint text-loss">持仓加载失败</div>';
|
|
}
|
|
});
|
|
}
|
|
|
|
function renderRecommendations(data) {
|
|
if (!recommendList || !data) return;
|
|
var recCap = document.getElementById('rec-capital');
|
|
if (recCap && data.capital != null) recCap.textContent = Number(data.capital).toFixed(2);
|
|
var rows = data.rows || [];
|
|
if (!rows.length) {
|
|
recommendList.innerHTML = '<tr><td colspan="6" class="empty-hint">当前资金下暂无推荐品种</td></tr>';
|
|
return;
|
|
}
|
|
recommendList.innerHTML = rows.map(function (r) {
|
|
return (
|
|
'<tr class="rec-' + (r.status || '') + '">' +
|
|
'<td><strong>' + (r.name || '') + '</strong> <span class="text-muted">' + (r.ths || '') + '</span></td>' +
|
|
'<td>' + (r.exchange || '') + '</td>' +
|
|
'<td>' + (r.price != null ? r.price : '—') + '</td>' +
|
|
'<td>' + (r.margin_one_lot != null ? r.margin_one_lot : '—') + '</td>' +
|
|
'<td>' + (r.min_capital_one_lot != null ? r.min_capital_one_lot : '—') + '</td>' +
|
|
'<td><span class="badge ' + (r.status === 'ok' ? 'profit' : 'planned') + '">' + (r.status_label || '') + '</span></td>' +
|
|
'</tr>'
|
|
);
|
|
}).join('');
|
|
}
|
|
|
|
function connectRecommendStream() {
|
|
if (recommendSource) { recommendSource.close(); recommendSource = null; }
|
|
recommendSource = new EventSource('/api/recommend/stream');
|
|
recommendSource.addEventListener('recommend', function (ev) {
|
|
try { renderRecommendations(JSON.parse(ev.data)); } catch (e) { /* ignore */ }
|
|
});
|
|
recommendSource.onerror = function () {
|
|
if (recommendSource) { recommendSource.close(); recommendSource = null; }
|
|
setTimeout(connectRecommendStream, 5000);
|
|
};
|
|
}
|
|
|
|
document.querySelectorAll('.price-tab').forEach(function (btn) {
|
|
btn.addEventListener('click', function () {
|
|
setPriceType(btn.getAttribute('data-type'));
|
|
scheduleQuote();
|
|
});
|
|
});
|
|
|
|
if (symInput) symInput.addEventListener('input', scheduleQuote);
|
|
if (lotsInput) lotsInput.addEventListener('input', scheduleQuote);
|
|
if (slInput) slInput.addEventListener('input', function () {
|
|
if (isRiskMode() && lotsCalc) lotsCalc.value = '';
|
|
});
|
|
if (priceInput) {
|
|
priceInput.addEventListener('input', function () {
|
|
if (priceType === 'limit') priceInput.dataset.manual = '1';
|
|
});
|
|
}
|
|
|
|
var btnCalc = document.getElementById('btn-calc-lots');
|
|
if (btnCalc) btnCalc.addEventListener('click', calcLotsPreview);
|
|
|
|
var btnOpen = document.getElementById('btn-open');
|
|
var btnClose = document.getElementById('btn-close-pos');
|
|
if (btnOpen) btnOpen.addEventListener('click', function () { postOrder('open'); });
|
|
if (btnClose) btnClose.addEventListener('click', function () { postOrder('close'); });
|
|
|
|
var btnConnect = document.getElementById('btn-ctp-connect');
|
|
if (btnConnect) {
|
|
btnConnect.addEventListener('click', function () {
|
|
btnConnect.disabled = true;
|
|
btnConnect.textContent = '连接中…';
|
|
fetch('/api/ctp/connect', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: '{}' })
|
|
.then(function (r) { return r.json(); })
|
|
.then(function (d) {
|
|
if (!d.ok) { alert(d.error || '连接失败'); return; }
|
|
location.reload();
|
|
})
|
|
.finally(function () {
|
|
btnConnect.disabled = false;
|
|
btnConnect.textContent = '连接 CTP';
|
|
});
|
|
});
|
|
}
|
|
|
|
runWhenReady(function () {
|
|
setPriceType('limit');
|
|
pollPositions();
|
|
connectRecommendStream();
|
|
pollTimer = setInterval(pollPositions, 3000);
|
|
scheduleQuote();
|
|
});
|
|
})();
|