fixparser-plugin-mcp 9.1.7-c6228661 → 9.1.7-d47799ff
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 +596 -1543
- package/build/cjs/MCPLocal.js.map +4 -4
- package/build/cjs/MCPRemote.js +435 -1618
- package/build/cjs/MCPRemote.js.map +4 -4
- package/build/esm/MCPLocal.mjs +596 -1533
- package/build/esm/MCPLocal.mjs.map +4 -4
- package/build/esm/MCPRemote.mjs +444 -1608
- package/build/esm/MCPRemote.mjs.map +4 -4
- package/build-examples/cjs/example_mcp_local.js +46 -10
- package/build-examples/cjs/example_mcp_local.js.map +4 -4
- package/build-examples/esm/example_mcp_local.mjs +46 -10
- package/build-examples/esm/example_mcp_local.mjs.map +4 -4
- package/package.json +8 -9
- package/types/MCPLocal.d.ts +14 -0
- package/types/MCPRemote.d.ts +60 -0
- package/types/PluginOptions.d.ts +6 -0
- package/types/index.d.ts +3 -0
- package/build/cjs/index.js +0 -1917
- package/build/cjs/index.js.map +0 -7
- package/build/esm/index.mjs +0 -1879
- package/build/esm/index.mjs.map +0 -7
- package/build-examples/cjs/example_mcp_remote.js +0 -18
- package/build-examples/cjs/example_mcp_remote.js.map +0 -7
- package/build-examples/esm/example_mcp_remote.mjs +0 -18
- package/build-examples/esm/example_mcp_remote.mjs.map +0 -7
package/build/esm/MCPLocal.mjs
CHANGED
|
@@ -1,1600 +1,663 @@
|
|
|
1
1
|
// src/MCPLocal.ts
|
|
2
|
-
import {
|
|
2
|
+
import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
3
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
4
|
+
import { Field, Fields, Messages } from "fixparser";
|
|
4
5
|
import { z } from "zod";
|
|
5
|
-
|
|
6
|
-
// src/MCPBase.ts
|
|
7
|
-
var MCPBase = class {
|
|
8
|
-
/**
|
|
9
|
-
* Optional logger instance for diagnostics and output.
|
|
10
|
-
* @protected
|
|
11
|
-
*/
|
|
12
|
-
logger;
|
|
13
|
-
/**
|
|
14
|
-
* FIXParser instance, set during plugin register().
|
|
15
|
-
* @protected
|
|
16
|
-
*/
|
|
6
|
+
var MCPLocal = class {
|
|
17
7
|
parser;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
8
|
+
server = new McpServer(
|
|
9
|
+
{
|
|
10
|
+
name: "fixparser",
|
|
11
|
+
version: "1.0.0"
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
capabilities: {
|
|
15
|
+
tools: {},
|
|
16
|
+
prompts: {},
|
|
17
|
+
resources: {}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
);
|
|
21
|
+
transport = new StdioServerTransport();
|
|
22
22
|
onReady = void 0;
|
|
23
|
-
/**
|
|
24
|
-
* Map to store verified orders before execution
|
|
25
|
-
* @protected
|
|
26
|
-
*/
|
|
27
|
-
verifiedOrders = /* @__PURE__ */ new Map();
|
|
28
|
-
/**
|
|
29
|
-
* Map to store pending market data requests
|
|
30
|
-
* @protected
|
|
31
|
-
*/
|
|
32
23
|
pendingRequests = /* @__PURE__ */ new Map();
|
|
33
|
-
|
|
34
|
-
* Map to store market data prices
|
|
35
|
-
* @protected
|
|
36
|
-
*/
|
|
37
|
-
marketDataPrices = /* @__PURE__ */ new Map();
|
|
38
|
-
/**
|
|
39
|
-
* Maximum number of price history entries to keep per symbol
|
|
40
|
-
* @protected
|
|
41
|
-
*/
|
|
42
|
-
MAX_PRICE_HISTORY = 1e5;
|
|
24
|
+
verifiedOrders = /* @__PURE__ */ new Map();
|
|
43
25
|
constructor({ logger, onReady }) {
|
|
44
|
-
this.
|
|
45
|
-
this.onReady = onReady;
|
|
26
|
+
if (onReady) this.onReady = onReady;
|
|
46
27
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
28
|
+
async register(parser) {
|
|
29
|
+
this.parser = parser;
|
|
30
|
+
this.parser.addOnMessageCallback((message) => {
|
|
31
|
+
const msgType = message.messageType;
|
|
32
|
+
if (msgType === Messages.MarketDataSnapshotFullRefresh || msgType === Messages.ExecutionReport || msgType === Messages.Reject) {
|
|
33
|
+
let id;
|
|
34
|
+
if (msgType === Messages.MarketDataSnapshotFullRefresh) {
|
|
35
|
+
const mdReqID = message.getField(Fields.MDReqID);
|
|
36
|
+
if (mdReqID) id = String(mdReqID.value);
|
|
37
|
+
} else if (msgType === Messages.ExecutionReport) {
|
|
38
|
+
const clOrdID = message.getField(Fields.ClOrdID);
|
|
39
|
+
if (clOrdID) id = String(clOrdID.value);
|
|
40
|
+
} else if (msgType === Messages.Reject) {
|
|
41
|
+
const refSeqNum = message.getField(Fields.RefSeqNum);
|
|
42
|
+
if (refSeqNum) id = String(refSeqNum.value);
|
|
43
|
+
}
|
|
44
|
+
if (id) {
|
|
45
|
+
const callback = this.pendingRequests.get(id);
|
|
46
|
+
if (callback) {
|
|
47
|
+
callback(message);
|
|
48
|
+
this.pendingRequests.delete(id);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
this.addWorkflows();
|
|
54
|
+
await this.server.connect(this.transport);
|
|
55
|
+
if (this.onReady) {
|
|
56
|
+
this.onReady();
|
|
59
57
|
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
type: "object",
|
|
65
|
-
properties: {
|
|
66
|
-
fixString: { type: "string" }
|
|
67
|
-
},
|
|
68
|
-
required: ["fixString"]
|
|
58
|
+
}
|
|
59
|
+
addWorkflows() {
|
|
60
|
+
if (!this.parser) {
|
|
61
|
+
return;
|
|
69
62
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
description: "Verifies order parameters before execution. verifyOrder must be called before executeOrder.",
|
|
73
|
-
schema: {
|
|
74
|
-
type: "object",
|
|
75
|
-
properties: {
|
|
76
|
-
clOrdID: { type: "string" },
|
|
77
|
-
handlInst: {
|
|
78
|
-
type: "string",
|
|
79
|
-
enum: ["1", "2", "3"],
|
|
80
|
-
description: "Handling Instructions: 1=Automated Execution No Intervention, 2=Automated Execution Intervention OK, 3=Manual Order"
|
|
81
|
-
},
|
|
82
|
-
quantity: { type: "string" },
|
|
83
|
-
price: { type: "string" },
|
|
84
|
-
ordType: {
|
|
85
|
-
type: "string",
|
|
86
|
-
enum: [
|
|
87
|
-
"1",
|
|
88
|
-
"2",
|
|
89
|
-
"3",
|
|
90
|
-
"4",
|
|
91
|
-
"5",
|
|
92
|
-
"6",
|
|
93
|
-
"7",
|
|
94
|
-
"8",
|
|
95
|
-
"9",
|
|
96
|
-
"A",
|
|
97
|
-
"B",
|
|
98
|
-
"C",
|
|
99
|
-
"D",
|
|
100
|
-
"E",
|
|
101
|
-
"F",
|
|
102
|
-
"G",
|
|
103
|
-
"H",
|
|
104
|
-
"I",
|
|
105
|
-
"J",
|
|
106
|
-
"K",
|
|
107
|
-
"L",
|
|
108
|
-
"M",
|
|
109
|
-
"P",
|
|
110
|
-
"Q",
|
|
111
|
-
"R",
|
|
112
|
-
"S"
|
|
113
|
-
],
|
|
114
|
-
description: "Order Type: 1=Market, 2=Limit, 3=Stop, 4=StopLimit, 5=MarketOnClose, 6=WithOrWithout, 7=LimitOrBetter, 8=LimitWithOrWithout, 9=OnBasis, A=OnClose, B=LimitOnClose, C=ForexMarket, D=PreviouslyQuoted, E=PreviouslyIndicated, F=ForexLimit, G=ForexSwap, H=ForexPreviouslyQuoted, I=Funari, J=MarketIfTouched, K=MarketWithLeftOverAsLimit, L=PreviousFundValuationPoint, M=NextFundValuationPoint, P=Pegged, Q=CounterOrderSelection, R=StopOnBidOrOffer, S=StopLimitOnBidOrOffer"
|
|
115
|
-
},
|
|
116
|
-
side: {
|
|
117
|
-
type: "string",
|
|
118
|
-
enum: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H"],
|
|
119
|
-
description: "Side: 1=Buy, 2=Sell, 3=BuyMinus, 4=SellPlus, 5=SellShort, 6=SellShortExempt, 7=Undisclosed, 8=Cross, 9=CrossShort, A=CrossShortExempt, B=AsDefined, C=Opposite, D=Subscribe, E=Redeem, F=Lend, G=Borrow, H=SellUndisclosed"
|
|
120
|
-
},
|
|
121
|
-
symbol: { type: "string" },
|
|
122
|
-
timeInForce: {
|
|
123
|
-
type: "string",
|
|
124
|
-
enum: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C"],
|
|
125
|
-
description: "Time In Force: 0=Day, 1=GoodTillCancel, 2=AtTheOpening, 3=ImmediateOrCancel, 4=FillOrKill, 5=GoodTillCrossing, 6=GoodTillDate, 7=AtTheClose, 8=GoodThroughCrossing, 9=AtCrossing, A=GoodForTime, B=GoodForAuction, C=GoodForMonth"
|
|
126
|
-
}
|
|
127
|
-
},
|
|
128
|
-
required: ["clOrdID", "handlInst", "quantity", "price", "ordType", "side", "symbol", "timeInForce"]
|
|
63
|
+
if (!this.server) {
|
|
64
|
+
return;
|
|
129
65
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
properties: {
|
|
136
|
-
clOrdID: { type: "string" },
|
|
137
|
-
handlInst: {
|
|
138
|
-
type: "string",
|
|
139
|
-
enum: ["1", "2", "3"],
|
|
140
|
-
description: "Handling Instructions: 1=Automated Execution No Intervention, 2=Automated Execution Intervention OK, 3=Manual Order"
|
|
141
|
-
},
|
|
142
|
-
quantity: { type: "string" },
|
|
143
|
-
price: { type: "string" },
|
|
144
|
-
ordType: {
|
|
145
|
-
type: "string",
|
|
146
|
-
enum: [
|
|
147
|
-
"1",
|
|
148
|
-
"2",
|
|
149
|
-
"3",
|
|
150
|
-
"4",
|
|
151
|
-
"5",
|
|
152
|
-
"6",
|
|
153
|
-
"7",
|
|
154
|
-
"8",
|
|
155
|
-
"9",
|
|
156
|
-
"A",
|
|
157
|
-
"B",
|
|
158
|
-
"C",
|
|
159
|
-
"D",
|
|
160
|
-
"E",
|
|
161
|
-
"F",
|
|
162
|
-
"G",
|
|
163
|
-
"H",
|
|
164
|
-
"I",
|
|
165
|
-
"J",
|
|
166
|
-
"K",
|
|
167
|
-
"L",
|
|
168
|
-
"M",
|
|
169
|
-
"P",
|
|
170
|
-
"Q",
|
|
171
|
-
"R",
|
|
172
|
-
"S"
|
|
173
|
-
],
|
|
174
|
-
description: "Order Type: 1=Market, 2=Limit, 3=Stop, 4=StopLimit, 5=MarketOnClose, 6=WithOrWithout, 7=LimitOrBetter, 8=LimitWithOrWithout, 9=OnBasis, A=OnClose, B=LimitOnClose, C=ForexMarket, D=PreviouslyQuoted, E=PreviouslyIndicated, F=ForexLimit, G=ForexSwap, H=ForexPreviouslyQuoted, I=Funari, J=MarketIfTouched, K=MarketWithLeftOverAsLimit, L=PreviousFundValuationPoint, M=NextFundValuationPoint, P=Pegged, Q=CounterOrderSelection, R=StopOnBidOrOffer, S=StopLimitOnBidOrOffer"
|
|
175
|
-
},
|
|
176
|
-
side: {
|
|
177
|
-
type: "string",
|
|
178
|
-
enum: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H"],
|
|
179
|
-
description: "Side: 1=Buy, 2=Sell, 3=BuyMinus, 4=SellPlus, 5=SellShort, 6=SellShortExempt, 7=Undisclosed, 8=Cross, 9=CrossShort, A=CrossShortExempt, B=AsDefined, C=Opposite, D=Subscribe, E=Redeem, F=Lend, G=Borrow, H=SellUndisclosed"
|
|
180
|
-
},
|
|
181
|
-
symbol: { type: "string" },
|
|
182
|
-
timeInForce: {
|
|
183
|
-
type: "string",
|
|
184
|
-
enum: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C"],
|
|
185
|
-
description: "Time In Force: 0=Day, 1=GoodTillCancel, 2=AtTheOpening, 3=ImmediateOrCancel, 4=FillOrKill, 5=GoodTillCrossing, 6=GoodTillDate, 7=AtTheClose, 8=GoodThroughCrossing, 9=AtCrossing, A=GoodForTime, B=GoodForAuction, C=GoodForMonth"
|
|
186
|
-
}
|
|
66
|
+
this.server.tool(
|
|
67
|
+
"parse",
|
|
68
|
+
"Parses a FIX message and describes it in plain language",
|
|
69
|
+
{
|
|
70
|
+
fixString: z.string().describe("FIX message string to parse")
|
|
187
71
|
},
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
type: "string",
|
|
205
|
-
enum: ["0", "1", "2"],
|
|
206
|
-
description: "Subscription Request Type: 0=Snapshot, 1=Snapshot + Updates, 2=Disable Previous Snapshot + Update Request"
|
|
207
|
-
},
|
|
208
|
-
mdEntryTypes: {
|
|
209
|
-
type: "array",
|
|
210
|
-
items: {
|
|
211
|
-
type: "string",
|
|
212
|
-
enum: [
|
|
213
|
-
"0",
|
|
214
|
-
"1",
|
|
215
|
-
"2",
|
|
216
|
-
"3",
|
|
217
|
-
"4",
|
|
218
|
-
"5",
|
|
219
|
-
"6",
|
|
220
|
-
"7",
|
|
221
|
-
"8",
|
|
222
|
-
"9",
|
|
223
|
-
"A",
|
|
224
|
-
"B",
|
|
225
|
-
"C",
|
|
226
|
-
"D",
|
|
227
|
-
"E",
|
|
228
|
-
"F",
|
|
229
|
-
"G",
|
|
230
|
-
"H",
|
|
231
|
-
"I",
|
|
232
|
-
"J",
|
|
233
|
-
"K",
|
|
234
|
-
"L",
|
|
235
|
-
"M",
|
|
236
|
-
"N",
|
|
237
|
-
"O",
|
|
238
|
-
"P",
|
|
239
|
-
"Q",
|
|
240
|
-
"R",
|
|
241
|
-
"S",
|
|
242
|
-
"T",
|
|
243
|
-
"U",
|
|
244
|
-
"V",
|
|
245
|
-
"W",
|
|
246
|
-
"X",
|
|
247
|
-
"Y",
|
|
248
|
-
"Z"
|
|
72
|
+
async (args) => {
|
|
73
|
+
try {
|
|
74
|
+
const parsedMessage = this.parser?.parse(args.fixString);
|
|
75
|
+
if (!parsedMessage || parsedMessage.length === 0) {
|
|
76
|
+
return {
|
|
77
|
+
content: [{ type: "text", text: "Error: Failed to parse FIX string" }],
|
|
78
|
+
isError: true
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
content: [
|
|
83
|
+
{
|
|
84
|
+
type: "text",
|
|
85
|
+
text: `${parsedMessage[0].description}
|
|
86
|
+
${parsedMessage[0].messageTypeDescription}`
|
|
87
|
+
}
|
|
249
88
|
]
|
|
250
|
-
}
|
|
251
|
-
|
|
89
|
+
};
|
|
90
|
+
} catch (error) {
|
|
91
|
+
return {
|
|
92
|
+
content: [
|
|
93
|
+
{
|
|
94
|
+
type: "text",
|
|
95
|
+
text: `Error: ${error instanceof Error ? error.message : "Failed to parse FIX string"}`
|
|
96
|
+
}
|
|
97
|
+
],
|
|
98
|
+
isError: true
|
|
99
|
+
};
|
|
252
100
|
}
|
|
101
|
+
}
|
|
102
|
+
);
|
|
103
|
+
this.server.tool(
|
|
104
|
+
"parseToJSON",
|
|
105
|
+
"Parses a FIX message into JSON",
|
|
106
|
+
{
|
|
107
|
+
fixString: z.string().describe("FIX message string to parse")
|
|
253
108
|
},
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
properties: {
|
|
282
|
-
symbol: {
|
|
283
|
-
type: "string",
|
|
284
|
-
description: "The trading symbol to analyze (e.g., AAPL, MSFT, EURUSD)"
|
|
109
|
+
async (args) => {
|
|
110
|
+
try {
|
|
111
|
+
const parsedMessage = this.parser?.parse(args.fixString);
|
|
112
|
+
if (!parsedMessage || parsedMessage.length === 0) {
|
|
113
|
+
return {
|
|
114
|
+
content: [{ type: "text", text: "Error: Failed to parse FIX string" }],
|
|
115
|
+
isError: true
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
content: [
|
|
120
|
+
{
|
|
121
|
+
type: "text",
|
|
122
|
+
text: `${parsedMessage[0].toFIXJSON()}`
|
|
123
|
+
}
|
|
124
|
+
]
|
|
125
|
+
};
|
|
126
|
+
} catch (error) {
|
|
127
|
+
return {
|
|
128
|
+
content: [
|
|
129
|
+
{
|
|
130
|
+
type: "text",
|
|
131
|
+
text: `Error: ${error instanceof Error ? error.message : "Failed to parse FIX string"}`
|
|
132
|
+
}
|
|
133
|
+
],
|
|
134
|
+
isError: true
|
|
135
|
+
};
|
|
285
136
|
}
|
|
137
|
+
}
|
|
138
|
+
);
|
|
139
|
+
this.server.tool(
|
|
140
|
+
"verifyOrder",
|
|
141
|
+
"Verifies all parameters for a New Order Single. This is the first step - verification only, no order is sent.",
|
|
142
|
+
{
|
|
143
|
+
clOrdID: z.string().describe("Client Order ID"),
|
|
144
|
+
handlInst: z.enum(["1", "2", "3"]).describe("Handling instruction (1=Manual, 2=Automated, 3=AutomatedNoIntervention)"),
|
|
145
|
+
quantity: z.string().describe("Order quantity"),
|
|
146
|
+
price: z.string().describe("Order price"),
|
|
147
|
+
ordType: z.string().describe("Order type (1=Market, 2=Limit, 3=Stop)"),
|
|
148
|
+
side: z.string().describe("Order side (1=Buy, 2=Sell, etc.)"),
|
|
149
|
+
symbol: z.string().describe("Trading symbol"),
|
|
150
|
+
timeInForce: z.string().describe("Time in force (0=Day, 1=Good Till Cancel, etc.)")
|
|
286
151
|
},
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
152
|
+
async (args) => {
|
|
153
|
+
try {
|
|
154
|
+
this.verifiedOrders.set(args.clOrdID, {
|
|
155
|
+
clOrdID: args.clOrdID,
|
|
156
|
+
handlInst: args.handlInst,
|
|
157
|
+
quantity: Number.parseFloat(args.quantity),
|
|
158
|
+
price: Number.parseFloat(args.price),
|
|
159
|
+
ordType: args.ordType,
|
|
160
|
+
side: args.side,
|
|
161
|
+
symbol: args.symbol,
|
|
162
|
+
timeInForce: args.timeInForce
|
|
163
|
+
});
|
|
164
|
+
return {
|
|
165
|
+
content: [
|
|
166
|
+
{
|
|
167
|
+
type: "text",
|
|
168
|
+
text: `VERIFICATION: All parameters valid. Ready to proceed with order execution.
|
|
169
|
+
|
|
170
|
+
Parameters verified:
|
|
171
|
+
- ClOrdID: ${args.clOrdID}
|
|
172
|
+
- HandlInst: ${args.handlInst}
|
|
173
|
+
- Quantity: ${args.quantity}
|
|
174
|
+
- Price: ${args.price}
|
|
175
|
+
- OrdType: ${args.ordType}
|
|
176
|
+
- Side: ${args.side}
|
|
177
|
+
- Symbol: ${args.symbol}
|
|
178
|
+
- TimeInForce: ${args.timeInForce}
|
|
291
179
|
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
// Calculate Simple Moving Average
|
|
308
|
-
calculateSMA(data, period) {
|
|
309
|
-
const sma = [];
|
|
310
|
-
for (let i = period - 1; i < data.length; i++) {
|
|
311
|
-
const sum2 = data.slice(i - period + 1, i + 1).reduce((a, b) => a + b, 0);
|
|
312
|
-
sma.push(sum2 / period);
|
|
313
|
-
}
|
|
314
|
-
return sma;
|
|
315
|
-
}
|
|
316
|
-
// Calculate Exponential Moving Average
|
|
317
|
-
calculateEMA(data, period) {
|
|
318
|
-
const multiplier = 2 / (period + 1);
|
|
319
|
-
const ema = [data[0]];
|
|
320
|
-
for (let i = 1; i < data.length; i++) {
|
|
321
|
-
ema.push(data[i] * multiplier + ema[i - 1] * (1 - multiplier));
|
|
322
|
-
}
|
|
323
|
-
return ema;
|
|
324
|
-
}
|
|
325
|
-
// Calculate RSI
|
|
326
|
-
calculateRSI(data, period = 14) {
|
|
327
|
-
if (data.length < period + 1) return [];
|
|
328
|
-
const changes = [];
|
|
329
|
-
for (let i = 1; i < data.length; i++) {
|
|
330
|
-
changes.push(data[i] - data[i - 1]);
|
|
331
|
-
}
|
|
332
|
-
const gains = changes.map((change) => change > 0 ? change : 0);
|
|
333
|
-
const losses = changes.map((change) => change < 0 ? Math.abs(change) : 0);
|
|
334
|
-
let avgGain = gains.slice(0, period).reduce((a, b) => a + b, 0) / period;
|
|
335
|
-
let avgLoss = losses.slice(0, period).reduce((a, b) => a + b, 0) / period;
|
|
336
|
-
const rsi = [];
|
|
337
|
-
for (let i = period; i < changes.length; i++) {
|
|
338
|
-
const rs = avgGain / avgLoss;
|
|
339
|
-
rsi.push(100 - 100 / (1 + rs));
|
|
340
|
-
avgGain = (avgGain * (period - 1) + gains[i]) / period;
|
|
341
|
-
avgLoss = (avgLoss * (period - 1) + losses[i]) / period;
|
|
342
|
-
}
|
|
343
|
-
return rsi;
|
|
344
|
-
}
|
|
345
|
-
// Calculate Bollinger Bands
|
|
346
|
-
calculateBollingerBands(data, period = 20, stdDev = 2) {
|
|
347
|
-
if (data.length < period) return [];
|
|
348
|
-
const sma = this.calculateSMA(data, period);
|
|
349
|
-
const bands = [];
|
|
350
|
-
for (let i = 0; i < sma.length; i++) {
|
|
351
|
-
const dataSlice = data.slice(i, i + period);
|
|
352
|
-
const mean = sma[i];
|
|
353
|
-
const variance = dataSlice.reduce((sum2, price) => sum2 + (price - mean) ** 2, 0) / period;
|
|
354
|
-
const standardDeviation = Math.sqrt(variance);
|
|
355
|
-
bands.push({
|
|
356
|
-
upper: mean + standardDeviation * stdDev,
|
|
357
|
-
middle: mean,
|
|
358
|
-
lower: mean - standardDeviation * stdDev
|
|
359
|
-
});
|
|
360
|
-
}
|
|
361
|
-
return bands;
|
|
362
|
-
}
|
|
363
|
-
// Calculate maximum drawdown
|
|
364
|
-
calculateMaxDrawdown(prices) {
|
|
365
|
-
let maxPrice = prices[0];
|
|
366
|
-
let maxDrawdown = 0;
|
|
367
|
-
for (let i = 1; i < prices.length; i++) {
|
|
368
|
-
if (prices[i] > maxPrice) {
|
|
369
|
-
maxPrice = prices[i];
|
|
370
|
-
}
|
|
371
|
-
const drawdown = (maxPrice - prices[i]) / maxPrice;
|
|
372
|
-
if (drawdown > maxDrawdown) {
|
|
373
|
-
maxDrawdown = drawdown;
|
|
180
|
+
To execute this order, call the executeOrder tool with these exact same parameters.`
|
|
181
|
+
}
|
|
182
|
+
]
|
|
183
|
+
};
|
|
184
|
+
} catch (error) {
|
|
185
|
+
return {
|
|
186
|
+
content: [
|
|
187
|
+
{
|
|
188
|
+
type: "text",
|
|
189
|
+
text: `Error: ${error instanceof Error ? error.message : "Failed to verify order parameters"}`
|
|
190
|
+
}
|
|
191
|
+
],
|
|
192
|
+
isError: true
|
|
193
|
+
};
|
|
194
|
+
}
|
|
374
195
|
}
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
const currentPrice = this.prices[this.prices.length - 1];
|
|
389
|
-
const startPrice = this.prices[0];
|
|
390
|
-
const sessionHigh = Math.max(...this.highs);
|
|
391
|
-
const sessionLow = Math.min(...this.lows);
|
|
392
|
-
const totalVolume = sum(this.volumes);
|
|
393
|
-
const avgVolume = totalVolume / this.volumes.length;
|
|
394
|
-
const priceChanges = this.calculatePriceChanges();
|
|
395
|
-
const volatility = priceChanges.length > 0 ? Math.sqrt(
|
|
396
|
-
priceChanges.reduce((sum2, change) => sum2 + change ** 2, 0) / priceChanges.length
|
|
397
|
-
) * Math.sqrt(252) * 100 : 0;
|
|
398
|
-
const sessionReturn = (currentPrice - startPrice) / startPrice * 100;
|
|
399
|
-
const pricePosition = (currentPrice - sessionLow) / (sessionHigh - sessionLow) * 100;
|
|
400
|
-
const trueVWAP = this.prices.reduce((sum2, price, i) => sum2 + price * this.volumes[i], 0) / totalVolume;
|
|
401
|
-
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;
|
|
402
|
-
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;
|
|
403
|
-
const maxDrawdown = this.calculateMaxDrawdown(this.prices);
|
|
404
|
-
return {
|
|
405
|
-
currentPrice,
|
|
406
|
-
startPrice,
|
|
407
|
-
sessionHigh,
|
|
408
|
-
sessionLow,
|
|
409
|
-
totalVolume,
|
|
410
|
-
avgVolume,
|
|
411
|
-
volatility,
|
|
412
|
-
sessionReturn,
|
|
413
|
-
pricePosition,
|
|
414
|
-
trueVWAP,
|
|
415
|
-
momentum5,
|
|
416
|
-
momentum10,
|
|
417
|
-
maxDrawdown
|
|
418
|
-
};
|
|
419
|
-
}
|
|
420
|
-
// Generate technical indicators
|
|
421
|
-
getTechnicalIndicators() {
|
|
422
|
-
return {
|
|
423
|
-
sma5: this.calculateSMA(this.prices, 5),
|
|
424
|
-
sma10: this.calculateSMA(this.prices, 10),
|
|
425
|
-
ema8: this.calculateEMA(this.prices, 8),
|
|
426
|
-
ema21: this.calculateEMA(this.prices, 21),
|
|
427
|
-
rsi: this.calculateRSI(this.prices, 14),
|
|
428
|
-
bollinger: this.calculateBollingerBands(this.prices, 20, 2)
|
|
429
|
-
};
|
|
430
|
-
}
|
|
431
|
-
// Generate trading signals
|
|
432
|
-
generateSignals() {
|
|
433
|
-
const analysis = this.analyze();
|
|
434
|
-
let bullishSignals = 0;
|
|
435
|
-
let bearishSignals = 0;
|
|
436
|
-
const signals = [];
|
|
437
|
-
if (analysis.currentPrice > analysis.trueVWAP) {
|
|
438
|
-
signals.push(
|
|
439
|
-
`\u2713 BULLISH: Price above VWAP (+${((analysis.currentPrice - analysis.trueVWAP) / analysis.trueVWAP * 100).toFixed(2)}%)`
|
|
440
|
-
);
|
|
441
|
-
bullishSignals++;
|
|
442
|
-
} else {
|
|
443
|
-
signals.push(
|
|
444
|
-
`\u2717 BEARISH: Price below VWAP (${((analysis.currentPrice - analysis.trueVWAP) / analysis.trueVWAP * 100).toFixed(2)}%)`
|
|
445
|
-
);
|
|
446
|
-
bearishSignals++;
|
|
447
|
-
}
|
|
448
|
-
if (analysis.momentum5 > 0 && analysis.momentum10 > 0) {
|
|
449
|
-
signals.push("\u2713 BULLISH: Positive momentum on both timeframes");
|
|
450
|
-
bullishSignals++;
|
|
451
|
-
} else if (analysis.momentum5 < 0 && analysis.momentum10 < 0) {
|
|
452
|
-
signals.push("\u2717 BEARISH: Negative momentum on both timeframes");
|
|
453
|
-
bearishSignals++;
|
|
454
|
-
} else {
|
|
455
|
-
signals.push("\u25D0 MIXED: Conflicting momentum signals");
|
|
456
|
-
}
|
|
457
|
-
const currentVolume = this.volumes[this.volumes.length - 1];
|
|
458
|
-
const volumeRatio = currentVolume / analysis.avgVolume;
|
|
459
|
-
if (volumeRatio > 1.2 && analysis.sessionReturn > 0) {
|
|
460
|
-
signals.push("\u2713 BULLISH: Above-average volume supporting upward move");
|
|
461
|
-
bullishSignals++;
|
|
462
|
-
} else if (volumeRatio > 1.2 && analysis.sessionReturn < 0) {
|
|
463
|
-
signals.push("\u2717 BEARISH: Above-average volume supporting downward move");
|
|
464
|
-
bearishSignals++;
|
|
465
|
-
} else {
|
|
466
|
-
signals.push("\u25D0 NEUTRAL: Volume not providing clear direction");
|
|
467
|
-
}
|
|
468
|
-
if (analysis.pricePosition > 65 && analysis.volatility > 30) {
|
|
469
|
-
signals.push("\u2717 BEARISH: High in range with elevated volatility - reversal risk");
|
|
470
|
-
bearishSignals++;
|
|
471
|
-
} else if (analysis.pricePosition < 35 && analysis.volatility > 30) {
|
|
472
|
-
signals.push("\u2713 BULLISH: Low in range with volatility - potential bounce");
|
|
473
|
-
bullishSignals++;
|
|
474
|
-
} else {
|
|
475
|
-
signals.push("\u25D0 NEUTRAL: Price position and volatility not extreme");
|
|
476
|
-
}
|
|
477
|
-
return { bullishSignals, bearishSignals, signals };
|
|
478
|
-
}
|
|
479
|
-
// Generate comprehensive JSON analysis
|
|
480
|
-
generateJSONAnalysis(symbol) {
|
|
481
|
-
const analysis = this.analyze();
|
|
482
|
-
const indicators = this.getTechnicalIndicators();
|
|
483
|
-
const signals = this.generateSignals();
|
|
484
|
-
const currentSMA5 = indicators.sma5.length > 0 ? indicators.sma5[indicators.sma5.length - 1] : null;
|
|
485
|
-
const currentSMA10 = indicators.sma10.length > 0 ? indicators.sma10[indicators.sma10.length - 1] : null;
|
|
486
|
-
const currentEMA8 = indicators.ema8[indicators.ema8.length - 1];
|
|
487
|
-
const currentEMA21 = indicators.ema21[indicators.ema21.length - 1];
|
|
488
|
-
const currentRSI = indicators.rsi.length > 0 ? indicators.rsi[indicators.rsi.length - 1] : null;
|
|
489
|
-
const currentBB = indicators.bollinger.length > 0 ? indicators.bollinger[indicators.bollinger.length - 1] : null;
|
|
490
|
-
const currentVolume = this.volumes[this.volumes.length - 1];
|
|
491
|
-
const volumeRatio = currentVolume / analysis.avgVolume;
|
|
492
|
-
const currentDrawdown = (analysis.sessionHigh - analysis.currentPrice) / analysis.sessionHigh * 100;
|
|
493
|
-
const rangeWidth = (analysis.sessionHigh - analysis.sessionLow) / analysis.sessionLow * 100;
|
|
494
|
-
const priceVsVWAP = (analysis.currentPrice - analysis.trueVWAP) / analysis.trueVWAP * 100;
|
|
495
|
-
const totalScore = signals.bullishSignals - signals.bearishSignals;
|
|
496
|
-
const overallSignal = totalScore > 0 ? "BULLISH_BIAS" : totalScore < 0 ? "BEARISH_BIAS" : "NEUTRAL";
|
|
497
|
-
const targetEntry = Math.max(analysis.sessionLow * 1.005, analysis.trueVWAP * 0.998);
|
|
498
|
-
const stopLoss = analysis.sessionLow * 0.995;
|
|
499
|
-
const profitTarget = analysis.sessionHigh * 0.995;
|
|
500
|
-
const riskRewardRatio = (profitTarget - analysis.currentPrice) / (analysis.currentPrice - stopLoss);
|
|
501
|
-
return {
|
|
502
|
-
symbol,
|
|
503
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
504
|
-
marketStructure: {
|
|
505
|
-
currentPrice: analysis.currentPrice,
|
|
506
|
-
startPrice: analysis.startPrice,
|
|
507
|
-
sessionHigh: analysis.sessionHigh,
|
|
508
|
-
sessionLow: analysis.sessionLow,
|
|
509
|
-
rangeWidth,
|
|
510
|
-
totalVolume: analysis.totalVolume,
|
|
511
|
-
sessionPerformance: analysis.sessionReturn,
|
|
512
|
-
positionInRange: analysis.pricePosition
|
|
513
|
-
},
|
|
514
|
-
volatility: {
|
|
515
|
-
impliedVolatility: analysis.volatility,
|
|
516
|
-
maxDrawdown: analysis.maxDrawdown * 100,
|
|
517
|
-
currentDrawdown
|
|
518
|
-
},
|
|
519
|
-
technicalIndicators: {
|
|
520
|
-
sma5: currentSMA5,
|
|
521
|
-
sma10: currentSMA10,
|
|
522
|
-
ema8: currentEMA8,
|
|
523
|
-
ema21: currentEMA21,
|
|
524
|
-
rsi: currentRSI,
|
|
525
|
-
bollingerBands: currentBB ? {
|
|
526
|
-
upper: currentBB.upper,
|
|
527
|
-
middle: currentBB.middle,
|
|
528
|
-
lower: currentBB.lower,
|
|
529
|
-
position: (analysis.currentPrice - currentBB.lower) / (currentBB.upper - currentBB.lower) * 100
|
|
530
|
-
} : null
|
|
531
|
-
},
|
|
532
|
-
volumeAnalysis: {
|
|
533
|
-
currentVolume,
|
|
534
|
-
averageVolume: Math.round(analysis.avgVolume),
|
|
535
|
-
volumeRatio,
|
|
536
|
-
trueVWAP: analysis.trueVWAP,
|
|
537
|
-
priceVsVWAP
|
|
538
|
-
},
|
|
539
|
-
momentum: {
|
|
540
|
-
momentum5: analysis.momentum5,
|
|
541
|
-
momentum10: analysis.momentum10,
|
|
542
|
-
sessionROC: analysis.sessionReturn
|
|
543
|
-
},
|
|
544
|
-
tradingSignals: {
|
|
545
|
-
...signals,
|
|
546
|
-
overallSignal,
|
|
547
|
-
signalScore: totalScore
|
|
196
|
+
);
|
|
197
|
+
this.server.tool(
|
|
198
|
+
"executeOrder",
|
|
199
|
+
"Executes a New Order Single after verification. This is the second step - only call after successful verification.",
|
|
200
|
+
{
|
|
201
|
+
clOrdID: z.string().describe("Client Order ID"),
|
|
202
|
+
handlInst: z.enum(["1", "2", "3"]).describe("Handling instruction (1=Manual, 2=Automated, 3=AutomatedNoIntervention)"),
|
|
203
|
+
quantity: z.string().describe("Order quantity"),
|
|
204
|
+
price: z.string().describe("Order price"),
|
|
205
|
+
ordType: z.string().describe("Order type (1=Market, 2=Limit, 3=Stop)"),
|
|
206
|
+
side: z.string().describe("Order side (1=Buy, 2=Sell, etc.)"),
|
|
207
|
+
symbol: z.string().describe("Trading symbol"),
|
|
208
|
+
timeInForce: z.string().describe("Time in force (0=Day, 1=Good Till Cancel, etc.)")
|
|
548
209
|
},
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
const priceHistory = marketDataPrices.get(symbol) || [];
|
|
563
|
-
if (priceHistory.length === 0) {
|
|
564
|
-
return {
|
|
565
|
-
content: [
|
|
566
|
-
{
|
|
567
|
-
type: "text",
|
|
568
|
-
text: `No price data available for ${symbol}. Please request market data first.`,
|
|
569
|
-
uri: "technicalAnalysis"
|
|
570
|
-
}
|
|
571
|
-
]
|
|
572
|
-
};
|
|
573
|
-
}
|
|
574
|
-
const analyzer = new TechnicalAnalyzer(priceHistory);
|
|
575
|
-
const analysis = analyzer.generateJSONAnalysis(symbol);
|
|
576
|
-
return {
|
|
577
|
-
content: [
|
|
578
|
-
{
|
|
579
|
-
type: "text",
|
|
580
|
-
text: `Technical Analysis for ${symbol}:
|
|
581
|
-
|
|
582
|
-
${JSON.stringify(analysis, null, 2)}`,
|
|
583
|
-
uri: "technicalAnalysis"
|
|
210
|
+
async (args) => {
|
|
211
|
+
try {
|
|
212
|
+
const verifiedOrder = this.verifiedOrders.get(args.clOrdID);
|
|
213
|
+
if (!verifiedOrder) {
|
|
214
|
+
return {
|
|
215
|
+
content: [
|
|
216
|
+
{
|
|
217
|
+
type: "text",
|
|
218
|
+
text: `Error: Order ${args.clOrdID} has not been verified. Please call verifyOrder first.`
|
|
219
|
+
}
|
|
220
|
+
],
|
|
221
|
+
isError: true
|
|
222
|
+
};
|
|
584
223
|
}
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
224
|
+
if (verifiedOrder.handlInst !== args.handlInst || verifiedOrder.quantity !== Number.parseFloat(args.quantity) || verifiedOrder.price !== Number.parseFloat(args.price) || verifiedOrder.ordType !== args.ordType || verifiedOrder.side !== args.side || verifiedOrder.symbol !== args.symbol || verifiedOrder.timeInForce !== args.timeInForce) {
|
|
225
|
+
return {
|
|
226
|
+
content: [
|
|
227
|
+
{
|
|
228
|
+
type: "text",
|
|
229
|
+
text: "Error: Order parameters do not match the verified order. Please use the exact same parameters that were verified."
|
|
230
|
+
}
|
|
231
|
+
],
|
|
232
|
+
isError: true
|
|
233
|
+
};
|
|
594
234
|
}
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
MDEntryType.OpeningPrice,
|
|
625
|
-
MDEntryType.ClosingPrice,
|
|
626
|
-
MDEntryType.SettlementPrice,
|
|
627
|
-
MDEntryType.TradingSessionHighPrice,
|
|
628
|
-
MDEntryType.TradingSessionLowPrice,
|
|
629
|
-
MDEntryType.VWAP,
|
|
630
|
-
MDEntryType.Imbalance,
|
|
631
|
-
MDEntryType.TradeVolume,
|
|
632
|
-
MDEntryType.OpenInterest,
|
|
633
|
-
MDEntryType.CompositeUnderlyingPrice,
|
|
634
|
-
MDEntryType.SimulatedSellPrice,
|
|
635
|
-
MDEntryType.SimulatedBuyPrice,
|
|
636
|
-
MDEntryType.MarginRate,
|
|
637
|
-
MDEntryType.MidPrice,
|
|
638
|
-
MDEntryType.EmptyBook,
|
|
639
|
-
MDEntryType.SettleHighPrice,
|
|
640
|
-
MDEntryType.SettleLowPrice,
|
|
641
|
-
MDEntryType.PriorSettlePrice,
|
|
642
|
-
MDEntryType.SessionHighBid,
|
|
643
|
-
MDEntryType.SessionLowOffer,
|
|
644
|
-
MDEntryType.EarlyPrices,
|
|
645
|
-
MDEntryType.AuctionClearingPrice,
|
|
646
|
-
MDEntryType.SwapValueFactor,
|
|
647
|
-
MDEntryType.DailyValueAdjustmentForLongPositions,
|
|
648
|
-
MDEntryType.CumulativeValueAdjustmentForLongPositions,
|
|
649
|
-
MDEntryType.DailyValueAdjustmentForShortPositions,
|
|
650
|
-
MDEntryType.CumulativeValueAdjustmentForShortPositions,
|
|
651
|
-
MDEntryType.FixingPrice,
|
|
652
|
-
MDEntryType.CashRate,
|
|
653
|
-
MDEntryType.RecoveryRate,
|
|
654
|
-
MDEntryType.RecoveryRateForLong,
|
|
655
|
-
MDEntryType.RecoveryRateForShort,
|
|
656
|
-
MDEntryType.MarketBid,
|
|
657
|
-
MDEntryType.MarketOffer,
|
|
658
|
-
MDEntryType.ShortSaleMinPrice,
|
|
659
|
-
MDEntryType.PreviousClosingPrice,
|
|
660
|
-
MDEntryType.ThresholdLimitPriceBanding,
|
|
661
|
-
MDEntryType.DailyFinancingValue,
|
|
662
|
-
MDEntryType.AccruedFinancingValue,
|
|
663
|
-
MDEntryType.TWAP
|
|
664
|
-
];
|
|
665
|
-
const messageFields = [
|
|
666
|
-
new Field(Fields.MsgType, Messages.MarketDataRequest),
|
|
667
|
-
new Field(Fields.SenderCompID, parser.sender),
|
|
668
|
-
new Field(Fields.MsgSeqNum, parser.getNextTargetMsgSeqNum()),
|
|
669
|
-
new Field(Fields.TargetCompID, parser.target),
|
|
670
|
-
new Field(Fields.SendingTime, parser.getTimestamp()),
|
|
671
|
-
new Field(Fields.MDReqID, args.mdReqID),
|
|
672
|
-
new Field(Fields.SubscriptionRequestType, args.subscriptionRequestType),
|
|
673
|
-
new Field(Fields.MarketDepth, 0),
|
|
674
|
-
new Field(Fields.MDUpdateType, args.mdUpdateType)
|
|
675
|
-
];
|
|
676
|
-
messageFields.push(new Field(Fields.NoRelatedSym, args.symbols.length));
|
|
677
|
-
args.symbols.forEach((symbol) => {
|
|
678
|
-
messageFields.push(new Field(Fields.Symbol, symbol));
|
|
679
|
-
});
|
|
680
|
-
messageFields.push(new Field(Fields.NoMDEntryTypes, entryTypes.length));
|
|
681
|
-
entryTypes.forEach((entryType) => {
|
|
682
|
-
messageFields.push(new Field(Fields.MDEntryType, entryType));
|
|
683
|
-
});
|
|
684
|
-
const mdr = parser.createMessage(...messageFields);
|
|
685
|
-
if (!parser.connected) {
|
|
686
|
-
parser.logger.log({
|
|
687
|
-
level: "error",
|
|
688
|
-
message: "Not connected. Cannot send market data request."
|
|
689
|
-
});
|
|
690
|
-
return {
|
|
691
|
-
content: [
|
|
692
|
-
{
|
|
693
|
-
type: "text",
|
|
694
|
-
text: "Error: Not connected. Ignoring message.",
|
|
695
|
-
uri: "marketDataRequest"
|
|
696
|
-
}
|
|
697
|
-
],
|
|
698
|
-
isError: true
|
|
699
|
-
};
|
|
700
|
-
}
|
|
701
|
-
parser.logger.log({
|
|
702
|
-
level: "info",
|
|
703
|
-
message: `Sending market data request message: ${JSON.stringify(mdr?.toFIXJSON())}`
|
|
704
|
-
});
|
|
705
|
-
parser.send(mdr);
|
|
706
|
-
const fixData = await response;
|
|
707
|
-
parser.logger.log({
|
|
708
|
-
level: "info",
|
|
709
|
-
message: `Received market data response for request ID: ${args.mdReqID}`
|
|
710
|
-
});
|
|
711
|
-
return {
|
|
712
|
-
content: [
|
|
713
|
-
{
|
|
714
|
-
type: "text",
|
|
715
|
-
text: `Market data for ${args.symbols.join(", ")}: ${JSON.stringify(fixData.toFIXJSON())}`,
|
|
716
|
-
uri: "marketDataRequest"
|
|
235
|
+
const response = new Promise((resolve) => {
|
|
236
|
+
this.pendingRequests.set(args.clOrdID, resolve);
|
|
237
|
+
});
|
|
238
|
+
const order = this.parser?.createMessage(
|
|
239
|
+
new Field(Fields.MsgType, Messages.NewOrderSingle),
|
|
240
|
+
new Field(Fields.MsgSeqNum, this.parser?.getNextTargetMsgSeqNum()),
|
|
241
|
+
new Field(Fields.SenderCompID, this.parser?.sender),
|
|
242
|
+
new Field(Fields.TargetCompID, this.parser?.target),
|
|
243
|
+
new Field(Fields.SendingTime, this.parser?.getTimestamp()),
|
|
244
|
+
new Field(Fields.ClOrdID, args.clOrdID),
|
|
245
|
+
new Field(Fields.Side, args.side),
|
|
246
|
+
new Field(Fields.Symbol, args.symbol),
|
|
247
|
+
new Field(Fields.OrderQty, Number.parseFloat(args.quantity)),
|
|
248
|
+
new Field(Fields.Price, Number.parseFloat(args.price)),
|
|
249
|
+
new Field(Fields.OrdType, args.ordType),
|
|
250
|
+
new Field(Fields.HandlInst, args.handlInst),
|
|
251
|
+
new Field(Fields.TimeInForce, args.timeInForce),
|
|
252
|
+
new Field(Fields.TransactTime, this.parser?.getTimestamp())
|
|
253
|
+
);
|
|
254
|
+
if (!this.parser?.connected) {
|
|
255
|
+
return {
|
|
256
|
+
content: [
|
|
257
|
+
{
|
|
258
|
+
type: "text",
|
|
259
|
+
text: "Error: Not connected. Ignoring message."
|
|
260
|
+
}
|
|
261
|
+
],
|
|
262
|
+
isError: true
|
|
263
|
+
};
|
|
717
264
|
}
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
265
|
+
this.parser?.send(order);
|
|
266
|
+
const fixData = await response;
|
|
267
|
+
this.verifiedOrders.delete(args.clOrdID);
|
|
268
|
+
return {
|
|
269
|
+
content: [
|
|
270
|
+
{
|
|
271
|
+
type: "text",
|
|
272
|
+
text: fixData.messageType === Messages.Reject ? `Reject message for order ${args.clOrdID}: ${JSON.stringify(fixData.toFIXJSON())}` : `Execution Report for order ${args.clOrdID}: ${JSON.stringify(fixData.toFIXJSON())}`
|
|
273
|
+
}
|
|
274
|
+
]
|
|
275
|
+
};
|
|
276
|
+
} catch (error) {
|
|
277
|
+
return {
|
|
278
|
+
content: [
|
|
279
|
+
{
|
|
280
|
+
type: "text",
|
|
281
|
+
text: `Error: ${error instanceof Error ? error.message : "Failed to execute order"}`
|
|
282
|
+
}
|
|
283
|
+
],
|
|
284
|
+
isError: true
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
);
|
|
289
|
+
this.server.tool(
|
|
290
|
+
"marketDataRequest",
|
|
291
|
+
"Sends a request for Market Data with the given symbol. IMPORTANT: All parameters must be explicitly provided by the user - no assumptions will be made.",
|
|
292
|
+
{
|
|
293
|
+
mdUpdateType: z.enum(["0", "1"]).describe("Market data update type (0=FullRefresh, 1=IncrementalRefresh)"),
|
|
294
|
+
symbol: z.string().describe("Trading symbol"),
|
|
295
|
+
mdReqID: z.string().describe("Market data request ID"),
|
|
296
|
+
subscriptionRequestType: z.enum(["0", "1", "2"]).describe("Subscription request type (0=Snapshot + Updates, 1=Snapshot, 2=Unsubscribe)"),
|
|
297
|
+
mdEntryType: z.string().describe("Market data entry type (0=Bid, 1=Offer, 2=Trade, etc.)")
|
|
298
|
+
},
|
|
299
|
+
async (args) => {
|
|
300
|
+
try {
|
|
301
|
+
const response = new Promise((resolve) => {
|
|
302
|
+
this.pendingRequests.set(args.mdReqID, resolve);
|
|
303
|
+
});
|
|
304
|
+
const marketDataRequest = this.parser?.createMessage(
|
|
305
|
+
new Field(Fields.MsgType, Messages.MarketDataRequest),
|
|
306
|
+
new Field(Fields.SenderCompID, this.parser?.sender),
|
|
307
|
+
new Field(Fields.MsgSeqNum, this.parser?.getNextTargetMsgSeqNum()),
|
|
308
|
+
new Field(Fields.TargetCompID, this.parser?.target),
|
|
309
|
+
new Field(Fields.SendingTime, this.parser?.getTimestamp()),
|
|
310
|
+
new Field(Fields.MarketDepth, 0),
|
|
311
|
+
new Field(Fields.MDUpdateType, args.mdUpdateType),
|
|
312
|
+
new Field(Fields.NoRelatedSym, 1),
|
|
313
|
+
new Field(Fields.Symbol, args.symbol),
|
|
314
|
+
new Field(Fields.MDReqID, args.mdReqID),
|
|
315
|
+
new Field(Fields.SubscriptionRequestType, args.subscriptionRequestType),
|
|
316
|
+
new Field(Fields.NoMDEntryTypes, 1),
|
|
317
|
+
new Field(Fields.MDEntryType, args.mdEntryType)
|
|
318
|
+
);
|
|
319
|
+
if (!this.parser?.connected) {
|
|
320
|
+
return {
|
|
321
|
+
content: [
|
|
322
|
+
{
|
|
323
|
+
type: "text",
|
|
324
|
+
text: "Error: Not connected. Ignoring message."
|
|
325
|
+
}
|
|
326
|
+
],
|
|
327
|
+
isError: true
|
|
328
|
+
};
|
|
727
329
|
}
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
330
|
+
this.parser?.send(marketDataRequest);
|
|
331
|
+
const fixData = await response;
|
|
332
|
+
return {
|
|
333
|
+
content: [
|
|
334
|
+
{
|
|
335
|
+
type: "text",
|
|
336
|
+
text: `Market data for ${args.symbol}: ${JSON.stringify(fixData.toFIXJSON())}`
|
|
337
|
+
}
|
|
338
|
+
]
|
|
339
|
+
};
|
|
340
|
+
} catch (error) {
|
|
341
|
+
return {
|
|
342
|
+
content: [
|
|
343
|
+
{
|
|
344
|
+
type: "text",
|
|
345
|
+
text: `Error: ${error instanceof Error ? error.message : "Failed to request market data"}`
|
|
346
|
+
}
|
|
347
|
+
],
|
|
348
|
+
isError: true
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
);
|
|
353
|
+
this.server.prompt(
|
|
354
|
+
"parse",
|
|
355
|
+
"Parses a FIX message and describes it in plain language",
|
|
356
|
+
{
|
|
357
|
+
fixString: z.string().describe("FIX message string to parse")
|
|
358
|
+
},
|
|
359
|
+
async (args) => {
|
|
740
360
|
return {
|
|
741
|
-
|
|
361
|
+
messages: [
|
|
742
362
|
{
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
363
|
+
role: "user",
|
|
364
|
+
content: {
|
|
365
|
+
type: "text",
|
|
366
|
+
text: `Please parse and explain this FIX message: ${args.fixString}`
|
|
367
|
+
}
|
|
746
368
|
}
|
|
747
369
|
]
|
|
748
370
|
};
|
|
749
371
|
}
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
const volumeData = priceHistory.map((point) => point.volume);
|
|
759
|
-
const tradeData = priceHistory.map((point) => point.trade);
|
|
760
|
-
const vwapData = priceHistory.map((point) => point.vwap);
|
|
761
|
-
const twapData = priceHistory.map((point) => point.twap);
|
|
762
|
-
const config = {
|
|
763
|
-
type: "line",
|
|
764
|
-
data: {
|
|
765
|
-
labels,
|
|
766
|
-
datasets: [
|
|
767
|
-
{
|
|
768
|
-
label: "Bid",
|
|
769
|
-
data: bidData,
|
|
770
|
-
borderColor: "#28a745",
|
|
771
|
-
backgroundColor: "rgba(40, 167, 69, 0.1)",
|
|
772
|
-
fill: false,
|
|
773
|
-
tension: 0.4
|
|
774
|
-
},
|
|
775
|
-
{
|
|
776
|
-
label: "Offer",
|
|
777
|
-
data: offerData,
|
|
778
|
-
borderColor: "#dc3545",
|
|
779
|
-
backgroundColor: "rgba(220, 53, 69, 0.1)",
|
|
780
|
-
fill: false,
|
|
781
|
-
tension: 0.4
|
|
782
|
-
},
|
|
783
|
-
{
|
|
784
|
-
label: "Spread",
|
|
785
|
-
data: spreadData,
|
|
786
|
-
borderColor: "#6c757d",
|
|
787
|
-
backgroundColor: "rgba(108, 117, 125, 0.1)",
|
|
788
|
-
fill: false,
|
|
789
|
-
tension: 0.4
|
|
790
|
-
},
|
|
791
|
-
{
|
|
792
|
-
label: "Trade",
|
|
793
|
-
data: tradeData,
|
|
794
|
-
borderColor: "#ffc107",
|
|
795
|
-
backgroundColor: "rgba(255, 193, 7, 0.1)",
|
|
796
|
-
fill: false,
|
|
797
|
-
tension: 0.4
|
|
798
|
-
},
|
|
799
|
-
{
|
|
800
|
-
label: "VWAP",
|
|
801
|
-
data: vwapData,
|
|
802
|
-
borderColor: "#17a2b8",
|
|
803
|
-
backgroundColor: "rgba(23, 162, 184, 0.1)",
|
|
804
|
-
fill: false,
|
|
805
|
-
tension: 0.4
|
|
806
|
-
},
|
|
807
|
-
{
|
|
808
|
-
label: "TWAP",
|
|
809
|
-
data: twapData,
|
|
810
|
-
borderColor: "#6610f2",
|
|
811
|
-
backgroundColor: "rgba(102, 16, 242, 0.1)",
|
|
812
|
-
fill: false,
|
|
813
|
-
tension: 0.4
|
|
814
|
-
},
|
|
815
|
-
{
|
|
816
|
-
label: "Volume",
|
|
817
|
-
data: volumeData,
|
|
818
|
-
borderColor: "#007bff",
|
|
819
|
-
backgroundColor: "rgba(0, 123, 255, 0.1)",
|
|
820
|
-
fill: true,
|
|
821
|
-
tension: 0.4
|
|
822
|
-
}
|
|
823
|
-
]
|
|
824
|
-
},
|
|
825
|
-
options: {
|
|
826
|
-
responsive: true,
|
|
827
|
-
plugins: {
|
|
828
|
-
title: {
|
|
829
|
-
display: true,
|
|
830
|
-
text: `${symbol} Market Data`
|
|
831
|
-
}
|
|
832
|
-
},
|
|
833
|
-
scales: {
|
|
834
|
-
y: {
|
|
835
|
-
beginAtZero: false
|
|
836
|
-
}
|
|
837
|
-
}
|
|
838
|
-
}
|
|
839
|
-
};
|
|
840
|
-
chart.setConfig(config);
|
|
841
|
-
const imageBuffer = await chart.toBinary();
|
|
842
|
-
const base64 = imageBuffer.toString("base64");
|
|
843
|
-
return {
|
|
844
|
-
content: [
|
|
845
|
-
{
|
|
846
|
-
type: "resource",
|
|
847
|
-
resource: {
|
|
848
|
-
uri: "resource://graph",
|
|
849
|
-
mimeType: "image/png",
|
|
850
|
-
blob: base64
|
|
851
|
-
}
|
|
852
|
-
}
|
|
853
|
-
]
|
|
854
|
-
};
|
|
855
|
-
} catch (error) {
|
|
856
|
-
return {
|
|
857
|
-
content: [
|
|
858
|
-
{
|
|
859
|
-
type: "text",
|
|
860
|
-
text: `Error: ${error instanceof Error ? error.message : "Failed to generate graph"}`,
|
|
861
|
-
uri: "getStockGraph"
|
|
862
|
-
}
|
|
863
|
-
],
|
|
864
|
-
isError: true
|
|
865
|
-
};
|
|
866
|
-
}
|
|
867
|
-
};
|
|
868
|
-
};
|
|
869
|
-
var createGetStockPriceHistoryHandler = (marketDataPrices) => {
|
|
870
|
-
return async (args) => {
|
|
871
|
-
try {
|
|
872
|
-
const symbol = args.symbol;
|
|
873
|
-
const priceHistory = marketDataPrices.get(symbol) || [];
|
|
874
|
-
if (priceHistory.length === 0) {
|
|
372
|
+
);
|
|
373
|
+
this.server.prompt(
|
|
374
|
+
"parseToJSON",
|
|
375
|
+
"Parses a FIX message into JSON",
|
|
376
|
+
{
|
|
377
|
+
fixString: z.string().describe("FIX message string to parse")
|
|
378
|
+
},
|
|
379
|
+
async (args) => {
|
|
875
380
|
return {
|
|
876
|
-
|
|
381
|
+
messages: [
|
|
877
382
|
{
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
383
|
+
role: "user",
|
|
384
|
+
content: {
|
|
385
|
+
type: "text",
|
|
386
|
+
text: `Please parse the FIX message to JSON: ${args.fixString}`
|
|
387
|
+
}
|
|
881
388
|
}
|
|
882
389
|
]
|
|
883
390
|
};
|
|
884
391
|
}
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
indexValue: point.indexValue,
|
|
901
|
-
openingPrice: point.openingPrice,
|
|
902
|
-
closingPrice: point.closingPrice,
|
|
903
|
-
settlementPrice: point.settlementPrice,
|
|
904
|
-
tradingSessionHighPrice: point.tradingSessionHighPrice,
|
|
905
|
-
tradingSessionLowPrice: point.tradingSessionLowPrice,
|
|
906
|
-
vwap: point.vwap,
|
|
907
|
-
imbalance: point.imbalance,
|
|
908
|
-
openInterest: point.openInterest,
|
|
909
|
-
compositeUnderlyingPrice: point.compositeUnderlyingPrice,
|
|
910
|
-
simulatedSellPrice: point.simulatedSellPrice,
|
|
911
|
-
simulatedBuyPrice: point.simulatedBuyPrice,
|
|
912
|
-
marginRate: point.marginRate,
|
|
913
|
-
midPrice: point.midPrice,
|
|
914
|
-
emptyBook: point.emptyBook,
|
|
915
|
-
settleHighPrice: point.settleHighPrice,
|
|
916
|
-
settleLowPrice: point.settleLowPrice,
|
|
917
|
-
priorSettlePrice: point.priorSettlePrice,
|
|
918
|
-
sessionHighBid: point.sessionHighBid,
|
|
919
|
-
sessionLowOffer: point.sessionLowOffer,
|
|
920
|
-
earlyPrices: point.earlyPrices,
|
|
921
|
-
auctionClearingPrice: point.auctionClearingPrice,
|
|
922
|
-
swapValueFactor: point.swapValueFactor,
|
|
923
|
-
dailyValueAdjustmentForLongPositions: point.dailyValueAdjustmentForLongPositions,
|
|
924
|
-
cumulativeValueAdjustmentForLongPositions: point.cumulativeValueAdjustmentForLongPositions,
|
|
925
|
-
dailyValueAdjustmentForShortPositions: point.dailyValueAdjustmentForShortPositions,
|
|
926
|
-
cumulativeValueAdjustmentForShortPositions: point.cumulativeValueAdjustmentForShortPositions,
|
|
927
|
-
fixingPrice: point.fixingPrice,
|
|
928
|
-
cashRate: point.cashRate,
|
|
929
|
-
recoveryRate: point.recoveryRate,
|
|
930
|
-
recoveryRateForLong: point.recoveryRateForLong,
|
|
931
|
-
recoveryRateForShort: point.recoveryRateForShort,
|
|
932
|
-
marketBid: point.marketBid,
|
|
933
|
-
marketOffer: point.marketOffer,
|
|
934
|
-
shortSaleMinPrice: point.shortSaleMinPrice,
|
|
935
|
-
previousClosingPrice: point.previousClosingPrice,
|
|
936
|
-
thresholdLimitPriceBanding: point.thresholdLimitPriceBanding,
|
|
937
|
-
dailyFinancingValue: point.dailyFinancingValue,
|
|
938
|
-
accruedFinancingValue: point.accruedFinancingValue,
|
|
939
|
-
twap: point.twap
|
|
940
|
-
}))
|
|
941
|
-
},
|
|
942
|
-
null,
|
|
943
|
-
2
|
|
944
|
-
),
|
|
945
|
-
uri: "getStockPriceHistory"
|
|
946
|
-
}
|
|
947
|
-
]
|
|
948
|
-
};
|
|
949
|
-
} catch (error) {
|
|
950
|
-
return {
|
|
951
|
-
content: [
|
|
952
|
-
{
|
|
953
|
-
type: "text",
|
|
954
|
-
text: `Error: ${error instanceof Error ? error.message : "Failed to get price history"}`,
|
|
955
|
-
uri: "getStockPriceHistory"
|
|
956
|
-
}
|
|
957
|
-
],
|
|
958
|
-
isError: true
|
|
959
|
-
};
|
|
960
|
-
}
|
|
961
|
-
};
|
|
962
|
-
};
|
|
963
|
-
|
|
964
|
-
// src/tools/order.ts
|
|
965
|
-
import { Field as Field2, Fields as Fields2, Messages as Messages2 } from "fixparser";
|
|
966
|
-
var ordTypeNames = {
|
|
967
|
-
"1": "Market",
|
|
968
|
-
"2": "Limit",
|
|
969
|
-
"3": "Stop",
|
|
970
|
-
"4": "StopLimit",
|
|
971
|
-
"5": "MarketOnClose",
|
|
972
|
-
"6": "WithOrWithout",
|
|
973
|
-
"7": "LimitOrBetter",
|
|
974
|
-
"8": "LimitWithOrWithout",
|
|
975
|
-
"9": "OnBasis",
|
|
976
|
-
A: "OnClose",
|
|
977
|
-
B: "LimitOnClose",
|
|
978
|
-
C: "ForexMarket",
|
|
979
|
-
D: "PreviouslyQuoted",
|
|
980
|
-
E: "PreviouslyIndicated",
|
|
981
|
-
F: "ForexLimit",
|
|
982
|
-
G: "ForexSwap",
|
|
983
|
-
H: "ForexPreviouslyQuoted",
|
|
984
|
-
I: "Funari",
|
|
985
|
-
J: "MarketIfTouched",
|
|
986
|
-
K: "MarketWithLeftOverAsLimit",
|
|
987
|
-
L: "PreviousFundValuationPoint",
|
|
988
|
-
M: "NextFundValuationPoint",
|
|
989
|
-
P: "Pegged",
|
|
990
|
-
Q: "CounterOrderSelection",
|
|
991
|
-
R: "StopOnBidOrOffer",
|
|
992
|
-
S: "StopLimitOnBidOrOffer"
|
|
993
|
-
};
|
|
994
|
-
var sideNames = {
|
|
995
|
-
"1": "Buy",
|
|
996
|
-
"2": "Sell",
|
|
997
|
-
"3": "BuyMinus",
|
|
998
|
-
"4": "SellPlus",
|
|
999
|
-
"5": "SellShort",
|
|
1000
|
-
"6": "SellShortExempt",
|
|
1001
|
-
"7": "Undisclosed",
|
|
1002
|
-
"8": "Cross",
|
|
1003
|
-
"9": "CrossShort",
|
|
1004
|
-
A: "CrossShortExempt",
|
|
1005
|
-
B: "AsDefined",
|
|
1006
|
-
C: "Opposite",
|
|
1007
|
-
D: "Subscribe",
|
|
1008
|
-
E: "Redeem",
|
|
1009
|
-
F: "Lend",
|
|
1010
|
-
G: "Borrow",
|
|
1011
|
-
H: "SellUndisclosed"
|
|
1012
|
-
};
|
|
1013
|
-
var timeInForceNames = {
|
|
1014
|
-
"0": "Day",
|
|
1015
|
-
"1": "GoodTillCancel",
|
|
1016
|
-
"2": "AtTheOpening",
|
|
1017
|
-
"3": "ImmediateOrCancel",
|
|
1018
|
-
"4": "FillOrKill",
|
|
1019
|
-
"5": "GoodTillCrossing",
|
|
1020
|
-
"6": "GoodTillDate",
|
|
1021
|
-
"7": "AtTheClose",
|
|
1022
|
-
"8": "GoodThroughCrossing",
|
|
1023
|
-
"9": "AtCrossing",
|
|
1024
|
-
A: "GoodForTime",
|
|
1025
|
-
B: "GoodForAuction",
|
|
1026
|
-
C: "GoodForMonth"
|
|
1027
|
-
};
|
|
1028
|
-
var handlInstNames = {
|
|
1029
|
-
"1": "AutomatedExecutionNoIntervention",
|
|
1030
|
-
"2": "AutomatedExecutionInterventionOK",
|
|
1031
|
-
"3": "ManualOrder"
|
|
1032
|
-
};
|
|
1033
|
-
var createVerifyOrderHandler = (parser, verifiedOrders) => {
|
|
1034
|
-
return async (args) => {
|
|
1035
|
-
try {
|
|
1036
|
-
verifiedOrders.set(args.clOrdID, {
|
|
1037
|
-
clOrdID: args.clOrdID,
|
|
1038
|
-
handlInst: args.handlInst,
|
|
1039
|
-
quantity: Number.parseFloat(String(args.quantity)),
|
|
1040
|
-
price: Number.parseFloat(String(args.price)),
|
|
1041
|
-
ordType: args.ordType,
|
|
1042
|
-
side: args.side,
|
|
1043
|
-
symbol: args.symbol,
|
|
1044
|
-
timeInForce: args.timeInForce
|
|
1045
|
-
});
|
|
1046
|
-
return {
|
|
1047
|
-
content: [
|
|
1048
|
-
{
|
|
1049
|
-
type: "text",
|
|
1050
|
-
text: `VERIFICATION: All parameters valid. Ready to proceed with order execution.
|
|
1051
|
-
|
|
1052
|
-
Parameters verified:
|
|
1053
|
-
- ClOrdID: ${args.clOrdID}
|
|
1054
|
-
- HandlInst: ${args.handlInst} (${handlInstNames[args.handlInst]})
|
|
1055
|
-
- Quantity: ${args.quantity}
|
|
1056
|
-
- Price: ${args.price}
|
|
1057
|
-
- OrdType: ${args.ordType} (${ordTypeNames[args.ordType]})
|
|
1058
|
-
- Side: ${args.side} (${sideNames[args.side]})
|
|
1059
|
-
- Symbol: ${args.symbol}
|
|
1060
|
-
- TimeInForce: ${args.timeInForce} (${timeInForceNames[args.timeInForce]})
|
|
1061
|
-
|
|
1062
|
-
To execute this order, call the executeOrder tool with these exact same parameters. Important: The user has to explicitly confirm before executeOrder is called!`,
|
|
1063
|
-
uri: "verifyOrder"
|
|
1064
|
-
}
|
|
1065
|
-
]
|
|
1066
|
-
};
|
|
1067
|
-
} catch (error) {
|
|
1068
|
-
return {
|
|
1069
|
-
content: [
|
|
1070
|
-
{
|
|
1071
|
-
type: "text",
|
|
1072
|
-
text: `Error: ${error instanceof Error ? error.message : "Failed to verify order parameters"}`,
|
|
1073
|
-
uri: "verifyOrder"
|
|
1074
|
-
}
|
|
1075
|
-
],
|
|
1076
|
-
isError: true
|
|
1077
|
-
};
|
|
1078
|
-
}
|
|
1079
|
-
};
|
|
1080
|
-
};
|
|
1081
|
-
var createExecuteOrderHandler = (parser, verifiedOrders, pendingRequests) => {
|
|
1082
|
-
return async (args) => {
|
|
1083
|
-
try {
|
|
1084
|
-
const verifiedOrder = verifiedOrders.get(args.clOrdID);
|
|
1085
|
-
if (!verifiedOrder) {
|
|
392
|
+
);
|
|
393
|
+
this.server.prompt(
|
|
394
|
+
"verifyOrder",
|
|
395
|
+
"Verifies all parameters for a New Order Single. This is the first step - verification only, no order is sent.",
|
|
396
|
+
{
|
|
397
|
+
clOrdID: z.string().describe("Client Order ID"),
|
|
398
|
+
handlInst: z.enum(["1", "2", "3"]).describe("Handling instruction (1=Manual, 2=Automated, 3=AutomatedNoIntervention)"),
|
|
399
|
+
quantity: z.string().describe("Order quantity"),
|
|
400
|
+
price: z.string().describe("Order price"),
|
|
401
|
+
ordType: z.string().describe("Order type (1=Market, 2=Limit, 3=Stop)"),
|
|
402
|
+
side: z.string().describe("Order side (1=Buy, 2=Sell, etc.)"),
|
|
403
|
+
symbol: z.string().describe("Trading symbol"),
|
|
404
|
+
timeInForce: z.string().describe("Time in force (0=Day, 1=Good Till Cancel, etc.)")
|
|
405
|
+
},
|
|
406
|
+
async (args) => {
|
|
1086
407
|
return {
|
|
1087
|
-
|
|
408
|
+
messages: [
|
|
1088
409
|
{
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
410
|
+
role: "user",
|
|
411
|
+
content: {
|
|
412
|
+
type: "text",
|
|
413
|
+
text: [
|
|
414
|
+
"You are an AI assistant that helps users verify FIX New Order Single parameters.",
|
|
415
|
+
"This is STEP 1 of 2 - VERIFICATION ONLY. No order will be sent at this stage.",
|
|
416
|
+
"",
|
|
417
|
+
"You must verify that all required fields are provided and valid:",
|
|
418
|
+
"- ClOrdID (Client Order ID)",
|
|
419
|
+
"- HandlInst (1=Manual, 2=Automated, 3=AutomatedNoIntervention)",
|
|
420
|
+
"- Quantity (Order quantity)",
|
|
421
|
+
"- Price (Order price)",
|
|
422
|
+
"- OrdType (1=Market, 2=Limit, 3=Stop)",
|
|
423
|
+
"- Side (1=Buy, 2=Sell, 3=BuyMinus, 4=SellPlus, 5=SellShort, 6=SellShortExempt, 7=Undisclosed, 8=Cross, 9=CrossShort, A=CrossShortExempt, B=AsDefined, C=Opposite, D=Subscribe, E=Redeem, F=Lend, G=Borrow, H=SellUndisclosed)",
|
|
424
|
+
"- Symbol (Trading symbol)",
|
|
425
|
+
"- TimeInForce (0=Day, 1=Good Till Cancel, 2=At Opening, 3=Immediate or Cancel, 4=Fill or Kill, 5=Good Till Crossing, 6=Good Till Date)",
|
|
426
|
+
"",
|
|
427
|
+
"Current parameters to verify:",
|
|
428
|
+
`- ClOrdID: ${args.clOrdID}`,
|
|
429
|
+
`- HandlInst: ${args.handlInst}`,
|
|
430
|
+
`- Quantity: ${args.quantity}`,
|
|
431
|
+
`- Price: ${args.price}`,
|
|
432
|
+
`- OrdType: ${args.ordType}`,
|
|
433
|
+
`- Side: ${args.side}`,
|
|
434
|
+
`- Symbol: ${args.symbol}`,
|
|
435
|
+
`- TimeInForce: ${args.timeInForce}`,
|
|
436
|
+
"",
|
|
437
|
+
"If all parameters are valid, respond with:",
|
|
438
|
+
"`VERIFICATION: All parameters valid. Ready to proceed.`",
|
|
439
|
+
"",
|
|
440
|
+
"Otherwise, list the missing or invalid ones.",
|
|
441
|
+
"",
|
|
442
|
+
"IMPORTANT: This is only verification. To actually send the order, the user must call the executeOrder tool with the same parameters."
|
|
443
|
+
].join("\n")
|
|
444
|
+
}
|
|
1092
445
|
}
|
|
1093
|
-
]
|
|
1094
|
-
isError: true
|
|
446
|
+
]
|
|
1095
447
|
};
|
|
1096
448
|
}
|
|
1097
|
-
|
|
449
|
+
);
|
|
450
|
+
this.server.prompt(
|
|
451
|
+
"executeOrder",
|
|
452
|
+
"Executes a New Order Single after verification. This is the second step - only call after successful verification.",
|
|
453
|
+
{
|
|
454
|
+
clOrdID: z.string().describe("Client Order ID"),
|
|
455
|
+
handlInst: z.enum(["1", "2", "3"]).describe("Handling instruction (1=Manual, 2=Automated, 3=AutomatedNoIntervention)"),
|
|
456
|
+
quantity: z.string().describe("Order quantity"),
|
|
457
|
+
price: z.string().describe("Order price"),
|
|
458
|
+
ordType: z.string().describe("Order type (1=Market, 2=Limit, 3=Stop)"),
|
|
459
|
+
side: z.string().describe("Order side (1=Buy, 2=Sell, etc.)"),
|
|
460
|
+
symbol: z.string().describe("Trading symbol"),
|
|
461
|
+
timeInForce: z.string().describe("Time in force (0=Day, 1=Good Till Cancel, etc.)")
|
|
462
|
+
},
|
|
463
|
+
async (args) => {
|
|
1098
464
|
return {
|
|
1099
|
-
|
|
465
|
+
messages: [
|
|
1100
466
|
{
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
467
|
+
role: "user",
|
|
468
|
+
content: {
|
|
469
|
+
type: "text",
|
|
470
|
+
text: [
|
|
471
|
+
"You are an AI assistant that helps users execute FIX New Order Single messages.",
|
|
472
|
+
"This is STEP 2 of 2 - EXECUTION. This will send the order to the market.",
|
|
473
|
+
"",
|
|
474
|
+
"IMPORTANT: This tool should only be called after successful verification of all parameters.",
|
|
475
|
+
"",
|
|
476
|
+
"Parameters to execute:",
|
|
477
|
+
`- ClOrdID: ${args.clOrdID}`,
|
|
478
|
+
`- HandlInst: ${args.handlInst}`,
|
|
479
|
+
`- Quantity: ${args.quantity}`,
|
|
480
|
+
`- Price: ${args.price}`,
|
|
481
|
+
`- OrdType: ${args.ordType}`,
|
|
482
|
+
`- Side: ${args.side}`,
|
|
483
|
+
`- Symbol: ${args.symbol}`,
|
|
484
|
+
`- TimeInForce: ${args.timeInForce}`,
|
|
485
|
+
"",
|
|
486
|
+
"IMPORTANT: The response will be either:",
|
|
487
|
+
"1. An Execution Report (MsgType=8) if the order was successfully placed",
|
|
488
|
+
"2. A Reject message (MsgType=3) if the order failed to execute (e.g., due to missing or invalid parameters)"
|
|
489
|
+
].join("\n")
|
|
490
|
+
}
|
|
1104
491
|
}
|
|
1105
|
-
]
|
|
1106
|
-
isError: true
|
|
492
|
+
]
|
|
1107
493
|
};
|
|
1108
494
|
}
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
new Field2(Fields2.OrderQty, Number.parseFloat(String(args.quantity))),
|
|
1122
|
-
new Field2(Fields2.Price, Number.parseFloat(String(args.price))),
|
|
1123
|
-
new Field2(Fields2.OrdType, args.ordType),
|
|
1124
|
-
new Field2(Fields2.HandlInst, args.handlInst),
|
|
1125
|
-
new Field2(Fields2.TimeInForce, args.timeInForce),
|
|
1126
|
-
new Field2(Fields2.TransactTime, parser.getTimestamp())
|
|
1127
|
-
);
|
|
1128
|
-
if (!parser.connected) {
|
|
495
|
+
);
|
|
496
|
+
this.server.prompt(
|
|
497
|
+
"marketDataRequest",
|
|
498
|
+
"Sends a request for Market Data with the given symbol. IMPORTANT: All parameters must be explicitly provided by the user - no assumptions will be made.",
|
|
499
|
+
{
|
|
500
|
+
mdUpdateType: z.enum(["0", "1"]).describe("Market data update type (0=FullRefresh, 1=IncrementalRefresh)"),
|
|
501
|
+
symbol: z.string().describe("Trading symbol"),
|
|
502
|
+
mdReqID: z.string().describe("Market data request ID"),
|
|
503
|
+
subscriptionRequestType: z.enum(["0", "1", "2"]).describe("Subscription request type (0=Snapshot + Updates, 1=Snapshot, 2=Unsubscribe)"),
|
|
504
|
+
mdEntryType: z.string().describe("Market data entry type (0=Bid, 1=Offer, 2=Trade, etc.)")
|
|
505
|
+
},
|
|
506
|
+
async (args) => {
|
|
1129
507
|
return {
|
|
1130
|
-
|
|
508
|
+
messages: [
|
|
1131
509
|
{
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
510
|
+
role: "user",
|
|
511
|
+
content: {
|
|
512
|
+
type: "text",
|
|
513
|
+
text: [
|
|
514
|
+
"You are an AI assistant that helps users create FIX Market Data Request messages.",
|
|
515
|
+
"You must **first verify** that all required fields are provided:",
|
|
516
|
+
"- MDUpdateType (0=FullRefresh, 1=IncrementalRefresh)",
|
|
517
|
+
"- Symbol (Trading symbol)",
|
|
518
|
+
"- MDReqID (Market data request ID)",
|
|
519
|
+
"- SubscriptionRequestType (0=Snapshot + Updates, 1=Snapshot, 2=Unsubscribe)",
|
|
520
|
+
"- MDEntryType (0=Bid, 1=Offer, 2=Trade, 3=Index Value, 4=Opening Price)",
|
|
521
|
+
"",
|
|
522
|
+
"Only when all fields are present and valid, respond with:",
|
|
523
|
+
"`VERIFICATION: All parameters valid. Ready to proceed.`",
|
|
524
|
+
"",
|
|
525
|
+
"Otherwise, list the missing or invalid ones.",
|
|
526
|
+
"",
|
|
527
|
+
"Do not create the FIX message until verification is complete.",
|
|
528
|
+
"",
|
|
529
|
+
"Current parameters:",
|
|
530
|
+
`- MDUpdateType: ${args.mdUpdateType}`,
|
|
531
|
+
`- Symbol: ${args.symbol}`,
|
|
532
|
+
`- MDReqID: ${args.mdReqID}`,
|
|
533
|
+
`- SubscriptionRequestType: ${args.subscriptionRequestType}`,
|
|
534
|
+
`- MDEntryType: ${args.mdEntryType}`
|
|
535
|
+
].join("\n")
|
|
536
|
+
}
|
|
1135
537
|
}
|
|
1136
|
-
]
|
|
1137
|
-
isError: true
|
|
538
|
+
]
|
|
1138
539
|
};
|
|
1139
540
|
}
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
type: "text",
|
|
1147
|
-
text: fixData.messageType === Messages2.Reject ? `Reject message for order ${args.clOrdID}: ${JSON.stringify(fixData.toFIXJSON())}` : `Execution Report for order ${args.clOrdID}: ${JSON.stringify(fixData.toFIXJSON())}`,
|
|
1148
|
-
uri: "executeOrder"
|
|
1149
|
-
}
|
|
1150
|
-
]
|
|
1151
|
-
};
|
|
1152
|
-
} catch (error) {
|
|
1153
|
-
return {
|
|
1154
|
-
content: [
|
|
1155
|
-
{
|
|
1156
|
-
type: "text",
|
|
1157
|
-
text: `Error: ${error instanceof Error ? error.message : "Failed to execute order"}`,
|
|
1158
|
-
uri: "executeOrder"
|
|
1159
|
-
}
|
|
1160
|
-
],
|
|
1161
|
-
isError: true
|
|
1162
|
-
};
|
|
1163
|
-
}
|
|
1164
|
-
};
|
|
1165
|
-
};
|
|
1166
|
-
|
|
1167
|
-
// src/tools/parse.ts
|
|
1168
|
-
var createParseHandler = (parser) => {
|
|
1169
|
-
return async (args) => {
|
|
1170
|
-
try {
|
|
1171
|
-
const parsedMessage = parser.parse(args.fixString);
|
|
1172
|
-
if (!parsedMessage || parsedMessage.length === 0) {
|
|
541
|
+
);
|
|
542
|
+
this.server.resource(
|
|
543
|
+
"greeting-resource",
|
|
544
|
+
"https://example.com/greetings/default",
|
|
545
|
+
{ mimeType: "text/plain" },
|
|
546
|
+
async () => {
|
|
1173
547
|
return {
|
|
1174
|
-
|
|
548
|
+
contents: [
|
|
1175
549
|
{
|
|
1176
|
-
|
|
1177
|
-
text: "
|
|
1178
|
-
uri: "parse"
|
|
550
|
+
uri: "https://example.com/greetings/default",
|
|
551
|
+
text: "Hello, world!"
|
|
1179
552
|
}
|
|
1180
|
-
]
|
|
1181
|
-
isError: true
|
|
553
|
+
]
|
|
1182
554
|
};
|
|
1183
555
|
}
|
|
1184
|
-
|
|
1185
|
-
|
|
556
|
+
);
|
|
557
|
+
this.server.resource(
|
|
558
|
+
"greeting",
|
|
559
|
+
new ResourceTemplate("greeting://{name}", { list: void 0 }),
|
|
560
|
+
async (uri, { name }) => ({
|
|
561
|
+
contents: [
|
|
1186
562
|
{
|
|
1187
|
-
|
|
1188
|
-
text:
|
|
1189
|
-
${parsedMessage[0].messageTypeDescription}`,
|
|
1190
|
-
uri: "parse"
|
|
563
|
+
uri: uri.href,
|
|
564
|
+
text: `Hello, ${name}!`
|
|
1191
565
|
}
|
|
1192
566
|
]
|
|
1193
|
-
}
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
567
|
+
})
|
|
568
|
+
);
|
|
569
|
+
this.server.resource(
|
|
570
|
+
"echo",
|
|
571
|
+
new ResourceTemplate("echo://{message}", { list: void 0 }),
|
|
572
|
+
async (uri, { message }) => ({
|
|
573
|
+
contents: [
|
|
1197
574
|
{
|
|
1198
|
-
|
|
1199
|
-
text: `
|
|
1200
|
-
uri: "parse"
|
|
575
|
+
uri: uri.href,
|
|
576
|
+
text: `Resource echo: ${message}`
|
|
1201
577
|
}
|
|
1202
|
-
]
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
}
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
578
|
+
]
|
|
579
|
+
})
|
|
580
|
+
);
|
|
581
|
+
this.server.resource(
|
|
582
|
+
"stockGraph",
|
|
583
|
+
new ResourceTemplate("stock://{symbol}", { list: void 0 }),
|
|
584
|
+
async (uri, variables) => {
|
|
585
|
+
const symbol = variables.symbol;
|
|
586
|
+
const data = [
|
|
587
|
+
{ time: "09:30", price: 150 },
|
|
588
|
+
{ time: "10:30", price: 152.5 },
|
|
589
|
+
{ time: "11:30", price: 151.75 },
|
|
590
|
+
{ time: "12:30", price: 153.25 },
|
|
591
|
+
{ time: "13:30", price: 154 },
|
|
592
|
+
{ time: "14:30", price: 153.5 },
|
|
593
|
+
{ time: "15:30", price: 155 },
|
|
594
|
+
{ time: "16:00", price: 154.75 }
|
|
595
|
+
];
|
|
596
|
+
const width = 600;
|
|
597
|
+
const height = 300;
|
|
598
|
+
const padding = 40;
|
|
599
|
+
const xScale = (width - 2 * padding) / (data.length - 1);
|
|
600
|
+
const yMin = Math.min(...data.map((d) => d.price));
|
|
601
|
+
const yMax = Math.max(...data.map((d) => d.price));
|
|
602
|
+
const yScale = (height - 2 * padding) / (yMax - yMin);
|
|
603
|
+
const points = data.map((d, i) => {
|
|
604
|
+
const x = padding + i * xScale;
|
|
605
|
+
const y = height - padding - (d.price - yMin) * yScale;
|
|
606
|
+
return `${x},${y}`;
|
|
607
|
+
}).join(" L ");
|
|
608
|
+
const svg = `<?xml version="1.0" encoding="UTF-8"?>
|
|
609
|
+
<svg width="${width}" height="${height}" xmlns="http://www.w3.org/2000/svg">
|
|
610
|
+
<!-- Background -->
|
|
611
|
+
<rect width="100%" height="100%" fill="#f8f9fa"/>
|
|
612
|
+
|
|
613
|
+
<!-- Grid lines -->
|
|
614
|
+
<g stroke="#e9ecef" stroke-width="1">
|
|
615
|
+
${Array.from({ length: 5 }, (_, i) => {
|
|
616
|
+
const y = padding + (height - 2 * padding) * i / 4;
|
|
617
|
+
return `<line x1="${padding}" y1="${y}" x2="${width - padding}" y2="${y}"/>`;
|
|
618
|
+
}).join("\n")}
|
|
619
|
+
</g>
|
|
620
|
+
|
|
621
|
+
<!-- Price line -->
|
|
622
|
+
<path d="M ${points}"
|
|
623
|
+
fill="none"
|
|
624
|
+
stroke="#007bff"
|
|
625
|
+
stroke-width="2"/>
|
|
626
|
+
|
|
627
|
+
<!-- Data points -->
|
|
628
|
+
${data.map((d, i) => {
|
|
629
|
+
const x = padding + i * xScale;
|
|
630
|
+
const y = height - padding - (d.price - yMin) * yScale;
|
|
631
|
+
return `<circle cx="${x}" cy="${y}" r="3" fill="#007bff"/>`;
|
|
632
|
+
}).join("\n")}
|
|
633
|
+
|
|
634
|
+
<!-- Labels -->
|
|
635
|
+
<g font-family="Arial" font-size="12" fill="#495057">
|
|
636
|
+
${data.map((d, i) => {
|
|
637
|
+
const x = padding + i * xScale;
|
|
638
|
+
return `<text x="${x}" y="${height - padding + 20}" text-anchor="middle">${d.time}</text>`;
|
|
639
|
+
}).join("\n")}
|
|
640
|
+
${Array.from({ length: 5 }, (_, i) => {
|
|
641
|
+
const y = padding + (height - 2 * padding) * i / 4;
|
|
642
|
+
const price = yMax - (yMax - yMin) * i / 4;
|
|
643
|
+
return `<text x="${padding - 5}" y="${y + 4}" text-anchor="end">$${price.toFixed(2)}</text>`;
|
|
644
|
+
}).join("\n")}
|
|
645
|
+
</g>
|
|
646
|
+
|
|
647
|
+
<!-- Title -->
|
|
648
|
+
<text x="${width / 2}" y="${padding / 2}"
|
|
649
|
+
font-family="Arial" font-size="16" font-weight="bold"
|
|
650
|
+
text-anchor="middle" fill="#212529">
|
|
651
|
+
${symbol} - 1 Day Price Chart
|
|
652
|
+
</text>
|
|
653
|
+
</svg>`;
|
|
1215
654
|
return {
|
|
1216
|
-
|
|
655
|
+
contents: [
|
|
1217
656
|
{
|
|
1218
|
-
|
|
1219
|
-
text:
|
|
1220
|
-
uri: "parseToJSON"
|
|
657
|
+
uri: uri.href,
|
|
658
|
+
text: svg
|
|
1221
659
|
}
|
|
1222
|
-
]
|
|
1223
|
-
isError: true
|
|
1224
|
-
};
|
|
1225
|
-
}
|
|
1226
|
-
return {
|
|
1227
|
-
content: [
|
|
1228
|
-
{
|
|
1229
|
-
type: "text",
|
|
1230
|
-
text: `${parsedMessage[0].toFIXJSON()}`,
|
|
1231
|
-
uri: "parseToJSON"
|
|
1232
|
-
}
|
|
1233
|
-
]
|
|
1234
|
-
};
|
|
1235
|
-
} catch (error) {
|
|
1236
|
-
return {
|
|
1237
|
-
content: [
|
|
1238
|
-
{
|
|
1239
|
-
type: "text",
|
|
1240
|
-
text: `Error: ${error instanceof Error ? error.message : "Failed to parse FIX string"}`,
|
|
1241
|
-
uri: "parseToJSON"
|
|
1242
|
-
}
|
|
1243
|
-
],
|
|
1244
|
-
isError: true
|
|
1245
|
-
};
|
|
1246
|
-
}
|
|
1247
|
-
};
|
|
1248
|
-
};
|
|
1249
|
-
|
|
1250
|
-
// src/tools/index.ts
|
|
1251
|
-
var createToolHandlers = (parser, verifiedOrders, pendingRequests, marketDataPrices) => ({
|
|
1252
|
-
parse: createParseHandler(parser),
|
|
1253
|
-
parseToJSON: createParseToJSONHandler(parser),
|
|
1254
|
-
verifyOrder: createVerifyOrderHandler(parser, verifiedOrders),
|
|
1255
|
-
executeOrder: createExecuteOrderHandler(parser, verifiedOrders, pendingRequests),
|
|
1256
|
-
marketDataRequest: createMarketDataRequestHandler(parser, pendingRequests),
|
|
1257
|
-
getStockGraph: createGetStockGraphHandler(marketDataPrices),
|
|
1258
|
-
getStockPriceHistory: createGetStockPriceHistoryHandler(marketDataPrices),
|
|
1259
|
-
technicalAnalysis: createTechnicalAnalysisHandler(marketDataPrices)
|
|
1260
|
-
});
|
|
1261
|
-
|
|
1262
|
-
// src/utils/messageHandler.ts
|
|
1263
|
-
import { Fields as Fields3, MDEntryType as MDEntryType2, Messages as Messages3 } from "fixparser";
|
|
1264
|
-
function getEnumValue(enumObj, name) {
|
|
1265
|
-
return enumObj[name] || name;
|
|
1266
|
-
}
|
|
1267
|
-
function handleMessage(message, parser, pendingRequests, marketDataPrices, maxPriceHistory, onPriceUpdate) {
|
|
1268
|
-
const msgType = message.messageType;
|
|
1269
|
-
if (msgType === Messages3.MarketDataSnapshotFullRefresh || msgType === Messages3.MarketDataIncrementalRefresh) {
|
|
1270
|
-
const symbol = message.getField(Fields3.Symbol)?.value;
|
|
1271
|
-
const fixJson = message.toFIXJSON();
|
|
1272
|
-
const entries = fixJson.Body?.NoMDEntries || [];
|
|
1273
|
-
const data = {
|
|
1274
|
-
timestamp: Date.now(),
|
|
1275
|
-
bid: 0,
|
|
1276
|
-
offer: 0,
|
|
1277
|
-
spread: 0,
|
|
1278
|
-
volume: 0,
|
|
1279
|
-
trade: 0,
|
|
1280
|
-
indexValue: 0,
|
|
1281
|
-
openingPrice: 0,
|
|
1282
|
-
closingPrice: 0,
|
|
1283
|
-
settlementPrice: 0,
|
|
1284
|
-
tradingSessionHighPrice: 0,
|
|
1285
|
-
tradingSessionLowPrice: 0,
|
|
1286
|
-
vwap: 0,
|
|
1287
|
-
imbalance: 0,
|
|
1288
|
-
openInterest: 0,
|
|
1289
|
-
compositeUnderlyingPrice: 0,
|
|
1290
|
-
simulatedSellPrice: 0,
|
|
1291
|
-
simulatedBuyPrice: 0,
|
|
1292
|
-
marginRate: 0,
|
|
1293
|
-
midPrice: 0,
|
|
1294
|
-
emptyBook: 0,
|
|
1295
|
-
settleHighPrice: 0,
|
|
1296
|
-
settleLowPrice: 0,
|
|
1297
|
-
priorSettlePrice: 0,
|
|
1298
|
-
sessionHighBid: 0,
|
|
1299
|
-
sessionLowOffer: 0,
|
|
1300
|
-
earlyPrices: 0,
|
|
1301
|
-
auctionClearingPrice: 0,
|
|
1302
|
-
swapValueFactor: 0,
|
|
1303
|
-
dailyValueAdjustmentForLongPositions: 0,
|
|
1304
|
-
cumulativeValueAdjustmentForLongPositions: 0,
|
|
1305
|
-
dailyValueAdjustmentForShortPositions: 0,
|
|
1306
|
-
cumulativeValueAdjustmentForShortPositions: 0,
|
|
1307
|
-
fixingPrice: 0,
|
|
1308
|
-
cashRate: 0,
|
|
1309
|
-
recoveryRate: 0,
|
|
1310
|
-
recoveryRateForLong: 0,
|
|
1311
|
-
recoveryRateForShort: 0,
|
|
1312
|
-
marketBid: 0,
|
|
1313
|
-
marketOffer: 0,
|
|
1314
|
-
shortSaleMinPrice: 0,
|
|
1315
|
-
previousClosingPrice: 0,
|
|
1316
|
-
thresholdLimitPriceBanding: 0,
|
|
1317
|
-
dailyFinancingValue: 0,
|
|
1318
|
-
accruedFinancingValue: 0,
|
|
1319
|
-
twap: 0
|
|
1320
|
-
};
|
|
1321
|
-
for (const entry of entries) {
|
|
1322
|
-
const entryType = entry.MDEntryType;
|
|
1323
|
-
const price = entry.MDEntryPx ? Number.parseFloat(entry.MDEntryPx) : 0;
|
|
1324
|
-
const size = entry.MDEntrySize ? Number.parseFloat(entry.MDEntrySize) : 0;
|
|
1325
|
-
const enumValue = getEnumValue(MDEntryType2, entryType);
|
|
1326
|
-
switch (enumValue) {
|
|
1327
|
-
case MDEntryType2.Bid:
|
|
1328
|
-
data.bid = price;
|
|
1329
|
-
break;
|
|
1330
|
-
case MDEntryType2.Offer:
|
|
1331
|
-
data.offer = price;
|
|
1332
|
-
break;
|
|
1333
|
-
case MDEntryType2.Trade:
|
|
1334
|
-
data.trade = price;
|
|
1335
|
-
break;
|
|
1336
|
-
case MDEntryType2.IndexValue:
|
|
1337
|
-
data.indexValue = price;
|
|
1338
|
-
break;
|
|
1339
|
-
case MDEntryType2.OpeningPrice:
|
|
1340
|
-
data.openingPrice = price;
|
|
1341
|
-
break;
|
|
1342
|
-
case MDEntryType2.ClosingPrice:
|
|
1343
|
-
data.closingPrice = price;
|
|
1344
|
-
break;
|
|
1345
|
-
case MDEntryType2.SettlementPrice:
|
|
1346
|
-
data.settlementPrice = price;
|
|
1347
|
-
break;
|
|
1348
|
-
case MDEntryType2.TradingSessionHighPrice:
|
|
1349
|
-
data.tradingSessionHighPrice = price;
|
|
1350
|
-
break;
|
|
1351
|
-
case MDEntryType2.TradingSessionLowPrice:
|
|
1352
|
-
data.tradingSessionLowPrice = price;
|
|
1353
|
-
break;
|
|
1354
|
-
case MDEntryType2.VWAP:
|
|
1355
|
-
data.vwap = price;
|
|
1356
|
-
break;
|
|
1357
|
-
case MDEntryType2.Imbalance:
|
|
1358
|
-
data.imbalance = size;
|
|
1359
|
-
break;
|
|
1360
|
-
case MDEntryType2.TradeVolume:
|
|
1361
|
-
data.volume = size;
|
|
1362
|
-
break;
|
|
1363
|
-
case MDEntryType2.OpenInterest:
|
|
1364
|
-
data.openInterest = size;
|
|
1365
|
-
break;
|
|
1366
|
-
case MDEntryType2.CompositeUnderlyingPrice:
|
|
1367
|
-
data.compositeUnderlyingPrice = price;
|
|
1368
|
-
break;
|
|
1369
|
-
case MDEntryType2.SimulatedSellPrice:
|
|
1370
|
-
data.simulatedSellPrice = price;
|
|
1371
|
-
break;
|
|
1372
|
-
case MDEntryType2.SimulatedBuyPrice:
|
|
1373
|
-
data.simulatedBuyPrice = price;
|
|
1374
|
-
break;
|
|
1375
|
-
case MDEntryType2.MarginRate:
|
|
1376
|
-
data.marginRate = price;
|
|
1377
|
-
break;
|
|
1378
|
-
case MDEntryType2.MidPrice:
|
|
1379
|
-
data.midPrice = price;
|
|
1380
|
-
break;
|
|
1381
|
-
case MDEntryType2.EmptyBook:
|
|
1382
|
-
data.emptyBook = 1;
|
|
1383
|
-
break;
|
|
1384
|
-
case MDEntryType2.SettleHighPrice:
|
|
1385
|
-
data.settleHighPrice = price;
|
|
1386
|
-
break;
|
|
1387
|
-
case MDEntryType2.SettleLowPrice:
|
|
1388
|
-
data.settleLowPrice = price;
|
|
1389
|
-
break;
|
|
1390
|
-
case MDEntryType2.PriorSettlePrice:
|
|
1391
|
-
data.priorSettlePrice = price;
|
|
1392
|
-
break;
|
|
1393
|
-
case MDEntryType2.SessionHighBid:
|
|
1394
|
-
data.sessionHighBid = price;
|
|
1395
|
-
break;
|
|
1396
|
-
case MDEntryType2.SessionLowOffer:
|
|
1397
|
-
data.sessionLowOffer = price;
|
|
1398
|
-
break;
|
|
1399
|
-
case MDEntryType2.EarlyPrices:
|
|
1400
|
-
data.earlyPrices = price;
|
|
1401
|
-
break;
|
|
1402
|
-
case MDEntryType2.AuctionClearingPrice:
|
|
1403
|
-
data.auctionClearingPrice = price;
|
|
1404
|
-
break;
|
|
1405
|
-
case MDEntryType2.SwapValueFactor:
|
|
1406
|
-
data.swapValueFactor = price;
|
|
1407
|
-
break;
|
|
1408
|
-
case MDEntryType2.DailyValueAdjustmentForLongPositions:
|
|
1409
|
-
data.dailyValueAdjustmentForLongPositions = price;
|
|
1410
|
-
break;
|
|
1411
|
-
case MDEntryType2.CumulativeValueAdjustmentForLongPositions:
|
|
1412
|
-
data.cumulativeValueAdjustmentForLongPositions = price;
|
|
1413
|
-
break;
|
|
1414
|
-
case MDEntryType2.DailyValueAdjustmentForShortPositions:
|
|
1415
|
-
data.dailyValueAdjustmentForShortPositions = price;
|
|
1416
|
-
break;
|
|
1417
|
-
case MDEntryType2.CumulativeValueAdjustmentForShortPositions:
|
|
1418
|
-
data.cumulativeValueAdjustmentForShortPositions = price;
|
|
1419
|
-
break;
|
|
1420
|
-
case MDEntryType2.FixingPrice:
|
|
1421
|
-
data.fixingPrice = price;
|
|
1422
|
-
break;
|
|
1423
|
-
case MDEntryType2.CashRate:
|
|
1424
|
-
data.cashRate = price;
|
|
1425
|
-
break;
|
|
1426
|
-
case MDEntryType2.RecoveryRate:
|
|
1427
|
-
data.recoveryRate = price;
|
|
1428
|
-
break;
|
|
1429
|
-
case MDEntryType2.RecoveryRateForLong:
|
|
1430
|
-
data.recoveryRateForLong = price;
|
|
1431
|
-
break;
|
|
1432
|
-
case MDEntryType2.RecoveryRateForShort:
|
|
1433
|
-
data.recoveryRateForShort = price;
|
|
1434
|
-
break;
|
|
1435
|
-
case MDEntryType2.MarketBid:
|
|
1436
|
-
data.marketBid = price;
|
|
1437
|
-
break;
|
|
1438
|
-
case MDEntryType2.MarketOffer:
|
|
1439
|
-
data.marketOffer = price;
|
|
1440
|
-
break;
|
|
1441
|
-
case MDEntryType2.ShortSaleMinPrice:
|
|
1442
|
-
data.shortSaleMinPrice = price;
|
|
1443
|
-
break;
|
|
1444
|
-
case MDEntryType2.PreviousClosingPrice:
|
|
1445
|
-
data.previousClosingPrice = price;
|
|
1446
|
-
break;
|
|
1447
|
-
case MDEntryType2.ThresholdLimitPriceBanding:
|
|
1448
|
-
data.thresholdLimitPriceBanding = price;
|
|
1449
|
-
break;
|
|
1450
|
-
case MDEntryType2.DailyFinancingValue:
|
|
1451
|
-
data.dailyFinancingValue = price;
|
|
1452
|
-
break;
|
|
1453
|
-
case MDEntryType2.AccruedFinancingValue:
|
|
1454
|
-
data.accruedFinancingValue = price;
|
|
1455
|
-
break;
|
|
1456
|
-
case MDEntryType2.TWAP:
|
|
1457
|
-
data.twap = price;
|
|
1458
|
-
break;
|
|
1459
|
-
}
|
|
1460
|
-
}
|
|
1461
|
-
data.spread = data.offer - data.bid;
|
|
1462
|
-
if (!marketDataPrices.has(symbol)) {
|
|
1463
|
-
marketDataPrices.set(symbol, []);
|
|
1464
|
-
}
|
|
1465
|
-
const prices = marketDataPrices.get(symbol);
|
|
1466
|
-
prices.push(data);
|
|
1467
|
-
if (prices.length > maxPriceHistory) {
|
|
1468
|
-
prices.splice(0, prices.length - maxPriceHistory);
|
|
1469
|
-
}
|
|
1470
|
-
onPriceUpdate?.(symbol, data);
|
|
1471
|
-
const mdReqID = message.getField(Fields3.MDReqID)?.value;
|
|
1472
|
-
if (mdReqID) {
|
|
1473
|
-
const callback = pendingRequests.get(mdReqID);
|
|
1474
|
-
if (callback) {
|
|
1475
|
-
callback(message);
|
|
1476
|
-
pendingRequests.delete(mdReqID);
|
|
1477
|
-
}
|
|
1478
|
-
}
|
|
1479
|
-
} else if (msgType === Messages3.ExecutionReport) {
|
|
1480
|
-
const reqId = message.getField(Fields3.ClOrdID)?.value;
|
|
1481
|
-
const callback = pendingRequests.get(reqId);
|
|
1482
|
-
if (callback) {
|
|
1483
|
-
callback(message);
|
|
1484
|
-
pendingRequests.delete(reqId);
|
|
1485
|
-
}
|
|
1486
|
-
}
|
|
1487
|
-
}
|
|
1488
|
-
|
|
1489
|
-
// src/MCPLocal.ts
|
|
1490
|
-
var MCPLocal = class extends MCPBase {
|
|
1491
|
-
/**
|
|
1492
|
-
* Map to store verified orders before execution
|
|
1493
|
-
* @private
|
|
1494
|
-
*/
|
|
1495
|
-
verifiedOrders = /* @__PURE__ */ new Map();
|
|
1496
|
-
/**
|
|
1497
|
-
* Map to store pending requests and their callbacks
|
|
1498
|
-
* @private
|
|
1499
|
-
*/
|
|
1500
|
-
pendingRequests = /* @__PURE__ */ new Map();
|
|
1501
|
-
/**
|
|
1502
|
-
* Map to store market data prices for each symbol
|
|
1503
|
-
* @private
|
|
1504
|
-
*/
|
|
1505
|
-
marketDataPrices = /* @__PURE__ */ new Map();
|
|
1506
|
-
/**
|
|
1507
|
-
* Maximum number of price history entries to keep per symbol
|
|
1508
|
-
* @private
|
|
1509
|
-
*/
|
|
1510
|
-
MAX_PRICE_HISTORY = 1e5;
|
|
1511
|
-
server = new Server(
|
|
1512
|
-
{
|
|
1513
|
-
name: "fixparser",
|
|
1514
|
-
version: "1.0.0"
|
|
1515
|
-
},
|
|
1516
|
-
{
|
|
1517
|
-
capabilities: {
|
|
1518
|
-
tools: Object.entries(toolSchemas).reduce(
|
|
1519
|
-
(acc, [name, { description, schema }]) => {
|
|
1520
|
-
acc[name] = {
|
|
1521
|
-
description,
|
|
1522
|
-
parameters: schema
|
|
1523
|
-
};
|
|
1524
|
-
return acc;
|
|
1525
|
-
},
|
|
1526
|
-
{}
|
|
1527
|
-
)
|
|
1528
|
-
}
|
|
1529
|
-
}
|
|
1530
|
-
);
|
|
1531
|
-
transport = new StdioServerTransport();
|
|
1532
|
-
constructor({ logger, onReady }) {
|
|
1533
|
-
super({ logger, onReady });
|
|
1534
|
-
}
|
|
1535
|
-
async register(parser) {
|
|
1536
|
-
this.parser = parser;
|
|
1537
|
-
this.parser.addOnMessageCallback((message) => {
|
|
1538
|
-
handleMessage(message, this.parser, this.pendingRequests, this.marketDataPrices, this.MAX_PRICE_HISTORY);
|
|
1539
|
-
});
|
|
1540
|
-
this.addWorkflows();
|
|
1541
|
-
await this.server.connect(this.transport);
|
|
1542
|
-
if (this.onReady) {
|
|
1543
|
-
this.onReady();
|
|
1544
|
-
}
|
|
1545
|
-
}
|
|
1546
|
-
addWorkflows() {
|
|
1547
|
-
if (!this.parser) {
|
|
1548
|
-
return;
|
|
1549
|
-
}
|
|
1550
|
-
if (!this.server) {
|
|
1551
|
-
return;
|
|
1552
|
-
}
|
|
1553
|
-
this.server.setRequestHandler(z.object({ method: z.literal("tools/list") }), async () => {
|
|
1554
|
-
return {
|
|
1555
|
-
tools: Object.entries(toolSchemas).map(([name, { description, schema }]) => ({
|
|
1556
|
-
name,
|
|
1557
|
-
description,
|
|
1558
|
-
inputSchema: schema
|
|
1559
|
-
}))
|
|
1560
|
-
};
|
|
1561
|
-
});
|
|
1562
|
-
this.server.setRequestHandler(
|
|
1563
|
-
z.object({
|
|
1564
|
-
method: z.literal("tools/call"),
|
|
1565
|
-
params: z.object({
|
|
1566
|
-
name: z.string(),
|
|
1567
|
-
arguments: z.any(),
|
|
1568
|
-
_meta: z.object({
|
|
1569
|
-
progressToken: z.number()
|
|
1570
|
-
}).optional()
|
|
1571
|
-
})
|
|
1572
|
-
}),
|
|
1573
|
-
async (request) => {
|
|
1574
|
-
const { name, arguments: args } = request.params;
|
|
1575
|
-
const toolHandlers = createToolHandlers(
|
|
1576
|
-
this.parser,
|
|
1577
|
-
this.verifiedOrders,
|
|
1578
|
-
this.pendingRequests,
|
|
1579
|
-
this.marketDataPrices
|
|
1580
|
-
);
|
|
1581
|
-
const handler = toolHandlers[name];
|
|
1582
|
-
if (!handler) {
|
|
1583
|
-
return {
|
|
1584
|
-
content: [
|
|
1585
|
-
{
|
|
1586
|
-
type: "text",
|
|
1587
|
-
text: `Tool not found: ${name}`,
|
|
1588
|
-
uri: name
|
|
1589
|
-
}
|
|
1590
|
-
],
|
|
1591
|
-
isError: true
|
|
1592
|
-
};
|
|
1593
|
-
}
|
|
1594
|
-
const result = await handler(args);
|
|
1595
|
-
return {
|
|
1596
|
-
content: result.content,
|
|
1597
|
-
isError: result.isError
|
|
660
|
+
]
|
|
1598
661
|
};
|
|
1599
662
|
}
|
|
1600
663
|
);
|