more fixes

This commit is contained in:
Bojan Kucera 2025-06-04 18:37:26 -04:00
parent ab7ef2b678
commit 4c2220d148
5 changed files with 138 additions and 43 deletions

View file

@ -220,18 +220,14 @@ export function sharpeOptimizedPositionSize(
): number {
// Input validation
if (volatility <= 0 || accountSize <= 0 || expectedReturn <= riskFreeRate || maxLeverage <= 0) return 0;
// Kelly criterion with Sharpe ratio optimization
// Kelly criterion with Sharpe ratio optimization
const excessReturn = expectedReturn - riskFreeRate;
const kellyFraction = excessReturn / (volatility * volatility);
// Apply maximum leverage constraint and ensure reasonable bounds
// Apply maximum leverage constraint
const constrainedFraction = Math.max(0, Math.min(kellyFraction, maxLeverage));
// Further cap at 100% of account for safety
const finalFraction = Math.min(constrainedFraction, 1);
return accountSize * finalFraction;
return accountSize * constrainedFraction;
}
/**
@ -405,14 +401,12 @@ export function riskParityPositionSize(
return assets.map(asset => {
if (asset.volatility === 0 || asset.price === 0) return 0;
// Calculate weight based on inverse volatility
// Calculate weight based on inverse volatility
const weight = (1 / asset.volatility) / totalInverseVol;
// Scale by target risk
const riskAdjustedWeight = weight * (targetRisk / asset.volatility);
const positionValue = accountSize * riskAdjustedWeight;
// The weight itself already accounts for risk parity
// We just need to scale by target risk once
const positionValue = accountSize * weight * targetRisk;
return Math.floor(positionValue / asset.price);
});
}