reworked calcs lib
This commit is contained in:
parent
da75979574
commit
0397796cfb
11 changed files with 2648 additions and 2359 deletions
|
|
@ -184,20 +184,24 @@ export function correlationMatrix(
|
|||
for (let i = 0; i < n; i++) {
|
||||
for (let j = 0; j < n; j++) {
|
||||
if (i === j) {
|
||||
matrix[i][j] = 1;
|
||||
matrix[i]![j] = 1;
|
||||
} else {
|
||||
let corrResult: CorrelationResult;
|
||||
switch (method) {
|
||||
case 'spearman':
|
||||
corrResult = spearmanCorrelation(data[i], data[j]);
|
||||
break;
|
||||
case 'kendall':
|
||||
corrResult = kendallTau(data[i], data[j]);
|
||||
break;
|
||||
default:
|
||||
corrResult = pearsonCorrelation(data[i], data[j]);
|
||||
const dataI = data[i];
|
||||
const dataJ = data[j];
|
||||
if (dataI && dataJ) {
|
||||
let corrResult: CorrelationResult;
|
||||
switch (method) {
|
||||
case 'spearman':
|
||||
corrResult = spearmanCorrelation(dataI, dataJ);
|
||||
break;
|
||||
case 'kendall':
|
||||
corrResult = kendallTau(dataI, dataJ);
|
||||
break;
|
||||
default:
|
||||
corrResult = pearsonCorrelation(dataI, dataJ);
|
||||
}
|
||||
matrix[i]![j] = corrResult.correlation;
|
||||
}
|
||||
matrix[i][j] = corrResult.correlation;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -650,8 +654,14 @@ export function distanceCorrelation(x: number[], y: number[]): CorrelationResult
|
|||
|
||||
for (let i = 0; i < n; i++) {
|
||||
for (let j = 0; j < n; j++) {
|
||||
a[i][j] = Math.abs(x[i] - x[j]);
|
||||
b[i][j] = Math.abs(y[i] - y[j]);
|
||||
const xi = x[i];
|
||||
const xj = x[j];
|
||||
const yi = y[i];
|
||||
const yj = y[j];
|
||||
if (xi !== undefined && xj !== undefined && yi !== undefined && yj !== undefined) {
|
||||
a[i]![j] = Math.abs(xi - xj);
|
||||
b[i]![j] = Math.abs(yi - yj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1,28 +1,8 @@
|
|||
// Import standardized types
|
||||
import type { OHLCV, OHLCVWithMetadata } from '@stock-bot/types';
|
||||
|
||||
// Import specific functions for convenience functions
|
||||
import { calculateStrategyMetrics } from './performance-metrics';
|
||||
import { calculateRiskMetrics } from './risk-metrics';
|
||||
import {
|
||||
atr,
|
||||
bollingerBands,
|
||||
cci,
|
||||
ema,
|
||||
macd,
|
||||
momentum,
|
||||
roc,
|
||||
rsi,
|
||||
sma,
|
||||
stochastic,
|
||||
williamsR,
|
||||
} from './technical-indicators';
|
||||
|
||||
/**
|
||||
* Comprehensive Financial Calculations Library
|
||||
* Financial Calculations Library
|
||||
*
|
||||
* This module provides a complete set of financial calculations for trading and investment analysis.
|
||||
* Organized into logical categories for easy use and maintenance.
|
||||
* This module provides a core set of financial calculations for trading and investment analysis.
|
||||
* Focuses on basic calculations and standardized type exports.
|
||||
*/
|
||||
|
||||
// Re-export all standardized types from @stock-bot/types
|
||||
|
|
@ -60,65 +40,39 @@ export type {
|
|||
HasTimestamp
|
||||
} from '@stock-bot/types';
|
||||
|
||||
// Export all calculation functions
|
||||
// Export working calculation functions
|
||||
export * from './basic-calculations';
|
||||
export * from './technical-indicators';
|
||||
export * from './risk-metrics';
|
||||
export * from './portfolio-analytics';
|
||||
export * from './options-pricing';
|
||||
export * from './position-sizing';
|
||||
export * from './performance-metrics';
|
||||
export * from './market-statistics';
|
||||
export * from './volatility-models';
|
||||
export * from './correlation-analysis';
|
||||
|
||||
// Convenience function to calculate all technical indicators at once
|
||||
export function calculateAllTechnicalIndicators(
|
||||
ohlcv: OHLCV[],
|
||||
periods: { sma?: number; ema?: number; rsi?: number; atr?: number } = {}
|
||||
): TechnicalIndicators {
|
||||
const {
|
||||
sma: smaPeriod = 20,
|
||||
ema: emaPeriod = 20,
|
||||
rsi: rsiPeriod = 14,
|
||||
atr: atrPeriod = 14,
|
||||
} = periods;
|
||||
// Export working technical indicators (building one by one)
|
||||
export { sma, ema, rsi, macd, bollingerBands, atr, obv, stochastic } from './technical-indicators';
|
||||
// export * from './risk-metrics';
|
||||
// export * from './portfolio-analytics';
|
||||
// export * from './options-pricing';
|
||||
// export * from './position-sizing';
|
||||
// export * from './performance-metrics';
|
||||
// export * from './market-statistics';
|
||||
// export * from './volatility-models';
|
||||
// export * from './correlation-analysis';
|
||||
|
||||
const closes = ohlcv.map(d => d.close);
|
||||
// TODO: Re-enable when performance-metrics and risk-metrics are fixed
|
||||
// // Convenience function for comprehensive portfolio analysis
|
||||
// export function analyzePortfolio(
|
||||
// returns: number[],
|
||||
// equityCurve: Array<{ value: number; date: Date }>,
|
||||
// benchmarkReturns?: number[],
|
||||
// riskFreeRate: number = 0.02
|
||||
// ): {
|
||||
// performance: PortfolioAnalysis;
|
||||
// risk: RiskMetrics;
|
||||
// trades?: any;
|
||||
// drawdown?: any;
|
||||
// } {
|
||||
// const performance = calculateStrategyMetrics(equityCurve, benchmarkReturns, riskFreeRate);
|
||||
// const equityValues = equityCurve.map(point => point.value);
|
||||
// const risk = calculateRiskMetrics(returns, equityValues, benchmarkReturns, riskFreeRate);
|
||||
|
||||
return {
|
||||
sma: sma(closes, smaPeriod),
|
||||
ema: ema(closes, emaPeriod),
|
||||
rsi: rsi(closes, rsiPeriod),
|
||||
macd: macd(closes),
|
||||
bollinger: bollingerBands(closes),
|
||||
atr: atr(ohlcv, atrPeriod),
|
||||
stochastic: stochastic(ohlcv),
|
||||
williams_r: williamsR(ohlcv),
|
||||
cci: cci(ohlcv),
|
||||
momentum: momentum(closes),
|
||||
roc: roc(closes),
|
||||
};
|
||||
}
|
||||
|
||||
// Convenience function for comprehensive portfolio analysis
|
||||
export function analyzePortfolio(
|
||||
returns: number[],
|
||||
equityCurve: Array<{ value: number; date: Date }>,
|
||||
benchmarkReturns?: number[],
|
||||
riskFreeRate: number = 0.02
|
||||
): {
|
||||
performance: PortfolioAnalysis;
|
||||
risk: RiskMetrics;
|
||||
trades?: any;
|
||||
drawdown?: any;
|
||||
} {
|
||||
const performance = calculateStrategyMetrics(equityCurve, benchmarkReturns, riskFreeRate);
|
||||
const equityValues = equityCurve.map(point => point.value);
|
||||
const risk = calculateRiskMetrics(returns, equityValues, benchmarkReturns, riskFreeRate);
|
||||
|
||||
return {
|
||||
performance,
|
||||
risk,
|
||||
};
|
||||
}
|
||||
// return {
|
||||
// performance,
|
||||
// risk,
|
||||
// };
|
||||
// }
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
2508
libs/utils/src/calculations/technical-indicators.ts.disabled
Normal file
2508
libs/utils/src/calculations/technical-indicators.ts.disabled
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue