-
Notifications
You must be signed in to change notification settings - Fork 2
/
utility.js
executable file
·285 lines (248 loc) · 10.3 KB
/
utility.js
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
284
285
// return the number of nanoseconds since midnight
function getTime(){
var m = moment.tz("America/Los_Angeles");
var hours = m.hours() *(60*60)*1000000000;
var minutes = m.minutes() *60*1000000000;
var seconds = m.seconds() *1000000000;
var millis = m.milliseconds()*1000000;
return hours+minutes+seconds+millis;
}
// Adds padding to a javascript printTime() fomratted timestamp
// position: which interval is to be padded [hr:min:sec:ms] => [0:1:2:3]
// padwidth: number of digits allowed
function padTime(timestamp, position=3, padWidth=3){
var splitTimestamp = timestamp.split(':');
while(splitTimestamp[position].length != padWidth + 1){
splitTimestamp[position] = '0' + splitTimestamp[position];
}
return splitTimestamp.join(':');
}
function printTime(nanoseconds){
var str = "";
var millis = Math.floor((nanoseconds / 1000000) % 1000);
var seconds = Math.floor((nanoseconds / 1000000000) % 60);
var minutes = Math.floor(nanoseconds / (60*1000000000) % 60);
var hours = Math.floor(nanoseconds / (60*60*1000000000) % 24);
str = "[" + hours + ":" + minutes + ":" + seconds + ":" + millis + "]";
return str;
}
function OuchMessage(msgType, subjectID, price, IOC) {
this.protocol = "OUCH"; //Ouch, Update, Cancel -> 'O','U','X'
this.timeStamp = getTime(); //timeStamp is not sent to the server
this.msgType = msgType; //EBUY,ESELL,RBUY,RSELL,UBUY,USELL
this.price = price; //must be multiplied by 1000 before sent to server
this.subjectID = subjectID; //0,1,2,3,4
this.IOC = IOC; //needs to be converted to either 3 or 99999
this.delay = false; //false by default
this.senderId;
this.msgId;
this.prevMsgId;
//this.numShares = 0;
}
function ItchMessage(msgType, subjectID, price, timeStamp, buyerID, sellerID){
this.protocol = "ITCH"; //'A','C','U','E','S'
this.msgType = msgType; //C_UBUY,C_USELL,C_EBUY,C_ESELL,C_CANC,C_TRA,BATCH
this.timeStamp = timeStamp; //timeStamp from the server
//this.timeStamp = getTime(); //test 7/18/17
this.price = price; //must be multiplied by 1000 before sent to server
this.buyerID = buyerID; //0,1,2,3,4
this.sellerID = sellerID; //0,1,2,3,4
this.subjectID = subjectID; //0,1,2,3,4
this.FPC;
this.batchType; //'B' for Start, 'P' for End
this.msgId;
this.numShares = 0;
}
// Message object. Used to communicate between group manager, subject manager, and market algorithm
function Message(protocol, msgType, msgData) {
this.protocol = protocol;
this.delay = false;
this.msgType = msgType;
this.msgData = msgData;
this.timeStamp = getTime();
// formats message as string for debugging
this.asString = function(){
var s = '';
//var s = msgType + " timestamp:" + printTime(this.timeStamp) + " subjID:" + msgData[0];
if(msgType == "C_EBUY" || msgType == "C_ESELL" || msgType == "C_UBUY" || msgType == "C_USELL"){
s += msgType + " timestamp:" + printTime(this.timeStamp) + " buyer/sellerID: " + msgData[0] + " price: " + msgData[1] + " time-order-entered: " + printTime(msgData[2]);
}
else if(msgType == "UBUY" || msgType == "USELL"){
s += msgType + " timestamp:" + printTime(this.timeStamp) + " buyer/sellerID:" + this.prevMsgId + "->" + this.msgId + " price: " + msgData[1];
}
else if (msgType == "C_TRA"){
s += msgType + " timestamp:" + printTime(this.timeStamp) + " buyerID:" + msgData[1] + " sellerID: " + msgData[2] + " price: " + msgData[3];
}
else if(msgType == "EBUY" || msgType == "ESELL"){
s += msgType + " timestamp:" + printTime(this.timeStamp) + " buyer/sellerID:" + msgData[0] + " price:" + msgData[1] + " IOC: " + msgData[2] + " msgID: " + this.msgId;
}
else{
s += msgType + " timestamp:" + printTime(this.timeStamp) + " subjID:" + msgData[0] + " msgID:" + this.msgId;
}
return s;
}
this.senderId;
this.msgId;
this.prevMsgId;
this.numShares = 0;
}
// Updates timestamp of message to current timestamp
function updateMsgTime(msg) {
msg.timeStamp = getTime();
}
// Returns packed message with "actionTime" tag used to simulate latency
function packMsg(msg, delay) {
msg.delay = delay;
return {
"actionTime": msg.timeStamp + msg.delay,
"msg": msg
};
}
// OBSOLETE SINCE WE MOVED TO NEW TIMESTAMP SYSTEM
// Converts timestamp to readable time
function millisToTime(millis) {
var date = new Date(millis);
var str = '';
str += date.getUTCHours() + "h:";
str += date.getUTCMinutes() + "m:";
str += date.getUTCSeconds() + "s:";
str += date.getUTCMilliseconds() + "millis";
return str;
}
// Logger object used to debug messages as they are recieved and sent
function MessageLogger(name, nameColor, elementId) {
this.name = name;
this.nameColor = nameColor;
this.element = $("#" + elementId);
this.logSend = function (msg, reciever) {
this.element.append('<div class="log-line"><span style="color:'
+ this.nameColor + '"> ' + this.name
+ '</span> <span>sent message to ' + reciever + ' at</span> <span class="timestamp">'
+ millisToTime(msg.timeStamp) + '</span> <span> containing:</span> <span class="message">'
+ msg.msgType + '</span></div>');
this.scrollDown();
};
this.logRecv = function (msg, sender) {
this.element.append('<div class="log-line"><span style="color:'
+ this.nameColor + '"> ' + this.name
+ '</span> <span>recieved message from ' + sender + ' at </span> <span class="timestamp">'
+ millisToTime(msg.timeStamp) + '</span> <span> containing:</span> <span class="message">'
+ msg.msgType + '</span></div>');
this.scrollDown();
};
this.logSendWait = function (msg) {
this.element.append('<div class="log-line"><span style="color:'
+ this.nameColor + '"> ' + this.name
+ '</span> <span>out wait list at</span> <span class="timestamp">'
+ millisToTime(msg.timeStamp) + '</span> <span> delay</span> <span class="delay">'
+ String(msg.delay) + '</span> <span> containing:</span> <span class="message">'
+ msg.msgType + '</span></div>');
this.scrollDown();
};
this.logRecvWait = function (msg) {
this.element.append('<div class="log-line"><span style="color:'
+ this.nameColor + '"> ' + this.name
+ '</span> <span>in wait list at</span> <span class="timestamp">'
+ millisToTime(msg.timeStamp) + '</span> <span> delay</span> <span class="delay">'
+ String(msg.delay) + '</span> <span> containing:</span> <span class="message">'
+ msg.msgType + '</span></div>');
this.scrollDown();
};
this.logString = function (str) {
this.element.append('<div class="log-line"><span style="color:'
+ this.nameColor + '"> ' + this.name
+ '</span> <span>' + str + '</span></div>');
this.scrollDown();
};
this.scrollDown = function () {
this.element.scrollTop(this.element[0].scrollHeight);
};
}
// array for synchronizing events. Initialize with an array holding all of the keys, then
// mark each key ready one at a time using markReady. All keys are marked when allReady returns true.
function SynchronizeArray(key_array) {
this.readyFlags = {};
this.readyCount = 0;
this.targetReadyCount = key_array.length;
for (var key of key_array) {
this.readyFlags[key] = false;
}
this.markReady = function (key) {
if (this.readyFlags[key] === undefined) {
console.error("did not find element with key: " + String(key) + " in synchronizing array.");
return;
}
if (!this.readyFlags[key]) {
this.readyCount++;
this.readyFlags[key] = true;
}
};
this.allReady = function () {
return this.readyCount === this.targetReadyCount;
};
this.reset = function () {
this.readyFlags = {};
this.readyCount = 0;
};
}
function getTimeToString(){
byteArrayToString(intToByteArray(getTime()));
}
// simple exchange server that always accepts enter buy/sell cancels, and replaces. Will generate simple crosses
function DebugMarket(sendFunction){
var LOG_MSGS = true;
console.log("Setting up DebugMarket...");
this.recvMessage = function(msgArr){
var msg = "";
for(num of msgArr){
msg += String.fromCharCode(num);
}
if(LOG_MSGS){
console.log("Debug Market recieved: " + msg);
}
// Enter order message
if(msg.charAt(0) === "O"){
console.log("order");
var testAccept = "A" + getTimeToString() + msg.subString(1, 15) + "LEEPS \0\0\1\001\0\001"+String.fromCharCode(134)+String.fromCharCode(159)+"SUBF";
}
}
this.sendMessage = function(msg){
sendFunction(msg);
}
}
// allows user to download string into file on local computer
function download(strData, strFileName, strMimeType) {
var D = document,
A = arguments,
a = D.createElement("a"),
d = A[0],
n = A[1],
t = A[2] || "text/plain";
//build download link:
a.href = "data:" + strMimeType + "charset=utf-8," + escape(strData);
if (window.MSBlobBuilder) { // IE10
var bb = new MSBlobBuilder();
bb.append(strData);
return navigator.msSaveBlob(bb, strFileName);
} /* end if(window.MSBlobBuilder) */
if ('download' in a) { //FF20, CH19
a.setAttribute("download", n);
a.innerHTML = "downloading...";
D.body.appendChild(a);
setTimeout(function() {
var e = D.createEvent("MouseEvents");
e.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
a.dispatchEvent(e);
D.body.removeChild(a);
}, 66);
return true;
}; /* end if('download' in a) */
//do iframe dataURL download: (older W3)
var f = D.createElement("iframe");
D.body.appendChild(f);
f.src = "data:" + (A[2] ? A[2] : "application/octet-stream") + (window.btoa ? ";base64" : "") + "," + (window.btoa ? window.btoa : escape)(strData);
setTimeout(function() {
D.body.removeChild(f);
}, 333);
return true;
}