전체적인 리팩토링
This commit is contained in:
153
scripts/autotrade-dev-smoke.mjs
Normal file
153
scripts/autotrade-dev-smoke.mjs
Normal file
@@ -0,0 +1,153 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* [Purpose]
|
||||
* Fast dev smoke test for autotrade flow:
|
||||
* compile -> validate -> start -> heartbeat -> signal -> worker tick -> stop.
|
||||
*/
|
||||
|
||||
const appUrl = process.env.AUTOTRADE_APP_URL || "http://127.0.0.1:3001";
|
||||
const bypassToken = process.env.AUTOTRADE_DEV_BYPASS_TOKEN || "autotrade-dev-bypass";
|
||||
const workerToken = process.env.AUTOTRADE_WORKER_TOKEN || "autotrade-worker-local";
|
||||
|
||||
async function main() {
|
||||
// [Step 1] compile strategy
|
||||
const compile = await callApi("/api/autotrade/strategies/compile", {
|
||||
method: "POST",
|
||||
body: {
|
||||
aiMode: "auto",
|
||||
prompt: "Use ORB and VWAP reversion conservatively, prefer hold on uncertainty.",
|
||||
selectedTechniques: ["orb", "vwap_reversion"],
|
||||
confidenceThreshold: 0.65,
|
||||
},
|
||||
});
|
||||
|
||||
// [Step 2] validate risk
|
||||
const validation = await callApi("/api/autotrade/strategies/validate", {
|
||||
method: "POST",
|
||||
body: {
|
||||
cashBalance: 2_000_000,
|
||||
allocationPercent: 10,
|
||||
allocationAmount: 300_000,
|
||||
dailyLossPercent: 2,
|
||||
dailyLossAmount: 30_000,
|
||||
},
|
||||
});
|
||||
|
||||
if (!validation?.validation?.isValid) {
|
||||
throw new Error("validation failed");
|
||||
}
|
||||
|
||||
// [Step 3] start session
|
||||
const start = await callApi("/api/autotrade/sessions/start", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"x-kis-app-key": "dev-app-key",
|
||||
"x-kis-app-secret": "dev-app-secret",
|
||||
"x-kis-account-no": "12345678-01",
|
||||
"x-kis-trading-env": "mock",
|
||||
},
|
||||
body: {
|
||||
symbol: "005930",
|
||||
leaderTabId: "autotrade-dev-smoke-tab",
|
||||
effectiveAllocationAmount: validation.validation.effectiveAllocationAmount,
|
||||
effectiveDailyLossLimit: validation.validation.effectiveDailyLossLimit,
|
||||
strategySummary: compile.compiledStrategy.summary,
|
||||
},
|
||||
});
|
||||
|
||||
// [Step 4] send heartbeat
|
||||
await callApi("/api/autotrade/sessions/heartbeat", {
|
||||
method: "POST",
|
||||
body: {
|
||||
sessionId: start.session.sessionId,
|
||||
leaderTabId: "autotrade-dev-smoke-tab",
|
||||
},
|
||||
});
|
||||
|
||||
// [Step 5] generate a signal
|
||||
const signal = await callApi("/api/autotrade/signals/generate", {
|
||||
method: "POST",
|
||||
body: {
|
||||
aiMode: "auto",
|
||||
strategy: compile.compiledStrategy,
|
||||
snapshot: {
|
||||
symbol: "005930",
|
||||
currentPrice: 73_000,
|
||||
changeRate: 0.35,
|
||||
open: 72_800,
|
||||
high: 73_100,
|
||||
low: 72_600,
|
||||
tradeVolume: 120_000,
|
||||
accumulatedVolume: 450_000,
|
||||
recentPrices: [72_600, 72_700, 72_800, 72_900, 73_000, 73_050, 73_100],
|
||||
marketDataLatencySec: 1,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// [Step 6] worker tick auth/path check
|
||||
await callApi("/api/autotrade/worker/tick", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"x-autotrade-worker-token": workerToken,
|
||||
},
|
||||
body: {},
|
||||
});
|
||||
|
||||
// [Step 7] stop session
|
||||
const stop = await callApi("/api/autotrade/sessions/stop", {
|
||||
method: "POST",
|
||||
body: {
|
||||
sessionId: start.session.sessionId,
|
||||
reason: "manual",
|
||||
},
|
||||
});
|
||||
|
||||
if (stop?.session?.runtimeState !== "STOPPED") {
|
||||
throw new Error("stop failed");
|
||||
}
|
||||
|
||||
console.log(
|
||||
"[autotrade-dev-smoke] PASS",
|
||||
JSON.stringify(
|
||||
{
|
||||
appUrl,
|
||||
compileProvider: compile?.compiledStrategy?.provider,
|
||||
signal: signal?.signal?.signal,
|
||||
signalSource: signal?.signal?.source,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
async function callApi(path, options) {
|
||||
const headers = {
|
||||
"content-type": "application/json",
|
||||
"x-autotrade-dev-bypass": bypassToken,
|
||||
...(options.headers || {}),
|
||||
};
|
||||
|
||||
const response = await fetch(`${appUrl}${path}`, {
|
||||
method: options.method,
|
||||
headers,
|
||||
body: options.body ? JSON.stringify(options.body) : undefined,
|
||||
});
|
||||
|
||||
const payload = await response.json().catch(() => null);
|
||||
if (!response.ok) {
|
||||
const message = payload?.message || `${path} failed (${response.status})`;
|
||||
throw new Error(message);
|
||||
}
|
||||
|
||||
return payload;
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
const message = error instanceof Error ? error.message : "autotrade dev smoke failed";
|
||||
console.error(`[autotrade-dev-smoke] FAIL: ${message}`);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
117
scripts/autotrade-session-e2e.mjs
Normal file
117
scripts/autotrade-session-e2e.mjs
Normal file
@@ -0,0 +1,117 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const appUrl = process.env.AUTOTRADE_APP_URL || "http://127.0.0.1:3001";
|
||||
const bypassToken = process.env.AUTOTRADE_DEV_BYPASS_TOKEN || "autotrade-dev-bypass";
|
||||
|
||||
async function main() {
|
||||
const compile = await callApi("/api/autotrade/strategies/compile", {
|
||||
method: "POST",
|
||||
body: {
|
||||
prompt: "장 초반 변동성은 보수적으로 보고, ORB와 VWAP 신호가 동시에 나올 때만 진입",
|
||||
selectedTechniques: ["orb", "vwap_reversion"],
|
||||
confidenceThreshold: 0.65,
|
||||
},
|
||||
});
|
||||
|
||||
const validation = await callApi("/api/autotrade/strategies/validate", {
|
||||
method: "POST",
|
||||
body: {
|
||||
cashBalance: 2_000_000,
|
||||
allocationPercent: 10,
|
||||
allocationAmount: 300_000,
|
||||
dailyLossPercent: 2,
|
||||
dailyLossAmount: 30_000,
|
||||
},
|
||||
});
|
||||
|
||||
if (!validation?.validation?.isValid) {
|
||||
throw new Error("리스크 검증이 실패했습니다.");
|
||||
}
|
||||
|
||||
const start = await callApi("/api/autotrade/sessions/start", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"x-kis-app-key": "dev-app-key",
|
||||
"x-kis-app-secret": "dev-app-secret",
|
||||
"x-kis-account-no": "12345678-01",
|
||||
"x-kis-trading-env": "mock",
|
||||
},
|
||||
body: {
|
||||
symbol: "005930",
|
||||
leaderTabId: "autotrade-e2e-tab",
|
||||
effectiveAllocationAmount: validation.validation.effectiveAllocationAmount,
|
||||
effectiveDailyLossLimit: validation.validation.effectiveDailyLossLimit,
|
||||
strategySummary: compile.compiledStrategy.summary,
|
||||
},
|
||||
});
|
||||
|
||||
const heartbeat = await callApi("/api/autotrade/sessions/heartbeat", {
|
||||
method: "POST",
|
||||
body: {
|
||||
sessionId: start.session.sessionId,
|
||||
leaderTabId: "autotrade-e2e-tab",
|
||||
},
|
||||
});
|
||||
|
||||
if (heartbeat.session.sessionId !== start.session.sessionId) {
|
||||
throw new Error("heartbeat 결과의 sessionId가 시작 세션과 다릅니다.");
|
||||
}
|
||||
|
||||
const active = await callApi("/api/autotrade/sessions/active", {
|
||||
method: "GET",
|
||||
});
|
||||
|
||||
if (!active.session || active.session.sessionId !== start.session.sessionId) {
|
||||
throw new Error("active 세션 조회 결과가 기대와 다릅니다.");
|
||||
}
|
||||
|
||||
const stop = await callApi("/api/autotrade/sessions/stop", {
|
||||
method: "POST",
|
||||
body: {
|
||||
sessionId: start.session.sessionId,
|
||||
reason: "manual",
|
||||
},
|
||||
});
|
||||
|
||||
if (!stop.session || stop.session.runtimeState !== "STOPPED") {
|
||||
throw new Error("세션 중지가 정상 반영되지 않았습니다.");
|
||||
}
|
||||
|
||||
const activeAfterStop = await callApi("/api/autotrade/sessions/active", {
|
||||
method: "GET",
|
||||
});
|
||||
|
||||
if (activeAfterStop.session !== null) {
|
||||
throw new Error("중지 이후 active 세션이 남아 있습니다.");
|
||||
}
|
||||
|
||||
console.log("[autotrade-session-e2e] PASS: start -> heartbeat -> stop lifecycle verified");
|
||||
}
|
||||
|
||||
async function callApi(path, options) {
|
||||
const headers = {
|
||||
"content-type": "application/json",
|
||||
"x-autotrade-dev-bypass": bypassToken,
|
||||
...(options.headers || {}),
|
||||
};
|
||||
|
||||
const response = await fetch(`${appUrl}${path}`, {
|
||||
method: options.method,
|
||||
headers,
|
||||
body: options.body ? JSON.stringify(options.body) : undefined,
|
||||
});
|
||||
|
||||
const payload = await response.json().catch(() => null);
|
||||
if (!response.ok) {
|
||||
const message = payload?.message || `${path} failed (${response.status})`;
|
||||
throw new Error(message);
|
||||
}
|
||||
|
||||
return payload;
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
const message = error instanceof Error ? error.message : "autotrade lifecycle test failed";
|
||||
console.error(`[autotrade-session-e2e] FAIL: ${message}`);
|
||||
process.exit(1);
|
||||
});
|
||||
96
scripts/autotrade-worker.mjs
Normal file
96
scripts/autotrade-worker.mjs
Normal file
@@ -0,0 +1,96 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* [목적]
|
||||
* heartbeat가 끊긴 자동매매 세션을 주기적으로 정리하는 백그라운드 워커입니다.
|
||||
*
|
||||
* [데이터 흐름]
|
||||
* worker loop -> /api/autotrade/worker/tick 호출 -> 만료 세션 sweep -> 로그 출력
|
||||
*/
|
||||
const args = new Set(process.argv.slice(2));
|
||||
const once = args.has("--once");
|
||||
|
||||
const appUrl = process.env.AUTOTRADE_APP_URL || "http://127.0.0.1:3001";
|
||||
const workerToken = process.env.AUTOTRADE_WORKER_TOKEN || "autotrade-worker-local";
|
||||
const pollMsRaw = Number.parseInt(process.env.AUTOTRADE_WORKER_POLL_MS || "5000", 10);
|
||||
const pollMs = Number.isFinite(pollMsRaw) ? Math.max(1000, pollMsRaw) : 5000;
|
||||
|
||||
let isShuttingDown = false;
|
||||
|
||||
function log(message, level = "INFO") {
|
||||
const time = new Date().toISOString();
|
||||
const line = `[autotrade-worker][${level}][${time}] ${message}`;
|
||||
console.log(line);
|
||||
}
|
||||
|
||||
async function runTick() {
|
||||
// Next API로 tick 요청을 보내 세션 만료 정리를 수행합니다.
|
||||
const response = await fetch(`${appUrl}/api/autotrade/worker/tick`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
"x-autotrade-worker-token": workerToken,
|
||||
},
|
||||
body: JSON.stringify({}),
|
||||
});
|
||||
|
||||
const payload = await response.json().catch(() => null);
|
||||
|
||||
if (!response.ok) {
|
||||
const message = payload?.message || `HTTP ${response.status}`;
|
||||
throw new Error(message);
|
||||
}
|
||||
|
||||
return payload;
|
||||
}
|
||||
|
||||
async function loop() {
|
||||
if (once) {
|
||||
const payload = await runTick();
|
||||
log(
|
||||
`single tick done: expired=${payload?.sweep?.expiredCount ?? 0}, running=${payload?.runningSessions ?? 0}`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
log(`started (poll=${pollMs}ms, app=${appUrl})`);
|
||||
|
||||
// 지정 주기마다 tick을 반복 호출합니다.
|
||||
while (!isShuttingDown) {
|
||||
try {
|
||||
const payload = await runTick();
|
||||
log(
|
||||
`tick ok: expired=${payload?.sweep?.expiredCount ?? 0}, running=${payload?.runningSessions ?? 0}, stopped=${payload?.stoppedSessions ?? 0}`,
|
||||
);
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "tick failed";
|
||||
log(message, "ERROR");
|
||||
}
|
||||
|
||||
await wait(pollMs);
|
||||
}
|
||||
|
||||
log("graceful shutdown complete");
|
||||
}
|
||||
|
||||
function wait(ms) {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(resolve, ms);
|
||||
});
|
||||
}
|
||||
|
||||
process.on("SIGINT", () => {
|
||||
isShuttingDown = true;
|
||||
log("SIGINT received, stopping...");
|
||||
});
|
||||
|
||||
process.on("SIGTERM", () => {
|
||||
isShuttingDown = true;
|
||||
log("SIGTERM received, stopping...");
|
||||
});
|
||||
|
||||
loop().catch((error) => {
|
||||
const message = error instanceof Error ? error.message : "worker exited with error";
|
||||
log(message, "ERROR");
|
||||
process.exit(1);
|
||||
});
|
||||
20
scripts/pm2.autotrade-worker.config.cjs
Normal file
20
scripts/pm2.autotrade-worker.config.cjs
Normal file
@@ -0,0 +1,20 @@
|
||||
module.exports = {
|
||||
apps: [
|
||||
{
|
||||
name: "autotrade-worker",
|
||||
script: "./scripts/autotrade-worker.mjs",
|
||||
interpreter: "node",
|
||||
instances: 1,
|
||||
autorestart: true,
|
||||
watch: false,
|
||||
max_memory_restart: "300M",
|
||||
env: {
|
||||
NODE_ENV: "production",
|
||||
// 서비스 단위 시크릿: "사용자별"이 아니라 앱/워커 프로세스가 공유하는 값입니다.
|
||||
AUTOTRADE_WORKER_TOKEN: process.env.AUTOTRADE_WORKER_TOKEN || "",
|
||||
AUTOTRADE_APP_URL: process.env.AUTOTRADE_APP_URL || "http://127.0.0.1:3001",
|
||||
AUTOTRADE_WORKER_POLL_MS: process.env.AUTOTRADE_WORKER_POLL_MS || "5000",
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
Reference in New Issue
Block a user