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>
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
|
||||
export interface RiskSettings {
|
||||
accountSize: number;
|
||||
riskPct: number;
|
||||
}
|
||||
|
||||
const STORAGE_KEY = 'risk-settings';
|
||||
const DEFAULTS: RiskSettings = { accountSize: 10000, riskPct: 1 };
|
||||
|
||||
/** Account size + per-trade risk %, persisted in localStorage (per browser). */
|
||||
export function useRiskSettings() {
|
||||
const [settings, setSettings] = useState<RiskSettings>(() => {
|
||||
try {
|
||||
const raw = localStorage.getItem(STORAGE_KEY);
|
||||
if (raw) return { ...DEFAULTS, ...JSON.parse(raw) };
|
||||
} catch {
|
||||
/* ignore malformed storage */
|
||||
}
|
||||
return DEFAULTS;
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
try {
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(settings));
|
||||
} catch {
|
||||
/* ignore quota/availability errors */
|
||||
}
|
||||
}, [settings]);
|
||||
|
||||
const update = useCallback(
|
||||
(patch: Partial<RiskSettings>) => setSettings((s) => ({ ...s, ...patch })),
|
||||
[],
|
||||
);
|
||||
|
||||
return { settings, update };
|
||||
}
|
||||
Reference in New Issue
Block a user