@tradejs/strategy-trading-patterns 0.0.0-stage → 0.1.1-beta.1
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.
- package/LICENSE +96 -0
- package/README.md +92 -2
- package/dist/index.cjs +560 -0
- package/dist/index.d.cts +72 -0
- package/dist/index.d.ts +72 -0
- package/dist/index.js +532 -0
- package/package.json +82 -4
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,560 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
PATTERN_NAMES: () => PATTERN_NAMES,
|
|
24
|
+
TradingPatternsStrategyDefinition: () => TradingPatternsStrategyDefinition,
|
|
25
|
+
createTradingPatternsCore: () => createTradingPatternsCore,
|
|
26
|
+
createTradingPatternsCoreWithDefinitions: () => createTradingPatternsCoreWithDefinitions,
|
|
27
|
+
default: () => index_default,
|
|
28
|
+
getBuiltInStrategyDefaultConfig: () => getBuiltInStrategyDefaultConfig,
|
|
29
|
+
parseTradingPatternsConfig: () => parseTradingPatternsConfig,
|
|
30
|
+
patternDefinitions: () => patternDefinitions,
|
|
31
|
+
strategyEntries: () => strategyEntries,
|
|
32
|
+
tradingPatternsDefaultConfig: () => config,
|
|
33
|
+
tradingPatternsManifest: () => tradingPatternsManifest
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
var import_config5 = require("@tradejs/core/config");
|
|
37
|
+
|
|
38
|
+
// src/TradingPatterns/config.ts
|
|
39
|
+
var import_config = require("@tradejs/strategy-kit/config");
|
|
40
|
+
|
|
41
|
+
// src/TradingPatterns/patterns.ts
|
|
42
|
+
var import_strategy_diamond = require("@tradejs/strategy-diamond");
|
|
43
|
+
var import_strategy_flag = require("@tradejs/strategy-flag");
|
|
44
|
+
var import_strategy_gartley = require("@tradejs/strategy-gartley");
|
|
45
|
+
var import_strategy_head_and_shoulders = require("@tradejs/strategy-head-and-shoulders");
|
|
46
|
+
var patternDefinitions = [
|
|
47
|
+
{
|
|
48
|
+
name: "Diamond",
|
|
49
|
+
code: "DIAMOND",
|
|
50
|
+
definition: import_strategy_diamond.DiamondStrategyDefinition
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
name: "Flag",
|
|
54
|
+
code: "FLAG",
|
|
55
|
+
definition: import_strategy_flag.FlagStrategyDefinition
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
name: "Gartley",
|
|
59
|
+
code: "GARTLEY",
|
|
60
|
+
definition: import_strategy_gartley.GartleyStrategyDefinition
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
name: "HeadAndShoulders",
|
|
64
|
+
code: "HEAD_AND_SHOULDERS",
|
|
65
|
+
definition: import_strategy_head_and_shoulders.HeadAndShouldersStrategyDefinition
|
|
66
|
+
}
|
|
67
|
+
];
|
|
68
|
+
var PATTERN_NAMES = patternDefinitions.map(({ name }) => name);
|
|
69
|
+
var patternDefinitionByName = new Map(patternDefinitions.map((pattern) => [pattern.name, pattern]));
|
|
70
|
+
|
|
71
|
+
// src/TradingPatterns/config.ts
|
|
72
|
+
var cloneValue = (value) => {
|
|
73
|
+
if (Array.isArray(value)) return value.map(cloneValue);
|
|
74
|
+
if (typeof value === "object" && value !== null) {
|
|
75
|
+
return Object.fromEntries(
|
|
76
|
+
Object.entries(value).map(([key, nested]) => [key, cloneValue(nested)])
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
return value;
|
|
80
|
+
};
|
|
81
|
+
var serialized = (value) => JSON.stringify(value);
|
|
82
|
+
var mergePatternDefaults = () => {
|
|
83
|
+
const merged = {};
|
|
84
|
+
const ownerByKey = /* @__PURE__ */ new Map();
|
|
85
|
+
for (const pattern of patternDefinitions) {
|
|
86
|
+
for (const [key, value] of Object.entries(pattern.definition.defaults)) {
|
|
87
|
+
if (key === "INTERVAL" || key === "LONG" || key === "SHORT") continue;
|
|
88
|
+
if (key in merged && serialized(merged[key]) !== serialized(value)) {
|
|
89
|
+
const firstOwner = ownerByKey.get(key);
|
|
90
|
+
throw new Error(
|
|
91
|
+
`Conflicting default ${key} in ${String(firstOwner)} and ${pattern.name}`
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
merged[key] = cloneValue(value);
|
|
95
|
+
ownerByKey.set(key, pattern.name);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return merged;
|
|
99
|
+
};
|
|
100
|
+
var referenceSides = patternDefinitions[0].definition.defaults;
|
|
101
|
+
var patternDefaults = mergePatternDefaults();
|
|
102
|
+
var createPatternToggles = () => Object.fromEntries(
|
|
103
|
+
patternDefinitions.map(({ name, definition }) => {
|
|
104
|
+
const defaults = definition.defaults;
|
|
105
|
+
const long = defaults.LONG;
|
|
106
|
+
const short = defaults.SHORT;
|
|
107
|
+
return [
|
|
108
|
+
name,
|
|
109
|
+
{
|
|
110
|
+
enable: true,
|
|
111
|
+
LONG: {
|
|
112
|
+
enable: long?.enable !== false,
|
|
113
|
+
minRiskRatio: Number(long?.minRiskRatio ?? 0.7)
|
|
114
|
+
},
|
|
115
|
+
SHORT: {
|
|
116
|
+
enable: short?.enable !== false,
|
|
117
|
+
minRiskRatio: Number(short?.minRiskRatio ?? 0.7)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
];
|
|
121
|
+
})
|
|
122
|
+
);
|
|
123
|
+
var config = {
|
|
124
|
+
...patternDefaults,
|
|
125
|
+
INTERVAL: "60",
|
|
126
|
+
AI_ENABLED: patternDefaults.AI_ENABLED,
|
|
127
|
+
AI_MODE: patternDefaults.AI_MODE,
|
|
128
|
+
MIN_AI_QUALITY: patternDefaults.MIN_AI_QUALITY,
|
|
129
|
+
TRADING_PATTERNS_PRIORITY: [...PATTERN_NAMES],
|
|
130
|
+
TRADING_PATTERNS: createPatternToggles(),
|
|
131
|
+
LONG: {
|
|
132
|
+
...cloneValue(referenceSides.LONG),
|
|
133
|
+
minRiskRatio: 0
|
|
134
|
+
},
|
|
135
|
+
SHORT: {
|
|
136
|
+
...cloneValue(referenceSides.SHORT),
|
|
137
|
+
minRiskRatio: 0
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
var baseParseConfig = (0, import_config.createCostIsolatedStrategyConfigParser)({
|
|
141
|
+
strategyName: "TradingPatterns",
|
|
142
|
+
defaults: config
|
|
143
|
+
});
|
|
144
|
+
var validatePriority = (priority) => {
|
|
145
|
+
const issues = [];
|
|
146
|
+
const known = new Set(PATTERN_NAMES);
|
|
147
|
+
const received = /* @__PURE__ */ new Set();
|
|
148
|
+
priority.forEach((name, index) => {
|
|
149
|
+
if (!known.has(name)) {
|
|
150
|
+
issues.push(
|
|
151
|
+
`TradingPatterns.TRADING_PATTERNS_PRIORITY[${index}] contains unknown pattern ${String(name)}`
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
if (received.has(name)) {
|
|
155
|
+
issues.push(
|
|
156
|
+
`TradingPatterns.TRADING_PATTERNS_PRIORITY contains duplicate pattern ${String(name)}`
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
received.add(name);
|
|
160
|
+
});
|
|
161
|
+
for (const name of PATTERN_NAMES) {
|
|
162
|
+
if (!received.has(name)) {
|
|
163
|
+
issues.push(
|
|
164
|
+
`TradingPatterns.TRADING_PATTERNS_PRIORITY is missing pattern ${name}`
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return issues;
|
|
169
|
+
};
|
|
170
|
+
var parseTradingPatternsConfig = (input) => {
|
|
171
|
+
const parsed = baseParseConfig(input);
|
|
172
|
+
const issues = validatePriority(parsed.TRADING_PATTERNS_PRIORITY);
|
|
173
|
+
if (parsed.INTERVAL !== "60") {
|
|
174
|
+
issues.push("TradingPatterns.INTERVAL must be 60 (1h)");
|
|
175
|
+
}
|
|
176
|
+
if (!Object.values(parsed.TRADING_PATTERNS).some((pattern) => pattern.enable)) {
|
|
177
|
+
issues.push(
|
|
178
|
+
"TradingPatterns.TRADING_PATTERNS must enable at least one pattern"
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
if (issues.length > 0) {
|
|
182
|
+
throw new import_config.StrategyConfigValidationError("TradingPatterns", issues);
|
|
183
|
+
}
|
|
184
|
+
return parsed;
|
|
185
|
+
};
|
|
186
|
+
var buildSideConfig = ({
|
|
187
|
+
direction,
|
|
188
|
+
definition,
|
|
189
|
+
config: tradingPatternsConfig,
|
|
190
|
+
toggle
|
|
191
|
+
}) => {
|
|
192
|
+
const defaults = definition.defaults;
|
|
193
|
+
const defaultSide = defaults[direction];
|
|
194
|
+
const sharedSide = tradingPatternsConfig[direction];
|
|
195
|
+
return {
|
|
196
|
+
...defaultSide,
|
|
197
|
+
...sharedSide,
|
|
198
|
+
enable: sharedSide.enable && toggle[direction].enable,
|
|
199
|
+
minRiskRatio: Math.max(
|
|
200
|
+
sharedSide.minRiskRatio,
|
|
201
|
+
toggle[direction].minRiskRatio
|
|
202
|
+
),
|
|
203
|
+
direction
|
|
204
|
+
};
|
|
205
|
+
};
|
|
206
|
+
var buildChildConfig = ({
|
|
207
|
+
pattern,
|
|
208
|
+
config: tradingPatternsConfig
|
|
209
|
+
}) => {
|
|
210
|
+
const toggle = tradingPatternsConfig.TRADING_PATTERNS[pattern.name];
|
|
211
|
+
const input = Object.fromEntries(
|
|
212
|
+
Object.keys(pattern.definition.defaults).map((key) => {
|
|
213
|
+
if (key === "INTERVAL") return [key, "60"];
|
|
214
|
+
if (key === "LONG") {
|
|
215
|
+
return [
|
|
216
|
+
key,
|
|
217
|
+
buildSideConfig({
|
|
218
|
+
direction: "LONG",
|
|
219
|
+
definition: pattern.definition,
|
|
220
|
+
config: tradingPatternsConfig,
|
|
221
|
+
toggle
|
|
222
|
+
})
|
|
223
|
+
];
|
|
224
|
+
}
|
|
225
|
+
if (key === "SHORT") {
|
|
226
|
+
return [
|
|
227
|
+
key,
|
|
228
|
+
buildSideConfig({
|
|
229
|
+
direction: "SHORT",
|
|
230
|
+
definition: pattern.definition,
|
|
231
|
+
config: tradingPatternsConfig,
|
|
232
|
+
toggle
|
|
233
|
+
})
|
|
234
|
+
];
|
|
235
|
+
}
|
|
236
|
+
return [key, tradingPatternsConfig[key]];
|
|
237
|
+
})
|
|
238
|
+
);
|
|
239
|
+
return pattern.definition.parseConfig(input);
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
// src/TradingPatterns/core.ts
|
|
243
|
+
var asCapturedEntryDecision = (params) => ({
|
|
244
|
+
kind: "entry",
|
|
245
|
+
code: params.code ?? "CAPTURED_ENTRY",
|
|
246
|
+
entryContext: {},
|
|
247
|
+
orderPlan: params.orderPlan,
|
|
248
|
+
runtime: params.runtime
|
|
249
|
+
});
|
|
250
|
+
var asCapturedExitDecision = (params) => ({
|
|
251
|
+
kind: "exit",
|
|
252
|
+
code: params.code ?? "CAPTURED_EXIT",
|
|
253
|
+
closePlan: {}
|
|
254
|
+
});
|
|
255
|
+
var asCapturedProtectDecision = (params) => ({
|
|
256
|
+
kind: "protect",
|
|
257
|
+
code: params.code ?? "CAPTURED_PROTECT",
|
|
258
|
+
protectPlan: params.protectPlan
|
|
259
|
+
});
|
|
260
|
+
var createCapturingApi = ({
|
|
261
|
+
strategyApi,
|
|
262
|
+
capture,
|
|
263
|
+
stagedControllers
|
|
264
|
+
}) => {
|
|
265
|
+
let visiblePosition = null;
|
|
266
|
+
const api = {
|
|
267
|
+
...strategyApi,
|
|
268
|
+
entry: async (params) => {
|
|
269
|
+
capture.entry = params;
|
|
270
|
+
return asCapturedEntryDecision(params);
|
|
271
|
+
},
|
|
272
|
+
exit: async (params) => {
|
|
273
|
+
capture.exit = params;
|
|
274
|
+
return asCapturedExitDecision(params);
|
|
275
|
+
},
|
|
276
|
+
protect: (params) => {
|
|
277
|
+
capture.protect = params;
|
|
278
|
+
return asCapturedProtectDecision(params);
|
|
279
|
+
},
|
|
280
|
+
getCurrentPosition: async () => visiblePosition,
|
|
281
|
+
createLastTradeController: (params) => {
|
|
282
|
+
const delegate = strategyApi.createLastTradeController(params);
|
|
283
|
+
const staged = {
|
|
284
|
+
controller: delegate,
|
|
285
|
+
pendingTimestamp: null
|
|
286
|
+
};
|
|
287
|
+
stagedControllers.push(staged);
|
|
288
|
+
return {
|
|
289
|
+
isInCooldown: delegate.isInCooldown,
|
|
290
|
+
getLastTradeTimestamp: delegate.getLastTradeTimestamp,
|
|
291
|
+
markTrade: (timestamp) => {
|
|
292
|
+
staged.pendingTimestamp = timestamp;
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
return {
|
|
298
|
+
api,
|
|
299
|
+
setVisiblePosition: (position) => {
|
|
300
|
+
visiblePosition = position;
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
};
|
|
304
|
+
var resetRuntimeCapture = (runtime) => {
|
|
305
|
+
delete runtime.capture.entry;
|
|
306
|
+
delete runtime.capture.exit;
|
|
307
|
+
delete runtime.capture.protect;
|
|
308
|
+
runtime.stagedControllers.forEach((staged) => {
|
|
309
|
+
staged.pendingTimestamp = null;
|
|
310
|
+
});
|
|
311
|
+
};
|
|
312
|
+
var commitStagedTrade = (runtime) => {
|
|
313
|
+
runtime.stagedControllers.forEach((staged) => {
|
|
314
|
+
if (staged.pendingTimestamp !== null) {
|
|
315
|
+
staged.controller.markTrade(staged.pendingTimestamp);
|
|
316
|
+
staged.pendingTimestamp = null;
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
};
|
|
320
|
+
var codeFor = (pattern, sourceCode) => `TRADING_PATTERNS_${pattern.code}_${sourceCode ?? "DECISION"}`;
|
|
321
|
+
var createTradingPatternsCoreWithDefinitions = (definitions) => async ({ config: config2, data, strategyApi, indicatorsState }) => {
|
|
322
|
+
const ownership = strategyApi.createStateController(
|
|
323
|
+
"TradingPatterns",
|
|
324
|
+
() => ({ activePattern: null }),
|
|
325
|
+
{
|
|
326
|
+
configKey: JSON.stringify({
|
|
327
|
+
priority: config2.TRADING_PATTERNS_PRIORITY,
|
|
328
|
+
enabled: config2.TRADING_PATTERNS
|
|
329
|
+
})
|
|
330
|
+
}
|
|
331
|
+
);
|
|
332
|
+
const runtimes = [];
|
|
333
|
+
for (const patternName of config2.TRADING_PATTERNS_PRIORITY) {
|
|
334
|
+
const pattern = definitions.find((item) => item.name === patternName);
|
|
335
|
+
if (!pattern || !config2.TRADING_PATTERNS[pattern.name].enable) continue;
|
|
336
|
+
const capture = {};
|
|
337
|
+
const stagedControllers = [];
|
|
338
|
+
const { api, setVisiblePosition } = createCapturingApi({
|
|
339
|
+
strategyApi,
|
|
340
|
+
capture,
|
|
341
|
+
stagedControllers
|
|
342
|
+
});
|
|
343
|
+
const runner = await pattern.definition.createCore({
|
|
344
|
+
config: buildChildConfig({ pattern, config: config2 }),
|
|
345
|
+
data,
|
|
346
|
+
strategyApi: api,
|
|
347
|
+
indicatorsState
|
|
348
|
+
});
|
|
349
|
+
runtimes.push({
|
|
350
|
+
pattern,
|
|
351
|
+
capture,
|
|
352
|
+
stagedControllers,
|
|
353
|
+
setVisiblePosition,
|
|
354
|
+
runner
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
return async (candle, btcCandle) => {
|
|
358
|
+
const currentPosition = await strategyApi.getCurrentPosition();
|
|
359
|
+
const state = ownership.get();
|
|
360
|
+
if (!currentPosition && state.activePattern !== null) {
|
|
361
|
+
ownership.update((current) => {
|
|
362
|
+
current.activePattern = null;
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
let selectedEntry;
|
|
366
|
+
let selectedExit;
|
|
367
|
+
let selectedProtect;
|
|
368
|
+
for (const runtime of runtimes) {
|
|
369
|
+
resetRuntimeCapture(runtime);
|
|
370
|
+
const ownsPosition = currentPosition !== null && ownership.get().activePattern === runtime.pattern.name;
|
|
371
|
+
runtime.setVisiblePosition(ownsPosition ? currentPosition : null);
|
|
372
|
+
await runtime.runner(candle, btcCandle);
|
|
373
|
+
if (!currentPosition && !selectedEntry && runtime.capture.entry) {
|
|
374
|
+
selectedEntry = runtime;
|
|
375
|
+
}
|
|
376
|
+
if (ownsPosition && !selectedExit && runtime.capture.exit) {
|
|
377
|
+
selectedExit = runtime;
|
|
378
|
+
}
|
|
379
|
+
if (ownsPosition && !selectedProtect && runtime.capture.protect) {
|
|
380
|
+
selectedProtect = runtime;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
if (currentPosition) {
|
|
384
|
+
if (selectedExit?.capture.exit) {
|
|
385
|
+
ownership.update((current) => {
|
|
386
|
+
current.activePattern = null;
|
|
387
|
+
});
|
|
388
|
+
return strategyApi.exit({
|
|
389
|
+
...selectedExit.capture.exit,
|
|
390
|
+
code: codeFor(selectedExit.pattern, selectedExit.capture.exit.code)
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
if (selectedProtect?.capture.protect) {
|
|
394
|
+
return strategyApi.protect({
|
|
395
|
+
...selectedProtect.capture.protect,
|
|
396
|
+
code: codeFor(
|
|
397
|
+
selectedProtect.pattern,
|
|
398
|
+
selectedProtect.capture.protect.code
|
|
399
|
+
)
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
return strategyApi.skip("TRADING_PATTERNS_POSITION_EXISTS");
|
|
403
|
+
}
|
|
404
|
+
if (!selectedEntry?.capture.entry) {
|
|
405
|
+
return strategyApi.skip("TRADING_PATTERNS_NO_PATTERN");
|
|
406
|
+
}
|
|
407
|
+
commitStagedTrade(selectedEntry);
|
|
408
|
+
ownership.update((current) => {
|
|
409
|
+
current.activePattern = selectedEntry.pattern.name;
|
|
410
|
+
});
|
|
411
|
+
const sourceEntry = selectedEntry.capture.entry;
|
|
412
|
+
return strategyApi.entry({
|
|
413
|
+
...sourceEntry,
|
|
414
|
+
code: codeFor(selectedEntry.pattern, sourceEntry.code),
|
|
415
|
+
additionalIndicators: {
|
|
416
|
+
...sourceEntry.additionalIndicators ?? {},
|
|
417
|
+
tradingPatternsContext: {
|
|
418
|
+
selectedPattern: selectedEntry.pattern.name,
|
|
419
|
+
sourceCode: sourceEntry.code ?? null
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
};
|
|
424
|
+
};
|
|
425
|
+
var createTradingPatternsCore = createTradingPatternsCoreWithDefinitions(patternDefinitions);
|
|
426
|
+
|
|
427
|
+
// src/TradingPatterns/adapters.ts
|
|
428
|
+
var import_strategies = require("@tradejs/core/strategies");
|
|
429
|
+
var import_ai_gate = require("@tradejs/strategy-kit/ai-gate");
|
|
430
|
+
var getSelectedPattern = (additionalIndicators) => {
|
|
431
|
+
const additional = additionalIndicators ?? {};
|
|
432
|
+
const context = additional.tradingPatternsContext ?? {};
|
|
433
|
+
const selected = context.selectedPattern;
|
|
434
|
+
return typeof selected === "string" && patternDefinitionByName.has(selected) ? selected : void 0;
|
|
435
|
+
};
|
|
436
|
+
var getSourceAdapter = ({
|
|
437
|
+
signal,
|
|
438
|
+
payload
|
|
439
|
+
}) => {
|
|
440
|
+
const selectedPattern = getSelectedPattern(signal.additionalIndicators) ?? getSelectedPattern(payload?.additionalIndicators);
|
|
441
|
+
return selectedPattern ? patternDefinitionByName.get(selectedPattern)?.definition.manifest.aiAdapter : void 0;
|
|
442
|
+
};
|
|
443
|
+
var shortReplacementGate = (0, import_ai_gate.withStrategyLocalAiGate)(
|
|
444
|
+
{},
|
|
445
|
+
{
|
|
446
|
+
id: "trading_patterns_short_D_protected_flag_economics_2026_10_05",
|
|
447
|
+
approves: ({ signal, payload }) => {
|
|
448
|
+
if (signal.direction !== "SHORT") return false;
|
|
449
|
+
const nearestSupportDistanceAtr = (0, import_ai_gate.getAiPayloadNumber)(
|
|
450
|
+
payload,
|
|
451
|
+
"additionalIndicators.baseContext.structure.srZones.nearestSupport.distanceAtr"
|
|
452
|
+
);
|
|
453
|
+
const maStackScore = (0, import_ai_gate.getAiPayloadNumber)(
|
|
454
|
+
payload,
|
|
455
|
+
"additionalIndicators.baseContext.regime.trend.maStackScore"
|
|
456
|
+
);
|
|
457
|
+
const trailDistancePct = (0, import_ai_gate.getAiPayloadNumber)(
|
|
458
|
+
payload,
|
|
459
|
+
"additionalIndicators.baseContext.regime.trend.trendFollow.distanceToTrailStopPct"
|
|
460
|
+
);
|
|
461
|
+
const passesD = nearestSupportDistanceAtr != null && Number.isFinite(nearestSupportDistanceAtr) && nearestSupportDistanceAtr <= 0.7 && maStackScore != null && Number.isFinite(maStackScore) && maStackScore <= 0 || trailDistancePct != null && Number.isFinite(trailDistancePct) && trailDistancePct >= -1.3;
|
|
462
|
+
if (!passesD) return false;
|
|
463
|
+
if (getSelectedPattern(payload.additionalIndicators) !== "Flag")
|
|
464
|
+
return true;
|
|
465
|
+
const grossRiskRatio = (0, import_ai_gate.getAiPayloadNumber)(
|
|
466
|
+
payload,
|
|
467
|
+
"additionalIndicators.flagContext.executionEconomics.grossRiskRatio"
|
|
468
|
+
);
|
|
469
|
+
const priceDistanceToMaFastAtr = (0, import_ai_gate.getAiPayloadNumber)(
|
|
470
|
+
payload,
|
|
471
|
+
"additionalIndicators.baseContext.regime.trend.priceDistanceToMaFastAtr"
|
|
472
|
+
);
|
|
473
|
+
return grossRiskRatio != null && Number.isFinite(grossRiskRatio) && grossRiskRatio > 3 || priceDistanceToMaFastAtr != null && Number.isFinite(priceDistanceToMaFastAtr) && priceDistanceToMaFastAtr > 0.4;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
);
|
|
477
|
+
var tradingPatternsAiAdapter = {
|
|
478
|
+
buildPayload: ({ signal, basePayload }) => {
|
|
479
|
+
const sourceAdapter = getSourceAdapter({ signal, payload: basePayload });
|
|
480
|
+
return sourceAdapter?.buildPayload?.({ signal, basePayload }) ?? basePayload;
|
|
481
|
+
},
|
|
482
|
+
buildSystemPromptAddon: ({ signal }) => getSourceAdapter({ signal })?.buildSystemPromptAddon?.({ signal }) ?? "",
|
|
483
|
+
buildHumanPromptAddon: ({ signal, payload }) => {
|
|
484
|
+
const additional = payload.additionalIndicators ?? {};
|
|
485
|
+
const context = additional.tradingPatternsContext ?? {};
|
|
486
|
+
const sourceAddon = getSourceAdapter({ signal, payload })?.buildHumanPromptAddon?.({
|
|
487
|
+
signal,
|
|
488
|
+
payload
|
|
489
|
+
}) ?? "";
|
|
490
|
+
return `
|
|
491
|
+
Additional TradingPatterns context:
|
|
492
|
+
- selectedPattern=${String(context.selectedPattern ?? "n/a")}
|
|
493
|
+
- sourceCode=${String(context.sourceCode ?? "n/a")}
|
|
494
|
+
|
|
495
|
+
Interpret the geometry fields supplied by the selected source pattern. Do not
|
|
496
|
+
mix rules from other pattern families into this signal.
|
|
497
|
+
|
|
498
|
+
${sourceAddon}
|
|
499
|
+
`.trim();
|
|
500
|
+
},
|
|
501
|
+
postProcessAnalysis: ({ signal, payload, analysis }) => getSourceAdapter({ signal, payload })?.postProcessAnalysis?.({
|
|
502
|
+
signal,
|
|
503
|
+
payload,
|
|
504
|
+
analysis
|
|
505
|
+
}) ?? analysis,
|
|
506
|
+
postProcessLocalAnalysis: ({ signal, payload, analysis }) => {
|
|
507
|
+
if (signal.direction === "SHORT") {
|
|
508
|
+
return shortReplacementGate.postProcessLocalAnalysis?.({
|
|
509
|
+
signal,
|
|
510
|
+
payload,
|
|
511
|
+
analysis
|
|
512
|
+
}) ?? analysis;
|
|
513
|
+
}
|
|
514
|
+
return getSourceAdapter({ signal, payload })?.postProcessLocalAnalysis?.({
|
|
515
|
+
signal,
|
|
516
|
+
payload,
|
|
517
|
+
analysis
|
|
518
|
+
}) ?? analysis;
|
|
519
|
+
},
|
|
520
|
+
mapEntryRuntimeFromConfig: (config2) => (0, import_strategies.mapAiRuntimeFromConfig)(
|
|
521
|
+
config2
|
|
522
|
+
)
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
// src/TradingPatterns/manifest.ts
|
|
526
|
+
var tradingPatternsManifest = {
|
|
527
|
+
name: "TradingPatterns",
|
|
528
|
+
aiAdapter: tradingPatternsAiAdapter
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
// src/TradingPatterns/strategy.ts
|
|
532
|
+
var TradingPatternsStrategyDefinition = {
|
|
533
|
+
defaults: config,
|
|
534
|
+
parseConfig: parseTradingPatternsConfig,
|
|
535
|
+
createCore: createTradingPatternsCore,
|
|
536
|
+
manifest: tradingPatternsManifest
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
// src/index.ts
|
|
540
|
+
var strategyEntries = [
|
|
541
|
+
TradingPatternsStrategyDefinition
|
|
542
|
+
];
|
|
543
|
+
var defaultConfigs = {
|
|
544
|
+
TradingPatterns: config
|
|
545
|
+
};
|
|
546
|
+
var getBuiltInStrategyDefaultConfig = (strategyName) => defaultConfigs[strategyName];
|
|
547
|
+
var index_default = (0, import_config5.defineStrategyPlugin)({ strategyEntries });
|
|
548
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
549
|
+
0 && (module.exports = {
|
|
550
|
+
PATTERN_NAMES,
|
|
551
|
+
TradingPatternsStrategyDefinition,
|
|
552
|
+
createTradingPatternsCore,
|
|
553
|
+
createTradingPatternsCoreWithDefinitions,
|
|
554
|
+
getBuiltInStrategyDefaultConfig,
|
|
555
|
+
parseTradingPatternsConfig,
|
|
556
|
+
patternDefinitions,
|
|
557
|
+
strategyEntries,
|
|
558
|
+
tradingPatternsDefaultConfig,
|
|
559
|
+
tradingPatternsManifest
|
|
560
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { ValidatedStrategyRegistryEntry } from '@tradejs/strategy-kit/config';
|
|
2
|
+
import { StrategyConfig, Direction, CreateStrategyCore, StrategyManifest } from '@tradejs/types';
|
|
3
|
+
|
|
4
|
+
type TradingPatternName = (typeof patternDefinitions)[number]["name"];
|
|
5
|
+
interface TradingPatternDefinition {
|
|
6
|
+
name: TradingPatternName;
|
|
7
|
+
code: string;
|
|
8
|
+
definition: ValidatedStrategyRegistryEntry;
|
|
9
|
+
}
|
|
10
|
+
declare const patternDefinitions: readonly [{
|
|
11
|
+
readonly name: "Diamond";
|
|
12
|
+
readonly code: "DIAMOND";
|
|
13
|
+
readonly definition: ValidatedStrategyRegistryEntry;
|
|
14
|
+
}, {
|
|
15
|
+
readonly name: "Flag";
|
|
16
|
+
readonly code: "FLAG";
|
|
17
|
+
readonly definition: ValidatedStrategyRegistryEntry;
|
|
18
|
+
}, {
|
|
19
|
+
readonly name: "Gartley";
|
|
20
|
+
readonly code: "GARTLEY";
|
|
21
|
+
readonly definition: ValidatedStrategyRegistryEntry;
|
|
22
|
+
}, {
|
|
23
|
+
readonly name: "HeadAndShoulders";
|
|
24
|
+
readonly code: "HEAD_AND_SHOULDERS";
|
|
25
|
+
readonly definition: ValidatedStrategyRegistryEntry;
|
|
26
|
+
}];
|
|
27
|
+
declare const PATTERN_NAMES: readonly TradingPatternName[];
|
|
28
|
+
|
|
29
|
+
interface TradingPatternsSideConfig {
|
|
30
|
+
enable: boolean;
|
|
31
|
+
direction: Direction;
|
|
32
|
+
minRiskRatio: number;
|
|
33
|
+
}
|
|
34
|
+
interface TradingPatternDirectionConfig {
|
|
35
|
+
enable: boolean;
|
|
36
|
+
minRiskRatio: number;
|
|
37
|
+
}
|
|
38
|
+
interface TradingPatternToggle {
|
|
39
|
+
enable: boolean;
|
|
40
|
+
LONG: TradingPatternDirectionConfig;
|
|
41
|
+
SHORT: TradingPatternDirectionConfig;
|
|
42
|
+
}
|
|
43
|
+
type TradingPatternToggles = Record<TradingPatternName, TradingPatternToggle>;
|
|
44
|
+
interface TradingPatternsConfig extends StrategyConfig {
|
|
45
|
+
INTERVAL: "60";
|
|
46
|
+
AI_ENABLED: boolean;
|
|
47
|
+
AI_MODE: "gate" | "llm";
|
|
48
|
+
MIN_AI_QUALITY: number;
|
|
49
|
+
TRADING_PATTERNS_PRIORITY: TradingPatternName[];
|
|
50
|
+
TRADING_PATTERNS: TradingPatternToggles;
|
|
51
|
+
LONG: TradingPatternsSideConfig;
|
|
52
|
+
SHORT: TradingPatternsSideConfig;
|
|
53
|
+
[key: string]: unknown;
|
|
54
|
+
}
|
|
55
|
+
declare const config: TradingPatternsConfig;
|
|
56
|
+
declare const parseTradingPatternsConfig: (input: unknown) => TradingPatternsConfig;
|
|
57
|
+
|
|
58
|
+
declare const createTradingPatternsCoreWithDefinitions: (definitions: readonly TradingPatternDefinition[]) => CreateStrategyCore<TradingPatternsConfig>;
|
|
59
|
+
declare const createTradingPatternsCore: CreateStrategyCore<TradingPatternsConfig>;
|
|
60
|
+
|
|
61
|
+
declare const tradingPatternsManifest: StrategyManifest;
|
|
62
|
+
|
|
63
|
+
declare const TradingPatternsStrategyDefinition: ValidatedStrategyRegistryEntry<TradingPatternsConfig>;
|
|
64
|
+
|
|
65
|
+
declare const strategyEntries: ValidatedStrategyRegistryEntry<any>[];
|
|
66
|
+
declare const getBuiltInStrategyDefaultConfig: (strategyName: string) => StrategyConfig | undefined;
|
|
67
|
+
|
|
68
|
+
declare const _default: {
|
|
69
|
+
strategyEntries: ValidatedStrategyRegistryEntry<any>[];
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export { PATTERN_NAMES, type TradingPatternDefinition, type TradingPatternDirectionConfig, type TradingPatternName, type TradingPatternToggle, type TradingPatternToggles, type TradingPatternsConfig, type TradingPatternsSideConfig, TradingPatternsStrategyDefinition, createTradingPatternsCore, createTradingPatternsCoreWithDefinitions, _default as default, getBuiltInStrategyDefaultConfig, parseTradingPatternsConfig, patternDefinitions, strategyEntries, config as tradingPatternsDefaultConfig, tradingPatternsManifest };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { ValidatedStrategyRegistryEntry } from '@tradejs/strategy-kit/config';
|
|
2
|
+
import { StrategyConfig, Direction, CreateStrategyCore, StrategyManifest } from '@tradejs/types';
|
|
3
|
+
|
|
4
|
+
type TradingPatternName = (typeof patternDefinitions)[number]["name"];
|
|
5
|
+
interface TradingPatternDefinition {
|
|
6
|
+
name: TradingPatternName;
|
|
7
|
+
code: string;
|
|
8
|
+
definition: ValidatedStrategyRegistryEntry;
|
|
9
|
+
}
|
|
10
|
+
declare const patternDefinitions: readonly [{
|
|
11
|
+
readonly name: "Diamond";
|
|
12
|
+
readonly code: "DIAMOND";
|
|
13
|
+
readonly definition: ValidatedStrategyRegistryEntry;
|
|
14
|
+
}, {
|
|
15
|
+
readonly name: "Flag";
|
|
16
|
+
readonly code: "FLAG";
|
|
17
|
+
readonly definition: ValidatedStrategyRegistryEntry;
|
|
18
|
+
}, {
|
|
19
|
+
readonly name: "Gartley";
|
|
20
|
+
readonly code: "GARTLEY";
|
|
21
|
+
readonly definition: ValidatedStrategyRegistryEntry;
|
|
22
|
+
}, {
|
|
23
|
+
readonly name: "HeadAndShoulders";
|
|
24
|
+
readonly code: "HEAD_AND_SHOULDERS";
|
|
25
|
+
readonly definition: ValidatedStrategyRegistryEntry;
|
|
26
|
+
}];
|
|
27
|
+
declare const PATTERN_NAMES: readonly TradingPatternName[];
|
|
28
|
+
|
|
29
|
+
interface TradingPatternsSideConfig {
|
|
30
|
+
enable: boolean;
|
|
31
|
+
direction: Direction;
|
|
32
|
+
minRiskRatio: number;
|
|
33
|
+
}
|
|
34
|
+
interface TradingPatternDirectionConfig {
|
|
35
|
+
enable: boolean;
|
|
36
|
+
minRiskRatio: number;
|
|
37
|
+
}
|
|
38
|
+
interface TradingPatternToggle {
|
|
39
|
+
enable: boolean;
|
|
40
|
+
LONG: TradingPatternDirectionConfig;
|
|
41
|
+
SHORT: TradingPatternDirectionConfig;
|
|
42
|
+
}
|
|
43
|
+
type TradingPatternToggles = Record<TradingPatternName, TradingPatternToggle>;
|
|
44
|
+
interface TradingPatternsConfig extends StrategyConfig {
|
|
45
|
+
INTERVAL: "60";
|
|
46
|
+
AI_ENABLED: boolean;
|
|
47
|
+
AI_MODE: "gate" | "llm";
|
|
48
|
+
MIN_AI_QUALITY: number;
|
|
49
|
+
TRADING_PATTERNS_PRIORITY: TradingPatternName[];
|
|
50
|
+
TRADING_PATTERNS: TradingPatternToggles;
|
|
51
|
+
LONG: TradingPatternsSideConfig;
|
|
52
|
+
SHORT: TradingPatternsSideConfig;
|
|
53
|
+
[key: string]: unknown;
|
|
54
|
+
}
|
|
55
|
+
declare const config: TradingPatternsConfig;
|
|
56
|
+
declare const parseTradingPatternsConfig: (input: unknown) => TradingPatternsConfig;
|
|
57
|
+
|
|
58
|
+
declare const createTradingPatternsCoreWithDefinitions: (definitions: readonly TradingPatternDefinition[]) => CreateStrategyCore<TradingPatternsConfig>;
|
|
59
|
+
declare const createTradingPatternsCore: CreateStrategyCore<TradingPatternsConfig>;
|
|
60
|
+
|
|
61
|
+
declare const tradingPatternsManifest: StrategyManifest;
|
|
62
|
+
|
|
63
|
+
declare const TradingPatternsStrategyDefinition: ValidatedStrategyRegistryEntry<TradingPatternsConfig>;
|
|
64
|
+
|
|
65
|
+
declare const strategyEntries: ValidatedStrategyRegistryEntry<any>[];
|
|
66
|
+
declare const getBuiltInStrategyDefaultConfig: (strategyName: string) => StrategyConfig | undefined;
|
|
67
|
+
|
|
68
|
+
declare const _default: {
|
|
69
|
+
strategyEntries: ValidatedStrategyRegistryEntry<any>[];
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export { PATTERN_NAMES, type TradingPatternDefinition, type TradingPatternDirectionConfig, type TradingPatternName, type TradingPatternToggle, type TradingPatternToggles, type TradingPatternsConfig, type TradingPatternsSideConfig, TradingPatternsStrategyDefinition, createTradingPatternsCore, createTradingPatternsCoreWithDefinitions, _default as default, getBuiltInStrategyDefaultConfig, parseTradingPatternsConfig, patternDefinitions, strategyEntries, config as tradingPatternsDefaultConfig, tradingPatternsManifest };
|