전략 시간봉 오류 수정
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
import { loadStrategyTimeframes, saveStrategy, type StrategyDto } from './backendApi';
|
||||
import { loadStrategyFlowLayout } from './strategyEditorLayoutStorage';
|
||||
import {
|
||||
alignBuySellStartCandleTypesForSave,
|
||||
collectDslBranches,
|
||||
collectTimeframesFromEditorState,
|
||||
encodeConditionForSave,
|
||||
type EditorConditionState,
|
||||
} from './strategyConditionSerde';
|
||||
import { START_NODE_ID, getStartCandleTypes, normalizeStartCandleType } from './strategyStartNodes';
|
||||
import type { LogicNode } from './strategyTypes';
|
||||
import type { StrategyFlowLayoutStore } from './strategyEditorLayoutStorage';
|
||||
|
||||
function collectFromFlowLayout(strategyId: number): string[] {
|
||||
const layout = loadStrategyFlowLayout(String(strategyId));
|
||||
if (!layout) return [];
|
||||
const set = new Set<string>();
|
||||
for (const side of ['buy', 'sell'] as const) {
|
||||
const meta = layout[side]?.startMeta?.[START_NODE_ID];
|
||||
if (!meta) continue;
|
||||
for (const ct of getStartCandleTypes(meta)) set.add(normalizeStartCandleType(ct));
|
||||
}
|
||||
return [...set];
|
||||
}
|
||||
|
||||
function collectFromStrategyDto(strategy: StrategyDto | null | undefined): string[] {
|
||||
if (!strategy) return [];
|
||||
const set = new Set<string>();
|
||||
const buy = strategy.buyCondition as LogicNode | null | undefined;
|
||||
const sell = strategy.sellCondition as LogicNode | null | undefined;
|
||||
for (const b of [
|
||||
...collectDslBranches(buy ?? null),
|
||||
...collectDslBranches(sell ?? null),
|
||||
]) {
|
||||
const types = b.candleTypes?.length ? b.candleTypes : [b.candleType];
|
||||
for (const ct of types) set.add(normalizeStartCandleType(ct));
|
||||
}
|
||||
return [...set];
|
||||
}
|
||||
|
||||
/** 편집기·flow layout·전략 DTO에서 UI 평가 분봉 수집 (서버 DSL보다 우선) */
|
||||
export function collectUiEvaluationTimeframes(
|
||||
strategyId: number,
|
||||
strategy?: StrategyDto | null,
|
||||
editorState?: { buy: EditorConditionState; sell: EditorConditionState },
|
||||
): string[] {
|
||||
if (editorState) {
|
||||
const fromEditor = [
|
||||
...collectTimeframesFromEditorState(editorState.buy),
|
||||
...collectTimeframesFromEditorState(editorState.sell),
|
||||
].map(normalizeStartCandleType);
|
||||
if (fromEditor.length > 0) return [...new Set(fromEditor)];
|
||||
}
|
||||
const fromLayout = collectFromFlowLayout(strategyId);
|
||||
if (fromLayout.length > 0) return fromLayout;
|
||||
return collectFromStrategyDto(strategy);
|
||||
}
|
||||
|
||||
/**
|
||||
* UI 분봉과 서버 DSL 불일치 시 안내.
|
||||
* @returns false — 진행 불가(저장 필요)
|
||||
*/
|
||||
export async function warnStrategyTimeframeMismatch(
|
||||
strategyId: number,
|
||||
strategy?: StrategyDto | null,
|
||||
editorState?: { buy: EditorConditionState; sell: EditorConditionState },
|
||||
): Promise<boolean> {
|
||||
const uiTfs = collectUiEvaluationTimeframes(strategyId, strategy, editorState);
|
||||
if (uiTfs.length === 0) return true;
|
||||
|
||||
let serverTfs: string[];
|
||||
try {
|
||||
serverTfs = await loadStrategyTimeframes(strategyId);
|
||||
} catch {
|
||||
return true;
|
||||
}
|
||||
const uiSet = new Set(uiTfs.map(tf => normalizeStartCandleType(tf)));
|
||||
const serverSet = new Set(serverTfs.map(tf => normalizeStartCandleType(tf)));
|
||||
const missingOnServer = uiTfs.filter(tf => !serverSet.has(normalizeStartCandleType(tf)));
|
||||
const extraOnServer = [...serverSet].filter(tf => !uiSet.has(tf));
|
||||
|
||||
if (missingOnServer.length === 0 && extraOnServer.length === 0) return true;
|
||||
// 양쪽 모두 1m만 — 일치로 간주
|
||||
if (uiSet.size === 1 && uiSet.has('1m') && serverSet.size === 1 && serverSet.has('1m')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const uiLabel = uiTfs.map(tf => normalizeStartCandleType(tf)).join(', ') || '1m';
|
||||
const serverLabel = [...serverSet].join(', ') || '1m';
|
||||
window.alert(
|
||||
`전략 화면에는 ${uiLabel} 봉으로 설정되어 있지만, 서버에는 ${serverLabel} 만 저장되어 있습니다.\n\n`
|
||||
+ '전략편집기에서 「저장」을 누르거나 START 분봉을 다시 선택해 자동 저장된 뒤 실시간 체크를 켜 주세요.',
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
/** START 분봉 변경 시 전략 DSL을 서버에 즉시 반영 */
|
||||
export async function persistStrategyEvaluationTimeframes(
|
||||
strategyId: number,
|
||||
name: string,
|
||||
description: string,
|
||||
buy: EditorConditionState,
|
||||
sell: EditorConditionState,
|
||||
): Promise<void> {
|
||||
const aligned = alignBuySellStartCandleTypesForSave(buy, sell);
|
||||
const encodedBuy = encodeConditionForSave(aligned.buy);
|
||||
const encodedSell = encodeConditionForSave(aligned.sell);
|
||||
await saveStrategy({
|
||||
id: strategyId,
|
||||
name,
|
||||
description,
|
||||
buyCondition: encodedBuy,
|
||||
sellCondition: encodedSell,
|
||||
enabled: true,
|
||||
});
|
||||
}
|
||||
|
||||
export type { StrategyFlowLayoutStore };
|
||||
Reference in New Issue
Block a user