매수, 매도 색상 수정
This commit is contained in:
@@ -1,117 +0,0 @@
|
||||
/**
|
||||
* 가상투자 Match(전략 조건 100% 충족) 시 자동 모의 체결
|
||||
* 설정 · 가상투자 · 자동매매 ON + 세션 running 일 때만 동작
|
||||
*/
|
||||
import { useEffect, useRef } from 'react';
|
||||
import type { TickerData } from './useMarketTicker';
|
||||
import type { VirtualIndicatorSnapshot } from './useVirtualIndicatorSnapshots';
|
||||
import type { VirtualSessionConfig, VirtualTargetItem } from '../utils/virtualTradingStorage';
|
||||
import { placePaperOrder, type PaperSummaryDto } from '../utils/backendApi';
|
||||
import {
|
||||
buildConditionMetrics,
|
||||
resolveVirtualTradeTiming,
|
||||
type VirtualTradeTiming,
|
||||
} from '../utils/virtualSignalMetrics';
|
||||
import { coerceFiniteNumber } from '../utils/safeFormat';
|
||||
|
||||
const EXEC_COOLDOWN_MS = 15_000;
|
||||
|
||||
interface Options {
|
||||
enabled: boolean;
|
||||
session: VirtualSessionConfig;
|
||||
targets: VirtualTargetItem[];
|
||||
snapshots: Record<string, VirtualIndicatorSnapshot | undefined>;
|
||||
tickers?: Map<string, TickerData>;
|
||||
paperAutoTradeBudgetPct: number;
|
||||
positions?: PaperSummaryDto['positions'];
|
||||
onFilled?: () => void;
|
||||
}
|
||||
|
||||
export function useVirtualAutoTrade({
|
||||
enabled,
|
||||
session,
|
||||
targets,
|
||||
snapshots,
|
||||
tickers,
|
||||
paperAutoTradeBudgetPct,
|
||||
positions = [],
|
||||
onFilled,
|
||||
}: Options): void {
|
||||
const prevTimingRef = useRef<Record<string, VirtualTradeTiming>>({});
|
||||
const inflightRef = useRef<Set<string>>(new Set());
|
||||
const lastExecRef = useRef<Record<string, number>>({});
|
||||
|
||||
useEffect(() => {
|
||||
if (!enabled || !session.running) {
|
||||
prevTimingRef.current = {};
|
||||
return;
|
||||
}
|
||||
|
||||
for (const target of targets) {
|
||||
const snap = snapshots[target.market];
|
||||
if (!snap?.rows?.length) continue;
|
||||
|
||||
const metrics = buildConditionMetrics(snap.rows);
|
||||
const timing = resolveVirtualTradeTiming(metrics);
|
||||
const prev = prevTimingRef.current[target.market] ?? 'neutral';
|
||||
prevTimingRef.current[target.market] = timing;
|
||||
|
||||
if (timing !== 'buy' && timing !== 'sell') continue;
|
||||
if (timing === prev) continue;
|
||||
|
||||
const price = coerceFiniteNumber(tickers?.get(target.market)?.tradePrice);
|
||||
if (price == null || price <= 0) continue;
|
||||
|
||||
const heldQty = coerceFiniteNumber(
|
||||
positions.find(p => p.symbol === target.market)?.quantity,
|
||||
) ?? 0;
|
||||
|
||||
if (session.positionMode === 'LONG_ONLY') {
|
||||
if (timing === 'buy' && heldQty > 0) continue;
|
||||
if (timing === 'sell' && heldQty <= 0) continue;
|
||||
}
|
||||
|
||||
const side = timing === 'buy' ? 'BUY' : 'SELL';
|
||||
const execKey = `${target.market}:${side}`;
|
||||
if (inflightRef.current.has(execKey)) continue;
|
||||
if (Date.now() - (lastExecRef.current[execKey] ?? 0) < EXEC_COOLDOWN_MS) continue;
|
||||
|
||||
const strategyId = target.strategyId ?? session.globalStrategyId ?? null;
|
||||
inflightRef.current.add(execKey);
|
||||
|
||||
void placePaperOrder({
|
||||
market: target.market,
|
||||
side,
|
||||
orderKind: 'market',
|
||||
price,
|
||||
quantity: 0,
|
||||
budgetPct: timing === 'buy' ? paperAutoTradeBudgetPct : 100,
|
||||
source: 'STRATEGY',
|
||||
strategyId,
|
||||
})
|
||||
.then(trade => {
|
||||
if (trade) {
|
||||
lastExecRef.current[execKey] = Date.now();
|
||||
onFilled?.();
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
console.warn('[VirtualAutoTrade]', target.market, side, err);
|
||||
})
|
||||
.finally(() => {
|
||||
inflightRef.current.delete(execKey);
|
||||
});
|
||||
}
|
||||
}, [
|
||||
enabled,
|
||||
session.running,
|
||||
session.globalStrategyId,
|
||||
session.positionMode,
|
||||
targets,
|
||||
snapshots,
|
||||
tickers,
|
||||
paperAutoTradeBudgetPct,
|
||||
positions,
|
||||
onFilled,
|
||||
]);
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
/**
|
||||
* 가상투자 자동매매 — 백엔드 BarBuilder/LiveStrategyEvaluator 가 발행한 STOMP BUY/SELL 만 체결.
|
||||
* 프론트 근사 일치율(resolveVirtualTradeTiming) 사용 안 함.
|
||||
*/
|
||||
import { useEffect, useRef } from 'react';
|
||||
import type { TickerData } from './useMarketTicker';
|
||||
import type { VirtualSessionConfig, VirtualTargetItem } from '../utils/virtualTradingStorage';
|
||||
import { loadStrategyTimeframes, placePaperOrder, type PaperSummaryDto } from '../utils/backendApi';
|
||||
import { parseStompJson } from '../utils/stompMessage';
|
||||
import { subscribeStompTopic } from '../utils/stompChartBroker';
|
||||
import { signalBelongsToCurrentSession } from '../utils/tradeSignalOwnership';
|
||||
import { resolveVirtualTargetStrategyId } from '../utils/virtualTargetStrategy';
|
||||
import { coerceFiniteNumber } from '../utils/safeFormat';
|
||||
|
||||
const EXEC_COOLDOWN_MS = 15_000;
|
||||
|
||||
interface Options {
|
||||
enabled: boolean;
|
||||
session: VirtualSessionConfig;
|
||||
targets: VirtualTargetItem[];
|
||||
tickers?: Map<string, TickerData>;
|
||||
paperAutoTradeBudgetPct: number;
|
||||
positions?: PaperSummaryDto['positions'];
|
||||
onFilled?: () => void;
|
||||
}
|
||||
|
||||
export function useVirtualBackendTradeSignals({
|
||||
enabled,
|
||||
session,
|
||||
targets,
|
||||
tickers,
|
||||
paperAutoTradeBudgetPct,
|
||||
positions = [],
|
||||
onFilled,
|
||||
}: Options): void {
|
||||
const inflightRef = useRef<Set<string>>(new Set());
|
||||
const lastExecRef = useRef<Record<string, number>>({});
|
||||
const seenSignalRef = useRef<Set<string>>(new Set());
|
||||
|
||||
useEffect(() => {
|
||||
if (!enabled || !session.running) {
|
||||
seenSignalRef.current.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
const activeTargets = targets.filter(
|
||||
t => resolveVirtualTargetStrategyId(t, session.globalStrategyId) != null,
|
||||
);
|
||||
if (activeTargets.length === 0) return;
|
||||
|
||||
const unsubs: Array<() => void> = [];
|
||||
let cancelled = false;
|
||||
|
||||
void (async () => {
|
||||
for (const target of activeTargets) {
|
||||
if (cancelled) break;
|
||||
const strategyId = resolveVirtualTargetStrategyId(target, session.globalStrategyId)!;
|
||||
let timeframes: string[] = [];
|
||||
try {
|
||||
timeframes = await loadStrategyTimeframes(strategyId);
|
||||
} catch {
|
||||
timeframes = ['1m'];
|
||||
}
|
||||
if (timeframes.length === 0) timeframes = ['1m'];
|
||||
|
||||
for (const candleType of timeframes) {
|
||||
const topic = `/sub/charts/${target.market}/${candleType}`;
|
||||
const unsub = subscribeStompTopic(topic, msg => {
|
||||
const data = parseStompJson<{
|
||||
time: number;
|
||||
close: number;
|
||||
signal?: string;
|
||||
candleType?: string;
|
||||
strategyId?: number | null;
|
||||
userId?: number | null;
|
||||
deviceId?: string | null;
|
||||
}>(msg);
|
||||
if (!data?.signal || (data.signal !== 'BUY' && data.signal !== 'SELL')) return;
|
||||
if (data.strategyId != null && data.strategyId !== strategyId) return;
|
||||
if (!signalBelongsToCurrentSession({ userId: data.userId, deviceId: data.deviceId })) {
|
||||
return;
|
||||
}
|
||||
|
||||
const dedup = `${target.market}:${data.candleType ?? candleType}:${data.time}:${data.signal}:${strategyId}`;
|
||||
if (seenSignalRef.current.has(dedup)) return;
|
||||
seenSignalRef.current.add(dedup);
|
||||
if (seenSignalRef.current.size > 5000) {
|
||||
seenSignalRef.current.clear();
|
||||
}
|
||||
|
||||
const side = data.signal as 'BUY' | 'SELL';
|
||||
const price = coerceFiniteNumber(tickers?.get(target.market)?.tradePrice)
|
||||
?? coerceFiniteNumber(data.close);
|
||||
if (price == null || price <= 0) return;
|
||||
|
||||
const heldQty = coerceFiniteNumber(
|
||||
positions.find(p => p.symbol === target.market)?.quantity,
|
||||
) ?? 0;
|
||||
if (session.positionMode === 'LONG_ONLY') {
|
||||
if (side === 'BUY' && heldQty > 0) return;
|
||||
if (side === 'SELL' && heldQty <= 0) return;
|
||||
}
|
||||
|
||||
const execKey = `${target.market}:${side}`;
|
||||
if (inflightRef.current.has(execKey)) return;
|
||||
if (Date.now() - (lastExecRef.current[execKey] ?? 0) < EXEC_COOLDOWN_MS) return;
|
||||
|
||||
inflightRef.current.add(execKey);
|
||||
void placePaperOrder({
|
||||
market: target.market,
|
||||
side,
|
||||
orderKind: 'market',
|
||||
price,
|
||||
quantity: 0,
|
||||
budgetPct: side === 'BUY' ? paperAutoTradeBudgetPct : 100,
|
||||
source: 'STRATEGY',
|
||||
strategyId,
|
||||
})
|
||||
.then(trade => {
|
||||
if (trade) {
|
||||
lastExecRef.current[execKey] = Date.now();
|
||||
onFilled?.();
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
console.warn('[VirtualBackendTrade]', target.market, side, err);
|
||||
})
|
||||
.finally(() => {
|
||||
inflightRef.current.delete(execKey);
|
||||
});
|
||||
});
|
||||
unsubs.push(unsub);
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
unsubs.forEach(u => u());
|
||||
};
|
||||
}, [
|
||||
enabled,
|
||||
session.running,
|
||||
session.globalStrategyId,
|
||||
session.positionMode,
|
||||
targets,
|
||||
tickers,
|
||||
paperAutoTradeBudgetPct,
|
||||
positions,
|
||||
onFilled,
|
||||
]);
|
||||
}
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
type StrategyDto,
|
||||
} from '../utils/backendApi';
|
||||
import { useVirtualIndicatorSnapshots } from './useVirtualIndicatorSnapshots';
|
||||
import { useVirtualAutoTrade } from './useVirtualAutoTrade';
|
||||
import { useVirtualBackendTradeSignals } from './useVirtualBackendTradeSignals';
|
||||
import { useVirtualTargetLiveStatus } from './useVirtualTargetLiveStatus';
|
||||
import {
|
||||
loadVirtualSession,
|
||||
@@ -206,11 +206,10 @@ export function useVirtualTradingCore(options: UseVirtualTradingCoreOptions = {}
|
||||
session.running,
|
||||
);
|
||||
|
||||
useVirtualAutoTrade({
|
||||
useVirtualBackendTradeSignals({
|
||||
enabled: paperTradingEnabled && paperAutoTradeEnabled,
|
||||
session,
|
||||
targets,
|
||||
snapshots,
|
||||
tickers,
|
||||
paperAutoTradeBudgetPct,
|
||||
positions: summary?.positions,
|
||||
|
||||
Reference in New Issue
Block a user