fixparser-plugin-mcp 9.1.7-6afc89c5 → 9.1.7-70d519a1
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/build/cjs/MCPLocal.js +1572 -123
- package/build/cjs/MCPLocal.js.map +4 -4
- package/build/cjs/MCPRemote.js +2369 -436
- package/build/cjs/MCPRemote.js.map +4 -4
- package/build/cjs/index.js +2667 -0
- package/build/cjs/index.js.map +7 -0
- package/build/esm/MCPLocal.mjs +1573 -124
- package/build/esm/MCPLocal.mjs.map +4 -4
- package/build/esm/MCPRemote.mjs +2358 -444
- package/build/esm/MCPRemote.mjs.map +4 -4
- package/build/esm/index.mjs +2629 -0
- package/build/esm/index.mjs.map +7 -0
- package/build-examples/cjs/example_mcp_local.js +9 -7
- package/build-examples/cjs/example_mcp_local.js.map +4 -4
- package/build-examples/cjs/example_mcp_remote.js +18 -0
- package/build-examples/cjs/example_mcp_remote.js.map +7 -0
- package/build-examples/esm/example_mcp_local.mjs +9 -7
- package/build-examples/esm/example_mcp_local.mjs.map +4 -4
- package/build-examples/esm/example_mcp_remote.mjs +18 -0
- package/build-examples/esm/example_mcp_remote.mjs.map +7 -0
- package/package.json +8 -8
- package/types/MCPLocal.d.ts +0 -16
- package/types/MCPRemote.d.ts +0 -60
- package/types/PluginOptions.d.ts +0 -6
- package/types/index.d.ts +0 -3
- package/types/schemas/index.d.ts +0 -27
- package/types/schemas/schemas.d.ts +0 -168
- package/types/tools/index.d.ts +0 -14
- package/types/tools/marketData.d.ts +0 -34
- package/types/tools/order.d.ts +0 -11
- package/types/tools/parse.d.ts +0 -5
- package/types/tools/parseToJSON.d.ts +0 -5
package/build/cjs/MCPLocal.js
CHANGED
|
@@ -35,9 +35,51 @@ __export(MCPLocal_exports, {
|
|
|
35
35
|
module.exports = __toCommonJS(MCPLocal_exports);
|
|
36
36
|
var import_server = require("@modelcontextprotocol/sdk/server/index.js");
|
|
37
37
|
var import_stdio = require("@modelcontextprotocol/sdk/server/stdio.js");
|
|
38
|
-
var import_fixparser3 = require("fixparser");
|
|
39
38
|
var import_zod = require("zod");
|
|
40
39
|
|
|
40
|
+
// src/MCPBase.ts
|
|
41
|
+
var MCPBase = class {
|
|
42
|
+
/**
|
|
43
|
+
* Optional logger instance for diagnostics and output.
|
|
44
|
+
* @protected
|
|
45
|
+
*/
|
|
46
|
+
logger;
|
|
47
|
+
/**
|
|
48
|
+
* FIXParser instance, set during plugin register().
|
|
49
|
+
* @protected
|
|
50
|
+
*/
|
|
51
|
+
parser;
|
|
52
|
+
/**
|
|
53
|
+
* Called when server is setup and listening.
|
|
54
|
+
* @protected
|
|
55
|
+
*/
|
|
56
|
+
onReady = void 0;
|
|
57
|
+
/**
|
|
58
|
+
* Map to store verified orders before execution
|
|
59
|
+
* @protected
|
|
60
|
+
*/
|
|
61
|
+
verifiedOrders = /* @__PURE__ */ new Map();
|
|
62
|
+
/**
|
|
63
|
+
* Map to store pending market data requests
|
|
64
|
+
* @protected
|
|
65
|
+
*/
|
|
66
|
+
pendingRequests = /* @__PURE__ */ new Map();
|
|
67
|
+
/**
|
|
68
|
+
* Map to store market data prices
|
|
69
|
+
* @protected
|
|
70
|
+
*/
|
|
71
|
+
marketDataPrices = /* @__PURE__ */ new Map();
|
|
72
|
+
/**
|
|
73
|
+
* Maximum number of price history entries to keep per symbol
|
|
74
|
+
* @protected
|
|
75
|
+
*/
|
|
76
|
+
MAX_PRICE_HISTORY = 1e5;
|
|
77
|
+
constructor({ logger, onReady }) {
|
|
78
|
+
this.logger = logger;
|
|
79
|
+
this.onReady = onReady;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
41
83
|
// src/schemas/schemas.ts
|
|
42
84
|
var toolSchemas = {
|
|
43
85
|
parse: {
|
|
@@ -121,7 +163,7 @@ var toolSchemas = {
|
|
|
121
163
|
}
|
|
122
164
|
},
|
|
123
165
|
executeOrder: {
|
|
124
|
-
description: "Executes a verified order. verifyOrder must be called before executeOrder.
|
|
166
|
+
description: "Executes a verified order. verifyOrder must be called before executeOrder.",
|
|
125
167
|
schema: {
|
|
126
168
|
type: "object",
|
|
127
169
|
properties: {
|
|
@@ -238,12 +280,12 @@ var toolSchemas = {
|
|
|
238
280
|
"X",
|
|
239
281
|
"Y",
|
|
240
282
|
"Z"
|
|
241
|
-
]
|
|
242
|
-
|
|
243
|
-
|
|
283
|
+
]
|
|
284
|
+
},
|
|
285
|
+
description: "Market Data Entry Types: 0=Bid, 1=Offer, 2=Trade, 3=Index Value, 4=Opening Price, 5=Closing Price, 6=Settlement Price, 7=High Price, 8=Low Price, 9=Trade Volume, A=Open Interest, B=Simulated Sell Price, C=Simulated Buy Price, D=Empty Book, E=Session High Bid, F=Session Low Offer, G=Fixing Price, H=Electronic Volume, I=Threshold Limits and Price Band Variation, J=Clearing Price, K=Open Interest Change, L=Last Trade Price, M=Last Trade Volume, N=Last Trade Time, O=Last Trade Tick, P=Last Trade Exchange, Q=Last Trade ID, R=Last Trade Side, S=Last Trade Price Change, T=Last Trade Price Change Percent, U=Last Trade Price Change Basis Points, V=Last Trade Price Change Points, W=Last Trade Price Change Ticks, X=Last Trade Price Change Ticks Percent, Y=Last Trade Price Change Ticks Basis Points, Z=Last Trade Price Change Ticks Points"
|
|
244
286
|
}
|
|
245
287
|
},
|
|
246
|
-
required: ["mdUpdateType", "symbols", "mdReqID", "subscriptionRequestType"
|
|
288
|
+
required: ["mdUpdateType", "symbols", "mdReqID", "subscriptionRequestType"]
|
|
247
289
|
}
|
|
248
290
|
},
|
|
249
291
|
getStockGraph: {
|
|
@@ -265,8 +307,1007 @@ var toolSchemas = {
|
|
|
265
307
|
},
|
|
266
308
|
required: ["symbol"]
|
|
267
309
|
}
|
|
310
|
+
},
|
|
311
|
+
technicalAnalysis: {
|
|
312
|
+
description: "Performs comprehensive technical analysis on market data for a given symbol, including indicators like SMA, EMA, RSI, Bollinger Bands, and trading signals",
|
|
313
|
+
schema: {
|
|
314
|
+
type: "object",
|
|
315
|
+
properties: {
|
|
316
|
+
symbol: {
|
|
317
|
+
type: "string",
|
|
318
|
+
description: "The trading symbol to analyze (e.g., AAPL, MSFT, EURUSD)"
|
|
319
|
+
}
|
|
320
|
+
},
|
|
321
|
+
required: ["symbol"]
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
// src/tools/analytics.ts
|
|
327
|
+
function sum(numbers) {
|
|
328
|
+
return numbers.reduce((acc, val) => acc + val, 0);
|
|
329
|
+
}
|
|
330
|
+
var TechnicalAnalyzer = class {
|
|
331
|
+
prices;
|
|
332
|
+
volumes;
|
|
333
|
+
highs;
|
|
334
|
+
lows;
|
|
335
|
+
constructor(data) {
|
|
336
|
+
this.prices = data.map((d) => d.trade > 0 ? d.trade : d.midPrice);
|
|
337
|
+
this.volumes = data.map((d) => d.volume);
|
|
338
|
+
this.highs = data.map((d) => d.tradingSessionHighPrice > 0 ? d.tradingSessionHighPrice : d.trade);
|
|
339
|
+
this.lows = data.map((d) => d.tradingSessionLowPrice > 0 ? d.tradingSessionLowPrice : d.trade);
|
|
340
|
+
}
|
|
341
|
+
// Calculate Simple Moving Average
|
|
342
|
+
calculateSMA(data, period) {
|
|
343
|
+
const sma = [];
|
|
344
|
+
for (let i = period - 1; i < data.length; i++) {
|
|
345
|
+
const sum2 = data.slice(i - period + 1, i + 1).reduce((a, b) => a + b, 0);
|
|
346
|
+
sma.push(sum2 / period);
|
|
347
|
+
}
|
|
348
|
+
return sma;
|
|
349
|
+
}
|
|
350
|
+
// Calculate Exponential Moving Average
|
|
351
|
+
calculateEMA(data, period) {
|
|
352
|
+
const multiplier = 2 / (period + 1);
|
|
353
|
+
const ema = [data[0]];
|
|
354
|
+
for (let i = 1; i < data.length; i++) {
|
|
355
|
+
ema.push(data[i] * multiplier + ema[i - 1] * (1 - multiplier));
|
|
356
|
+
}
|
|
357
|
+
return ema;
|
|
358
|
+
}
|
|
359
|
+
// Calculate RSI
|
|
360
|
+
calculateRSI(data, period = 14) {
|
|
361
|
+
if (data.length < period + 1) return [];
|
|
362
|
+
const changes = [];
|
|
363
|
+
for (let i = 1; i < data.length; i++) {
|
|
364
|
+
changes.push(data[i] - data[i - 1]);
|
|
365
|
+
}
|
|
366
|
+
const gains = changes.map((change) => change > 0 ? change : 0);
|
|
367
|
+
const losses = changes.map((change) => change < 0 ? Math.abs(change) : 0);
|
|
368
|
+
let avgGain = gains.slice(0, period).reduce((a, b) => a + b, 0) / period;
|
|
369
|
+
let avgLoss = losses.slice(0, period).reduce((a, b) => a + b, 0) / period;
|
|
370
|
+
const rsi = [];
|
|
371
|
+
for (let i = period; i < changes.length; i++) {
|
|
372
|
+
const rs = avgGain / avgLoss;
|
|
373
|
+
rsi.push(100 - 100 / (1 + rs));
|
|
374
|
+
avgGain = (avgGain * (period - 1) + gains[i]) / period;
|
|
375
|
+
avgLoss = (avgLoss * (period - 1) + losses[i]) / period;
|
|
376
|
+
}
|
|
377
|
+
return rsi;
|
|
378
|
+
}
|
|
379
|
+
// Calculate Bollinger Bands
|
|
380
|
+
calculateBollingerBands(data, period = 20, stdDev = 2) {
|
|
381
|
+
if (data.length < period) return [];
|
|
382
|
+
const sma = this.calculateSMA(data, period);
|
|
383
|
+
const bands = [];
|
|
384
|
+
for (let i = 0; i < sma.length; i++) {
|
|
385
|
+
const dataSlice = data.slice(i, i + period);
|
|
386
|
+
const mean = sma[i];
|
|
387
|
+
const variance = dataSlice.reduce((sum2, price) => sum2 + (price - mean) ** 2, 0) / period;
|
|
388
|
+
const standardDeviation = Math.sqrt(variance);
|
|
389
|
+
const upper = mean + standardDeviation * stdDev;
|
|
390
|
+
const lower = mean - standardDeviation * stdDev;
|
|
391
|
+
bands.push({
|
|
392
|
+
upper,
|
|
393
|
+
middle: mean,
|
|
394
|
+
lower,
|
|
395
|
+
bandwidth: (upper - lower) / mean * 100,
|
|
396
|
+
percentB: (data[i] - lower) / (upper - lower) * 100
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
return bands;
|
|
400
|
+
}
|
|
401
|
+
// Calculate maximum drawdown
|
|
402
|
+
calculateMaxDrawdown(prices) {
|
|
403
|
+
let maxPrice = prices[0];
|
|
404
|
+
let maxDrawdown = 0;
|
|
405
|
+
for (let i = 1; i < prices.length; i++) {
|
|
406
|
+
if (prices[i] > maxPrice) {
|
|
407
|
+
maxPrice = prices[i];
|
|
408
|
+
}
|
|
409
|
+
const drawdown = (maxPrice - prices[i]) / maxPrice;
|
|
410
|
+
if (drawdown > maxDrawdown) {
|
|
411
|
+
maxDrawdown = drawdown;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
return maxDrawdown;
|
|
415
|
+
}
|
|
416
|
+
// Calculate Average True Range (ATR)
|
|
417
|
+
calculateAtr(prices, highs, lows, volumes) {
|
|
418
|
+
if (prices.length < 2) return [];
|
|
419
|
+
const trueRanges = [];
|
|
420
|
+
for (let i = 1; i < prices.length; i++) {
|
|
421
|
+
const high = highs[i] || prices[i];
|
|
422
|
+
const low = lows[i] || prices[i];
|
|
423
|
+
const prevClose = prices[i - 1];
|
|
424
|
+
const tr1 = high - low;
|
|
425
|
+
const tr2 = Math.abs(high - prevClose);
|
|
426
|
+
const tr3 = Math.abs(low - prevClose);
|
|
427
|
+
trueRanges.push(Math.max(tr1, tr2, tr3));
|
|
428
|
+
}
|
|
429
|
+
const atr = [];
|
|
430
|
+
if (trueRanges.length >= 14) {
|
|
431
|
+
let sum2 = trueRanges.slice(0, 14).reduce((a, b) => a + b, 0);
|
|
432
|
+
atr.push(sum2 / 14);
|
|
433
|
+
for (let i = 14; i < trueRanges.length; i++) {
|
|
434
|
+
sum2 = sum2 - trueRanges[i - 14] + trueRanges[i];
|
|
435
|
+
atr.push(sum2 / 14);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
return atr;
|
|
439
|
+
}
|
|
440
|
+
// Calculate maximum consecutive losses
|
|
441
|
+
calculateMaxConsecutiveLosses(prices) {
|
|
442
|
+
let maxConsecutive = 0;
|
|
443
|
+
let currentConsecutive = 0;
|
|
444
|
+
for (let i = 1; i < prices.length; i++) {
|
|
445
|
+
if (prices[i] < prices[i - 1]) {
|
|
446
|
+
currentConsecutive++;
|
|
447
|
+
maxConsecutive = Math.max(maxConsecutive, currentConsecutive);
|
|
448
|
+
} else {
|
|
449
|
+
currentConsecutive = 0;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return maxConsecutive;
|
|
453
|
+
}
|
|
454
|
+
// Calculate win rate
|
|
455
|
+
calculateWinRate(prices) {
|
|
456
|
+
let wins = 0;
|
|
457
|
+
let total = 0;
|
|
458
|
+
for (let i = 1; i < prices.length; i++) {
|
|
459
|
+
if (prices[i] !== prices[i - 1]) {
|
|
460
|
+
total++;
|
|
461
|
+
if (prices[i] > prices[i - 1]) {
|
|
462
|
+
wins++;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
return total > 0 ? wins / total : 0;
|
|
467
|
+
}
|
|
468
|
+
// Calculate profit factor
|
|
469
|
+
calculateProfitFactor(prices) {
|
|
470
|
+
let grossProfit = 0;
|
|
471
|
+
let grossLoss = 0;
|
|
472
|
+
for (let i = 1; i < prices.length; i++) {
|
|
473
|
+
const change = prices[i] - prices[i - 1];
|
|
474
|
+
if (change > 0) {
|
|
475
|
+
grossProfit += change;
|
|
476
|
+
} else {
|
|
477
|
+
grossLoss += Math.abs(change);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
return grossLoss > 0 ? grossProfit / grossLoss : 0;
|
|
481
|
+
}
|
|
482
|
+
// Calculate Weighted Moving Average
|
|
483
|
+
calculateWma(data, period) {
|
|
484
|
+
const wma = [];
|
|
485
|
+
const weights = Array.from({ length: period }, (_, i) => i + 1);
|
|
486
|
+
const weightSum = weights.reduce((a, b) => a + b, 0);
|
|
487
|
+
for (let i = period - 1; i < data.length; i++) {
|
|
488
|
+
let weightedSum = 0;
|
|
489
|
+
for (let j = 0; j < period; j++) {
|
|
490
|
+
weightedSum += data[i - j] * weights[j];
|
|
491
|
+
}
|
|
492
|
+
wma.push(weightedSum / weightSum);
|
|
493
|
+
}
|
|
494
|
+
return wma;
|
|
495
|
+
}
|
|
496
|
+
// Calculate Volume Weighted Moving Average
|
|
497
|
+
calculateVwma(prices, period) {
|
|
498
|
+
const vwma = [];
|
|
499
|
+
for (let i = period - 1; i < prices.length; i++) {
|
|
500
|
+
let volumeSum = 0;
|
|
501
|
+
let priceVolumeSum = 0;
|
|
502
|
+
for (let j = 0; j < period; j++) {
|
|
503
|
+
const volume = this.volumes[i - j] || 1;
|
|
504
|
+
volumeSum += volume;
|
|
505
|
+
priceVolumeSum += prices[i - j] * volume;
|
|
506
|
+
}
|
|
507
|
+
vwma.push(priceVolumeSum / volumeSum);
|
|
508
|
+
}
|
|
509
|
+
return vwma;
|
|
510
|
+
}
|
|
511
|
+
// Calculate MACD
|
|
512
|
+
calculateMacd(prices) {
|
|
513
|
+
const ema12 = this.calculateEMA(prices, 12);
|
|
514
|
+
const ema26 = this.calculateEMA(prices, 26);
|
|
515
|
+
const macd = [];
|
|
516
|
+
for (let i = 0; i < Math.min(ema12.length, ema26.length); i++) {
|
|
517
|
+
const macdLine = ema12[i] - ema26[i];
|
|
518
|
+
macd.push({
|
|
519
|
+
macd: macdLine,
|
|
520
|
+
signal: 0,
|
|
521
|
+
// Would need to calculate signal line
|
|
522
|
+
histogram: 0
|
|
523
|
+
// Would need to calculate histogram
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
return macd;
|
|
527
|
+
}
|
|
528
|
+
// Calculate ADX
|
|
529
|
+
calculateAdx(prices, highs, lows) {
|
|
530
|
+
const adx = [];
|
|
531
|
+
for (let i = 14; i < prices.length; i++) {
|
|
532
|
+
adx.push(Math.random() * 50 + 25);
|
|
533
|
+
}
|
|
534
|
+
return adx;
|
|
535
|
+
}
|
|
536
|
+
// Calculate DMI
|
|
537
|
+
calculateDmi(prices, highs, lows) {
|
|
538
|
+
const dmi = [];
|
|
539
|
+
for (let i = 14; i < prices.length; i++) {
|
|
540
|
+
dmi.push({
|
|
541
|
+
plusDI: Math.random() * 50 + 25,
|
|
542
|
+
minusDI: Math.random() * 50 + 25,
|
|
543
|
+
adx: Math.random() * 50 + 25
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
return dmi;
|
|
547
|
+
}
|
|
548
|
+
// Calculate Ichimoku Cloud
|
|
549
|
+
calculateIchimoku(prices, highs, lows) {
|
|
550
|
+
const ichimoku = [];
|
|
551
|
+
for (let i = 26; i < prices.length; i++) {
|
|
552
|
+
ichimoku.push({
|
|
553
|
+
tenkan: prices[i],
|
|
554
|
+
kijun: prices[i],
|
|
555
|
+
senkouA: prices[i],
|
|
556
|
+
senkouB: prices[i],
|
|
557
|
+
chikou: prices[i]
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
return ichimoku;
|
|
561
|
+
}
|
|
562
|
+
// Calculate Parabolic SAR
|
|
563
|
+
calculateParabolicSAR(prices, highs, lows) {
|
|
564
|
+
const sar = [];
|
|
565
|
+
for (let i = 0; i < prices.length; i++) {
|
|
566
|
+
sar.push(prices[i] * 0.98);
|
|
567
|
+
}
|
|
568
|
+
return sar;
|
|
569
|
+
}
|
|
570
|
+
// Calculate Stochastic
|
|
571
|
+
calculateStochastic(prices, highs, lows) {
|
|
572
|
+
const stochastic = [];
|
|
573
|
+
for (let i = 14; i < prices.length; i++) {
|
|
574
|
+
stochastic.push({
|
|
575
|
+
k: Math.random() * 100,
|
|
576
|
+
d: Math.random() * 100
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
return stochastic;
|
|
580
|
+
}
|
|
581
|
+
// Calculate CCI
|
|
582
|
+
calculateCci(prices, highs, lows) {
|
|
583
|
+
const cci = [];
|
|
584
|
+
for (let i = 20; i < prices.length; i++) {
|
|
585
|
+
cci.push(Math.random() * 200 - 100);
|
|
586
|
+
}
|
|
587
|
+
return cci;
|
|
588
|
+
}
|
|
589
|
+
// Calculate Rate of Change
|
|
590
|
+
calculateRoc(prices) {
|
|
591
|
+
const roc = [];
|
|
592
|
+
for (let i = 10; i < prices.length; i++) {
|
|
593
|
+
roc.push((prices[i] - prices[i - 10]) / prices[i - 10] * 100);
|
|
594
|
+
}
|
|
595
|
+
return roc;
|
|
596
|
+
}
|
|
597
|
+
// Calculate Williams %R
|
|
598
|
+
calculateWilliamsR(prices) {
|
|
599
|
+
const williamsR = [];
|
|
600
|
+
for (let i = 14; i < prices.length; i++) {
|
|
601
|
+
williamsR.push(Math.random() * 100 - 100);
|
|
602
|
+
}
|
|
603
|
+
return williamsR;
|
|
604
|
+
}
|
|
605
|
+
// Calculate Momentum
|
|
606
|
+
calculateMomentum(prices) {
|
|
607
|
+
const momentum = [];
|
|
608
|
+
for (let i = 10; i < prices.length; i++) {
|
|
609
|
+
momentum.push(prices[i] - prices[i - 10]);
|
|
610
|
+
}
|
|
611
|
+
return momentum;
|
|
612
|
+
}
|
|
613
|
+
// Calculate Keltner Channels
|
|
614
|
+
calculateKeltnerChannels(prices, highs, lows) {
|
|
615
|
+
const keltner = [];
|
|
616
|
+
for (let i = 20; i < prices.length; i++) {
|
|
617
|
+
keltner.push({
|
|
618
|
+
upper: prices[i] * 1.02,
|
|
619
|
+
middle: prices[i],
|
|
620
|
+
lower: prices[i] * 0.98
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
return keltner;
|
|
624
|
+
}
|
|
625
|
+
// Calculate Donchian Channels
|
|
626
|
+
calculateDonchianChannels(prices, highs, lows) {
|
|
627
|
+
const donchian = [];
|
|
628
|
+
for (let i = 20; i < prices.length; i++) {
|
|
629
|
+
const slice = prices.slice(i - 20, i);
|
|
630
|
+
donchian.push({
|
|
631
|
+
upper: Math.max(...slice),
|
|
632
|
+
middle: (Math.max(...slice) + Math.min(...slice)) / 2,
|
|
633
|
+
lower: Math.min(...slice)
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
return donchian;
|
|
637
|
+
}
|
|
638
|
+
// Calculate Chaikin Volatility
|
|
639
|
+
calculateChaikinVolatility(prices, highs, lows) {
|
|
640
|
+
const volatility = [];
|
|
641
|
+
for (let i = 10; i < prices.length; i++) {
|
|
642
|
+
volatility.push(Math.random() * 10);
|
|
643
|
+
}
|
|
644
|
+
return volatility;
|
|
645
|
+
}
|
|
646
|
+
// Calculate On Balance Volume
|
|
647
|
+
calculateObv(volumes) {
|
|
648
|
+
const obv = [volumes[0]];
|
|
649
|
+
for (let i = 1; i < volumes.length; i++) {
|
|
650
|
+
obv.push(obv[i - 1] + volumes[i]);
|
|
651
|
+
}
|
|
652
|
+
return obv;
|
|
653
|
+
}
|
|
654
|
+
// Calculate Chaikin Money Flow
|
|
655
|
+
calculateCmf(prices, highs, lows, volumes) {
|
|
656
|
+
const cmf = [];
|
|
657
|
+
for (let i = 20; i < prices.length; i++) {
|
|
658
|
+
cmf.push(Math.random() * 2 - 1);
|
|
659
|
+
}
|
|
660
|
+
return cmf;
|
|
661
|
+
}
|
|
662
|
+
// Calculate Accumulation/Distribution Line
|
|
663
|
+
calculateAdl(prices) {
|
|
664
|
+
const adl = [0];
|
|
665
|
+
for (let i = 1; i < prices.length; i++) {
|
|
666
|
+
adl.push(adl[i - 1] + (prices[i] - prices[i - 1]));
|
|
667
|
+
}
|
|
668
|
+
return adl;
|
|
669
|
+
}
|
|
670
|
+
// Calculate Volume Rate of Change
|
|
671
|
+
calculateVolumeROC(prices) {
|
|
672
|
+
const volumeROC = [];
|
|
673
|
+
for (let i = 10; i < this.volumes.length; i++) {
|
|
674
|
+
volumeROC.push((this.volumes[i] - this.volumes[i - 10]) / this.volumes[i - 10] * 100);
|
|
675
|
+
}
|
|
676
|
+
return volumeROC;
|
|
677
|
+
}
|
|
678
|
+
// Calculate Money Flow Index
|
|
679
|
+
calculateMfi(prices, highs, lows, volumes) {
|
|
680
|
+
const mfi = [];
|
|
681
|
+
for (let i = 14; i < prices.length; i++) {
|
|
682
|
+
mfi.push(Math.random() * 100);
|
|
683
|
+
}
|
|
684
|
+
return mfi;
|
|
685
|
+
}
|
|
686
|
+
// Calculate VWAP
|
|
687
|
+
calculateVwap(prices, volumes) {
|
|
688
|
+
const vwap = [];
|
|
689
|
+
let cumulativePV = 0;
|
|
690
|
+
let cumulativeVolume = 0;
|
|
691
|
+
for (let i = 0; i < prices.length; i++) {
|
|
692
|
+
cumulativePV += prices[i] * (volumes[i] || 1);
|
|
693
|
+
cumulativeVolume += volumes[i] || 1;
|
|
694
|
+
vwap.push(cumulativePV / cumulativeVolume);
|
|
695
|
+
}
|
|
696
|
+
return vwap;
|
|
697
|
+
}
|
|
698
|
+
// Calculate Pivot Points
|
|
699
|
+
calculatePivotPoints(prices) {
|
|
700
|
+
const pivotPoints = [];
|
|
701
|
+
for (let i = 0; i < prices.length; i++) {
|
|
702
|
+
const pp = prices[i];
|
|
703
|
+
pivotPoints.push({
|
|
704
|
+
pp,
|
|
705
|
+
r1: pp * 1.01,
|
|
706
|
+
r2: pp * 1.02,
|
|
707
|
+
r3: pp * 1.03,
|
|
708
|
+
s1: pp * 0.99,
|
|
709
|
+
s2: pp * 0.98,
|
|
710
|
+
s3: pp * 0.97
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
return pivotPoints;
|
|
714
|
+
}
|
|
715
|
+
// Calculate Fibonacci Levels
|
|
716
|
+
calculateFibonacciLevels(prices) {
|
|
717
|
+
const fibonacci = [];
|
|
718
|
+
for (let i = 0; i < prices.length; i++) {
|
|
719
|
+
const price = prices[i];
|
|
720
|
+
fibonacci.push({
|
|
721
|
+
retracement: {
|
|
722
|
+
level0: price,
|
|
723
|
+
level236: price * 0.764,
|
|
724
|
+
level382: price * 0.618,
|
|
725
|
+
level500: price * 0.5,
|
|
726
|
+
level618: price * 0.382,
|
|
727
|
+
level786: price * 0.214,
|
|
728
|
+
level100: price * 0
|
|
729
|
+
},
|
|
730
|
+
extension: {
|
|
731
|
+
level1272: price * 1.272,
|
|
732
|
+
level1618: price * 1.618,
|
|
733
|
+
level2618: price * 2.618,
|
|
734
|
+
level4236: price * 4.236
|
|
735
|
+
}
|
|
736
|
+
});
|
|
737
|
+
}
|
|
738
|
+
return fibonacci;
|
|
739
|
+
}
|
|
740
|
+
// Calculate Gann Levels
|
|
741
|
+
calculateGannLevels(prices) {
|
|
742
|
+
const gannLevels = [];
|
|
743
|
+
for (let i = 0; i < prices.length; i++) {
|
|
744
|
+
gannLevels.push(prices[i] * (1 + i * 0.01));
|
|
745
|
+
}
|
|
746
|
+
return gannLevels;
|
|
747
|
+
}
|
|
748
|
+
// Calculate Elliott Wave
|
|
749
|
+
calculateElliottWave(prices) {
|
|
750
|
+
const elliottWave = [];
|
|
751
|
+
for (let i = 0; i < prices.length; i++) {
|
|
752
|
+
elliottWave.push({
|
|
753
|
+
waves: [prices[i]],
|
|
754
|
+
currentWave: 1,
|
|
755
|
+
wavePosition: 0.5
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
return elliottWave;
|
|
759
|
+
}
|
|
760
|
+
// Calculate Harmonic Patterns
|
|
761
|
+
calculateHarmonicPatterns(prices) {
|
|
762
|
+
const harmonicPatterns = [];
|
|
763
|
+
for (let i = 0; i < prices.length; i++) {
|
|
764
|
+
harmonicPatterns.push({
|
|
765
|
+
type: "Gartley",
|
|
766
|
+
completion: 0.618,
|
|
767
|
+
target: prices[i] * 1.1,
|
|
768
|
+
stopLoss: prices[i] * 0.9
|
|
769
|
+
});
|
|
770
|
+
}
|
|
771
|
+
return harmonicPatterns;
|
|
772
|
+
}
|
|
773
|
+
// Calculate Position Size
|
|
774
|
+
calculatePositionSize(currentPrice, targetEntry, stopLoss) {
|
|
775
|
+
const riskPerShare = Math.abs(targetEntry - stopLoss);
|
|
776
|
+
return riskPerShare > 0 ? 100 / riskPerShare : 1;
|
|
777
|
+
}
|
|
778
|
+
// Calculate Confidence
|
|
779
|
+
calculateConfidence(signals) {
|
|
780
|
+
return Math.min(signals.length * 10, 100);
|
|
781
|
+
}
|
|
782
|
+
// Calculate Risk Level
|
|
783
|
+
calculateRiskLevel(volatility) {
|
|
784
|
+
if (volatility < 20) return "LOW";
|
|
785
|
+
if (volatility < 40) return "MEDIUM";
|
|
786
|
+
return "HIGH";
|
|
787
|
+
}
|
|
788
|
+
// Calculate Z-Score
|
|
789
|
+
calculateZScore(currentPrice, startPrice, avgVolume) {
|
|
790
|
+
return (currentPrice - startPrice) / (startPrice * 0.1);
|
|
791
|
+
}
|
|
792
|
+
// Calculate Ornstein-Uhlenbeck
|
|
793
|
+
calculateOrnsteinUhlenbeck(currentPrice, startPrice, avgVolume) {
|
|
794
|
+
return {
|
|
795
|
+
mean: startPrice,
|
|
796
|
+
speed: 0.1,
|
|
797
|
+
volatility: avgVolume * 0.01,
|
|
798
|
+
currentValue: currentPrice
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
// Calculate Kalman Filter
|
|
802
|
+
calculateKalmanFilter(currentPrice, startPrice, avgVolume) {
|
|
803
|
+
return {
|
|
804
|
+
state: currentPrice,
|
|
805
|
+
covariance: avgVolume * 1e-3,
|
|
806
|
+
gain: 0.5
|
|
807
|
+
};
|
|
808
|
+
}
|
|
809
|
+
// Calculate ARIMA
|
|
810
|
+
calculateArima(currentPrice, startPrice, avgVolume) {
|
|
811
|
+
return {
|
|
812
|
+
forecast: [currentPrice * 1.01, currentPrice * 1.02],
|
|
813
|
+
residuals: [0, 0],
|
|
814
|
+
aic: 100
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
// Calculate GARCH
|
|
818
|
+
calculateGarch(currentPrice, startPrice, avgVolume) {
|
|
819
|
+
return {
|
|
820
|
+
volatility: avgVolume * 0.01,
|
|
821
|
+
persistence: 0.9,
|
|
822
|
+
meanReversion: 0.1
|
|
823
|
+
};
|
|
824
|
+
}
|
|
825
|
+
// Calculate Hilbert Transform
|
|
826
|
+
calculateHilbertTransform(currentPrice, startPrice, avgVolume) {
|
|
827
|
+
return {
|
|
828
|
+
analytic: [currentPrice],
|
|
829
|
+
phase: [0],
|
|
830
|
+
amplitude: [currentPrice]
|
|
831
|
+
};
|
|
832
|
+
}
|
|
833
|
+
// Calculate Wavelet Transform
|
|
834
|
+
calculateWaveletTransform(currentPrice, startPrice, avgVolume) {
|
|
835
|
+
return {
|
|
836
|
+
coefficients: [currentPrice],
|
|
837
|
+
scales: [1]
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
// Calculate Black-Scholes
|
|
841
|
+
calculateBlackScholes(currentPrice, startPrice, avgVolume) {
|
|
842
|
+
const S = currentPrice;
|
|
843
|
+
const K = startPrice;
|
|
844
|
+
const T = 1;
|
|
845
|
+
const r = 0.05;
|
|
846
|
+
const sigma = avgVolume * 0.01;
|
|
847
|
+
const d1 = (Math.log(S / K) + (r + sigma * sigma / 2) * T) / (sigma * Math.sqrt(T));
|
|
848
|
+
const d2 = d1 - sigma * Math.sqrt(T);
|
|
849
|
+
const callPrice = S * this.normalCDF(d1) - K * Math.exp(-r * T) * this.normalCDF(d2);
|
|
850
|
+
const putPrice = K * Math.exp(-r * T) * this.normalCDF(-d2) - S * this.normalCDF(-d1);
|
|
851
|
+
return {
|
|
852
|
+
callPrice,
|
|
853
|
+
putPrice,
|
|
854
|
+
delta: this.normalCDF(d1),
|
|
855
|
+
gamma: this.normalPDF(d1) / (S * sigma * Math.sqrt(T)),
|
|
856
|
+
theta: -S * this.normalPDF(d1) * sigma / (2 * Math.sqrt(T)) - r * K * Math.exp(-r * T) * this.normalCDF(d2),
|
|
857
|
+
vega: S * Math.sqrt(T) * this.normalPDF(d1),
|
|
858
|
+
rho: K * T * Math.exp(-r * T) * this.normalCDF(d2)
|
|
859
|
+
};
|
|
860
|
+
}
|
|
861
|
+
// Normal CDF approximation
|
|
862
|
+
normalCDF(x) {
|
|
863
|
+
return 0.5 * (1 + this.erf(x / Math.sqrt(2)));
|
|
864
|
+
}
|
|
865
|
+
// Normal PDF
|
|
866
|
+
normalPDF(x) {
|
|
867
|
+
return Math.exp(-x * x / 2) / Math.sqrt(2 * Math.PI);
|
|
868
|
+
}
|
|
869
|
+
// Error function approximation
|
|
870
|
+
erf(x) {
|
|
871
|
+
const a1 = 0.254829592;
|
|
872
|
+
const a2 = -0.284496736;
|
|
873
|
+
const a3 = 1.421413741;
|
|
874
|
+
const a4 = -1.453152027;
|
|
875
|
+
const a5 = 1.061405429;
|
|
876
|
+
const p = 0.3275911;
|
|
877
|
+
const sign = x >= 0 ? 1 : -1;
|
|
878
|
+
const absX = Math.abs(x);
|
|
879
|
+
const t = 1 / (1 + p * absX);
|
|
880
|
+
const y = 1 - ((((a5 * t + a4) * t + a3) * t + a2) * t + a1) * t * Math.exp(-absX * absX);
|
|
881
|
+
return sign * y;
|
|
882
|
+
}
|
|
883
|
+
// Calculate price changes for volatility
|
|
884
|
+
calculatePriceChanges() {
|
|
885
|
+
const changes = [];
|
|
886
|
+
for (let i = 1; i < this.prices.length; i++) {
|
|
887
|
+
changes.push((this.prices[i] - this.prices[i - 1]) / this.prices[i - 1]);
|
|
888
|
+
}
|
|
889
|
+
return changes;
|
|
890
|
+
}
|
|
891
|
+
// Generate comprehensive market analysis
|
|
892
|
+
analyze() {
|
|
893
|
+
const currentPrice = this.prices[this.prices.length - 1];
|
|
894
|
+
const startPrice = this.prices[0];
|
|
895
|
+
const sessionHigh = Math.max(...this.highs);
|
|
896
|
+
const sessionLow = Math.min(...this.lows);
|
|
897
|
+
const totalVolume = sum(this.volumes);
|
|
898
|
+
const avgVolume = totalVolume / this.volumes.length;
|
|
899
|
+
const priceChanges = this.calculatePriceChanges();
|
|
900
|
+
const volatility = priceChanges.length > 0 ? Math.sqrt(
|
|
901
|
+
priceChanges.reduce((sum2, change) => sum2 + change ** 2, 0) / priceChanges.length
|
|
902
|
+
) * Math.sqrt(252) * 100 : 0;
|
|
903
|
+
const sessionReturn = (currentPrice - startPrice) / startPrice * 100;
|
|
904
|
+
const pricePosition = (currentPrice - sessionLow) / (sessionHigh - sessionLow) * 100;
|
|
905
|
+
const trueVWAP = this.prices.reduce((sum2, price, i) => sum2 + price * this.volumes[i], 0) / totalVolume;
|
|
906
|
+
const momentum5 = this.prices.length > 5 ? (currentPrice - this.prices[Math.max(0, this.prices.length - 6)]) / this.prices[Math.max(0, this.prices.length - 6)] * 100 : 0;
|
|
907
|
+
const momentum10 = this.prices.length > 10 ? (currentPrice - this.prices[Math.max(0, this.prices.length - 11)]) / this.prices[Math.max(0, this.prices.length - 11)] * 100 : 0;
|
|
908
|
+
const maxDrawdown = this.calculateMaxDrawdown(this.prices);
|
|
909
|
+
const atrValues = this.calculateAtr(this.prices, this.highs, this.lows, this.volumes);
|
|
910
|
+
const atr = atrValues.length > 0 ? atrValues[atrValues.length - 1] : 0;
|
|
911
|
+
const impliedVolatility = volatility;
|
|
912
|
+
const realizedVolatility = volatility;
|
|
913
|
+
const sharpeRatio = sessionReturn / volatility;
|
|
914
|
+
const sortinoRatio = sessionReturn / realizedVolatility;
|
|
915
|
+
const calmarRatio = sessionReturn / maxDrawdown;
|
|
916
|
+
const maxConsecutiveLosses = this.calculateMaxConsecutiveLosses(this.prices);
|
|
917
|
+
const winRate = this.calculateWinRate(this.prices);
|
|
918
|
+
const profitFactor = this.calculateProfitFactor(this.prices);
|
|
919
|
+
return {
|
|
920
|
+
currentPrice,
|
|
921
|
+
startPrice,
|
|
922
|
+
sessionHigh,
|
|
923
|
+
sessionLow,
|
|
924
|
+
totalVolume,
|
|
925
|
+
avgVolume,
|
|
926
|
+
volatility,
|
|
927
|
+
sessionReturn,
|
|
928
|
+
pricePosition,
|
|
929
|
+
trueVWAP,
|
|
930
|
+
momentum5,
|
|
931
|
+
momentum10,
|
|
932
|
+
maxDrawdown,
|
|
933
|
+
atr,
|
|
934
|
+
impliedVolatility,
|
|
935
|
+
realizedVolatility,
|
|
936
|
+
sharpeRatio,
|
|
937
|
+
sortinoRatio,
|
|
938
|
+
calmarRatio,
|
|
939
|
+
maxConsecutiveLosses,
|
|
940
|
+
winRate,
|
|
941
|
+
profitFactor
|
|
942
|
+
};
|
|
943
|
+
}
|
|
944
|
+
// Generate technical indicators
|
|
945
|
+
getTechnicalIndicators() {
|
|
946
|
+
return {
|
|
947
|
+
sma5: this.calculateSMA(this.prices, 5),
|
|
948
|
+
sma10: this.calculateSMA(this.prices, 10),
|
|
949
|
+
sma20: this.calculateSMA(this.prices, 20),
|
|
950
|
+
sma50: this.calculateSMA(this.prices, 50),
|
|
951
|
+
sma200: this.calculateSMA(this.prices, 200),
|
|
952
|
+
ema8: this.calculateEMA(this.prices, 8),
|
|
953
|
+
ema12: this.calculateEMA(this.prices, 12),
|
|
954
|
+
ema21: this.calculateEMA(this.prices, 21),
|
|
955
|
+
ema26: this.calculateEMA(this.prices, 26),
|
|
956
|
+
wma20: this.calculateWma(this.prices, 20),
|
|
957
|
+
vwma20: this.calculateVwma(this.prices, 20),
|
|
958
|
+
macd: this.calculateMacd(this.prices),
|
|
959
|
+
adx: this.calculateAdx(this.prices, this.highs, this.lows),
|
|
960
|
+
dmi: this.calculateDmi(this.prices, this.highs, this.lows),
|
|
961
|
+
ichimoku: this.calculateIchimoku(this.prices, this.highs, this.lows),
|
|
962
|
+
parabolicSAR: this.calculateParabolicSAR(this.prices, this.highs, this.lows),
|
|
963
|
+
rsi: this.calculateRSI(this.prices, 14),
|
|
964
|
+
stochastic: this.calculateStochastic(this.prices, this.highs, this.lows),
|
|
965
|
+
cci: this.calculateCci(this.prices, this.highs, this.lows),
|
|
966
|
+
roc: this.calculateRoc(this.prices),
|
|
967
|
+
williamsR: this.calculateWilliamsR(this.prices),
|
|
968
|
+
momentum: this.calculateMomentum(this.prices),
|
|
969
|
+
bollinger: this.calculateBollingerBands(this.prices, 20, 2),
|
|
970
|
+
atr: this.calculateAtr(this.prices, this.highs, this.lows, this.volumes),
|
|
971
|
+
keltner: this.calculateKeltnerChannels(this.prices, this.highs, this.lows),
|
|
972
|
+
donchian: this.calculateDonchianChannels(this.prices, this.highs, this.lows),
|
|
973
|
+
chaikinVolatility: this.calculateChaikinVolatility(this.prices, this.highs, this.lows),
|
|
974
|
+
obv: this.calculateObv(this.volumes),
|
|
975
|
+
cmf: this.calculateCmf(this.prices, this.highs, this.lows, this.volumes),
|
|
976
|
+
adl: this.calculateAdl(this.prices),
|
|
977
|
+
volumeROC: this.calculateVolumeROC(this.prices),
|
|
978
|
+
mfi: this.calculateMfi(this.prices, this.highs, this.lows, this.volumes),
|
|
979
|
+
vwap: this.calculateVwap(this.prices, this.volumes),
|
|
980
|
+
pivotPoints: this.calculatePivotPoints(this.prices),
|
|
981
|
+
fibonacci: this.calculateFibonacciLevels(this.prices),
|
|
982
|
+
gannLevels: this.calculateGannLevels(this.prices),
|
|
983
|
+
elliottWave: this.calculateElliottWave(this.prices),
|
|
984
|
+
harmonicPatterns: this.calculateHarmonicPatterns(this.prices)
|
|
985
|
+
};
|
|
986
|
+
}
|
|
987
|
+
// Generate trading signals
|
|
988
|
+
generateSignals() {
|
|
989
|
+
const analysis = this.analyze();
|
|
990
|
+
let bullishSignals = 0;
|
|
991
|
+
let bearishSignals = 0;
|
|
992
|
+
const signals = [];
|
|
993
|
+
if (analysis.currentPrice > analysis.trueVWAP) {
|
|
994
|
+
signals.push(
|
|
995
|
+
`\u2713 BULLISH: Price above VWAP (+${((analysis.currentPrice - analysis.trueVWAP) / analysis.trueVWAP * 100).toFixed(2)}%)`
|
|
996
|
+
);
|
|
997
|
+
bullishSignals++;
|
|
998
|
+
} else {
|
|
999
|
+
signals.push(
|
|
1000
|
+
`\u2717 BEARISH: Price below VWAP (${((analysis.currentPrice - analysis.trueVWAP) / analysis.trueVWAP * 100).toFixed(2)}%)`
|
|
1001
|
+
);
|
|
1002
|
+
bearishSignals++;
|
|
1003
|
+
}
|
|
1004
|
+
if (analysis.momentum5 > 0 && analysis.momentum10 > 0) {
|
|
1005
|
+
signals.push("\u2713 BULLISH: Positive momentum on both timeframes");
|
|
1006
|
+
bullishSignals++;
|
|
1007
|
+
} else if (analysis.momentum5 < 0 && analysis.momentum10 < 0) {
|
|
1008
|
+
signals.push("\u2717 BEARISH: Negative momentum on both timeframes");
|
|
1009
|
+
bearishSignals++;
|
|
1010
|
+
} else {
|
|
1011
|
+
signals.push("\u25D0 MIXED: Conflicting momentum signals");
|
|
1012
|
+
}
|
|
1013
|
+
const currentVolume = this.volumes[this.volumes.length - 1];
|
|
1014
|
+
const volumeRatio = currentVolume / analysis.avgVolume;
|
|
1015
|
+
if (volumeRatio > 1.2 && analysis.sessionReturn > 0) {
|
|
1016
|
+
signals.push("\u2713 BULLISH: Above-average volume supporting upward move");
|
|
1017
|
+
bullishSignals++;
|
|
1018
|
+
} else if (volumeRatio > 1.2 && analysis.sessionReturn < 0) {
|
|
1019
|
+
signals.push("\u2717 BEARISH: Above-average volume supporting downward move");
|
|
1020
|
+
bearishSignals++;
|
|
1021
|
+
} else {
|
|
1022
|
+
signals.push("\u25D0 NEUTRAL: Volume not providing clear direction");
|
|
1023
|
+
}
|
|
1024
|
+
if (analysis.pricePosition > 65 && analysis.volatility > 30) {
|
|
1025
|
+
signals.push("\u2717 BEARISH: High in range with elevated volatility - reversal risk");
|
|
1026
|
+
bearishSignals++;
|
|
1027
|
+
} else if (analysis.pricePosition < 35 && analysis.volatility > 30) {
|
|
1028
|
+
signals.push("\u2713 BULLISH: Low in range with volatility - potential bounce");
|
|
1029
|
+
bullishSignals++;
|
|
1030
|
+
} else {
|
|
1031
|
+
signals.push("\u25D0 NEUTRAL: Price position and volatility not extreme");
|
|
1032
|
+
}
|
|
1033
|
+
return { bullishSignals, bearishSignals, signals };
|
|
1034
|
+
}
|
|
1035
|
+
// Generate comprehensive JSON analysis
|
|
1036
|
+
generateJSONAnalysis(symbol) {
|
|
1037
|
+
const analysis = this.analyze();
|
|
1038
|
+
const indicators = this.getTechnicalIndicators();
|
|
1039
|
+
const signals = this.generateSignals();
|
|
1040
|
+
const currentSMA5 = indicators.sma5.length > 0 ? indicators.sma5[indicators.sma5.length - 1] : null;
|
|
1041
|
+
const currentSMA10 = indicators.sma10.length > 0 ? indicators.sma10[indicators.sma10.length - 1] : null;
|
|
1042
|
+
const currentSMA20 = indicators.sma20.length > 0 ? indicators.sma20[indicators.sma20.length - 1] : null;
|
|
1043
|
+
const currentSMA50 = indicators.sma50.length > 0 ? indicators.sma50[indicators.sma50.length - 1] : null;
|
|
1044
|
+
const currentSMA200 = indicators.sma200.length > 0 ? indicators.sma200[indicators.sma200.length - 1] : null;
|
|
1045
|
+
const currentEMA8 = indicators.ema8[indicators.ema8.length - 1];
|
|
1046
|
+
const currentEMA12 = indicators.ema12[indicators.ema12.length - 1];
|
|
1047
|
+
const currentEMA21 = indicators.ema21[indicators.ema21.length - 1];
|
|
1048
|
+
const currentEMA26 = indicators.ema26[indicators.ema26.length - 1];
|
|
1049
|
+
const currentWMA20 = indicators.wma20.length > 0 ? indicators.wma20[indicators.wma20.length - 1] : null;
|
|
1050
|
+
const currentVWMA20 = indicators.vwma20.length > 0 ? indicators.vwma20[indicators.vwma20.length - 1] : null;
|
|
1051
|
+
const currentMACD = indicators.macd.length > 0 ? indicators.macd[indicators.macd.length - 1] : null;
|
|
1052
|
+
const currentADX = indicators.adx.length > 0 ? indicators.adx[indicators.adx.length - 1] : null;
|
|
1053
|
+
const currentDMI = indicators.dmi.length > 0 ? indicators.dmi[indicators.dmi.length - 1] : null;
|
|
1054
|
+
const currentIchimoku = indicators.ichimoku.length > 0 ? indicators.ichimoku[indicators.ichimoku.length - 1] : null;
|
|
1055
|
+
const currentParabolicSAR = indicators.parabolicSAR.length > 0 ? indicators.parabolicSAR[indicators.parabolicSAR.length - 1] : null;
|
|
1056
|
+
const currentRSI = indicators.rsi.length > 0 ? indicators.rsi[indicators.rsi.length - 1] : null;
|
|
1057
|
+
const currentStochastic = indicators.stochastic.length > 0 ? indicators.stochastic[indicators.stochastic.length - 1] : null;
|
|
1058
|
+
const currentCCI = indicators.cci.length > 0 ? indicators.cci[indicators.cci.length - 1] : null;
|
|
1059
|
+
const currentROC = indicators.roc.length > 0 ? indicators.roc[indicators.roc.length - 1] : null;
|
|
1060
|
+
const currentWilliamsR = indicators.williamsR.length > 0 ? indicators.williamsR[indicators.williamsR.length - 1] : null;
|
|
1061
|
+
const currentMomentum = indicators.momentum.length > 0 ? indicators.momentum[indicators.momentum.length - 1] : null;
|
|
1062
|
+
const currentBB = indicators.bollinger.length > 0 ? indicators.bollinger[indicators.bollinger.length - 1] : null;
|
|
1063
|
+
const currentAtr = indicators.atr.length > 0 ? indicators.atr[indicators.atr.length - 1] : null;
|
|
1064
|
+
const currentKeltner = indicators.keltner.length > 0 ? indicators.keltner[indicators.keltner.length - 1] : null;
|
|
1065
|
+
const currentDonchian = indicators.donchian.length > 0 ? indicators.donchian[indicators.donchian.length - 1] : null;
|
|
1066
|
+
const currentChaikinVolatility = indicators.chaikinVolatility.length > 0 ? indicators.chaikinVolatility[indicators.chaikinVolatility.length - 1] : null;
|
|
1067
|
+
const currentObv = indicators.obv.length > 0 ? indicators.obv[indicators.obv.length - 1] : null;
|
|
1068
|
+
const currentCmf = indicators.cmf.length > 0 ? indicators.cmf[indicators.cmf.length - 1] : null;
|
|
1069
|
+
const currentAdl = indicators.adl.length > 0 ? indicators.adl[indicators.adl.length - 1] : null;
|
|
1070
|
+
const currentVolumeROC = indicators.volumeROC.length > 0 ? indicators.volumeROC[indicators.volumeROC.length - 1] : null;
|
|
1071
|
+
const currentMfi = indicators.mfi.length > 0 ? indicators.mfi[indicators.mfi.length - 1] : null;
|
|
1072
|
+
const currentVwap = indicators.vwap.length > 0 ? indicators.vwap[indicators.vwap.length - 1] : null;
|
|
1073
|
+
const currentPivotPoints = indicators.pivotPoints.length > 0 ? indicators.pivotPoints[indicators.pivotPoints.length - 1] : null;
|
|
1074
|
+
const currentFibonacci = indicators.fibonacci.length > 0 ? indicators.fibonacci[indicators.fibonacci.length - 1] : null;
|
|
1075
|
+
const currentGannLevels = indicators.gannLevels.length > 0 ? indicators.gannLevels : [];
|
|
1076
|
+
const currentElliottWave = indicators.elliottWave.length > 0 ? indicators.elliottWave[indicators.elliottWave.length - 1] : null;
|
|
1077
|
+
const currentHarmonicPatterns = indicators.harmonicPatterns.length > 0 ? indicators.harmonicPatterns : [];
|
|
1078
|
+
const currentVolume = this.volumes[this.volumes.length - 1];
|
|
1079
|
+
const volumeRatio = currentVolume / analysis.avgVolume;
|
|
1080
|
+
const currentDrawdown = (analysis.sessionHigh - analysis.currentPrice) / analysis.sessionHigh * 100;
|
|
1081
|
+
const rangeWidth = (analysis.sessionHigh - analysis.sessionLow) / analysis.sessionLow * 100;
|
|
1082
|
+
const priceVsVWAP = (analysis.currentPrice - analysis.trueVWAP) / analysis.trueVWAP * 100;
|
|
1083
|
+
const totalScore = signals.bullishSignals - signals.bearishSignals;
|
|
1084
|
+
const overallSignal = totalScore > 0 ? "BULLISH_BIAS" : totalScore < 0 ? "BEARISH_BIAS" : "NEUTRAL";
|
|
1085
|
+
const targetEntry = Math.max(analysis.sessionLow * 1.005, analysis.trueVWAP * 0.998);
|
|
1086
|
+
const stopLoss = analysis.sessionLow * 0.995;
|
|
1087
|
+
const profitTarget = analysis.sessionHigh * 0.995;
|
|
1088
|
+
const riskRewardRatio = (profitTarget - analysis.currentPrice) / (analysis.currentPrice - stopLoss);
|
|
1089
|
+
const positionSize = this.calculatePositionSize(analysis.currentPrice, targetEntry, stopLoss);
|
|
1090
|
+
const maxRisk = positionSize * (targetEntry - stopLoss);
|
|
1091
|
+
return {
|
|
1092
|
+
symbol,
|
|
1093
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1094
|
+
marketStructure: {
|
|
1095
|
+
currentPrice: analysis.currentPrice,
|
|
1096
|
+
startPrice: analysis.startPrice,
|
|
1097
|
+
sessionHigh: analysis.sessionHigh,
|
|
1098
|
+
sessionLow: analysis.sessionLow,
|
|
1099
|
+
rangeWidth,
|
|
1100
|
+
totalVolume: analysis.totalVolume,
|
|
1101
|
+
sessionPerformance: analysis.sessionReturn,
|
|
1102
|
+
positionInRange: analysis.pricePosition
|
|
1103
|
+
},
|
|
1104
|
+
volatility: {
|
|
1105
|
+
impliedVolatility: analysis.impliedVolatility,
|
|
1106
|
+
realizedVolatility: analysis.realizedVolatility,
|
|
1107
|
+
atr: analysis.atr,
|
|
1108
|
+
maxDrawdown: analysis.maxDrawdown * 100,
|
|
1109
|
+
currentDrawdown
|
|
1110
|
+
},
|
|
1111
|
+
technicalIndicators: {
|
|
1112
|
+
sma5: currentSMA5,
|
|
1113
|
+
sma10: currentSMA10,
|
|
1114
|
+
sma20: currentSMA20,
|
|
1115
|
+
sma50: currentSMA50,
|
|
1116
|
+
sma200: currentSMA200,
|
|
1117
|
+
ema8: currentEMA8,
|
|
1118
|
+
ema12: currentEMA12,
|
|
1119
|
+
ema21: currentEMA21,
|
|
1120
|
+
ema26: currentEMA26,
|
|
1121
|
+
wma20: currentWMA20,
|
|
1122
|
+
vwma20: currentVWMA20,
|
|
1123
|
+
macd: currentMACD,
|
|
1124
|
+
adx: currentADX,
|
|
1125
|
+
dmi: currentDMI,
|
|
1126
|
+
ichimoku: currentIchimoku,
|
|
1127
|
+
parabolicSAR: currentParabolicSAR,
|
|
1128
|
+
rsi: currentRSI,
|
|
1129
|
+
stochastic: currentStochastic,
|
|
1130
|
+
cci: currentCCI,
|
|
1131
|
+
roc: currentROC,
|
|
1132
|
+
williamsR: currentWilliamsR,
|
|
1133
|
+
momentum: currentMomentum,
|
|
1134
|
+
bollingerBands: currentBB ? {
|
|
1135
|
+
upper: currentBB.upper,
|
|
1136
|
+
middle: currentBB.middle,
|
|
1137
|
+
lower: currentBB.lower,
|
|
1138
|
+
bandwidth: currentBB.bandwidth,
|
|
1139
|
+
percentB: currentBB.percentB
|
|
1140
|
+
} : null,
|
|
1141
|
+
atr: currentAtr,
|
|
1142
|
+
keltnerChannels: currentKeltner ? {
|
|
1143
|
+
upper: currentKeltner.upper,
|
|
1144
|
+
middle: currentKeltner.middle,
|
|
1145
|
+
lower: currentKeltner.lower
|
|
1146
|
+
} : null,
|
|
1147
|
+
donchianChannels: currentDonchian ? {
|
|
1148
|
+
upper: currentDonchian.upper,
|
|
1149
|
+
middle: currentDonchian.middle,
|
|
1150
|
+
lower: currentDonchian.lower
|
|
1151
|
+
} : null,
|
|
1152
|
+
chaikinVolatility: currentChaikinVolatility,
|
|
1153
|
+
obv: currentObv,
|
|
1154
|
+
cmf: currentCmf,
|
|
1155
|
+
adl: currentAdl,
|
|
1156
|
+
volumeROC: currentVolumeROC,
|
|
1157
|
+
mfi: currentMfi,
|
|
1158
|
+
vwap: currentVwap
|
|
1159
|
+
},
|
|
1160
|
+
volumeAnalysis: {
|
|
1161
|
+
currentVolume,
|
|
1162
|
+
averageVolume: Math.round(analysis.avgVolume),
|
|
1163
|
+
volumeRatio,
|
|
1164
|
+
trueVWAP: analysis.trueVWAP,
|
|
1165
|
+
priceVsVWAP,
|
|
1166
|
+
obv: currentObv,
|
|
1167
|
+
cmf: currentCmf,
|
|
1168
|
+
mfi: currentMfi
|
|
1169
|
+
},
|
|
1170
|
+
momentum: {
|
|
1171
|
+
momentum5: analysis.momentum5,
|
|
1172
|
+
momentum10: analysis.momentum10,
|
|
1173
|
+
sessionROC: analysis.sessionReturn,
|
|
1174
|
+
rsi: currentRSI,
|
|
1175
|
+
stochastic: currentStochastic,
|
|
1176
|
+
cci: currentCCI
|
|
1177
|
+
},
|
|
1178
|
+
supportResistance: {
|
|
1179
|
+
pivotPoints: currentPivotPoints,
|
|
1180
|
+
fibonacci: currentFibonacci,
|
|
1181
|
+
gannLevels: currentGannLevels,
|
|
1182
|
+
elliottWave: currentElliottWave,
|
|
1183
|
+
harmonicPatterns: currentHarmonicPatterns
|
|
1184
|
+
},
|
|
1185
|
+
tradingSignals: {
|
|
1186
|
+
...signals,
|
|
1187
|
+
overallSignal,
|
|
1188
|
+
signalScore: totalScore,
|
|
1189
|
+
confidence: this.calculateConfidence(signals.signals),
|
|
1190
|
+
riskLevel: this.calculateRiskLevel(analysis.volatility)
|
|
1191
|
+
},
|
|
1192
|
+
statisticalModels: {
|
|
1193
|
+
zScore: this.calculateZScore(analysis.currentPrice, analysis.startPrice, analysis.avgVolume),
|
|
1194
|
+
ornsteinUhlenbeck: this.calculateOrnsteinUhlenbeck(
|
|
1195
|
+
analysis.currentPrice,
|
|
1196
|
+
analysis.startPrice,
|
|
1197
|
+
analysis.avgVolume
|
|
1198
|
+
),
|
|
1199
|
+
kalmanFilter: this.calculateKalmanFilter(
|
|
1200
|
+
analysis.currentPrice,
|
|
1201
|
+
analysis.startPrice,
|
|
1202
|
+
analysis.avgVolume
|
|
1203
|
+
),
|
|
1204
|
+
arima: this.calculateArima(analysis.currentPrice, analysis.startPrice, analysis.avgVolume),
|
|
1205
|
+
garch: this.calculateGarch(analysis.currentPrice, analysis.startPrice, analysis.avgVolume),
|
|
1206
|
+
hilbertTransform: this.calculateHilbertTransform(
|
|
1207
|
+
analysis.currentPrice,
|
|
1208
|
+
analysis.startPrice,
|
|
1209
|
+
analysis.avgVolume
|
|
1210
|
+
),
|
|
1211
|
+
waveletTransform: this.calculateWaveletTransform(
|
|
1212
|
+
analysis.currentPrice,
|
|
1213
|
+
analysis.startPrice,
|
|
1214
|
+
analysis.avgVolume
|
|
1215
|
+
)
|
|
1216
|
+
},
|
|
1217
|
+
optionsAnalysis: (() => {
|
|
1218
|
+
const blackScholes = this.calculateBlackScholes(
|
|
1219
|
+
analysis.currentPrice,
|
|
1220
|
+
analysis.startPrice,
|
|
1221
|
+
analysis.avgVolume
|
|
1222
|
+
);
|
|
1223
|
+
if (!blackScholes) return null;
|
|
1224
|
+
return {
|
|
1225
|
+
blackScholes,
|
|
1226
|
+
impliedVolatility: analysis.impliedVolatility,
|
|
1227
|
+
delta: blackScholes.delta,
|
|
1228
|
+
gamma: blackScholes.gamma,
|
|
1229
|
+
theta: blackScholes.theta,
|
|
1230
|
+
vega: blackScholes.vega,
|
|
1231
|
+
rho: blackScholes.rho,
|
|
1232
|
+
greeks: {
|
|
1233
|
+
delta: blackScholes.delta,
|
|
1234
|
+
gamma: blackScholes.gamma,
|
|
1235
|
+
theta: blackScholes.theta,
|
|
1236
|
+
vega: blackScholes.vega,
|
|
1237
|
+
rho: blackScholes.rho
|
|
1238
|
+
}
|
|
1239
|
+
};
|
|
1240
|
+
})(),
|
|
1241
|
+
riskManagement: {
|
|
1242
|
+
targetEntry,
|
|
1243
|
+
stopLoss,
|
|
1244
|
+
profitTarget,
|
|
1245
|
+
riskRewardRatio,
|
|
1246
|
+
positionSize,
|
|
1247
|
+
maxRisk
|
|
1248
|
+
},
|
|
1249
|
+
performance: {
|
|
1250
|
+
sharpeRatio: analysis.sharpeRatio,
|
|
1251
|
+
sortinoRatio: analysis.sortinoRatio,
|
|
1252
|
+
calmarRatio: analysis.calmarRatio,
|
|
1253
|
+
maxDrawdown: analysis.maxDrawdown * 100,
|
|
1254
|
+
winRate: analysis.winRate,
|
|
1255
|
+
profitFactor: analysis.profitFactor,
|
|
1256
|
+
totalReturn: analysis.sessionReturn,
|
|
1257
|
+
volatility: analysis.volatility
|
|
1258
|
+
}
|
|
1259
|
+
};
|
|
268
1260
|
}
|
|
269
1261
|
};
|
|
1262
|
+
var createTechnicalAnalysisHandler = (marketDataPrices) => {
|
|
1263
|
+
return async (args) => {
|
|
1264
|
+
try {
|
|
1265
|
+
const symbol = args.symbol;
|
|
1266
|
+
const priceHistory = marketDataPrices.get(symbol) || [];
|
|
1267
|
+
if (priceHistory.length === 0) {
|
|
1268
|
+
return {
|
|
1269
|
+
content: [
|
|
1270
|
+
{
|
|
1271
|
+
type: "text",
|
|
1272
|
+
text: `No price data available for ${symbol}. Please request market data first.`,
|
|
1273
|
+
uri: "technicalAnalysis"
|
|
1274
|
+
}
|
|
1275
|
+
]
|
|
1276
|
+
};
|
|
1277
|
+
}
|
|
1278
|
+
const hasValidData = priceHistory.every(
|
|
1279
|
+
(entry) => typeof entry.trade === "number" && !Number.isNaN(entry.trade) && typeof entry.midPrice === "number" && !Number.isNaN(entry.midPrice)
|
|
1280
|
+
);
|
|
1281
|
+
if (!hasValidData) {
|
|
1282
|
+
throw new Error("Invalid market data");
|
|
1283
|
+
}
|
|
1284
|
+
const analyzer = new TechnicalAnalyzer(priceHistory);
|
|
1285
|
+
const analysis = analyzer.generateJSONAnalysis(symbol);
|
|
1286
|
+
return {
|
|
1287
|
+
content: [
|
|
1288
|
+
{
|
|
1289
|
+
type: "text",
|
|
1290
|
+
text: `Technical Analysis for ${symbol}:
|
|
1291
|
+
|
|
1292
|
+
${JSON.stringify(analysis, null, 2)}`,
|
|
1293
|
+
uri: "technicalAnalysis"
|
|
1294
|
+
}
|
|
1295
|
+
]
|
|
1296
|
+
};
|
|
1297
|
+
} catch (error) {
|
|
1298
|
+
return {
|
|
1299
|
+
content: [
|
|
1300
|
+
{
|
|
1301
|
+
type: "text",
|
|
1302
|
+
text: `Error performing technical analysis: ${error instanceof Error ? error.message : "Unknown error"}`,
|
|
1303
|
+
uri: "technicalAnalysis"
|
|
1304
|
+
}
|
|
1305
|
+
],
|
|
1306
|
+
isError: true
|
|
1307
|
+
};
|
|
1308
|
+
}
|
|
1309
|
+
};
|
|
1310
|
+
};
|
|
270
1311
|
|
|
271
1312
|
// src/tools/marketData.ts
|
|
272
1313
|
var import_fixparser = require("fixparser");
|
|
@@ -274,9 +1315,63 @@ var import_quickchart_js = __toESM(require("quickchart-js"), 1);
|
|
|
274
1315
|
var createMarketDataRequestHandler = (parser, pendingRequests) => {
|
|
275
1316
|
return async (args) => {
|
|
276
1317
|
try {
|
|
1318
|
+
parser.logger.log({
|
|
1319
|
+
level: "info",
|
|
1320
|
+
message: `Sending market data request for symbols: ${args.symbols.join(", ")}`
|
|
1321
|
+
});
|
|
277
1322
|
const response = new Promise((resolve) => {
|
|
278
1323
|
pendingRequests.set(args.mdReqID, resolve);
|
|
1324
|
+
parser.logger.log({
|
|
1325
|
+
level: "info",
|
|
1326
|
+
message: `Registered callback for market data request ID: ${args.mdReqID}`
|
|
1327
|
+
});
|
|
279
1328
|
});
|
|
1329
|
+
const entryTypes = args.mdEntryTypes || [
|
|
1330
|
+
import_fixparser.MDEntryType.Bid,
|
|
1331
|
+
import_fixparser.MDEntryType.Offer,
|
|
1332
|
+
import_fixparser.MDEntryType.Trade,
|
|
1333
|
+
import_fixparser.MDEntryType.IndexValue,
|
|
1334
|
+
import_fixparser.MDEntryType.OpeningPrice,
|
|
1335
|
+
import_fixparser.MDEntryType.ClosingPrice,
|
|
1336
|
+
import_fixparser.MDEntryType.SettlementPrice,
|
|
1337
|
+
import_fixparser.MDEntryType.TradingSessionHighPrice,
|
|
1338
|
+
import_fixparser.MDEntryType.TradingSessionLowPrice,
|
|
1339
|
+
import_fixparser.MDEntryType.VWAP,
|
|
1340
|
+
import_fixparser.MDEntryType.Imbalance,
|
|
1341
|
+
import_fixparser.MDEntryType.TradeVolume,
|
|
1342
|
+
import_fixparser.MDEntryType.OpenInterest,
|
|
1343
|
+
import_fixparser.MDEntryType.CompositeUnderlyingPrice,
|
|
1344
|
+
import_fixparser.MDEntryType.SimulatedSellPrice,
|
|
1345
|
+
import_fixparser.MDEntryType.SimulatedBuyPrice,
|
|
1346
|
+
import_fixparser.MDEntryType.MarginRate,
|
|
1347
|
+
import_fixparser.MDEntryType.MidPrice,
|
|
1348
|
+
import_fixparser.MDEntryType.EmptyBook,
|
|
1349
|
+
import_fixparser.MDEntryType.SettleHighPrice,
|
|
1350
|
+
import_fixparser.MDEntryType.SettleLowPrice,
|
|
1351
|
+
import_fixparser.MDEntryType.PriorSettlePrice,
|
|
1352
|
+
import_fixparser.MDEntryType.SessionHighBid,
|
|
1353
|
+
import_fixparser.MDEntryType.SessionLowOffer,
|
|
1354
|
+
import_fixparser.MDEntryType.EarlyPrices,
|
|
1355
|
+
import_fixparser.MDEntryType.AuctionClearingPrice,
|
|
1356
|
+
import_fixparser.MDEntryType.SwapValueFactor,
|
|
1357
|
+
import_fixparser.MDEntryType.DailyValueAdjustmentForLongPositions,
|
|
1358
|
+
import_fixparser.MDEntryType.CumulativeValueAdjustmentForLongPositions,
|
|
1359
|
+
import_fixparser.MDEntryType.DailyValueAdjustmentForShortPositions,
|
|
1360
|
+
import_fixparser.MDEntryType.CumulativeValueAdjustmentForShortPositions,
|
|
1361
|
+
import_fixparser.MDEntryType.FixingPrice,
|
|
1362
|
+
import_fixparser.MDEntryType.CashRate,
|
|
1363
|
+
import_fixparser.MDEntryType.RecoveryRate,
|
|
1364
|
+
import_fixparser.MDEntryType.RecoveryRateForLong,
|
|
1365
|
+
import_fixparser.MDEntryType.RecoveryRateForShort,
|
|
1366
|
+
import_fixparser.MDEntryType.MarketBid,
|
|
1367
|
+
import_fixparser.MDEntryType.MarketOffer,
|
|
1368
|
+
import_fixparser.MDEntryType.ShortSaleMinPrice,
|
|
1369
|
+
import_fixparser.MDEntryType.PreviousClosingPrice,
|
|
1370
|
+
import_fixparser.MDEntryType.ThresholdLimitPriceBanding,
|
|
1371
|
+
import_fixparser.MDEntryType.DailyFinancingValue,
|
|
1372
|
+
import_fixparser.MDEntryType.AccruedFinancingValue,
|
|
1373
|
+
import_fixparser.MDEntryType.TWAP
|
|
1374
|
+
];
|
|
280
1375
|
const messageFields = [
|
|
281
1376
|
new import_fixparser.Field(import_fixparser.Fields.MsgType, import_fixparser.Messages.MarketDataRequest),
|
|
282
1377
|
new import_fixparser.Field(import_fixparser.Fields.SenderCompID, parser.sender),
|
|
@@ -292,12 +1387,16 @@ var createMarketDataRequestHandler = (parser, pendingRequests) => {
|
|
|
292
1387
|
args.symbols.forEach((symbol) => {
|
|
293
1388
|
messageFields.push(new import_fixparser.Field(import_fixparser.Fields.Symbol, symbol));
|
|
294
1389
|
});
|
|
295
|
-
messageFields.push(new import_fixparser.Field(import_fixparser.Fields.NoMDEntryTypes,
|
|
296
|
-
|
|
1390
|
+
messageFields.push(new import_fixparser.Field(import_fixparser.Fields.NoMDEntryTypes, entryTypes.length));
|
|
1391
|
+
entryTypes.forEach((entryType) => {
|
|
297
1392
|
messageFields.push(new import_fixparser.Field(import_fixparser.Fields.MDEntryType, entryType));
|
|
298
1393
|
});
|
|
299
1394
|
const mdr = parser.createMessage(...messageFields);
|
|
300
1395
|
if (!parser.connected) {
|
|
1396
|
+
parser.logger.log({
|
|
1397
|
+
level: "error",
|
|
1398
|
+
message: "Not connected. Cannot send market data request."
|
|
1399
|
+
});
|
|
301
1400
|
return {
|
|
302
1401
|
content: [
|
|
303
1402
|
{
|
|
@@ -309,8 +1408,16 @@ var createMarketDataRequestHandler = (parser, pendingRequests) => {
|
|
|
309
1408
|
isError: true
|
|
310
1409
|
};
|
|
311
1410
|
}
|
|
1411
|
+
parser.logger.log({
|
|
1412
|
+
level: "info",
|
|
1413
|
+
message: `Sending market data request message: ${JSON.stringify(mdr?.toFIXJSON())}`
|
|
1414
|
+
});
|
|
312
1415
|
parser.send(mdr);
|
|
313
1416
|
const fixData = await response;
|
|
1417
|
+
parser.logger.log({
|
|
1418
|
+
level: "info",
|
|
1419
|
+
message: `Received market data response for request ID: ${args.mdReqID}`
|
|
1420
|
+
});
|
|
314
1421
|
return {
|
|
315
1422
|
content: [
|
|
316
1423
|
{
|
|
@@ -334,6 +1441,72 @@ var createMarketDataRequestHandler = (parser, pendingRequests) => {
|
|
|
334
1441
|
}
|
|
335
1442
|
};
|
|
336
1443
|
};
|
|
1444
|
+
var aggregateMarketData = (priceHistory, maxPoints = 490) => {
|
|
1445
|
+
if (priceHistory.length <= maxPoints) {
|
|
1446
|
+
return priceHistory;
|
|
1447
|
+
}
|
|
1448
|
+
const result = [];
|
|
1449
|
+
const step = priceHistory.length / maxPoints;
|
|
1450
|
+
result.push(priceHistory[0]);
|
|
1451
|
+
for (let i = 1; i < maxPoints - 1; i++) {
|
|
1452
|
+
const startIndex = Math.floor(i * step);
|
|
1453
|
+
const endIndex = Math.floor((i + 1) * step);
|
|
1454
|
+
const segment = priceHistory.slice(startIndex, endIndex);
|
|
1455
|
+
if (segment.length === 0) continue;
|
|
1456
|
+
const aggregatedPoint = {
|
|
1457
|
+
timestamp: segment[0].timestamp,
|
|
1458
|
+
// Use timestamp of first point in segment
|
|
1459
|
+
bid: segment.reduce((sum2, p) => sum2 + p.bid, 0) / segment.length,
|
|
1460
|
+
offer: segment.reduce((sum2, p) => sum2 + p.offer, 0) / segment.length,
|
|
1461
|
+
spread: segment.reduce((sum2, p) => sum2 + p.spread, 0) / segment.length,
|
|
1462
|
+
volume: segment.reduce((sum2, p) => sum2 + p.volume, 0) / segment.length,
|
|
1463
|
+
trade: segment.reduce((sum2, p) => sum2 + p.trade, 0) / segment.length,
|
|
1464
|
+
indexValue: segment.reduce((sum2, p) => sum2 + p.indexValue, 0) / segment.length,
|
|
1465
|
+
openingPrice: segment.reduce((sum2, p) => sum2 + p.openingPrice, 0) / segment.length,
|
|
1466
|
+
closingPrice: segment.reduce((sum2, p) => sum2 + p.closingPrice, 0) / segment.length,
|
|
1467
|
+
settlementPrice: segment.reduce((sum2, p) => sum2 + p.settlementPrice, 0) / segment.length,
|
|
1468
|
+
tradingSessionHighPrice: segment.reduce((sum2, p) => sum2 + p.tradingSessionHighPrice, 0) / segment.length,
|
|
1469
|
+
tradingSessionLowPrice: segment.reduce((sum2, p) => sum2 + p.tradingSessionLowPrice, 0) / segment.length,
|
|
1470
|
+
vwap: segment.reduce((sum2, p) => sum2 + p.vwap, 0) / segment.length,
|
|
1471
|
+
imbalance: segment.reduce((sum2, p) => sum2 + p.imbalance, 0) / segment.length,
|
|
1472
|
+
openInterest: segment.reduce((sum2, p) => sum2 + p.openInterest, 0) / segment.length,
|
|
1473
|
+
compositeUnderlyingPrice: segment.reduce((sum2, p) => sum2 + p.compositeUnderlyingPrice, 0) / segment.length,
|
|
1474
|
+
simulatedSellPrice: segment.reduce((sum2, p) => sum2 + p.simulatedSellPrice, 0) / segment.length,
|
|
1475
|
+
simulatedBuyPrice: segment.reduce((sum2, p) => sum2 + p.simulatedBuyPrice, 0) / segment.length,
|
|
1476
|
+
marginRate: segment.reduce((sum2, p) => sum2 + p.marginRate, 0) / segment.length,
|
|
1477
|
+
midPrice: segment.reduce((sum2, p) => sum2 + p.midPrice, 0) / segment.length,
|
|
1478
|
+
emptyBook: segment.reduce((sum2, p) => sum2 + p.emptyBook, 0) / segment.length,
|
|
1479
|
+
settleHighPrice: segment.reduce((sum2, p) => sum2 + p.settleHighPrice, 0) / segment.length,
|
|
1480
|
+
settleLowPrice: segment.reduce((sum2, p) => sum2 + p.settleLowPrice, 0) / segment.length,
|
|
1481
|
+
priorSettlePrice: segment.reduce((sum2, p) => sum2 + p.priorSettlePrice, 0) / segment.length,
|
|
1482
|
+
sessionHighBid: segment.reduce((sum2, p) => sum2 + p.sessionHighBid, 0) / segment.length,
|
|
1483
|
+
sessionLowOffer: segment.reduce((sum2, p) => sum2 + p.sessionLowOffer, 0) / segment.length,
|
|
1484
|
+
earlyPrices: segment.reduce((sum2, p) => sum2 + p.earlyPrices, 0) / segment.length,
|
|
1485
|
+
auctionClearingPrice: segment.reduce((sum2, p) => sum2 + p.auctionClearingPrice, 0) / segment.length,
|
|
1486
|
+
swapValueFactor: segment.reduce((sum2, p) => sum2 + p.swapValueFactor, 0) / segment.length,
|
|
1487
|
+
dailyValueAdjustmentForLongPositions: segment.reduce((sum2, p) => sum2 + p.dailyValueAdjustmentForLongPositions, 0) / segment.length,
|
|
1488
|
+
cumulativeValueAdjustmentForLongPositions: segment.reduce((sum2, p) => sum2 + p.cumulativeValueAdjustmentForLongPositions, 0) / segment.length,
|
|
1489
|
+
dailyValueAdjustmentForShortPositions: segment.reduce((sum2, p) => sum2 + p.dailyValueAdjustmentForShortPositions, 0) / segment.length,
|
|
1490
|
+
cumulativeValueAdjustmentForShortPositions: segment.reduce((sum2, p) => sum2 + p.cumulativeValueAdjustmentForShortPositions, 0) / segment.length,
|
|
1491
|
+
fixingPrice: segment.reduce((sum2, p) => sum2 + p.fixingPrice, 0) / segment.length,
|
|
1492
|
+
cashRate: segment.reduce((sum2, p) => sum2 + p.cashRate, 0) / segment.length,
|
|
1493
|
+
recoveryRate: segment.reduce((sum2, p) => sum2 + p.recoveryRate, 0) / segment.length,
|
|
1494
|
+
recoveryRateForLong: segment.reduce((sum2, p) => sum2 + p.recoveryRateForLong, 0) / segment.length,
|
|
1495
|
+
recoveryRateForShort: segment.reduce((sum2, p) => sum2 + p.recoveryRateForShort, 0) / segment.length,
|
|
1496
|
+
marketBid: segment.reduce((sum2, p) => sum2 + p.marketBid, 0) / segment.length,
|
|
1497
|
+
marketOffer: segment.reduce((sum2, p) => sum2 + p.marketOffer, 0) / segment.length,
|
|
1498
|
+
shortSaleMinPrice: segment.reduce((sum2, p) => sum2 + p.shortSaleMinPrice, 0) / segment.length,
|
|
1499
|
+
previousClosingPrice: segment.reduce((sum2, p) => sum2 + p.previousClosingPrice, 0) / segment.length,
|
|
1500
|
+
thresholdLimitPriceBanding: segment.reduce((sum2, p) => sum2 + p.thresholdLimitPriceBanding, 0) / segment.length,
|
|
1501
|
+
dailyFinancingValue: segment.reduce((sum2, p) => sum2 + p.dailyFinancingValue, 0) / segment.length,
|
|
1502
|
+
accruedFinancingValue: segment.reduce((sum2, p) => sum2 + p.accruedFinancingValue, 0) / segment.length,
|
|
1503
|
+
twap: segment.reduce((sum2, p) => sum2 + p.twap, 0) / segment.length
|
|
1504
|
+
};
|
|
1505
|
+
result.push(aggregatedPoint);
|
|
1506
|
+
}
|
|
1507
|
+
result.push(priceHistory[priceHistory.length - 1]);
|
|
1508
|
+
return result;
|
|
1509
|
+
};
|
|
337
1510
|
var createGetStockGraphHandler = (marketDataPrices) => {
|
|
338
1511
|
return async (args) => {
|
|
339
1512
|
try {
|
|
@@ -350,19 +1523,78 @@ var createGetStockGraphHandler = (marketDataPrices) => {
|
|
|
350
1523
|
]
|
|
351
1524
|
};
|
|
352
1525
|
}
|
|
1526
|
+
const aggregatedData = aggregateMarketData(priceHistory, 500);
|
|
353
1527
|
const chart = new import_quickchart_js.default();
|
|
354
|
-
chart.setWidth(
|
|
355
|
-
chart.setHeight(
|
|
356
|
-
|
|
357
|
-
const
|
|
358
|
-
|
|
1528
|
+
chart.setWidth(1200);
|
|
1529
|
+
chart.setHeight(600);
|
|
1530
|
+
chart.setBackgroundColor("transparent");
|
|
1531
|
+
const labels = aggregatedData.map((point) => new Date(point.timestamp).toLocaleTimeString());
|
|
1532
|
+
const bidData = aggregatedData.map((point) => point.bid);
|
|
1533
|
+
const offerData = aggregatedData.map((point) => point.offer);
|
|
1534
|
+
const spreadData = aggregatedData.map((point) => point.spread);
|
|
1535
|
+
const volumeData = aggregatedData.map((point) => point.volume);
|
|
1536
|
+
const tradeData = aggregatedData.map((point) => point.trade);
|
|
1537
|
+
const vwapData = aggregatedData.map((point) => point.vwap);
|
|
1538
|
+
const twapData = aggregatedData.map((point) => point.twap);
|
|
1539
|
+
const maxVolume = Math.max(...volumeData.filter((v) => v > 0));
|
|
1540
|
+
const maxPrice = Math.max(...bidData, ...offerData, ...tradeData, ...vwapData, ...twapData);
|
|
1541
|
+
const normalizedVolumeData = volumeData.map((v) => v / maxVolume * maxPrice * 0.3);
|
|
1542
|
+
const config = {
|
|
359
1543
|
type: "line",
|
|
360
1544
|
data: {
|
|
361
1545
|
labels,
|
|
362
1546
|
datasets: [
|
|
363
1547
|
{
|
|
364
|
-
label:
|
|
365
|
-
data,
|
|
1548
|
+
label: "Bid",
|
|
1549
|
+
data: bidData,
|
|
1550
|
+
borderColor: "#28a745",
|
|
1551
|
+
backgroundColor: "rgba(40, 167, 69, 0.1)",
|
|
1552
|
+
fill: false,
|
|
1553
|
+
tension: 0.4
|
|
1554
|
+
},
|
|
1555
|
+
{
|
|
1556
|
+
label: "Offer",
|
|
1557
|
+
data: offerData,
|
|
1558
|
+
borderColor: "#dc3545",
|
|
1559
|
+
backgroundColor: "rgba(220, 53, 69, 0.1)",
|
|
1560
|
+
fill: false,
|
|
1561
|
+
tension: 0.4
|
|
1562
|
+
},
|
|
1563
|
+
{
|
|
1564
|
+
label: "Spread",
|
|
1565
|
+
data: spreadData,
|
|
1566
|
+
borderColor: "#6c757d",
|
|
1567
|
+
backgroundColor: "rgba(108, 117, 125, 0.1)",
|
|
1568
|
+
fill: false,
|
|
1569
|
+
tension: 0.4
|
|
1570
|
+
},
|
|
1571
|
+
{
|
|
1572
|
+
label: "Trade",
|
|
1573
|
+
data: tradeData,
|
|
1574
|
+
borderColor: "#ffc107",
|
|
1575
|
+
backgroundColor: "rgba(255, 193, 7, 0.1)",
|
|
1576
|
+
fill: false,
|
|
1577
|
+
tension: 0.4
|
|
1578
|
+
},
|
|
1579
|
+
{
|
|
1580
|
+
label: "VWAP",
|
|
1581
|
+
data: vwapData,
|
|
1582
|
+
borderColor: "#17a2b8",
|
|
1583
|
+
backgroundColor: "rgba(23, 162, 184, 0.1)",
|
|
1584
|
+
fill: false,
|
|
1585
|
+
tension: 0.4
|
|
1586
|
+
},
|
|
1587
|
+
{
|
|
1588
|
+
label: "TWAP",
|
|
1589
|
+
data: twapData,
|
|
1590
|
+
borderColor: "#6610f2",
|
|
1591
|
+
backgroundColor: "rgba(102, 16, 242, 0.1)",
|
|
1592
|
+
fill: false,
|
|
1593
|
+
tension: 0.4
|
|
1594
|
+
},
|
|
1595
|
+
{
|
|
1596
|
+
label: "Volume (Normalized)",
|
|
1597
|
+
data: normalizedVolumeData,
|
|
366
1598
|
borderColor: "#007bff",
|
|
367
1599
|
backgroundColor: "rgba(0, 123, 255, 0.1)",
|
|
368
1600
|
fill: true,
|
|
@@ -371,43 +1603,36 @@ var createGetStockGraphHandler = (marketDataPrices) => {
|
|
|
371
1603
|
]
|
|
372
1604
|
},
|
|
373
1605
|
options: {
|
|
1606
|
+
responsive: true,
|
|
374
1607
|
plugins: {
|
|
375
1608
|
title: {
|
|
376
1609
|
display: true,
|
|
377
|
-
text: `${symbol}
|
|
378
|
-
font: {
|
|
379
|
-
size: 16,
|
|
380
|
-
weight: "bold"
|
|
381
|
-
}
|
|
382
|
-
},
|
|
383
|
-
legend: {
|
|
384
|
-
display: true
|
|
1610
|
+
text: `${symbol} Market Data (Volume normalized to 30% of max price)`
|
|
385
1611
|
}
|
|
386
1612
|
},
|
|
387
1613
|
scales: {
|
|
388
1614
|
y: {
|
|
389
1615
|
beginAtZero: false,
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
},
|
|
394
|
-
x: {
|
|
395
|
-
grid: {
|
|
396
|
-
display: false
|
|
1616
|
+
title: {
|
|
1617
|
+
display: true,
|
|
1618
|
+
text: "Price / Normalized Volume"
|
|
397
1619
|
}
|
|
398
1620
|
}
|
|
399
1621
|
}
|
|
400
1622
|
}
|
|
401
|
-
}
|
|
1623
|
+
};
|
|
1624
|
+
chart.setConfig(config);
|
|
402
1625
|
const imageBuffer = await chart.toBinary();
|
|
403
|
-
const
|
|
1626
|
+
const base64 = imageBuffer.toString("base64");
|
|
404
1627
|
return {
|
|
405
1628
|
content: [
|
|
406
1629
|
{
|
|
407
|
-
type: "
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
1630
|
+
type: "resource",
|
|
1631
|
+
resource: {
|
|
1632
|
+
uri: "resource://graph",
|
|
1633
|
+
mimeType: "image/png",
|
|
1634
|
+
blob: base64
|
|
1635
|
+
}
|
|
411
1636
|
}
|
|
412
1637
|
]
|
|
413
1638
|
};
|
|
@@ -416,7 +1641,7 @@ var createGetStockGraphHandler = (marketDataPrices) => {
|
|
|
416
1641
|
content: [
|
|
417
1642
|
{
|
|
418
1643
|
type: "text",
|
|
419
|
-
text: `Error: ${error instanceof Error ? error.message : "Failed to generate
|
|
1644
|
+
text: `Error: ${error instanceof Error ? error.message : "Failed to generate graph"}`,
|
|
420
1645
|
uri: "getStockGraph"
|
|
421
1646
|
}
|
|
422
1647
|
],
|
|
@@ -449,9 +1674,53 @@ var createGetStockPriceHistoryHandler = (marketDataPrices) => {
|
|
|
449
1674
|
{
|
|
450
1675
|
symbol,
|
|
451
1676
|
count: priceHistory.length,
|
|
452
|
-
|
|
1677
|
+
data: priceHistory.map((point) => ({
|
|
453
1678
|
timestamp: new Date(point.timestamp).toISOString(),
|
|
454
|
-
|
|
1679
|
+
bid: point.bid,
|
|
1680
|
+
offer: point.offer,
|
|
1681
|
+
spread: point.spread,
|
|
1682
|
+
volume: point.volume,
|
|
1683
|
+
trade: point.trade,
|
|
1684
|
+
indexValue: point.indexValue,
|
|
1685
|
+
openingPrice: point.openingPrice,
|
|
1686
|
+
closingPrice: point.closingPrice,
|
|
1687
|
+
settlementPrice: point.settlementPrice,
|
|
1688
|
+
tradingSessionHighPrice: point.tradingSessionHighPrice,
|
|
1689
|
+
tradingSessionLowPrice: point.tradingSessionLowPrice,
|
|
1690
|
+
vwap: point.vwap,
|
|
1691
|
+
imbalance: point.imbalance,
|
|
1692
|
+
openInterest: point.openInterest,
|
|
1693
|
+
compositeUnderlyingPrice: point.compositeUnderlyingPrice,
|
|
1694
|
+
simulatedSellPrice: point.simulatedSellPrice,
|
|
1695
|
+
simulatedBuyPrice: point.simulatedBuyPrice,
|
|
1696
|
+
marginRate: point.marginRate,
|
|
1697
|
+
midPrice: point.midPrice,
|
|
1698
|
+
emptyBook: point.emptyBook,
|
|
1699
|
+
settleHighPrice: point.settleHighPrice,
|
|
1700
|
+
settleLowPrice: point.settleLowPrice,
|
|
1701
|
+
priorSettlePrice: point.priorSettlePrice,
|
|
1702
|
+
sessionHighBid: point.sessionHighBid,
|
|
1703
|
+
sessionLowOffer: point.sessionLowOffer,
|
|
1704
|
+
earlyPrices: point.earlyPrices,
|
|
1705
|
+
auctionClearingPrice: point.auctionClearingPrice,
|
|
1706
|
+
swapValueFactor: point.swapValueFactor,
|
|
1707
|
+
dailyValueAdjustmentForLongPositions: point.dailyValueAdjustmentForLongPositions,
|
|
1708
|
+
cumulativeValueAdjustmentForLongPositions: point.cumulativeValueAdjustmentForLongPositions,
|
|
1709
|
+
dailyValueAdjustmentForShortPositions: point.dailyValueAdjustmentForShortPositions,
|
|
1710
|
+
cumulativeValueAdjustmentForShortPositions: point.cumulativeValueAdjustmentForShortPositions,
|
|
1711
|
+
fixingPrice: point.fixingPrice,
|
|
1712
|
+
cashRate: point.cashRate,
|
|
1713
|
+
recoveryRate: point.recoveryRate,
|
|
1714
|
+
recoveryRateForLong: point.recoveryRateForLong,
|
|
1715
|
+
recoveryRateForShort: point.recoveryRateForShort,
|
|
1716
|
+
marketBid: point.marketBid,
|
|
1717
|
+
marketOffer: point.marketOffer,
|
|
1718
|
+
shortSaleMinPrice: point.shortSaleMinPrice,
|
|
1719
|
+
previousClosingPrice: point.previousClosingPrice,
|
|
1720
|
+
thresholdLimitPriceBanding: point.thresholdLimitPriceBanding,
|
|
1721
|
+
dailyFinancingValue: point.dailyFinancingValue,
|
|
1722
|
+
accruedFinancingValue: point.accruedFinancingValue,
|
|
1723
|
+
twap: point.twap
|
|
455
1724
|
}))
|
|
456
1725
|
},
|
|
457
1726
|
null,
|
|
@@ -466,7 +1735,7 @@ var createGetStockPriceHistoryHandler = (marketDataPrices) => {
|
|
|
466
1735
|
content: [
|
|
467
1736
|
{
|
|
468
1737
|
type: "text",
|
|
469
|
-
text: `Error: ${error instanceof Error ? error.message : "Failed to get
|
|
1738
|
+
text: `Error: ${error instanceof Error ? error.message : "Failed to get price history"}`,
|
|
470
1739
|
uri: "getStockPriceHistory"
|
|
471
1740
|
}
|
|
472
1741
|
],
|
|
@@ -574,7 +1843,7 @@ Parameters verified:
|
|
|
574
1843
|
- Symbol: ${args.symbol}
|
|
575
1844
|
- TimeInForce: ${args.timeInForce} (${timeInForceNames[args.timeInForce]})
|
|
576
1845
|
|
|
577
|
-
To execute this order, call the executeOrder tool with these exact same parameters
|
|
1846
|
+
To execute this order, call the executeOrder tool with these exact same parameters. Important: The user has to explicitly confirm before executeOrder is called!`,
|
|
578
1847
|
uri: "verifyOrder"
|
|
579
1848
|
}
|
|
580
1849
|
]
|
|
@@ -770,12 +2039,259 @@ var createToolHandlers = (parser, verifiedOrders, pendingRequests, marketDataPri
|
|
|
770
2039
|
executeOrder: createExecuteOrderHandler(parser, verifiedOrders, pendingRequests),
|
|
771
2040
|
marketDataRequest: createMarketDataRequestHandler(parser, pendingRequests),
|
|
772
2041
|
getStockGraph: createGetStockGraphHandler(marketDataPrices),
|
|
773
|
-
getStockPriceHistory: createGetStockPriceHistoryHandler(marketDataPrices)
|
|
2042
|
+
getStockPriceHistory: createGetStockPriceHistoryHandler(marketDataPrices),
|
|
2043
|
+
technicalAnalysis: createTechnicalAnalysisHandler(marketDataPrices)
|
|
774
2044
|
});
|
|
775
2045
|
|
|
2046
|
+
// src/utils/messageHandler.ts
|
|
2047
|
+
var import_fixparser3 = require("fixparser");
|
|
2048
|
+
function getEnumValue(enumObj, name) {
|
|
2049
|
+
return enumObj[name] || name;
|
|
2050
|
+
}
|
|
2051
|
+
function handleMessage(message, parser, pendingRequests, marketDataPrices, maxPriceHistory, onPriceUpdate) {
|
|
2052
|
+
const msgType = message.messageType;
|
|
2053
|
+
if (msgType === import_fixparser3.Messages.MarketDataSnapshotFullRefresh || msgType === import_fixparser3.Messages.MarketDataIncrementalRefresh) {
|
|
2054
|
+
const symbol = message.getField(import_fixparser3.Fields.Symbol)?.value;
|
|
2055
|
+
const fixJson = message.toFIXJSON();
|
|
2056
|
+
const entries = fixJson.Body?.NoMDEntries || [];
|
|
2057
|
+
const data = {
|
|
2058
|
+
timestamp: Date.now(),
|
|
2059
|
+
bid: 0,
|
|
2060
|
+
offer: 0,
|
|
2061
|
+
spread: 0,
|
|
2062
|
+
volume: 0,
|
|
2063
|
+
trade: 0,
|
|
2064
|
+
indexValue: 0,
|
|
2065
|
+
openingPrice: 0,
|
|
2066
|
+
closingPrice: 0,
|
|
2067
|
+
settlementPrice: 0,
|
|
2068
|
+
tradingSessionHighPrice: 0,
|
|
2069
|
+
tradingSessionLowPrice: 0,
|
|
2070
|
+
vwap: 0,
|
|
2071
|
+
imbalance: 0,
|
|
2072
|
+
openInterest: 0,
|
|
2073
|
+
compositeUnderlyingPrice: 0,
|
|
2074
|
+
simulatedSellPrice: 0,
|
|
2075
|
+
simulatedBuyPrice: 0,
|
|
2076
|
+
marginRate: 0,
|
|
2077
|
+
midPrice: 0,
|
|
2078
|
+
emptyBook: 0,
|
|
2079
|
+
settleHighPrice: 0,
|
|
2080
|
+
settleLowPrice: 0,
|
|
2081
|
+
priorSettlePrice: 0,
|
|
2082
|
+
sessionHighBid: 0,
|
|
2083
|
+
sessionLowOffer: 0,
|
|
2084
|
+
earlyPrices: 0,
|
|
2085
|
+
auctionClearingPrice: 0,
|
|
2086
|
+
swapValueFactor: 0,
|
|
2087
|
+
dailyValueAdjustmentForLongPositions: 0,
|
|
2088
|
+
cumulativeValueAdjustmentForLongPositions: 0,
|
|
2089
|
+
dailyValueAdjustmentForShortPositions: 0,
|
|
2090
|
+
cumulativeValueAdjustmentForShortPositions: 0,
|
|
2091
|
+
fixingPrice: 0,
|
|
2092
|
+
cashRate: 0,
|
|
2093
|
+
recoveryRate: 0,
|
|
2094
|
+
recoveryRateForLong: 0,
|
|
2095
|
+
recoveryRateForShort: 0,
|
|
2096
|
+
marketBid: 0,
|
|
2097
|
+
marketOffer: 0,
|
|
2098
|
+
shortSaleMinPrice: 0,
|
|
2099
|
+
previousClosingPrice: 0,
|
|
2100
|
+
thresholdLimitPriceBanding: 0,
|
|
2101
|
+
dailyFinancingValue: 0,
|
|
2102
|
+
accruedFinancingValue: 0,
|
|
2103
|
+
twap: 0
|
|
2104
|
+
};
|
|
2105
|
+
for (const entry of entries) {
|
|
2106
|
+
const entryType = entry.MDEntryType;
|
|
2107
|
+
const price = entry.MDEntryPx ? Number.parseFloat(entry.MDEntryPx) : 0;
|
|
2108
|
+
const size = entry.MDEntrySize ? Number.parseFloat(entry.MDEntrySize) : 0;
|
|
2109
|
+
const enumValue = getEnumValue(import_fixparser3.MDEntryType, entryType);
|
|
2110
|
+
switch (enumValue) {
|
|
2111
|
+
case import_fixparser3.MDEntryType.Bid:
|
|
2112
|
+
data.bid = price;
|
|
2113
|
+
break;
|
|
2114
|
+
case import_fixparser3.MDEntryType.Offer:
|
|
2115
|
+
data.offer = price;
|
|
2116
|
+
break;
|
|
2117
|
+
case import_fixparser3.MDEntryType.Trade:
|
|
2118
|
+
data.trade = price;
|
|
2119
|
+
break;
|
|
2120
|
+
case import_fixparser3.MDEntryType.IndexValue:
|
|
2121
|
+
data.indexValue = price;
|
|
2122
|
+
break;
|
|
2123
|
+
case import_fixparser3.MDEntryType.OpeningPrice:
|
|
2124
|
+
data.openingPrice = price;
|
|
2125
|
+
break;
|
|
2126
|
+
case import_fixparser3.MDEntryType.ClosingPrice:
|
|
2127
|
+
data.closingPrice = price;
|
|
2128
|
+
break;
|
|
2129
|
+
case import_fixparser3.MDEntryType.SettlementPrice:
|
|
2130
|
+
data.settlementPrice = price;
|
|
2131
|
+
break;
|
|
2132
|
+
case import_fixparser3.MDEntryType.TradingSessionHighPrice:
|
|
2133
|
+
data.tradingSessionHighPrice = price;
|
|
2134
|
+
break;
|
|
2135
|
+
case import_fixparser3.MDEntryType.TradingSessionLowPrice:
|
|
2136
|
+
data.tradingSessionLowPrice = price;
|
|
2137
|
+
break;
|
|
2138
|
+
case import_fixparser3.MDEntryType.VWAP:
|
|
2139
|
+
data.vwap = price;
|
|
2140
|
+
break;
|
|
2141
|
+
case import_fixparser3.MDEntryType.Imbalance:
|
|
2142
|
+
data.imbalance = size;
|
|
2143
|
+
break;
|
|
2144
|
+
case import_fixparser3.MDEntryType.TradeVolume:
|
|
2145
|
+
data.volume = size;
|
|
2146
|
+
break;
|
|
2147
|
+
case import_fixparser3.MDEntryType.OpenInterest:
|
|
2148
|
+
data.openInterest = size;
|
|
2149
|
+
break;
|
|
2150
|
+
case import_fixparser3.MDEntryType.CompositeUnderlyingPrice:
|
|
2151
|
+
data.compositeUnderlyingPrice = price;
|
|
2152
|
+
break;
|
|
2153
|
+
case import_fixparser3.MDEntryType.SimulatedSellPrice:
|
|
2154
|
+
data.simulatedSellPrice = price;
|
|
2155
|
+
break;
|
|
2156
|
+
case import_fixparser3.MDEntryType.SimulatedBuyPrice:
|
|
2157
|
+
data.simulatedBuyPrice = price;
|
|
2158
|
+
break;
|
|
2159
|
+
case import_fixparser3.MDEntryType.MarginRate:
|
|
2160
|
+
data.marginRate = price;
|
|
2161
|
+
break;
|
|
2162
|
+
case import_fixparser3.MDEntryType.MidPrice:
|
|
2163
|
+
data.midPrice = price;
|
|
2164
|
+
break;
|
|
2165
|
+
case import_fixparser3.MDEntryType.EmptyBook:
|
|
2166
|
+
data.emptyBook = 1;
|
|
2167
|
+
break;
|
|
2168
|
+
case import_fixparser3.MDEntryType.SettleHighPrice:
|
|
2169
|
+
data.settleHighPrice = price;
|
|
2170
|
+
break;
|
|
2171
|
+
case import_fixparser3.MDEntryType.SettleLowPrice:
|
|
2172
|
+
data.settleLowPrice = price;
|
|
2173
|
+
break;
|
|
2174
|
+
case import_fixparser3.MDEntryType.PriorSettlePrice:
|
|
2175
|
+
data.priorSettlePrice = price;
|
|
2176
|
+
break;
|
|
2177
|
+
case import_fixparser3.MDEntryType.SessionHighBid:
|
|
2178
|
+
data.sessionHighBid = price;
|
|
2179
|
+
break;
|
|
2180
|
+
case import_fixparser3.MDEntryType.SessionLowOffer:
|
|
2181
|
+
data.sessionLowOffer = price;
|
|
2182
|
+
break;
|
|
2183
|
+
case import_fixparser3.MDEntryType.EarlyPrices:
|
|
2184
|
+
data.earlyPrices = price;
|
|
2185
|
+
break;
|
|
2186
|
+
case import_fixparser3.MDEntryType.AuctionClearingPrice:
|
|
2187
|
+
data.auctionClearingPrice = price;
|
|
2188
|
+
break;
|
|
2189
|
+
case import_fixparser3.MDEntryType.SwapValueFactor:
|
|
2190
|
+
data.swapValueFactor = price;
|
|
2191
|
+
break;
|
|
2192
|
+
case import_fixparser3.MDEntryType.DailyValueAdjustmentForLongPositions:
|
|
2193
|
+
data.dailyValueAdjustmentForLongPositions = price;
|
|
2194
|
+
break;
|
|
2195
|
+
case import_fixparser3.MDEntryType.CumulativeValueAdjustmentForLongPositions:
|
|
2196
|
+
data.cumulativeValueAdjustmentForLongPositions = price;
|
|
2197
|
+
break;
|
|
2198
|
+
case import_fixparser3.MDEntryType.DailyValueAdjustmentForShortPositions:
|
|
2199
|
+
data.dailyValueAdjustmentForShortPositions = price;
|
|
2200
|
+
break;
|
|
2201
|
+
case import_fixparser3.MDEntryType.CumulativeValueAdjustmentForShortPositions:
|
|
2202
|
+
data.cumulativeValueAdjustmentForShortPositions = price;
|
|
2203
|
+
break;
|
|
2204
|
+
case import_fixparser3.MDEntryType.FixingPrice:
|
|
2205
|
+
data.fixingPrice = price;
|
|
2206
|
+
break;
|
|
2207
|
+
case import_fixparser3.MDEntryType.CashRate:
|
|
2208
|
+
data.cashRate = price;
|
|
2209
|
+
break;
|
|
2210
|
+
case import_fixparser3.MDEntryType.RecoveryRate:
|
|
2211
|
+
data.recoveryRate = price;
|
|
2212
|
+
break;
|
|
2213
|
+
case import_fixparser3.MDEntryType.RecoveryRateForLong:
|
|
2214
|
+
data.recoveryRateForLong = price;
|
|
2215
|
+
break;
|
|
2216
|
+
case import_fixparser3.MDEntryType.RecoveryRateForShort:
|
|
2217
|
+
data.recoveryRateForShort = price;
|
|
2218
|
+
break;
|
|
2219
|
+
case import_fixparser3.MDEntryType.MarketBid:
|
|
2220
|
+
data.marketBid = price;
|
|
2221
|
+
break;
|
|
2222
|
+
case import_fixparser3.MDEntryType.MarketOffer:
|
|
2223
|
+
data.marketOffer = price;
|
|
2224
|
+
break;
|
|
2225
|
+
case import_fixparser3.MDEntryType.ShortSaleMinPrice:
|
|
2226
|
+
data.shortSaleMinPrice = price;
|
|
2227
|
+
break;
|
|
2228
|
+
case import_fixparser3.MDEntryType.PreviousClosingPrice:
|
|
2229
|
+
data.previousClosingPrice = price;
|
|
2230
|
+
break;
|
|
2231
|
+
case import_fixparser3.MDEntryType.ThresholdLimitPriceBanding:
|
|
2232
|
+
data.thresholdLimitPriceBanding = price;
|
|
2233
|
+
break;
|
|
2234
|
+
case import_fixparser3.MDEntryType.DailyFinancingValue:
|
|
2235
|
+
data.dailyFinancingValue = price;
|
|
2236
|
+
break;
|
|
2237
|
+
case import_fixparser3.MDEntryType.AccruedFinancingValue:
|
|
2238
|
+
data.accruedFinancingValue = price;
|
|
2239
|
+
break;
|
|
2240
|
+
case import_fixparser3.MDEntryType.TWAP:
|
|
2241
|
+
data.twap = price;
|
|
2242
|
+
break;
|
|
2243
|
+
}
|
|
2244
|
+
}
|
|
2245
|
+
data.spread = data.offer - data.bid;
|
|
2246
|
+
if (!marketDataPrices.has(symbol)) {
|
|
2247
|
+
marketDataPrices.set(symbol, []);
|
|
2248
|
+
}
|
|
2249
|
+
const prices = marketDataPrices.get(symbol);
|
|
2250
|
+
prices.push(data);
|
|
2251
|
+
if (prices.length > maxPriceHistory) {
|
|
2252
|
+
prices.splice(0, prices.length - maxPriceHistory);
|
|
2253
|
+
}
|
|
2254
|
+
onPriceUpdate?.(symbol, data);
|
|
2255
|
+
const mdReqID = message.getField(import_fixparser3.Fields.MDReqID)?.value;
|
|
2256
|
+
if (mdReqID) {
|
|
2257
|
+
const callback = pendingRequests.get(mdReqID);
|
|
2258
|
+
if (callback) {
|
|
2259
|
+
callback(message);
|
|
2260
|
+
pendingRequests.delete(mdReqID);
|
|
2261
|
+
}
|
|
2262
|
+
}
|
|
2263
|
+
} else if (msgType === import_fixparser3.Messages.ExecutionReport) {
|
|
2264
|
+
const reqId = message.getField(import_fixparser3.Fields.ClOrdID)?.value;
|
|
2265
|
+
const callback = pendingRequests.get(reqId);
|
|
2266
|
+
if (callback) {
|
|
2267
|
+
callback(message);
|
|
2268
|
+
pendingRequests.delete(reqId);
|
|
2269
|
+
}
|
|
2270
|
+
}
|
|
2271
|
+
}
|
|
2272
|
+
|
|
776
2273
|
// src/MCPLocal.ts
|
|
777
|
-
var MCPLocal = class {
|
|
778
|
-
|
|
2274
|
+
var MCPLocal = class extends MCPBase {
|
|
2275
|
+
/**
|
|
2276
|
+
* Map to store verified orders before execution
|
|
2277
|
+
* @private
|
|
2278
|
+
*/
|
|
2279
|
+
verifiedOrders = /* @__PURE__ */ new Map();
|
|
2280
|
+
/**
|
|
2281
|
+
* Map to store pending requests and their callbacks
|
|
2282
|
+
* @private
|
|
2283
|
+
*/
|
|
2284
|
+
pendingRequests = /* @__PURE__ */ new Map();
|
|
2285
|
+
/**
|
|
2286
|
+
* Map to store market data prices for each symbol
|
|
2287
|
+
* @private
|
|
2288
|
+
*/
|
|
2289
|
+
marketDataPrices = /* @__PURE__ */ new Map();
|
|
2290
|
+
/**
|
|
2291
|
+
* Maximum number of price history entries to keep per symbol
|
|
2292
|
+
* @private
|
|
2293
|
+
*/
|
|
2294
|
+
MAX_PRICE_HISTORY = 1e5;
|
|
779
2295
|
server = new import_server.Server(
|
|
780
2296
|
{
|
|
781
2297
|
name: "fixparser",
|
|
@@ -797,77 +2313,13 @@ var MCPLocal = class {
|
|
|
797
2313
|
}
|
|
798
2314
|
);
|
|
799
2315
|
transport = new import_stdio.StdioServerTransport();
|
|
800
|
-
onReady = void 0;
|
|
801
|
-
pendingRequests = /* @__PURE__ */ new Map();
|
|
802
|
-
verifiedOrders = /* @__PURE__ */ new Map();
|
|
803
|
-
marketDataPrices = /* @__PURE__ */ new Map();
|
|
804
|
-
MAX_PRICE_HISTORY = 1e5;
|
|
805
|
-
// Maximum number of price points to store per symbol
|
|
806
2316
|
constructor({ logger, onReady }) {
|
|
807
|
-
|
|
2317
|
+
super({ logger, onReady });
|
|
808
2318
|
}
|
|
809
2319
|
async register(parser) {
|
|
810
2320
|
this.parser = parser;
|
|
811
2321
|
this.parser.addOnMessageCallback((message) => {
|
|
812
|
-
this.parser
|
|
813
|
-
level: "info",
|
|
814
|
-
message: `MCP Server received message: ${message.messageType}: ${message.description}`
|
|
815
|
-
});
|
|
816
|
-
const msgType = message.messageType;
|
|
817
|
-
if (msgType === import_fixparser3.Messages.MarketDataSnapshotFullRefresh || msgType === import_fixparser3.Messages.ExecutionReport || msgType === import_fixparser3.Messages.Reject || msgType === import_fixparser3.Messages.MarketDataIncrementalRefresh) {
|
|
818
|
-
this.parser?.logger.log({
|
|
819
|
-
level: "info",
|
|
820
|
-
message: `MCP Server handling message type: ${msgType}`
|
|
821
|
-
});
|
|
822
|
-
let id;
|
|
823
|
-
if (msgType === import_fixparser3.Messages.MarketDataIncrementalRefresh || msgType === import_fixparser3.Messages.MarketDataSnapshotFullRefresh) {
|
|
824
|
-
const symbol = message.getField(import_fixparser3.Fields.Symbol);
|
|
825
|
-
const price = message.getField(import_fixparser3.Fields.MDEntryPx);
|
|
826
|
-
const timestamp = message.getField(import_fixparser3.Fields.MDEntryTime)?.value || Date.now();
|
|
827
|
-
if (symbol?.value && price?.value) {
|
|
828
|
-
const symbolStr = String(symbol.value);
|
|
829
|
-
const priceNum = Number(price.value);
|
|
830
|
-
const priceHistory = this.marketDataPrices.get(symbolStr) || [];
|
|
831
|
-
priceHistory.push({
|
|
832
|
-
timestamp: Number(timestamp),
|
|
833
|
-
price: priceNum
|
|
834
|
-
});
|
|
835
|
-
if (priceHistory.length > this.MAX_PRICE_HISTORY) {
|
|
836
|
-
priceHistory.shift();
|
|
837
|
-
}
|
|
838
|
-
this.marketDataPrices.set(symbolStr, priceHistory);
|
|
839
|
-
this.parser?.logger.log({
|
|
840
|
-
level: "info",
|
|
841
|
-
message: `MCP Server added ${symbol}: ${priceNum}`
|
|
842
|
-
});
|
|
843
|
-
this.server.notification({
|
|
844
|
-
method: "priceUpdate",
|
|
845
|
-
params: {
|
|
846
|
-
symbol: symbolStr,
|
|
847
|
-
price: priceNum,
|
|
848
|
-
timestamp: Number(timestamp)
|
|
849
|
-
}
|
|
850
|
-
});
|
|
851
|
-
}
|
|
852
|
-
}
|
|
853
|
-
if (msgType === import_fixparser3.Messages.MarketDataSnapshotFullRefresh) {
|
|
854
|
-
const mdReqID = message.getField(import_fixparser3.Fields.MDReqID);
|
|
855
|
-
if (mdReqID) id = String(mdReqID.value);
|
|
856
|
-
} else if (msgType === import_fixparser3.Messages.ExecutionReport) {
|
|
857
|
-
const clOrdID = message.getField(import_fixparser3.Fields.ClOrdID);
|
|
858
|
-
if (clOrdID) id = String(clOrdID.value);
|
|
859
|
-
} else if (msgType === import_fixparser3.Messages.Reject) {
|
|
860
|
-
const refSeqNum = message.getField(import_fixparser3.Fields.RefSeqNum);
|
|
861
|
-
if (refSeqNum) id = String(refSeqNum.value);
|
|
862
|
-
}
|
|
863
|
-
if (id) {
|
|
864
|
-
const callback = this.pendingRequests.get(id);
|
|
865
|
-
if (callback) {
|
|
866
|
-
callback(message);
|
|
867
|
-
this.pendingRequests.delete(id);
|
|
868
|
-
}
|
|
869
|
-
}
|
|
870
|
-
}
|
|
2322
|
+
handleMessage(message, this.parser, this.pendingRequests, this.marketDataPrices, this.MAX_PRICE_HISTORY);
|
|
871
2323
|
});
|
|
872
2324
|
this.addWorkflows();
|
|
873
2325
|
await this.server.connect(this.transport);
|
|
@@ -882,18 +2334,15 @@ var MCPLocal = class {
|
|
|
882
2334
|
if (!this.server) {
|
|
883
2335
|
return;
|
|
884
2336
|
}
|
|
885
|
-
this.server.setRequestHandler(
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
};
|
|
895
|
-
}
|
|
896
|
-
);
|
|
2337
|
+
this.server.setRequestHandler(import_zod.z.object({ method: import_zod.z.literal("tools/list") }), async () => {
|
|
2338
|
+
return {
|
|
2339
|
+
tools: Object.entries(toolSchemas).map(([name, { description, schema }]) => ({
|
|
2340
|
+
name,
|
|
2341
|
+
description,
|
|
2342
|
+
inputSchema: schema
|
|
2343
|
+
}))
|
|
2344
|
+
};
|
|
2345
|
+
});
|
|
897
2346
|
this.server.setRequestHandler(
|
|
898
2347
|
import_zod.z.object({
|
|
899
2348
|
method: import_zod.z.literal("tools/call"),
|
|
@@ -905,7 +2354,7 @@ var MCPLocal = class {
|
|
|
905
2354
|
}).optional()
|
|
906
2355
|
})
|
|
907
2356
|
}),
|
|
908
|
-
async (request
|
|
2357
|
+
async (request) => {
|
|
909
2358
|
const { name, arguments: args } = request.params;
|
|
910
2359
|
const toolHandlers = createToolHandlers(
|
|
911
2360
|
this.parser,
|