@tradejs/cli 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,99 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/scripts/selectStrategy.ts
31
+ var selectStrategy_exports = {};
32
+ __export(selectStrategy_exports, {
33
+ selectStrategy: () => selectStrategy
34
+ });
35
+ module.exports = __toCommonJS(selectStrategy_exports);
36
+ var import_readline = __toESM(require("readline"));
37
+ var import_chalk = __toESM(require("chalk"));
38
+ var import_strategies = require("@tradejs/node/strategies");
39
+ var defaultStrategy = "TrendLine";
40
+ var getStrategyChoices = async () => {
41
+ try {
42
+ const loaded = await (0, import_strategies.getAvailableStrategyNames)();
43
+ if (loaded.length) {
44
+ return loaded;
45
+ }
46
+ } catch (error) {
47
+ console.warn(`Failed to load strategy list: ${String(error)}`);
48
+ }
49
+ return [
50
+ "Breakout",
51
+ "MaStrategy",
52
+ "AdaptiveMomentumRibbon",
53
+ "TrendLine",
54
+ "VolumeDivergence"
55
+ ];
56
+ };
57
+ var selectStrategy = async (promptLabel = "Select strategy") => {
58
+ const strategies = await getStrategyChoices();
59
+ const fallbackStrategy = strategies.includes(defaultStrategy) ? defaultStrategy : strategies[0];
60
+ if (!process.stdin.isTTY) {
61
+ return fallbackStrategy;
62
+ }
63
+ console.log(import_chalk.default.cyan("Available strategies:"));
64
+ strategies.forEach((name, index) => {
65
+ const isDefault = name === defaultStrategy;
66
+ const label = isDefault ? import_chalk.default.green(name) : name;
67
+ const suffix = isDefault ? import_chalk.default.gray(" (default)") : "";
68
+ console.log(` ${import_chalk.default.yellow(String(index + 1))}) ${label}${suffix}`);
69
+ });
70
+ const rl = import_readline.default.createInterface({
71
+ input: process.stdin,
72
+ output: process.stdout
73
+ });
74
+ const question = (text) => new Promise((resolve) => rl.question(text, resolve));
75
+ const answer = await question(
76
+ `${promptLabel} [${import_chalk.default.green(fallbackStrategy)}]: `
77
+ );
78
+ rl.close();
79
+ const trimmed = answer.trim();
80
+ if (!trimmed) {
81
+ return fallbackStrategy;
82
+ }
83
+ const asNumber = Number(trimmed);
84
+ if (Number.isFinite(asNumber) && asNumber >= 1 && asNumber <= strategies.length) {
85
+ return strategies[asNumber - 1];
86
+ }
87
+ const byName = strategies.find(
88
+ (name) => name.toLowerCase() === trimmed.toLowerCase()
89
+ );
90
+ if (byName) {
91
+ return byName;
92
+ }
93
+ console.warn(`Unknown strategy "${trimmed}", using ${fallbackStrategy}.`);
94
+ return fallbackStrategy;
95
+ };
96
+ // Annotate the CommonJS export names for ESM import in node:
97
+ 0 && (module.exports = {
98
+ selectStrategy
99
+ });
@@ -0,0 +1,300 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (let key of __getOwnPropNames(from))
11
+ if (!__hasOwnProp.call(to, key) && key !== except)
12
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
+ }
14
+ return to;
15
+ };
16
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
+ // If the importer is in node compatibility mode or this is not an ESM
18
+ // file that has been converted to a CommonJS file using a Babel-
19
+ // compatible transform (i.e. "__esModule" has not been set), then set
20
+ // "default" to the CommonJS "module.exports" for node compatibility.
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+
25
+ // src/scripts/signals.ts
26
+ var import_config = require("dotenv/config");
27
+ var import_args = __toESM(require("args"));
28
+ var import_progress = __toESM(require("progress"));
29
+ var import_connectors = require("@tradejs/connectors");
30
+ var import_chalk = __toESM(require("chalk"));
31
+ var import_connectors2 = require("@tradejs/node/connectors");
32
+ var import_cli = require("@tradejs/node/cli");
33
+ var import_async = require("@tradejs/core/async");
34
+ var import_constants = require("@tradejs/core/constants");
35
+ var import_strategies = require("@tradejs/node/strategies");
36
+ var import_time = require("@tradejs/core/time");
37
+ var import_logger = require("@tradejs/infra/logger");
38
+ var import_redis = require("@tradejs/infra/redis");
39
+ import_args.default.option(["t", "tickers"], "Selected tickers");
40
+ import_args.default.option(["e", "exclude"], "Exclude tickers from tests");
41
+ import_args.default.option(["l", "tickersLimit"], "Tickers limit");
42
+ import_args.default.option(["f", "timeframe"], "Timeframe", 15);
43
+ import_args.default.option(["m", "makeOrders"], "Make orders");
44
+ import_args.default.option(["N", "notify"], "Send message in Telegram", false);
45
+ import_args.default.option(["S", "skipScreenshots"], "Skip screenshot generation", false);
46
+ import_args.default.option(["u", "updateOnly"], "Only update tickers history", false);
47
+ import_args.default.option(["C", "cacheOnly"], "Do not update tickers history", false);
48
+ import_args.default.option(["L", "showTickersList"], "Just show only ticker list", false);
49
+ import_args.default.option(["c", "chunk"], "Split by chunks, ex. 1/3");
50
+ import_args.default.option(["U", "user"], "Use user confg", "root");
51
+ import_args.default.option(
52
+ "connector",
53
+ "Connector provider or name for signals (e.g. bybit, binance, coinbase, custom)",
54
+ "bybit"
55
+ );
56
+ var PRELOAD_START = (0, import_time.getTimestamp)(import_constants.SIGNALS_PRELOAD_DAYS);
57
+ var flags = import_args.default.parse(process.argv);
58
+ var minTouches = parseInt(flags.points);
59
+ var offset = parseInt(flags.offset);
60
+ var interval = flags.timeframe.toString();
61
+ var resolveStrategyNameByConfigKey = (userName, key) => {
62
+ const parts = key.split(":");
63
+ if (parts.length !== 5) {
64
+ return null;
65
+ }
66
+ const [users, keyUserName, strategiesKey, strategyName, configKey] = parts;
67
+ if (users !== "users" || keyUserName !== userName || strategiesKey !== "strategies" || configKey !== "config" || !strategyName) {
68
+ return null;
69
+ }
70
+ return strategyName;
71
+ };
72
+ var loadRuntimeStrategies = async (userName) => {
73
+ const keys = await (0, import_redis.getKeys)(`${import_redis.redisKeys.strategies(userName)}:`);
74
+ const configKeys = keys.filter((key) => key.endsWith(":config")).sort((a, b) => a.localeCompare(b));
75
+ const strategyConfigs = await Promise.all(
76
+ configKeys.map(async (key) => {
77
+ const strategyName = resolveStrategyNameByConfigKey(userName, key);
78
+ if (!strategyName) {
79
+ return null;
80
+ }
81
+ const strategyCreator = await (0, import_strategies.getStrategyCreator)(strategyName);
82
+ if (!strategyCreator) {
83
+ import_logger.logger.warn("Skip unknown strategy config key: %s", key);
84
+ return null;
85
+ }
86
+ const strategyConfig = await (0, import_redis.getData)(key, {});
87
+ return {
88
+ strategyName,
89
+ strategyCreator,
90
+ strategyConfig
91
+ };
92
+ })
93
+ );
94
+ return strategyConfigs.filter(Boolean);
95
+ };
96
+ var resolveSignalsConnectorName = async (value) => {
97
+ const connectorName = await (0, import_connectors2.resolveConnectorName)(value);
98
+ if (connectorName) {
99
+ return connectorName;
100
+ }
101
+ import_logger.logger.warn(
102
+ 'Unknown connector "%s". Fallback to %s.',
103
+ String(value || "").trim() || String(value),
104
+ import_connectors2.DEFAULT_CONNECTOR_NAME
105
+ );
106
+ return import_connectors2.DEFAULT_CONNECTOR_NAME;
107
+ };
108
+ var findSignals = async (symbol, connector, btcBinanceData, btcCoinbaseData, runtimeStrategies) => {
109
+ const prevSignals = await (0, import_redis.getKeys)(import_redis.redisKeys.signalsBySymbol(symbol));
110
+ if (prevSignals.length) {
111
+ import_logger.logger.info("Exit by signal exists %s", symbol);
112
+ return null;
113
+ }
114
+ const currentTimestamp = (0, import_time.getTimestamp)();
115
+ const [cachedData, btcCachedData] = await Promise.all([
116
+ connector.kline({
117
+ symbol,
118
+ start: PRELOAD_START,
119
+ end: currentTimestamp,
120
+ cacheOnly: true,
121
+ interval
122
+ }),
123
+ connector.kline({
124
+ symbol: "BTCUSDT",
125
+ start: PRELOAD_START,
126
+ end: currentTimestamp,
127
+ cacheOnly: true,
128
+ interval
129
+ })
130
+ ]);
131
+ const lastCandle = cachedData.pop();
132
+ const btcLastCandle = btcCachedData.pop();
133
+ if (!lastCandle || !btcLastCandle) {
134
+ return;
135
+ }
136
+ for (const runtimeStrategy of runtimeStrategies) {
137
+ const { strategyName, strategyCreator, strategyConfig } = runtimeStrategy;
138
+ const strategy = await strategyCreator({
139
+ userName: flags.user,
140
+ connector,
141
+ symbol,
142
+ data: [...cachedData],
143
+ btcData: [...btcCachedData],
144
+ btcBinanceData: [...btcBinanceData],
145
+ btcCoinbaseData: [...btcCoinbaseData],
146
+ config: {
147
+ ...strategyConfig,
148
+ ENV: "CRON",
149
+ INTERVAL: interval,
150
+ MAKE_ORDERS: flags.makeOrders
151
+ }
152
+ });
153
+ const signal = await strategy(lastCandle, btcLastCandle);
154
+ if (!signal || typeof signal === "string") {
155
+ continue;
156
+ }
157
+ await (0, import_redis.setData)(import_redis.redisKeys.signal(symbol, signal.signalId), signal, {
158
+ expire: import_constants.TTL_1D
159
+ });
160
+ await (0, import_redis.setData)(import_redis.redisKeys.storeSignal(symbol, signal.signalId), signal, {
161
+ expire: import_constants.TTL_3M
162
+ });
163
+ import_logger.logger.info("Signal found %s by strategy %s", symbol, strategyName);
164
+ return signal;
165
+ }
166
+ };
167
+ var signals = async () => {
168
+ const signals2 = new Array();
169
+ const connectorName = await resolveSignalsConnectorName(flags.connector);
170
+ const connectorFactory = await (0, import_connectors2.getConnectorCreatorByName)(connectorName);
171
+ if (!connectorFactory) {
172
+ throw new Error(`Connector "${connectorName}" is not registered`);
173
+ }
174
+ const marketConnector = await connectorFactory({
175
+ userName: flags.user
176
+ });
177
+ let btcBinanceConnector = marketConnector;
178
+ let btcCoinbaseConnector = marketConnector;
179
+ if (connectorName.toLowerCase() === import_connectors2.DEFAULT_CONNECTOR_NAME.toLowerCase()) {
180
+ const binanceFactory = await (0, import_connectors2.getConnectorCreatorByName)(
181
+ import_connectors.ConnectorNames.Binance
182
+ );
183
+ if (binanceFactory) {
184
+ btcBinanceConnector = await binanceFactory({
185
+ userName: flags.user
186
+ });
187
+ } else {
188
+ import_logger.logger.warn(
189
+ "Binance connector is unavailable. Reusing %s.",
190
+ connectorName
191
+ );
192
+ }
193
+ const coinbaseFactory = await (0, import_connectors2.getConnectorCreatorByName)(
194
+ import_connectors.ConnectorNames.Coinbase
195
+ );
196
+ if (coinbaseFactory) {
197
+ btcCoinbaseConnector = await coinbaseFactory({
198
+ userName: flags.user
199
+ });
200
+ } else {
201
+ import_logger.logger.warn(
202
+ "Coinbase connector is unavailable. Reusing %s.",
203
+ connectorName
204
+ );
205
+ }
206
+ }
207
+ const tickers = await (0, import_cli.getTickers)(
208
+ marketConnector,
209
+ flags.tickers,
210
+ flags.exclude,
211
+ flags.tickersLimit,
212
+ flags.chunk
213
+ );
214
+ if (flags.showTickersList) {
215
+ console.log(import_chalk.default.gray(JSON.stringify(tickers.sort(), null, 2)));
216
+ return;
217
+ }
218
+ if (!flags.cacheOnly) {
219
+ await (0, import_cli.update)(marketConnector, interval, tickers);
220
+ if (btcBinanceConnector !== marketConnector) {
221
+ await (0, import_cli.update)(btcBinanceConnector, interval, ["BTCUSDT"]);
222
+ }
223
+ if (btcCoinbaseConnector !== marketConnector) {
224
+ await (0, import_cli.update)(btcCoinbaseConnector, interval, ["BTCUSDT"]);
225
+ }
226
+ }
227
+ const currentTimestamp = (0, import_time.getTimestamp)();
228
+ const [btcBinanceData, btcCoinbaseData] = await Promise.all([
229
+ btcBinanceConnector.kline({
230
+ symbol: "BTCUSDT",
231
+ start: PRELOAD_START,
232
+ end: currentTimestamp,
233
+ cacheOnly: true,
234
+ interval
235
+ }),
236
+ btcCoinbaseConnector.kline({
237
+ symbol: "BTCUSDT",
238
+ start: PRELOAD_START,
239
+ end: currentTimestamp,
240
+ cacheOnly: true,
241
+ interval
242
+ })
243
+ ]);
244
+ if (flags.updateOnly) {
245
+ return;
246
+ }
247
+ const runtimeStrategies = await loadRuntimeStrategies(flags.user);
248
+ if (!runtimeStrategies.length) {
249
+ import_logger.logger.warn(
250
+ "No strategy configs found by users:%s:strategies:*:config",
251
+ flags.user
252
+ );
253
+ return;
254
+ }
255
+ import_logger.logger.info(
256
+ import_chalk.default.yellow(
257
+ `loaded strategies: ${runtimeStrategies.map((s) => s.strategyName).join(", ")}`
258
+ )
259
+ );
260
+ const bar = new import_progress.default(
261
+ ":current/:total [:bar][:percent] :found :eta(s) :symbol",
262
+ {
263
+ total: tickers.length,
264
+ width: 30
265
+ }
266
+ );
267
+ import_logger.logger.info(import_chalk.default.yellow(`tickers: ${tickers.length}`));
268
+ await (0, import_async.runWithConcurrency)(tickers, 5, async (symbol) => {
269
+ const signal = await findSignals(
270
+ symbol,
271
+ marketConnector,
272
+ btcBinanceData,
273
+ btcCoinbaseData,
274
+ runtimeStrategies
275
+ );
276
+ if (signal) {
277
+ signals2.push(signal);
278
+ }
279
+ bar.tick(1, {
280
+ found: import_chalk.default.cyan(signals2.length),
281
+ symbol: import_chalk.default.gray(symbol)
282
+ });
283
+ });
284
+ if (!flags.skipScreenshots) {
285
+ await (0, import_cli.makeScreenshots)(signals2, "15");
286
+ await (0, import_cli.makeScreenshots)(signals2, "60");
287
+ }
288
+ if (flags.notify) {
289
+ await (0, import_cli.sendToTG)(signals2, "15");
290
+ }
291
+ import_logger.logger.info(
292
+ JSON.stringify(
293
+ signals2.map((s) => s.symbol),
294
+ null,
295
+ 2
296
+ )
297
+ );
298
+ process.exit();
299
+ };
300
+ signals();
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+
3
+ // src/scripts/test-ml.ts
4
+ var import_config = require("dotenv/config");
5
+ var import_constants = require("@tradejs/core/constants");
6
+ var import_ml = require("@tradejs/infra/ml");
7
+ var import_strategies = require("@tradejs/strategies");
8
+ var now = Date.now();
9
+ var INTERVAL_MIN = 15;
10
+ var CANDLES = import_constants.ML_BASE_CANDLES_WINDOW;
11
+ var makeSeries = (len, base, step = 0.1) => Array.from({ length: len }, (_, i) => base + i * step);
12
+ var makeCandles = (startPrice, startTs) => {
13
+ const candles = [];
14
+ let price = startPrice;
15
+ for (let i = 0; i < CANDLES; i += 1) {
16
+ const ts = startTs + i * INTERVAL_MIN * 6e4;
17
+ const delta = (i % 2 === 0 ? 1 : -1) * (0.2 + i * 0.01);
18
+ const open = price;
19
+ const close = price + delta;
20
+ const high = Math.max(open, close) + 0.15;
21
+ const low = Math.min(open, close) - 0.15;
22
+ const volume = 1e3 + i * 10;
23
+ candles.push({
24
+ open,
25
+ high,
26
+ low,
27
+ close,
28
+ volume,
29
+ turnover: volume * close,
30
+ timestamp: ts,
31
+ dt: new Date(ts).toISOString()
32
+ });
33
+ price = close;
34
+ }
35
+ return candles;
36
+ };
37
+ var buildTrendline = (entryTs, price) => ({
38
+ id: "mock-trendline",
39
+ mode: "lows",
40
+ distance: 0.75,
41
+ touches: [
42
+ { timestamp: entryTs - 6 * INTERVAL_MIN * 6e4, value: price * 0.985 },
43
+ { timestamp: entryTs - 3 * INTERVAL_MIN * 6e4, value: price * 0.99 }
44
+ ],
45
+ points: [
46
+ { timestamp: entryTs - 12 * INTERVAL_MIN * 6e4, value: price * 0.98 },
47
+ { timestamp: entryTs - 2 * INTERVAL_MIN * 6e4, value: price * 0.995 }
48
+ ]
49
+ });
50
+ var main = async () => {
51
+ const startTs = now - CANDLES * INTERVAL_MIN * 6e4;
52
+ const candles = makeCandles(120, startTs);
53
+ const btcCandles = makeCandles(42e3, startTs);
54
+ const lastCandle = candles[candles.length - 1];
55
+ const currentPrice = lastCandle.close;
56
+ const signal = {
57
+ signalId: "mock-signal",
58
+ symbol: "ETHUSDT",
59
+ interval: String(INTERVAL_MIN),
60
+ strategy: "TRENDLINE",
61
+ direction: "LONG",
62
+ timestamp: lastCandle.timestamp,
63
+ figures: {
64
+ trendLine: buildTrendline(lastCandle.timestamp, currentPrice)
65
+ },
66
+ prices: {
67
+ currentPrice,
68
+ takeProfitPrice: currentPrice * 1.03,
69
+ stopLossPrice: currentPrice * 0.99,
70
+ riskRatio: 2.2
71
+ },
72
+ indicators: {
73
+ correlation: 0.18,
74
+ touches: 6,
75
+ distance: 0.7,
76
+ candles15m: candles.slice(-import_constants.ML_BASE_CANDLES_WINDOW),
77
+ btcCandles15m: btcCandles.slice(-import_constants.ML_BASE_CANDLES_WINDOW),
78
+ atr: makeSeries(10, 1.2, 0.02),
79
+ atrPct: makeSeries(10, 1.05, 5e-3),
80
+ maFast: makeSeries(10, currentPrice * 0.995, 0.01),
81
+ maMedium: makeSeries(10, currentPrice * 1, 5e-3),
82
+ maSlow: makeSeries(10, currentPrice * 1.01, 2e-3),
83
+ bbUpper: makeSeries(10, currentPrice * 1.02, 0.01),
84
+ bbMiddle: makeSeries(10, currentPrice, 5e-3),
85
+ bbLower: makeSeries(10, currentPrice * 0.98, 0.01),
86
+ obv: makeSeries(10, 2e3, 50),
87
+ smaObv: makeSeries(10, 1950, 45),
88
+ macd: makeSeries(10, 0.2, 0.01),
89
+ macdSignal: makeSeries(10, 0.18, 0.01),
90
+ macdHistogram: makeSeries(10, 0.02, 5e-3),
91
+ price24hPcnt: makeSeries(10, 1.2, 0.05),
92
+ price1hPcnt: makeSeries(10, 0.2, 0.02),
93
+ highPrice1h: makeSeries(10, currentPrice * 1.01, 0.01),
94
+ lowPrice1h: makeSeries(10, currentPrice * 0.99, 0.01),
95
+ volume1h: makeSeries(10, 1200, 30),
96
+ highPrice24h: makeSeries(10, currentPrice * 1.03, 0.02),
97
+ lowPrice24h: makeSeries(10, currentPrice * 0.97, 0.02),
98
+ volume24h: makeSeries(10, 8e3, 200)
99
+ }
100
+ };
101
+ const { TRENDLINE, HIGHS, LOWS, ML_THRESHOLD } = import_strategies.trendLineDefaultConfig;
102
+ const fullRow = (0, import_ml.buildMlTrainingRow)(
103
+ {
104
+ signal,
105
+ context: {
106
+ strategyName: "TrendLine",
107
+ strategyConfig: {
108
+ TRENDLINE_CONFIG: TRENDLINE,
109
+ HIGHS,
110
+ LOWS
111
+ },
112
+ symbol: signal.symbol
113
+ }
114
+ },
115
+ null
116
+ );
117
+ const row = (0, import_ml.trimMlTrainingRowWindows)(fullRow, 5);
118
+ const features = (0, import_ml.buildMlFeatures)(row);
119
+ const mlResult = await (0, import_ml.fetchMlThreshold)({
120
+ strategy: "TrendLine",
121
+ features,
122
+ threshold: ML_THRESHOLD
123
+ });
124
+ if (!mlResult) {
125
+ console.log("ML result is null (check ml-infer or ML_GRPC_ADDRESS).");
126
+ return;
127
+ }
128
+ console.log("ML result:", mlResult);
129
+ };
130
+ main().catch((err) => {
131
+ console.error("test-ml failed:", err);
132
+ process.exit(1);
133
+ });
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+
3
+ // src/scripts/test.ts
4
+ var import_config = require("dotenv/config");
5
+ var import_connectors = require("@tradejs/connectors");
6
+ var getPositions = async (connector) => {
7
+ const res = await connector.getPositions();
8
+ console.log("res", res);
9
+ };
10
+ var main = async () => {
11
+ const byBitConnector = await import_connectors.connectors.ByBit({
12
+ userName: "root"
13
+ });
14
+ await getPositions(byBitConnector);
15
+ };
16
+ main();
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (let key of __getOwnPropNames(from))
11
+ if (!__hasOwnProp.call(to, key) && key !== except)
12
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
+ }
14
+ return to;
15
+ };
16
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
+ // If the importer is in node compatibility mode or this is not an ESM
18
+ // file that has been converted to a CommonJS file using a Babel-
19
+ // compatible transform (i.e. "__esModule" has not been set), then set
20
+ // "default" to the CommonJS "module.exports" for node compatibility.
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+
25
+ // src/scripts/user-add.ts
26
+ var import_args = __toESM(require("args"));
27
+ var import_chalk = __toESM(require("chalk"));
28
+ var import_bcryptjs = __toESM(require("bcryptjs"));
29
+ var import_crypto = __toESM(require("crypto"));
30
+ var import_redis = require("@tradejs/infra/redis");
31
+ import_args.default.example("yarn user-add -u myname -p 123456", "Create or update user");
32
+ import_args.default.option(["u", "user"], "Username", "");
33
+ import_args.default.option(["p", "password"], "Password (plain text)", "");
34
+ import_args.default.option(["t", "token"], "Persistent token (optional)", "");
35
+ var flags = import_args.default.parse(process.argv);
36
+ var run = async () => {
37
+ const userName = String(flags.user || "").trim();
38
+ const password = String(flags.password || "");
39
+ const requestedToken = String(flags.token || "").trim();
40
+ if (!userName || !password) {
41
+ console.error(import_chalk.default.red("Missing -U <user> or -P <password>"));
42
+ process.exit(1);
43
+ }
44
+ const passwordHash = await import_bcryptjs.default.hash(password, 10);
45
+ const existing = await (0, import_redis.getData)(import_redis.redisKeys.user(userName), null);
46
+ const existingToken = typeof existing === "object" && existing ? existing.token : void 0;
47
+ const token = requestedToken || existingToken || (userName === "root" ? import_crypto.default.randomBytes(24).toString("hex") : void 0);
48
+ const next = {
49
+ ...existing ?? {},
50
+ passwordHash,
51
+ userName,
52
+ ...token ? { token } : {},
53
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
54
+ };
55
+ await (0, import_redis.setData)(import_redis.redisKeys.user(userName), next, {
56
+ expire: 0
57
+ });
58
+ console.log(import_chalk.default.green(`User ${userName} updated`));
59
+ if (token) {
60
+ console.log(import_chalk.default.gray(`token: ${token}`));
61
+ }
62
+ process.exit(0);
63
+ };
64
+ run();
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+
3
+ // src/workers/testerWorker.ts
4
+ var import_backtest = require("@tradejs/node/backtest");
5
+ var import_logger = require("@tradejs/infra/logger");
6
+ var import_ml = require("@tradejs/infra/ml");
7
+ var import_redis = require("@tradejs/infra/redis");
8
+ var isProcessing = false;
9
+ process.on(
10
+ "message",
11
+ async ({ chunkId, userName }) => {
12
+ if (isProcessing) {
13
+ return;
14
+ }
15
+ isProcessing = true;
16
+ try {
17
+ const testSuite = await (0, import_redis.getData)(
18
+ import_redis.redisKeys.cacheChunk(userName, chunkId)
19
+ );
20
+ for await (const test of testSuite) {
21
+ try {
22
+ const testResult = await (0, import_backtest.testing)(test);
23
+ if (!testResult) {
24
+ throw new Error("No result");
25
+ }
26
+ const { stat, orderLogId } = testResult;
27
+ process.send?.({
28
+ stat,
29
+ orderLogId,
30
+ test
31
+ });
32
+ } catch (error) {
33
+ const errorMessage = error instanceof Error ? error.message : String(error);
34
+ const errorStack = error instanceof Error ? error.stack : void 0;
35
+ import_logger.logger.error("tester worker error: %s", errorMessage);
36
+ process.send?.({
37
+ error: true,
38
+ id: test.name,
39
+ msg: {
40
+ message: errorMessage,
41
+ stack: errorStack
42
+ }
43
+ });
44
+ }
45
+ }
46
+ } finally {
47
+ await (0, import_ml.closeAllMlDatasetWriters)();
48
+ (0, import_backtest.resetTestingKlineCache)();
49
+ }
50
+ process.send?.({ done: true });
51
+ process.disconnect?.();
52
+ process.exit(0);
53
+ }
54
+ );