Files
signal-platform/frontend/src/lib/position.ts
T
dennisthiessenandClaude Opus 4.8 1951531453
Deploy / lint (push) Successful in 5s
Deploy / test (push) Successful in 34s
Deploy / deploy (push) Successful in 23s
add position-size calculator to the recommendation panel
Risk-based sizing on each setup card: shares = floor((account × risk%) /
|entry − stop|), with position value and dollars-at-risk. Account size and
per-trade risk % are editable inline and persisted in localStorage. Flags when
a position would exceed the account (needs margin). Frontend-only.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-15 11:26:55 +02:00

38 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export interface PositionSize {
shares: number;
riskPerShare: number;
dollarRisk: number;
positionValue: number;
/** Position value exceeds the account → needs margin / not affordable in cash. */
exceedsAccount: boolean;
}
/**
* Risk-based position sizing. Risk a fixed % of the account per trade; the stop
* distance sets how many shares that budget buys:
* shares = floor((account × risk%) / |entry stop|)
* Returns null when inputs are unusable (no account, no risk, zero stop width).
*/
export function positionSize(
accountSize: number,
riskPct: number,
entry: number,
stop: number,
): PositionSize | null {
const riskPerShare = Math.abs(entry - stop);
if (!(accountSize > 0) || !(riskPct > 0) || !(riskPerShare > 0) || !(entry > 0)) {
return null;
}
const budget = accountSize * (riskPct / 100);
const shares = Math.floor(budget / riskPerShare);
const dollarRisk = shares * riskPerShare;
const positionValue = shares * entry;
return {
shares,
riskPerShare,
dollarRisk,
positionValue,
exceedsAccount: positionValue > accountSize,
};
}