-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathApp.js
More file actions
283 lines (237 loc) · 8.73 KB
/
Copy pathApp.js
File metadata and controls
283 lines (237 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
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
282
283
// DistributedATS - Mike Kipnis (c) 2022
import "./App.css";
import "bootstrap/dist/css/bootstrap.min.css";
import React, {
useEffect,
useState,
useRef,
useCallback,
} from "react";
import { Container, Row, Col } from "react-bootstrap/";
import Login from "./components/Login";
import PositionsAndMarketData from "./components/PositionsAndMarketData";
import Ticket from "./components/Ticket";
import History from "./components/History";
import { FIXWebSocketClient } from "./websocket_fix_utils/FIXWebSocketClient";
import { FIXMessageHandler } from "./websocket_fix_utils/FIXMessageHandler";
import { DataMan } from "./data_man/DataMan";
function App() {
const ticketRef = useRef();
const histRef = useRef();
const marketDataAndPositionsRef = useRef();
const fixSessionHandler = useRef(null);
const fixClient = useRef(null);
const dataMan = useRef(null);
const [sessionToken, setSessionToken] = useState(null);
const [loginState, setLoginState] = useState({
sessionStateCode: 0,
text: "Please login",
});
const [blotterData, setBlotterData] = useState();
const [selectedInstrument, setSelectedInstrument] = useState();
const [lastExecReport, setLastExecReport] = useState();
const [selectedInstrumentBlotterData, setSelectedInstrumentBlotterData] =
useState();
const [histData, setHistData] = useState([]);
/*
function getWebSocketUrl() {
const host = window.location.hostname; // localhost, docker container, or prod host
const port = 9002; // your FIX WS port
const protocol = window.location.protocol === "https:" ? "wss" : "ws";
return `${protocol}://${host}:${port}`;
}
*/
function getWebSocketUrl() {
const host = window.location.hostname;
const protocol = window.location.protocol === "https:" ? "wss" : "ws";
// Only special-case production domain
if (host === "ats.alpharesearch.online" || host === "ats.ustreasuries.online") {
return `${protocol}://${host}/ws`;
}
// everything else keeps old behavior
return `${protocol}://${host}:9002`;
}
// =====================================================================================
// FIX LOGON CALLBACK (USER PRESSES LOGIN BUTTON)
// =====================================================================================
const Logon_callback = useCallback((logonValue) => {
if (!fixClient.current) {
const wsUrl = getWebSocketUrl();
// Create FIX handler
fixSessionHandler.current = new FIXMessageHandler({
username: logonValue.username,
password: logonValue.password,
targetCompID: "FIX_GWY_1",
});
// Pass handler into client
fixClient.current = new FIXWebSocketClient(wsUrl, fixSessionHandler.current);
dataMan.current = new DataMan(fixSessionHandler.current);
fixClient.current.onopen = () => {
console.log("WS opened");
};
// -------------------------
// WS MESSAGE
// -------------------------
fixClient.current.onmessage = (msg) => {
try {
if (msg?.data_type !== "FIX") return;
const msgType = msg?.Header?.["35"];
// -------------------------
// FIX LOGON (35=A)
// -------------------------
if (msgType === "A") {
console.log("🔐 FIX Logon received");
setLoginState({ text: "Logon successful" });
setSessionToken({
token: msg.session_qualifier,
username: logonValue.username,
});
return;
} else if ( msgType === "5")
{
console.log("🔐 FIX Logout received", msg);
setLoginState({ text: msg?.Body?.["58"] });
return;
}
// -------------------------
// ALL OTHER FIX MSGS
// -------------------------
const result = dataMan.current.processFIXMessage(
fixClient.current,
msg
);
if (result !== undefined) {
setBlotterData(result);
}
const lastType = dataMan.current.get_last_msg_type();
if (lastType === "y") {
fixSessionHandler.current.sendMassOrderStatusRequest(fixClient.current);
} else if (lastType === "8") {
const orderMan = dataMan.current.get_order_man();
setHistData(orderMan.get_order_states());
}
} catch (err) {
console.error("Failed to parse FIX WS message:", err);
}
};
// -------------------------
// WS ERROR
// -------------------------
fixClient.current.onerror = (err) => {
console.error("❌ FIX WS error:", err);
setLoginState({ text: "FIX WebSocket connection failed" });
};
// -------------------------
// WS CLOSE
// -------------------------
fixClient.current.onclose = () => {
console.warn("⚠️ FIX WS closed");
setLoginState({ text: "Disconnected from FIX WS" });
setBlotterData(undefined);
};
}
fixClient.current.connect();
}, []);
// =====================================================================================
// UPDATE UI ON BLOTTER CHANGES
// =====================================================================================
useEffect(() => {
if (!blotterData) return;
if (selectedInstrument) {
setSelectedInstrumentBlotterData({
...blotterData[selectedInstrument],
});
setLastExecReport({
...blotterData["last_exec_report"],
});
}
if (marketDataAndPositionsRef.current) {
marketDataAndPositionsRef.current.update_data();
}
}, [blotterData, selectedInstrument]);
// =====================================================================================
// INSTRUMENT SELECTION CALLBACK
// =====================================================================================
const SelectedInstrument = (instrument) => {
setSelectedInstrument(instrument);
};
// =====================================================================================
// SESSION ESTABLISHED → REQUEST SECURITY LIST
// =====================================================================================
useEffect(() => {
if (!sessionToken || !fixClient.current) return;
console.log("🔐 FIX session token:", sessionToken);
fixSessionHandler.current.sendSecurityListRequest(fixClient.current);
}, [sessionToken]);
// =====================================================================================
// CLEANUP
// =====================================================================================
useEffect(() => {
return () => {
if (fixClient.current) fixClient.current.disconnect();
};
}, []);
// =====================================================================================
// RENDER UI
// =====================================================================================
return (
<div className="body">
<div className="ag-theme-balham-dark">
<nav>
<Login
loginState={loginState}
logonCallback={Logon_callback}
/>
</nav>
<Container fluid style={{ marginTop: 20 }}>
<div
style={
blotterData == null
? { pointerEvents: "none", opacity: 0.4 }
: {}
}
>
<Row>
<Col sm={8}>
<PositionsAndMarketData
blotterData={blotterData}
selectedInstrument={SelectedInstrument}
ref={marketDataAndPositionsRef}
/>
<div>Click on an instrument to trade.</div>
</Col>
<Col sm={4}>
<Ticket
instrumentName={selectedInstrument}
selectedInstrumentBlotterData={
selectedInstrumentBlotterData
}
fixSessionHandler={fixSessionHandler}
sendOrder={(msg) => {
return fixClient.current.sendJSON(msg);
}}
sendCancelAll={(msg) => {
return fixClient.current.sendJSON(msg);
}}
ref={ticketRef}
/>
</Col>
</Row>
<div style={{ marginTop: 10 }}>
<History
histData={histData}
blotterData={blotterData}
fixSessionHandler={fixSessionHandler}
sendCancelOrder={(msg) => {
return fixClient.current.sendJSON(msg);
}}
ref={histRef}
/>
</div>
</div>
</Container>
</div>
</div>
);
}
export default App;