@tradejs/strategies 1.0.0

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.
@@ -0,0 +1,156 @@
1
+ import {
2
+ config,
3
+ trendLineManifest
4
+ } from "./chunk-MVIV5ZII.mjs";
5
+ import "./chunk-HEBXNMVQ.mjs";
6
+
7
+ // src/TrendLine/strategy.ts
8
+ import { createStrategyRuntime } from "@tradejs/node/strategies";
9
+
10
+ // src/TrendLine/core.ts
11
+ import { round } from "@tradejs/core/math";
12
+ import { createTrendlineEngine } from "@tradejs/core/indicators";
13
+
14
+ // src/TrendLine/filters.ts
15
+ import { diffRel } from "@tradejs/core/math";
16
+ import { ATR_PCT } from "@tradejs/indicators";
17
+ var MAX_CANDLE_VOLATILITY = 0.025;
18
+ var filterByVeryVolatility = (data) => {
19
+ const lastCandle = data[data.length - 1];
20
+ const prevCandle = data[data.length - 2];
21
+ const isVeryVolatility = diffRel(lastCandle.low, lastCandle.high) > MAX_CANDLE_VOLATILITY || diffRel(prevCandle.low, prevCandle.high) > MAX_CANDLE_VOLATILITY;
22
+ if (isVeryVolatility) {
23
+ return false;
24
+ }
25
+ return true;
26
+ };
27
+
28
+ // src/TrendLine/figures.ts
29
+ var buildTrendLineFigures = (bestLine) => ({
30
+ lines: [
31
+ {
32
+ id: bestLine.id,
33
+ kind: "trendline",
34
+ points: [...bestLine.points ?? []].sort(
35
+ (left, right) => left.timestamp - right.timestamp
36
+ ),
37
+ color: bestLine.mode === "lows" ? "#facc15" : "#fb923c",
38
+ width: 2,
39
+ style: "solid"
40
+ }
41
+ ],
42
+ points: [
43
+ {
44
+ id: `${bestLine.id}-points`,
45
+ kind: "trendline_points",
46
+ points: [...bestLine.points ?? [], ...bestLine.touches ?? []].sort(
47
+ (left, right) => left.timestamp - right.timestamp
48
+ ),
49
+ color: "#ef4444",
50
+ radius: 4
51
+ }
52
+ ]
53
+ });
54
+
55
+ // src/TrendLine/core.ts
56
+ var createTrendLineCore = async ({ config: config2, data: cachedData, strategyApi, indicatorsState }) => {
57
+ const {
58
+ ENV,
59
+ TRENDLINE,
60
+ FEE_PERCENT,
61
+ MAX_LOSS_VALUE,
62
+ MAX_CORRELATION,
63
+ HIGHS,
64
+ LOWS
65
+ } = config2;
66
+ const lastTradeController = strategyApi.createLastTradeController();
67
+ const trendlineOptions = {
68
+ bestLines: 1,
69
+ capture: true,
70
+ ...TRENDLINE
71
+ };
72
+ const getLowsTrendlines = createTrendlineEngine(cachedData, {
73
+ mode: "lows",
74
+ ...trendlineOptions
75
+ });
76
+ const getHighsTrendlines = createTrendlineEngine(cachedData, {
77
+ mode: "highs",
78
+ ...trendlineOptions
79
+ });
80
+ return async (candle) => {
81
+ const lowsTrendlines = getLowsTrendlines.next(candle);
82
+ const highsTrendlines = getHighsTrendlines.next(candle);
83
+ indicatorsState.onBar();
84
+ const bestLine = lowsTrendlines.length > 0 ? lowsTrendlines[0] : highsTrendlines[0];
85
+ if (!bestLine) {
86
+ return strategyApi.skip("NO_TRENDLINE");
87
+ }
88
+ const positionExists = await strategyApi.isCurrentPositionExists();
89
+ if (positionExists) {
90
+ return strategyApi.skip("POSITION_EXISTS");
91
+ }
92
+ if (lastTradeController.isInCooldown(candle.timestamp)) {
93
+ return strategyApi.skip("DEV_TRADE_COOLDOWN");
94
+ }
95
+ const { fullData, timestamp, currentPrice } = await strategyApi.getMarketData();
96
+ if (!filterByVeryVolatility(fullData)) {
97
+ return strategyApi.skip("VERY_VOLATILITY");
98
+ }
99
+ const modeConfig = bestLine.mode === "highs" ? HIGHS : LOWS;
100
+ const { direction, TP, SL, minRiskRatio, enable } = modeConfig;
101
+ if (!enable) {
102
+ return strategyApi.skip("STRATEGY_DISABLED");
103
+ }
104
+ const { stopLossPrice, takeProfitPrice, riskRatio, qty } = strategyApi.getDirectionalTpSlPrices({
105
+ price: currentPrice,
106
+ direction,
107
+ takeProfitDelta: TP,
108
+ stopLossDelta: SL,
109
+ unit: "percent",
110
+ maxLossValue: MAX_LOSS_VALUE,
111
+ feePercent: Number(FEE_PERCENT ?? 0)
112
+ });
113
+ if (!qty || !Number.isFinite(qty) || qty <= 0) {
114
+ return strategyApi.skip("INVALID_QTY");
115
+ }
116
+ if (riskRatio <= minRiskRatio) {
117
+ return strategyApi.skip(`RISK_RATIO:${round(riskRatio)}`);
118
+ }
119
+ const indicators = indicatorsState.snapshot();
120
+ const correlation = indicatorsState.latestNumber("correlation");
121
+ if (ENV !== "BACKTEST" && correlation != null && correlation >= MAX_CORRELATION) {
122
+ return strategyApi.skip(`MAX_CORRELATION:${round(correlation)}`);
123
+ }
124
+ lastTradeController.markTrade(timestamp);
125
+ return strategyApi.entry({
126
+ code: "TRENDLINE_SIGNAL",
127
+ figures: {
128
+ ...buildTrendLineFigures(bestLine)
129
+ },
130
+ direction,
131
+ indicators,
132
+ additionalIndicators: {
133
+ touches: bestLine.touches.length + 2,
134
+ distance: bestLine.distance,
135
+ trendLine: bestLine
136
+ },
137
+ orderPlan: {
138
+ qty,
139
+ stopLossPrice,
140
+ takeProfits: [{ rate: 1, price: takeProfitPrice }]
141
+ }
142
+ });
143
+ };
144
+ };
145
+
146
+ // src/TrendLine/strategy.ts
147
+ var TrendlineStrategyCreator = createStrategyRuntime({
148
+ strategyName: "TrendLine",
149
+ defaults: config,
150
+ createCore: createTrendLineCore,
151
+ manifest: trendLineManifest,
152
+ strategyDirectory: __dirname
153
+ });
154
+ export {
155
+ TrendlineStrategyCreator
156
+ };