feat(settings): add settings route and API updates\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
||||
import { enrichExecutionPlan } from "../execution";
|
||||
|
||||
describe("enrichExecutionPlan with Alpaca account data", () => {
|
||||
beforeEach(() => {
|
||||
process.env.DEFAULT_ACCOUNT_EQUITY = "10000";
|
||||
});
|
||||
afterEach(() => {
|
||||
delete process.env.DEFAULT_ACCOUNT_EQUITY;
|
||||
});
|
||||
|
||||
it("uses input.account.cash for sizing when provided", () => {
|
||||
const decision: any = { action: "buy" };
|
||||
const input = { technicalData: { prices: [50, 52, 51] }, account: { cash: 5000 } };
|
||||
|
||||
const out = enrichExecutionPlan(decision, input);
|
||||
|
||||
// entryPrice = 51, ATR ~ 1.5 -> stopDistance = 1.5*1.5 = 2.25
|
||||
// riskAmount = 5000 * 0.01 = 50 -> amount = floor(50 / 2.25) = 22
|
||||
expect(out.executionPlan.amount).toBe(22);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
||||
import { enrichExecutionPlan } from "../execution";
|
||||
|
||||
describe("enrichExecutionPlan edge cases", () => {
|
||||
beforeEach(() => {
|
||||
process.env.DEFAULT_ACCOUNT_EQUITY = "10000";
|
||||
});
|
||||
afterEach(() => {
|
||||
delete process.env.DEFAULT_ACCOUNT_EQUITY;
|
||||
});
|
||||
|
||||
it("handles very small/zero ATR (flat prices) without crashing and uses percent fallback", () => {
|
||||
const decision: any = { action: "buy" };
|
||||
const input = { technicalData: { prices: [100, 100, 100] } };
|
||||
|
||||
const out = enrichExecutionPlan(decision, input);
|
||||
|
||||
expect(out.executionPlan).toBeDefined();
|
||||
// ATR ~ 0, so stopDistance should fall back to percent-based (1% of entry = 1)
|
||||
expect(out.executionPlan.stopLoss).toBeCloseTo(99, 2);
|
||||
// entry 100 + rr*stopDistance (2*1) => 102
|
||||
expect(out.executionPlan.takeProfit).toBeCloseTo(102, 2);
|
||||
expect(out.executionPlan.amount).toBeGreaterThanOrEqual(1);
|
||||
});
|
||||
|
||||
it("honors percent-based riskManagement from LLM (0.5%) and computes amount accordingly", () => {
|
||||
const decision: any = { action: "buy", executionPlan: { riskManagement: { maxLossPercent: 0.5 } } };
|
||||
const input = { technicalData: { prices: [200, 202] } };
|
||||
|
||||
const out = enrichExecutionPlan(decision, input);
|
||||
|
||||
expect(out.executionPlan).toBeDefined();
|
||||
expect(out.executionPlan.riskManagement).toBeDefined();
|
||||
expect(out.executionPlan.riskManagement.maxLossPercent).toBeCloseTo(0.5, 6);
|
||||
|
||||
// entryPrice = 202, rr/default: atr ~2, stopDistance = max(2*1.5=3, 202*0.005=1.01) => 3
|
||||
// riskAmount = 10000 * 0.005 = 50 -> shares = floor(50/3) = 16
|
||||
expect(out.executionPlan.amount).toBe(16);
|
||||
});
|
||||
|
||||
it("handles missing price data by producing a finite amount and no absolute stops", () => {
|
||||
const decision: any = { action: "buy" };
|
||||
const input = { technicalData: { prices: [] } };
|
||||
|
||||
const out = enrichExecutionPlan(decision, input);
|
||||
|
||||
expect(out.executionPlan).toBeDefined();
|
||||
// No entry price -> cannot compute absolute stopLoss/takeProfit
|
||||
expect(out.executionPlan.stopLoss).toBeUndefined();
|
||||
expect(out.executionPlan.takeProfit).toBeUndefined();
|
||||
// Amount should still be computed (uses small fallback stopDistance 0.0001) -> large but finite
|
||||
expect(typeof out.executionPlan.amount).toBe("number");
|
||||
expect(Number.isFinite(out.executionPlan.amount)).toBe(true);
|
||||
expect(out.executionPlan.amount).toBeGreaterThanOrEqual(1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
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");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user