- Orders, position closes, card answers and deposits are claimed atomically (one tick at a time, one evening at a time); cash updated in SQL - Each tick step is isolated: an AI or Yahoo failure no longer stops the rest - Missed monthly deposits are caught up; Trade Republic holidays handled - Gap opens fill at the open (target included); limit orders valid one trading day; stale sell orders cancelled; old cards expire (PEA transfer kept) - News queries bounded to 48 h, news older than 30 days purged - AI sim pot receives the same monthly deposit as the real account; performance measured on money contributed vs S&P 500 with the same flows; reset action - Yahoo data cached in the database (stale-while-revalidate); the order engine still requires fresh quotes; client router keeps tabs for 60 s Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
87 lines
3.3 KiB
TypeScript
87 lines
3.3 KiB
TypeScript
/**
|
|
* Moteur d'exécution simulé : décide si un ordre limite est exécuté et si une position
|
|
* touche son stop, son objectif ou sa date de sortie maximale.
|
|
* Fonctions pures, testées dans fills.test.ts.
|
|
*/
|
|
|
|
export type Bar = { date: string; open: number; high: number; low: number; close: number };
|
|
|
|
/** Frais Trade Republic par ordre, en euros. */
|
|
export const FEE_EUR = 1;
|
|
/** Écart achat/vente moyen supposé chez Trade Republic (0,1 %). */
|
|
export const SLIPPAGE = 0.001;
|
|
|
|
export function withSlippage(price: number, side: "buy" | "sell"): number {
|
|
return side === "buy" ? price * (1 + SLIPPAGE) : price * (1 - SLIPPAGE);
|
|
}
|
|
|
|
type BuyLimitInput = {
|
|
limit: number;
|
|
createdDate: string;
|
|
today: string;
|
|
validUntil: string | null;
|
|
bars: Bar[];
|
|
currentPrice: number | null;
|
|
brokerOpen: boolean;
|
|
};
|
|
|
|
export type BuyLimitResult =
|
|
| { status: "filled"; price: number; date: string }
|
|
| { status: "expired" }
|
|
| { status: "pending" };
|
|
|
|
export function evaluateBuyLimit(input: BuyLimitInput): BuyLimitResult {
|
|
const { limit, createdDate, today, validUntil, bars, currentPrice, brokerOpen } = input;
|
|
const lastDay = validUntil && validUntil < today ? validUntil : today;
|
|
|
|
// Jours complets après la création : si le plus bas est passé sous la limite, l'ordre est exécuté.
|
|
for (const bar of bars) {
|
|
if (bar.date <= createdDate || bar.date > lastDay) continue;
|
|
if (bar.low <= limit) return { status: "filled", price: Math.min(bar.open, limit), date: bar.date };
|
|
}
|
|
if (validUntil && today > validUntil) return { status: "expired" };
|
|
if (brokerOpen && currentPrice !== null && currentPrice <= limit) {
|
|
return { status: "filled", price: currentPrice, date: today };
|
|
}
|
|
return { status: "pending" };
|
|
}
|
|
|
|
type ExitInput = {
|
|
entryDate: string;
|
|
today: string;
|
|
stop: number | null;
|
|
target: number | null;
|
|
maxExitDate: string | null;
|
|
bars: Bar[];
|
|
currentPrice: number | null;
|
|
brokerOpen: boolean;
|
|
};
|
|
|
|
export type ExitResult = { price: number; reason: "stop" | "target" | "time"; date: string } | null;
|
|
|
|
export function evaluateExit(input: ExitInput): ExitResult {
|
|
const { entryDate, today, stop, target, maxExitDate, bars, currentPrice, brokerOpen } = input;
|
|
|
|
for (const bar of bars) {
|
|
if (bar.date <= entryDate || bar.date > today) continue;
|
|
// Ouverture au-delà du stop ou de l'objectif (trou de cotation) : l'ordre part à l'ouverture.
|
|
if (stop !== null && bar.open <= stop) return { price: bar.open, reason: "stop", date: bar.date };
|
|
if (target !== null && bar.open >= target) return { price: bar.open, reason: "target", date: bar.date };
|
|
// Stop et objectif touchés dans la même journée sans trou : on ne sait pas lequel d'abord, on suppose le pire.
|
|
if (stop !== null && bar.low <= stop) {
|
|
return { price: Math.min(bar.open, stop), reason: "stop", date: bar.date };
|
|
}
|
|
if (target !== null && bar.high >= target) {
|
|
return { price: Math.max(bar.open, target), reason: "target", date: bar.date };
|
|
}
|
|
}
|
|
if (brokerOpen && currentPrice !== null) {
|
|
if (stop !== null && currentPrice <= stop) return { price: currentPrice, reason: "stop", date: today };
|
|
if (target !== null && currentPrice >= target) {
|
|
return { price: currentPrice, reason: "target", date: today };
|
|
}
|
|
if (maxExitDate && today >= maxExitDate) return { price: currentPrice, reason: "time", date: today };
|
|
}
|
|
return null;
|
|
}
|