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 i = 0; i < n; i++) {
|
||||||
for (let j = 0; j < n; j++) {
|
for (let j = 0; j < n; j++) {
|
||||||
if (i === j) {
|
if (i === j) {
|
||||||
matrix[i][j] = 1;
|
matrix[i]![j] = 1;
|
||||||
} else {
|
} else {
|
||||||
let corrResult: CorrelationResult;
|
const dataI = data[i];
|
||||||
switch (method) {
|
const dataJ = data[j];
|
||||||
case 'spearman':
|
if (dataI && dataJ) {
|
||||||
corrResult = spearmanCorrelation(data[i], data[j]);
|
let corrResult: CorrelationResult;
|
||||||
break;
|
switch (method) {
|
||||||
case 'kendall':
|
case 'spearman':
|
||||||
corrResult = kendallTau(data[i], data[j]);
|
corrResult = spearmanCorrelation(dataI, dataJ);
|
||||||
break;
|
break;
|
||||||
default:
|
case 'kendall':
|
||||||
corrResult = pearsonCorrelation(data[i], data[j]);
|
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 i = 0; i < n; i++) {
|
||||||
for (let j = 0; j < n; j++) {
|
for (let j = 0; j < n; j++) {
|
||||||
a[i][j] = Math.abs(x[i] - x[j]);
|
const xi = x[i];
|
||||||
b[i][j] = Math.abs(y[i] - y[j]);
|
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.
|
* This module provides a core set of financial calculations for trading and investment analysis.
|
||||||
* Organized into logical categories for easy use and maintenance.
|
* Focuses on basic calculations and standardized type exports.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Re-export all standardized types from @stock-bot/types
|
// Re-export all standardized types from @stock-bot/types
|
||||||
|
|
@ -60,65 +40,39 @@ export type {
|
||||||
HasTimestamp
|
HasTimestamp
|
||||||
} from '@stock-bot/types';
|
} from '@stock-bot/types';
|
||||||
|
|
||||||
// Export all calculation functions
|
// Export working calculation functions
|
||||||
export * from './basic-calculations';
|
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 working technical indicators (building one by one)
|
||||||
export function calculateAllTechnicalIndicators(
|
export { sma, ema, rsi, macd, bollingerBands, atr, obv, stochastic } from './technical-indicators';
|
||||||
ohlcv: OHLCV[],
|
// export * from './risk-metrics';
|
||||||
periods: { sma?: number; ema?: number; rsi?: number; atr?: number } = {}
|
// export * from './portfolio-analytics';
|
||||||
): TechnicalIndicators {
|
// export * from './options-pricing';
|
||||||
const {
|
// export * from './position-sizing';
|
||||||
sma: smaPeriod = 20,
|
// export * from './performance-metrics';
|
||||||
ema: emaPeriod = 20,
|
// export * from './market-statistics';
|
||||||
rsi: rsiPeriod = 14,
|
// export * from './volatility-models';
|
||||||
atr: atrPeriod = 14,
|
// export * from './correlation-analysis';
|
||||||
} = periods;
|
|
||||||
|
|
||||||
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 {
|
// return {
|
||||||
sma: sma(closes, smaPeriod),
|
// performance,
|
||||||
ema: ema(closes, emaPeriod),
|
// risk,
|
||||||
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,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
|
||||||
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