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