Files
AITrader/app/lib/__tests__/execution.test.ts
T

56 lines
2.2 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { enrichExecutionPlan } from "../execution";
describe("enrichExecutionPlan", () => {
beforeEach(() => {
process.env.DEFAULT_ACCOUNT_EQUITY = "10000";
});
afterEach(() => {
delete process.env.DEFAULT_ACCOUNT_EQUITY;
});
it("computes stopLoss/takeProfit/amount for buy decision", () => {
const decision: any = { action: "buy" };
const input = { technicalData: { prices: [100, 102, 101] } };
const out = enrichExecutionPlan(decision, input);
expect(out.executionPlan).toBeDefined();
// ATR approx = 1.5 -> stopDistance = 1.5*1.5 = 2.25
// stopLoss = 101 - 2.25 = 98.75
// takeProfit = 101 + 2.25*2 = 105.5
// riskAmount = 10000 * 0.01 = 100 -> amount = floor(100 / 2.25) = 44
expect(out.executionPlan.amount).toBe(44);
expect(out.executionPlan.stopLoss).toBeCloseTo(98.75, 2);
expect(out.executionPlan.takeProfit).toBeCloseTo(105.5, 2);
});
it("computes stopLoss/takeProfit for sell decision (stop above entry)", () => {
const decision: any = { action: "sell" };
const input = { technicalData: { prices: [100, 102, 101] } };
const out = enrichExecutionPlan(decision, input);
// entryPrice = 101, stopDistance = 2.25
// stopLoss = 101 + 2.25 = 103.25
// takeProfit = 101 - 2.25*2 = 96.5
expect(out.executionPlan).toBeDefined();
expect(out.executionPlan.stopLoss).toBeCloseTo(103.25, 2);
expect(out.executionPlan.takeProfit).toBeCloseTo(96.5, 2);
expect(out.executionPlan.amount).toBeGreaterThanOrEqual(1);
});
it("preserves existing executionPlan fields and normalizes riskManagement", () => {
const decision: any = { action: "buy", executionPlan: { amount: 10, stopLoss: 90, takeProfit: 110 } };
const input = { technicalData: { prices: [100, 101] } };
const out = enrichExecutionPlan(decision, input);
expect(out.executionPlan.amount).toBe(10);
expect(out.executionPlan.stopLoss).toBe(90);
expect(out.executionPlan.takeProfit).toBe(110);
expect(out.executionPlan.riskManagement).toBeDefined();
expect(typeof out.executionPlan.riskManagement.maxLossPercent).toBe("number");
});
});