fixed up types and working on utils lib
This commit is contained in:
parent
25d9f2dd85
commit
da75979574
17 changed files with 1093 additions and 901 deletions
|
|
@ -95,8 +95,11 @@ export function internalRateOfReturn(
|
|||
let dnpv = 0;
|
||||
|
||||
for (let j = 0; j < cashFlows.length; j++) {
|
||||
npv += cashFlows[j] / Math.pow(1 + rate, j);
|
||||
dnpv += (-j * cashFlows[j]) / Math.pow(1 + rate, j + 1);
|
||||
const cashFlow = cashFlows[j];
|
||||
if (cashFlow !== undefined) {
|
||||
npv += cashFlow / Math.pow(1 + rate, j);
|
||||
dnpv += (-j * cashFlow) / Math.pow(1 + rate, j + 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (Math.abs(npv) < 1e-10) {
|
||||
|
|
@ -119,9 +122,12 @@ export function paybackPeriod(initialInvestment: number, cashFlows: number[]): n
|
|||
let cumulativeCashFlow = 0;
|
||||
|
||||
for (let i = 0; i < cashFlows.length; i++) {
|
||||
cumulativeCashFlow += cashFlows[i];
|
||||
if (cumulativeCashFlow >= initialInvestment) {
|
||||
return i + 1 - (cumulativeCashFlow - initialInvestment) / cashFlows[i];
|
||||
const cashFlow = cashFlows[i];
|
||||
if (cashFlow !== undefined) {
|
||||
cumulativeCashFlow += cashFlow;
|
||||
if (cumulativeCashFlow >= initialInvestment) {
|
||||
return i + 1 - (cumulativeCashFlow - initialInvestment) / cashFlow;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -65,7 +65,10 @@ export function pearsonCorrelation(x: number[], y: number[]): CorrelationResult
|
|||
const n = x.length;
|
||||
const sumX = x.reduce((a, b) => a + b, 0);
|
||||
const sumY = y.reduce((a, b) => a + b, 0);
|
||||
const sumXY = x.reduce((sum, xi, i) => sum + xi * y[i], 0);
|
||||
const sumXY = x.reduce((sum, xi, i) => {
|
||||
const yi = y[i];
|
||||
return yi !== undefined ? sum + xi * yi : sum;
|
||||
}, 0);
|
||||
const sumX2 = x.reduce((sum, xi) => sum + xi * xi, 0);
|
||||
const sumY2 = y.reduce((sum, yi) => sum + yi * yi, 0);
|
||||
|
||||
|
|
@ -127,13 +130,20 @@ export function kendallTau(x: number[], y: number[]): CorrelationResult {
|
|||
|
||||
for (let i = 0; i < n - 1; i++) {
|
||||
for (let j = i + 1; j < n; j++) {
|
||||
const xDiff = x[i] - x[j];
|
||||
const yDiff = 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) {
|
||||
const xDiff = xi - xj;
|
||||
const yDiff = yi - yj;
|
||||
|
||||
if (xDiff * yDiff > 0) {
|
||||
concordant++;
|
||||
} else if (xDiff * yDiff < 0) {
|
||||
discordant++;
|
||||
if (xDiff * yDiff > 0) {
|
||||
concordant++;
|
||||
} else if (xDiff * yDiff < 0) {
|
||||
discordant++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,98 +25,40 @@ import {
|
|||
* Organized into logical categories for easy use and maintenance.
|
||||
*/
|
||||
|
||||
// Re-export standardized types
|
||||
export type { OHLCV, OHLCVWithMetadata } from '@stock-bot/types';
|
||||
|
||||
// Legacy interface for backward compatibility - prefer OHLCV from @stock-bot/types
|
||||
/** @deprecated Use OHLCV from @stock-bot/types instead */
|
||||
export interface OHLCVData {
|
||||
open: number;
|
||||
high: number;
|
||||
low: number;
|
||||
close: number;
|
||||
volume: number;
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
export interface PriceData {
|
||||
price: number;
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
// Financial calculation result interfaces
|
||||
export interface PortfolioMetrics {
|
||||
totalValue: number;
|
||||
totalReturn: number;
|
||||
totalReturnPercent: number;
|
||||
dailyReturn: number;
|
||||
dailyReturnPercent: number;
|
||||
maxDrawdown: number;
|
||||
sharpeRatio: number;
|
||||
beta: number;
|
||||
alpha: number;
|
||||
volatility: number;
|
||||
}
|
||||
|
||||
export interface RiskMetrics {
|
||||
var95: number; // Value at Risk 95%
|
||||
var99: number; // Value at Risk 99%
|
||||
cvar95: number; // Conditional VaR 95%
|
||||
maxDrawdown: number;
|
||||
volatility: number;
|
||||
downside_deviation: number;
|
||||
calmar_ratio: number;
|
||||
sortino_ratio: number;
|
||||
beta: number;
|
||||
alpha: number;
|
||||
sharpeRatio: number;
|
||||
treynorRatio: number;
|
||||
trackingError: number;
|
||||
informationRatio: number;
|
||||
}
|
||||
|
||||
export interface TechnicalIndicators {
|
||||
sma: number[];
|
||||
ema: number[];
|
||||
rsi: number[];
|
||||
macd: { macd: number[]; signal: number[]; histogram: number[] };
|
||||
bollinger: { upper: number[]; middle: number[]; lower: number[] };
|
||||
atr: number[];
|
||||
stochastic: { k: number[]; d: number[] };
|
||||
williams_r: number[];
|
||||
cci: number[];
|
||||
momentum: number[];
|
||||
roc: number[];
|
||||
}
|
||||
|
||||
// Additional interfaces for new functionality
|
||||
export interface TradeExecution {
|
||||
entry: number;
|
||||
exit: number;
|
||||
peak?: number;
|
||||
trough?: number;
|
||||
volume: number;
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
export interface MarketData {
|
||||
price: number;
|
||||
volume: number;
|
||||
timestamp: Date;
|
||||
bid?: number;
|
||||
ask?: number;
|
||||
bidSize?: number;
|
||||
askSize?: number;
|
||||
}
|
||||
|
||||
export interface BacktestResults {
|
||||
trades: TradeExecution[];
|
||||
equityCurve: Array<{ value: number; date: Date }>;
|
||||
|
||||
performance: PortfolioMetrics;
|
||||
riskMetrics: RiskMetrics;
|
||||
drawdownAnalysis: any; // Import from performance-metrics
|
||||
}
|
||||
// Re-export all standardized types from @stock-bot/types
|
||||
export type {
|
||||
OHLCV,
|
||||
OHLCVWithMetadata,
|
||||
PortfolioPosition,
|
||||
PortfolioAnalysis,
|
||||
AssetAllocation,
|
||||
TradeExecution,
|
||||
TradePerformance,
|
||||
RiskMetrics,
|
||||
DrawdownAnalysis,
|
||||
ReturnAnalysis,
|
||||
OptionParameters,
|
||||
OptionPricing,
|
||||
GreeksCalculation,
|
||||
MarketData,
|
||||
LiquidityMetrics,
|
||||
MarketRegime,
|
||||
PositionSizeParams,
|
||||
KellyParams,
|
||||
BalanceSheet,
|
||||
IncomeStatement,
|
||||
CashFlowStatement,
|
||||
TechnicalIndicators,
|
||||
CorrelationResult,
|
||||
CorrelationMatrix,
|
||||
VolatilityEstimates,
|
||||
GARCHParameters,
|
||||
BacktestResults,
|
||||
HasClose,
|
||||
HasOHLC,
|
||||
HasVolume,
|
||||
HasTimestamp
|
||||
} from '@stock-bot/types';
|
||||
|
||||
// Export all calculation functions
|
||||
export * from './basic-calculations';
|
||||
|
|
@ -132,7 +74,7 @@ export * from './correlation-analysis';
|
|||
|
||||
// Convenience function to calculate all technical indicators at once
|
||||
export function calculateAllTechnicalIndicators(
|
||||
ohlcv: OHLCVData[],
|
||||
ohlcv: OHLCV[],
|
||||
periods: { sma?: number; ema?: number; rsi?: number; atr?: number } = {}
|
||||
): TechnicalIndicators {
|
||||
const {
|
||||
|
|
@ -166,7 +108,7 @@ export function analyzePortfolio(
|
|||
benchmarkReturns?: number[],
|
||||
riskFreeRate: number = 0.02
|
||||
): {
|
||||
performance: PortfolioMetrics;
|
||||
performance: PortfolioAnalysis;
|
||||
risk: RiskMetrics;
|
||||
trades?: any;
|
||||
drawdown?: any;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue