commit 2 refactor
This commit is contained in:
parent
588f666943
commit
32e43c9243
310 changed files with 33318 additions and 31189 deletions
|
|
@ -1,3 +1,4 @@
|
|||
import { defined } from '@stock-bot/invariants';
|
||||
/**
|
||||
* Generic utility functions that work with standardized types
|
||||
* These functions demonstrate how to use generic types with OHLCV data
|
||||
|
|
@ -69,10 +70,10 @@ export function calculateTrueRange<T extends HasOHLC>(data: T[]): number[] {
|
|||
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
if (i === 0) {
|
||||
result.push(data[i]!.high - data[i]!.low);
|
||||
result.push(defined(data[i]).high - defined(data[i]).low);
|
||||
} else {
|
||||
const current = data[i]!;
|
||||
const previous = data[i - 1]!;
|
||||
const current = defined(data[i]);
|
||||
const previous = defined(data[i - 1]);
|
||||
const tr = Math.max(
|
||||
current.high - current.low,
|
||||
Math.abs(current.high - previous.close),
|
||||
|
|
@ -93,8 +94,8 @@ export function calculateReturns<T extends HasClose>(data: T[]): number[] {
|
|||
const returns: number[] = [];
|
||||
|
||||
for (let i = 1; i < closes.length; i++) {
|
||||
const current = closes[i]!;
|
||||
const previous = closes[i - 1]!;
|
||||
const current = defined(closes[i]);
|
||||
const previous = defined(closes[i - 1]);
|
||||
if (previous > 0) {
|
||||
returns.push((current - previous) / previous);
|
||||
} else {
|
||||
|
|
@ -113,8 +114,8 @@ export function calculateLogReturns<T extends HasClose>(data: T[]): number[] {
|
|||
const logReturns: number[] = [];
|
||||
|
||||
for (let i = 1; i < closes.length; i++) {
|
||||
const current = closes[i]!;
|
||||
const previous = closes[i - 1]!;
|
||||
const current = defined(closes[i]);
|
||||
const previous = defined(closes[i - 1]);
|
||||
if (previous > 0 && current > 0) {
|
||||
logReturns.push(Math.log(current / previous));
|
||||
} else {
|
||||
|
|
@ -172,7 +173,7 @@ export function groupBySymbol(data: OHLCV[]): Record<string, OHLCV[]> {
|
|||
if (!grouped[item.symbol]) {
|
||||
grouped[item.symbol] = [];
|
||||
}
|
||||
grouped[item.symbol]!.push(item);
|
||||
defined(grouped[item.symbol]).push(item);
|
||||
}
|
||||
|
||||
return grouped;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue