주식 매매 프로그램 연동 및 페이지 개발 구체화
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import React, { useEffect, useMemo, useState } from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { createTradeOrder, getTradeBalance } from '../../api';
|
||||
import { getTradeBalance, requestAutoTrade } from '../../api';
|
||||
import './Stock.css';
|
||||
|
||||
const formatNumber = (value) => {
|
||||
@@ -10,19 +10,21 @@ const formatNumber = (value) => {
|
||||
return new Intl.NumberFormat('ko-KR').format(numeric);
|
||||
};
|
||||
|
||||
const formatPercent = (value) => {
|
||||
if (value === null || value === undefined || value === '') return '-';
|
||||
if (typeof value === 'string' && value.includes('%')) return value;
|
||||
const numeric = Number(value);
|
||||
if (Number.isNaN(numeric)) return value;
|
||||
return `${numeric.toFixed(2)}%`;
|
||||
};
|
||||
|
||||
const StockTrade = () => {
|
||||
const [balance, setBalance] = useState(null);
|
||||
const [balanceLoading, setBalanceLoading] = useState(false);
|
||||
const [balanceError, setBalanceError] = useState('');
|
||||
const [orderForm, setOrderForm] = useState({
|
||||
code: '',
|
||||
qty: 1,
|
||||
price: 0,
|
||||
type: 'buy',
|
||||
});
|
||||
const [orderLoading, setOrderLoading] = useState(false);
|
||||
const [orderMessage, setOrderMessage] = useState('');
|
||||
const [error, setError] = useState('');
|
||||
const [autoLoading, setAutoLoading] = useState(false);
|
||||
const [autoError, setAutoError] = useState('');
|
||||
const [autoResult, setAutoResult] = useState(null);
|
||||
|
||||
const loadBalance = async () => {
|
||||
setBalanceLoading(true);
|
||||
@@ -37,25 +39,20 @@ const StockTrade = () => {
|
||||
}
|
||||
};
|
||||
|
||||
const onOrderSubmit = async (event) => {
|
||||
event.preventDefault();
|
||||
setOrderLoading(true);
|
||||
setOrderMessage('');
|
||||
setError('');
|
||||
const runAutoTrade = async () => {
|
||||
setAutoLoading(true);
|
||||
setAutoError('');
|
||||
setAutoResult(null);
|
||||
try {
|
||||
const payload = {
|
||||
code: orderForm.code.trim(),
|
||||
qty: Number(orderForm.qty),
|
||||
price: Number(orderForm.price),
|
||||
type: orderForm.type,
|
||||
};
|
||||
const result = await createTradeOrder(payload);
|
||||
setOrderMessage(result?.message ?? '주문이 접수되었습니다.');
|
||||
await loadBalance();
|
||||
const result = await requestAutoTrade();
|
||||
setAutoResult(result);
|
||||
if (result?.status === 'success') {
|
||||
await loadBalance();
|
||||
}
|
||||
} catch (err) {
|
||||
setError(err?.message ?? String(err));
|
||||
setAutoError(err?.message ?? String(err));
|
||||
} finally {
|
||||
setOrderLoading(false);
|
||||
setAutoLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -63,244 +60,233 @@ const StockTrade = () => {
|
||||
loadBalance();
|
||||
}, []);
|
||||
|
||||
const holdings = useMemo(
|
||||
() => (Array.isArray(balance?.holdings) ? balance.holdings : []),
|
||||
[balance]
|
||||
);
|
||||
const summary = balance?.summary ?? {};
|
||||
const autoStatus = autoResult?.status ?? '';
|
||||
const decision = autoResult?.decision ?? null;
|
||||
const tradeResult = autoResult?.trade_result ?? null;
|
||||
|
||||
return (
|
||||
<div className="stock">
|
||||
<header className="stock-header">
|
||||
<div>
|
||||
<p className="stock-kicker">Trade Desk</p>
|
||||
<h1>Stock Trade</h1>
|
||||
<p className="stock-kicker">거래 데스크</p>
|
||||
<h1>주식 거래</h1>
|
||||
<p className="stock-sub">
|
||||
잔고 확인과 매수/매도 주문을 한 화면에서 집중적으로 처리합니다.
|
||||
연결된 계좌 잔고를 확인하고 AI 자동 매매 판단을
|
||||
요청하세요.
|
||||
</p>
|
||||
<div className="stock-actions">
|
||||
<Link className="button ghost" to="/stock">
|
||||
스톡 홈으로
|
||||
주식 랩으로 돌아가기
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
<div className="stock-card">
|
||||
<p className="stock-card__title">거래 안내</p>
|
||||
<p className="stock-card__title">계좌 요약</p>
|
||||
<div className="stock-status">
|
||||
<div>
|
||||
<span>주문 유형</span>
|
||||
<strong>시장가/지정가</strong>
|
||||
<span>총 평가금액</span>
|
||||
<strong>{formatNumber(summary.total_eval)}</strong>
|
||||
</div>
|
||||
<div>
|
||||
<span>시장가</span>
|
||||
<strong>가격 0 입력</strong>
|
||||
<span>예수금</span>
|
||||
<strong>{formatNumber(summary.deposit)}</strong>
|
||||
</div>
|
||||
<div>
|
||||
<span>안내</span>
|
||||
<strong>주문 전 코드 확인</strong>
|
||||
<span>보유 종목</span>
|
||||
<strong>{holdings.length}</strong>
|
||||
</div>
|
||||
</div>
|
||||
{summary.note ? (
|
||||
<p className="stock-status__note">{summary.note}</p>
|
||||
) : null}
|
||||
</div>
|
||||
</header>
|
||||
|
||||
{error ? <p className="stock-error">{error}</p> : null}
|
||||
{balanceError ? <p className="stock-error">{balanceError}</p> : null}
|
||||
{autoError ? <p className="stock-error">{autoError}</p> : null}
|
||||
|
||||
<section className="stock-panel stock-panel--wide">
|
||||
<div className="stock-panel__head">
|
||||
<div>
|
||||
<p className="stock-panel__eyebrow">Balance</p>
|
||||
<h3>잔고</h3>
|
||||
<p className="stock-panel__sub">
|
||||
보유 잔고와 보유 종목을 확인합니다.
|
||||
</p>
|
||||
</div>
|
||||
<div className="stock-panel__actions">
|
||||
{balanceLoading ? (
|
||||
<span className="stock-chip">조회 중</span>
|
||||
) : null}
|
||||
<button
|
||||
className="button ghost small"
|
||||
onClick={loadBalance}
|
||||
disabled={balanceLoading}
|
||||
>
|
||||
잔고 새로고침
|
||||
</button>
|
||||
</div>
|
||||
<div className="stock-panel__head">
|
||||
<div>
|
||||
<p className="stock-panel__eyebrow">잔고</p>
|
||||
<h3>보유 현황</h3>
|
||||
<p className="stock-panel__sub">
|
||||
연결 계좌의 실시간 잔고와 보유 종목을 확인합니다.
|
||||
</p>
|
||||
</div>
|
||||
{balanceError ? (
|
||||
<p className="stock-empty">{balanceError}</p>
|
||||
) : (
|
||||
<div className="stock-balance">
|
||||
<div className="stock-balance__summary">
|
||||
{[
|
||||
{
|
||||
label: '예수금',
|
||||
value:
|
||||
balance?.cash ??
|
||||
balance?.available_cash ??
|
||||
balance?.deposit,
|
||||
},
|
||||
{
|
||||
label: '총평가',
|
||||
value:
|
||||
balance?.total_eval ??
|
||||
balance?.total_value ??
|
||||
balance?.evaluation,
|
||||
},
|
||||
{
|
||||
label: '손익',
|
||||
value:
|
||||
balance?.pnl ??
|
||||
balance?.profit_loss ??
|
||||
balance?.total_pnl,
|
||||
},
|
||||
]
|
||||
.filter((item) => item.value !== undefined)
|
||||
.map((item) => (
|
||||
<div
|
||||
key={item.label}
|
||||
className="stock-balance__card"
|
||||
>
|
||||
<span>{item.label}</span>
|
||||
<strong>{formatNumber(item.value)}</strong>
|
||||
</div>
|
||||
))}
|
||||
<div className="stock-panel__actions">
|
||||
{balanceLoading ? (
|
||||
<span className="stock-chip">조회 중</span>
|
||||
) : null}
|
||||
<button
|
||||
className="button ghost small"
|
||||
onClick={loadBalance}
|
||||
disabled={balanceLoading}
|
||||
>
|
||||
새로고침
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div className="stock-balance">
|
||||
<div className="stock-balance__summary">
|
||||
{[
|
||||
{
|
||||
label: '총 평가',
|
||||
value: summary.total_eval,
|
||||
},
|
||||
{
|
||||
label: '예수금',
|
||||
value: summary.deposit,
|
||||
},
|
||||
].map((item) => (
|
||||
<div
|
||||
key={item.label}
|
||||
className="stock-balance__card"
|
||||
>
|
||||
<span>{item.label}</span>
|
||||
<strong>{formatNumber(item.value)}</strong>
|
||||
</div>
|
||||
{Array.isArray(
|
||||
balance?.holdings ??
|
||||
balance?.positions ??
|
||||
balance?.items
|
||||
) &&
|
||||
(balance?.holdings ??
|
||||
balance?.positions ??
|
||||
balance?.items).length ? (
|
||||
<div className="stock-holdings">
|
||||
{(balance?.holdings ??
|
||||
balance?.positions ??
|
||||
balance?.items
|
||||
).map((item, idx) => (
|
||||
<div
|
||||
key={
|
||||
item.code ??
|
||||
item.symbol ??
|
||||
`${item.name ?? 'item'}-${idx}`
|
||||
}
|
||||
className="stock-holdings__item"
|
||||
>
|
||||
<div>
|
||||
<p className="stock-holdings__name">
|
||||
{item.name ?? item.code ?? '종목'}
|
||||
</p>
|
||||
<span className="stock-holdings__code">
|
||||
{item.code ?? item.symbol ?? ''}
|
||||
</span>
|
||||
</div>
|
||||
<div>
|
||||
<span>수량</span>
|
||||
<strong>
|
||||
{formatNumber(
|
||||
item.qty ??
|
||||
item.quantity ??
|
||||
item.holding
|
||||
)}
|
||||
</strong>
|
||||
</div>
|
||||
<div>
|
||||
<span>평균단가</span>
|
||||
<strong>
|
||||
{formatNumber(
|
||||
item.avg_price ??
|
||||
item.avg ??
|
||||
item.price
|
||||
)}
|
||||
</strong>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
))}
|
||||
</div>
|
||||
{holdings.length ? (
|
||||
<div className="stock-holdings">
|
||||
{holdings.map((item, idx) => (
|
||||
<div
|
||||
key={item.code ?? `${item.name}-${idx}`}
|
||||
className="stock-holdings__item"
|
||||
>
|
||||
<div>
|
||||
<p className="stock-holdings__name">
|
||||
{item.name ?? item.code ?? 'N/A'}
|
||||
</p>
|
||||
<span className="stock-holdings__code">
|
||||
{item.code ?? ''}
|
||||
</span>
|
||||
</div>
|
||||
<div>
|
||||
<span>수량</span>
|
||||
<strong>
|
||||
{formatNumber(item.qty)}
|
||||
</strong>
|
||||
</div>
|
||||
<div>
|
||||
<span>매입가</span>
|
||||
<strong>
|
||||
{formatNumber(item.buy_price)}
|
||||
</strong>
|
||||
</div>
|
||||
<div>
|
||||
<span>현재가</span>
|
||||
<strong>
|
||||
{formatNumber(item.current_price)}
|
||||
</strong>
|
||||
</div>
|
||||
<div>
|
||||
<span>수익률</span>
|
||||
<strong>
|
||||
{formatPercent(item.profit_rate)}
|
||||
</strong>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<p className="stock-empty">보유 종목 없음</p>
|
||||
)}
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<p className="stock-empty">보유 종목이 없습니다.</p>
|
||||
)}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section className="stock-panel stock-panel--wide">
|
||||
<div className="stock-panel__head">
|
||||
<div>
|
||||
<p className="stock-panel__eyebrow">Order</p>
|
||||
<h3>주문 (매수/매도)</h3>
|
||||
<p className="stock-panel__sub">
|
||||
종목 코드, 수량, 가격을 입력해 주문을 전송합니다.
|
||||
</p>
|
||||
</div>
|
||||
<div className="stock-panel__head">
|
||||
<div>
|
||||
<p className="stock-panel__eyebrow">자동 매매</p>
|
||||
<h3>AI 매매 판단</h3>
|
||||
<p className="stock-panel__sub">
|
||||
분석에 몇 초 걸릴 수 있습니다. 결과는 아래에
|
||||
표시됩니다.
|
||||
</p>
|
||||
</div>
|
||||
<form className="stock-order" onSubmit={onOrderSubmit}>
|
||||
<label>
|
||||
종목 코드
|
||||
<input
|
||||
type="text"
|
||||
value={orderForm.code}
|
||||
onChange={(event) =>
|
||||
setOrderForm((prev) => ({
|
||||
...prev,
|
||||
code: event.target.value,
|
||||
}))
|
||||
}
|
||||
placeholder="005930"
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
수량
|
||||
<input
|
||||
type="number"
|
||||
min={1}
|
||||
step={1}
|
||||
value={orderForm.qty}
|
||||
onChange={(event) =>
|
||||
setOrderForm((prev) => ({
|
||||
...prev,
|
||||
qty: Number(event.target.value),
|
||||
}))
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
가격 (0=시장가)
|
||||
<input
|
||||
type="number"
|
||||
min={0}
|
||||
step={1}
|
||||
value={orderForm.price}
|
||||
onChange={(event) =>
|
||||
setOrderForm((prev) => ({
|
||||
...prev,
|
||||
price: Number(event.target.value),
|
||||
}))
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
주문 타입
|
||||
<select
|
||||
value={orderForm.type}
|
||||
onChange={(event) =>
|
||||
setOrderForm((prev) => ({
|
||||
...prev,
|
||||
type: event.target.value,
|
||||
}))
|
||||
}
|
||||
>
|
||||
<option value="buy">매수</option>
|
||||
<option value="sell">매도</option>
|
||||
</select>
|
||||
</label>
|
||||
<button
|
||||
className="button primary"
|
||||
type="submit"
|
||||
disabled={orderLoading}
|
||||
>
|
||||
{orderLoading ? '주문 중...' : '주문 실행'}
|
||||
</button>
|
||||
{orderMessage ? (
|
||||
<p className="stock-success">{orderMessage}</p>
|
||||
<div className="stock-panel__actions">
|
||||
{autoLoading ? (
|
||||
<span className="stock-chip">분석 중</span>
|
||||
) : null}
|
||||
</form>
|
||||
<button
|
||||
className="button primary small"
|
||||
onClick={runAutoTrade}
|
||||
disabled={autoLoading}
|
||||
>
|
||||
요청
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div className="stock-ai">
|
||||
{!autoResult ? (
|
||||
<p className="stock-empty">
|
||||
아직 자동 매매 요청이 없습니다.
|
||||
</p>
|
||||
) : autoStatus === 'failed_parse' ? (
|
||||
<div className="stock-ai__raw">
|
||||
<p className="stock-ai__title">원문 응답</p>
|
||||
<pre>
|
||||
{autoResult?.raw_response ??
|
||||
'원문 응답이 없습니다.'}
|
||||
</pre>
|
||||
</div>
|
||||
) : (
|
||||
<div className="stock-ai__grid">
|
||||
<div className="stock-ai__card">
|
||||
<p className="stock-ai__title">판단</p>
|
||||
<div className="stock-status">
|
||||
<div>
|
||||
<span>액션</span>
|
||||
<strong>{decision?.action ?? '-'}</strong>
|
||||
</div>
|
||||
<div>
|
||||
<span>종목코드</span>
|
||||
<strong>{decision?.ticker ?? '-'}</strong>
|
||||
</div>
|
||||
<div>
|
||||
<span>수량</span>
|
||||
<strong>
|
||||
{formatNumber(decision?.quantity)}
|
||||
</strong>
|
||||
</div>
|
||||
</div>
|
||||
{decision?.reason ? (
|
||||
<p className="stock-ai__reason">
|
||||
{decision.reason}
|
||||
</p>
|
||||
) : null}
|
||||
</div>
|
||||
<div className="stock-ai__card">
|
||||
<p className="stock-ai__title">주문 결과</p>
|
||||
<div className="stock-status">
|
||||
<div>
|
||||
<span>상태</span>
|
||||
<strong>
|
||||
{tradeResult?.success === true
|
||||
? '성공'
|
||||
: tradeResult?.success === false
|
||||
? '실패'
|
||||
: '대기'}
|
||||
</strong>
|
||||
</div>
|
||||
<div>
|
||||
<span>주문번호</span>
|
||||
<strong>
|
||||
{tradeResult?.order_no ?? '-'}
|
||||
</strong>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user