@tradejs/node 1.0.8 → 1.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,154 +0,0 @@
1
- import {
2
- getStrategyManifest
3
- } from "./chunk-WGOYR6AB.mjs";
4
-
5
- // src/strategyHelpers/derivativesContext.ts
6
- import {
7
- buildDerivativesContext,
8
- normalizeDerivativesIntervals
9
- } from "@tradejs/core/indicators";
10
- import { DERIVATIVES_CONTEXT_REFERENCE_SYMBOLS } from "@tradejs/core/constants";
11
- import { getDerivativesWindow } from "@tradejs/infra/timescale";
12
- import { logger } from "@tradejs/infra/logger";
13
- var DEFAULT_INTERVALS = ["15m", "1h"];
14
- var DEFAULT_LOOKBACK_HOURS = 48;
15
- var derivativesContextUnavailable = false;
16
- var parseEnabledFlag = (value, env) => {
17
- const normalized = String(value ?? "").trim().toLowerCase();
18
- if (!normalized) return false;
19
- if (["1", "true", "yes", "on"].includes(normalized)) return true;
20
- if (normalized === "backtest") return env === "BACKTEST";
21
- if (normalized === "live") return env !== "BACKTEST";
22
- return false;
23
- };
24
- var parseLookbackMs = () => {
25
- const hours = Number(process.env.DERIVATIVES_CONTEXT_LOOKBACK_HOURS);
26
- const normalizedHours = Number.isFinite(hours) && hours > 0 ? hours : DEFAULT_LOOKBACK_HOURS;
27
- return normalizedHours * 60 * 60 * 1e3;
28
- };
29
- var parseIntervals = () => {
30
- const fromEnv = normalizeDerivativesIntervals(
31
- process.env.DERIVATIVES_CONTEXT_INTERVALS
32
- );
33
- return fromEnv.length ? fromEnv : DEFAULT_INTERVALS;
34
- };
35
- var getDerivativesContextReferenceSymbols = () => [
36
- ...DERIVATIVES_CONTEXT_REFERENCE_SYMBOLS
37
- ];
38
- var normalizeSymbol = (symbol) => String(symbol || "").trim().toUpperCase();
39
- var resolvePrimaryReferenceSymbol = (signalSymbol) => {
40
- const symbol = normalizeSymbol(signalSymbol);
41
- const referenceSymbols = getDerivativesContextReferenceSymbols();
42
- return referenceSymbols.some((referenceSymbol) => referenceSymbol === symbol) ? symbol : referenceSymbols[0];
43
- };
44
- var buildReferenceDerivativesContext = (params) => {
45
- const { targetSymbol, primaryReferenceSymbol, referenceContexts } = params;
46
- const primaryContext = referenceContexts[primaryReferenceSymbol] ?? referenceContexts[getDerivativesContextReferenceSymbols()[0]];
47
- if (!primaryContext) {
48
- throw new Error("No derivatives reference contexts built");
49
- }
50
- return {
51
- ...primaryContext,
52
- targetSymbol,
53
- primaryReferenceSymbol: primaryContext.symbol,
54
- referenceSymbols: getDerivativesContextReferenceSymbols(),
55
- referenceContexts
56
- };
57
- };
58
- var isDerivativesContextEnabled = (env) => parseEnabledFlag(process.env.DERIVATIVES_CONTEXT_ENABLED, env);
59
- var enrichSignalWithDerivativesContext = async (params) => {
60
- const { signal, env, enabled = isDerivativesContextEnabled(env) } = params;
61
- if (!enabled || derivativesContextUnavailable) {
62
- return false;
63
- }
64
- try {
65
- const intervals = parseIntervals();
66
- const referenceSymbols = getDerivativesContextReferenceSymbols();
67
- const lookbackMs = parseLookbackMs();
68
- const contexts = await Promise.all(
69
- referenceSymbols.map(async (symbol) => {
70
- const rowsByInterval = await getDerivativesWindow({
71
- symbol,
72
- intervals,
73
- endMs: signal.timestamp,
74
- lookbackMs
75
- });
76
- return [
77
- symbol,
78
- buildDerivativesContext({
79
- symbol,
80
- direction: signal.direction,
81
- timestamp: signal.timestamp,
82
- rowsByInterval,
83
- intervals
84
- })
85
- ];
86
- })
87
- );
88
- const referenceContexts = Object.fromEntries(contexts);
89
- const derivativesContext = buildReferenceDerivativesContext({
90
- targetSymbol: signal.symbol,
91
- primaryReferenceSymbol: resolvePrimaryReferenceSymbol(signal.symbol),
92
- referenceContexts
93
- });
94
- signal.additionalIndicators = {
95
- ...signal.additionalIndicators ?? {},
96
- derivativesContext
97
- };
98
- return true;
99
- } catch (error) {
100
- derivativesContextUnavailable = true;
101
- logger.warn(
102
- "Derivatives context disabled after Timescale read failure: %s",
103
- String(error)
104
- );
105
- return false;
106
- }
107
- };
108
-
109
- // src/strategyAdapters/ml.ts
110
- var defaultMlAdapter = {
111
- normalizeStrategyConfig: (strategyConfig) => strategyConfig
112
- };
113
- var getStrategyMlAdapter = (strategy) => {
114
- const strategyAdapter = getStrategyManifest(strategy)?.mlAdapter;
115
- if (!strategyAdapter) return defaultMlAdapter;
116
- return {
117
- ...defaultMlAdapter,
118
- ...strategyAdapter
119
- };
120
- };
121
-
122
- // src/mlPayload.ts
123
- var normalizeStrategyConfig = (strategyConfig, strategyName) => {
124
- return getStrategyMlAdapter(strategyName).normalizeStrategyConfig?.(
125
- strategyConfig
126
- );
127
- };
128
- var buildMlPayload = (payload) => {
129
- const strategyName = payload.signal?.strategy ?? payload.context?.strategyName;
130
- const mlAdapter = getStrategyMlAdapter(strategyName);
131
- const normalizedSignal = mlAdapter.normalizeSignal?.(payload.signal) ?? payload.signal;
132
- const nextSignal = {
133
- ...normalizedSignal,
134
- indicators: {
135
- ...normalizedSignal?.indicators ?? {}
136
- }
137
- };
138
- const nextContext = payload.context ? {
139
- ...payload.context,
140
- strategyConfig: normalizeStrategyConfig(
141
- payload.context.strategyConfig,
142
- strategyName
143
- )
144
- } : void 0;
145
- return {
146
- signal: nextSignal,
147
- context: nextContext
148
- };
149
- };
150
-
151
- export {
152
- enrichSignalWithDerivativesContext,
153
- buildMlPayload
154
- };