@@ -37,7 +37,7 @@ if(symbol !== "BTC/USD"){
37
37
const insertTrade = await db . query ( queryString , [ symbol , type , price , size , time ] )
38
38
const insertTradeArr = insertTrade [ 0 ]
39
39
if ( insertTradeArr . affectedRows !== 0 ) {
40
- res . sendStatus ( 201 )
40
+ res . status ( 201 ) . json ( { message : "Limit Order Posted Successfully" , code : 201 } )
41
41
}
42
42
} else if ( direction === "sell" && type === "limit" ) {
43
43
@@ -47,11 +47,11 @@ if(symbol !== "BTC/USD"){
47
47
const insertTrade = await db . query ( queryString , [ symbol , type , price , size , time ] )
48
48
const insertTradeArr = insertTrade [ 0 ]
49
49
if ( insertTradeArr . affectedRows !== 0 ) {
50
- res . sendStatus ( 201 )
50
+ res . status ( 201 ) . json ( { message : "Limit Order Posted Successfully" , code : 201 } )
51
51
}
52
52
} else if ( type === "market" ) {
53
53
54
- //inserting market order buy into the database
54
+ //inserting market order into the database
55
55
56
56
57
57
const queryString = "insert into marketOrders(symbol,direction,type,size,time) values(?,?,?,?,?)"
@@ -72,7 +72,7 @@ if(symbol !== "BTC/USD"){
72
72
let index = 1 ;
73
73
let askPriceSum = updateAsksArr [ 0 ] . price ;
74
74
const time = Date . now ( )
75
- // const avgPrice = askPriceSum / index
75
+
76
76
77
77
for ( let i = 1 ; i < updateAsksArr . length ; i ++ ) {
78
78
@@ -99,112 +99,203 @@ if(symbol !== "BTC/USD"){
99
99
const queryStringTrades = "insert into trades(symbol,direction,price,size,time) values(?,?,?,?,?);"
100
100
101
101
const deleteRows = await db . query ( 'DELETE FROM askLimitOrders ORDER BY price ASC,time ASC LIMIT ?;' , index ) ;
102
- const tradesInsert = await db . query ( queryStringTrades , [ symbol , direction , avgPrice , limitSize , time ] ) ;
103
-
104
- } else {
105
-
106
- const rows = index - 1
107
- console . log ( rows ) ;
108
- const deleteRows = await db . query ( 'DELETE FROM askLimitOrders ORDER BY price ASC,time ASC LIMIT ?;' , rows ) ;
109
-
110
102
if ( deleteRows [ 0 ] . affectedRows !== 0 ) {
111
- const queryStringTrades = "insert into trades(symbol,direction,price,size,time) values(?,?,?,?,?);"
112
103
104
+ const tradesInsert = await db . query ( queryStringTrades , [ symbol , direction , avgPrice , limitSize , time ] ) ;
105
+
106
+ //Deleting market order from the database after the trade execution
107
+
108
+ const deleteRows = await db . query ( 'DELETE FROM marketOrders ORDER BY time ASC LIMIT ?;' , 1 ) ;
109
+ res . status ( 200 ) . json ( { message : "Market Order Partially Filled Successfully" , code : 200 } )
110
+
111
+ }
112
+
113
+ } else if ( limitSize > marketBuyArr [ 0 ] . size ) {
114
+ if ( index === 1 ) {
115
+ const queryStringTrades = "insert into trades(symbol,direction,price,size,time) values(?,?,?,?,?);"
116
+
113
117
const updateRow = await db . query ( 'UPDATE askLimitOrders SET size = ? ORDER BY price ASC,time ASC LIMIT ?' , [ sub , 1 ] ) ;
114
- const tradesInsert = await db . query ( queryStringTrades , [ symbol , direction , avgPrice , size , time ] ) ;
115
118
116
- console . log ( deleteRows [ 0 ] )
119
+
120
+ if ( updateRow [ 0 ] . changedRows !== 0 ) {
121
+ const tradesInsert = await db . query ( queryStringTrades , [ symbol , direction , avgPrice , limitSize , time ] ) ;
122
+
123
+ //Deleting market order from the database after the trade execution
124
+
125
+ const deleteRows = await db . query ( 'DELETE FROM marketOrders ORDER BY time ASC LIMIT ?;' , 1 ) ;
126
+ res . status ( 200 ) . json ( { message : "Market Order Filled Successfully" , code : 200 } )
127
+
128
+ }
129
+ } else {
130
+ const rows = index - 1
131
+
132
+ const deleteRows = await db . query ( 'DELETE FROM askLimitOrders ORDER BY price ASC,time ASC LIMIT ?;' , rows ) ;
133
+
134
+ if ( deleteRows [ 0 ] . affectedRows !== 0 ) {
135
+ const queryStringTrades = "insert into trades(symbol,direction,price,size,time) values(?,?,?,?,?);"
136
+
137
+ const updateRow = await db . query ( 'UPDATE askLimitOrders SET size = ? ORDER BY price ASC,time ASC LIMIT ?' , [ sub , 1 ] ) ;
138
+ const tradesInsert = await db . query ( queryStringTrades , [ symbol , direction , avgPrice , size , time ] ) ;
139
+
140
+
141
+
142
+ if ( updateRow [ 0 ] . changedRows !== 0 ) {
143
+ //Deleting market order from the database after the trade execution
144
+
145
+ const deleteRows = await db . query ( 'DELETE FROM marketOrders ORDER BY time ASC LIMIT ?;' , 1 ) ;
146
+ res . status ( 200 ) . json ( { message : "Market Order Filled Successfully" , code : 200 } )
147
+
148
+ }
149
+
150
+ }
151
+
117
152
118
153
}
119
154
120
- console . log ( index ) ;
121
155
}
156
+
157
+
158
+
122
159
123
160
} else if ( sub === 0 ) {
124
161
const deleteRows = await db . query ( 'DELETE FROM askLimitOrders ORDER BY price ASC,time ASC LIMIT ?;' , index ) ;
125
162
126
-
127
163
const queryStringTrades = "insert into trades(symbol,direction,price,size,time) values(?,?,?,?,?);"
128
- const tradesInsert = await db . query ( queryStringTrades , [ symbol , direction , avgPrice , size , time ] )
129
- console . log ( deleteRows [ 0 ] ) ;
164
+
165
+
166
+ if ( deleteRows [ 0 ] . affectedRows !== 0 ) {
167
+ const tradesInsert = await db . query ( queryStringTrades , [ symbol , direction , avgPrice , size , time ] )
168
+
169
+ //Deleting market order from the database after the trade execution
170
+
171
+ const deleteRows = await db . query ( 'DELETE FROM marketOrders ORDER BY time ASC LIMIT ?;' , 1 ) ;
172
+ res . status ( 200 ) . json ( { message : "Market Order Filled Successfully" , code : 200 } )
130
173
174
+ }
131
175
}
132
- // if (limitSize > marketBuyArr[0].size){
133
- // const queryStringTrades = "insert into trades(symbol,direction,price,size,time) values(?,?,?,?,?);"
176
+
177
+ } else if ( direction === "sell" ) {
178
+ const marketSell = await db . query ( 'SELECT * FROM marketOrders WHERE direction = ? ORDER BY time ASC;' , "sell" )
179
+ const updateBids = await db . query ( 'select size,price from bidLimitOrders ORDER BY price DESC,time ASC;' )
180
+ const marketSellArr = marketSell [ 0 ] ;
181
+ const updateBidsArr = updateBids [ 0 ] ;
134
182
135
- // const updateRow = await db.query('UPDATE askLimitOrders SET size = ? ORDER BY price ASC,time ASC LIMIT ?',[sub,1]);
136
- // const tradesInsert = await db.query(queryStringTrades,[symbol,direction,avgPrice,size,time]);
137
-
138
- // console.log(updateRow[0]);
183
+ let limitSize = updateBidsArr [ 0 ] . size ;
184
+ let index = 1 ;
185
+ let bidPriceSum = updateBidsArr [ 0 ] . price ;
186
+ const time = Date . now ( )
187
+
188
+
189
+ for ( let i = 1 ; i < updateBidsArr . length ; i ++ ) {
190
+
191
+ if ( marketSellArr [ 0 ] . size > limitSize ) {
192
+ limitSize += updateBidsArr [ i ] . size
193
+ index += 1
194
+ bidPriceSum += updateBidsArr [ i ] . price
195
+
196
+
197
+ } else {
198
+
199
+ break ;
139
200
140
- // }else if(sub === 0){
201
+ }
202
+ }
203
+
204
+ const sub = limitSize - marketSellArr [ 0 ] . size ;
205
+ const avgPrice = bidPriceSum / index
206
+
207
+ if ( sub !== 0 ) {
208
+
209
+ if ( marketSellArr [ 0 ] . size > limitSize ) {
210
+ const queryStringTrades = "insert into trades(symbol,direction,price,size,time) values(?,?,?,?,?);"
211
+
212
+ const deleteRows = await db . query ( 'DELETE FROM bidLimitOrders ORDER BY price DESC,time ASC LIMIT ?;' , index ) ;
213
+ if ( deleteRows [ 0 ] . affectedRows !== 0 ) {
214
+
215
+ const tradesInsert = await db . query ( queryStringTrades , [ symbol , direction , avgPrice , limitSize , time ] ) ;
141
216
142
- // const deleteRows = await db.query('DELETE FROM askLimitOrders ORDER BY price ASC,time ASC LIMIT ?;',index);
217
+ //Deleting market order from the database after the trade execution
143
218
219
+ const deleteRows = await db . query ( 'DELETE FROM marketOrders ORDER BY time ASC LIMIT ?;' , 1 ) ;
220
+ res . status ( 200 ) . json ( { message : "Market Order Partially Filled Successfully" , code : 200 } )
144
221
145
- // const queryStringTrades = "insert into trades(symbol,direction,price,size,time) values(?,?,?,?,?);"
146
- // const tradesInsert = await db.query(queryStringTrades,[symbol,direction,avgPrice,size,time])
147
-
222
+ }
148
223
149
- // }else if(sub !== 0){
150
- // const rows = index - 1
151
- // console.log(rows);
152
- // const deleteRows = await db.query('DELETE FROM askLimitOrders ORDER BY price ASC,time ASC LIMIT ?;',rows);
224
+ } else if ( limitSize > marketSellArr [ 0 ] . size ) {
225
+ if ( index === 1 ) {
226
+ const queryStringTrades = "insert into trades(symbol,direction,price,size,time) values(?,?,?,?,?);"
227
+
228
+ const updateRow = await db . query ( 'UPDATE bidLimitOrders SET size = ? ORDER BY price DESC,time ASC LIMIT ?' , [ sub , 1 ] ) ;
229
+
153
230
154
- // if(deleteRows[0].affectedRows !== 0){
155
- // const queryStringTrades = "insert into trades(symbol,direction,price,size,time) values(?,?,?,?,?);"
231
+ if ( updateRow [ 0 ] . changedRows !== 0 ) {
232
+ const tradesInsert = await db . query ( queryStringTrades , [ symbol , direction , avgPrice , limitSize , time ] ) ;
233
+
234
+ //Deleting market order from the database after the trade execution
235
+
236
+ const deleteRows = await db . query ( 'DELETE FROM marketOrders ORDER BY time ASC LIMIT ?;' , 1 ) ;
237
+ res . status ( 200 ) . json ( { message : "Market Order Filled Successfully" , code : 200 } )
238
+
239
+ }
240
+ } else {
241
+ const rows = index - 1
242
+
243
+ const deleteRows = await db . query ( 'DELETE FROM bidLimitOrders ORDER BY price DESC,time ASC LIMIT ?;' , rows ) ;
244
+
245
+ if ( deleteRows [ 0 ] . affectedRows !== 0 ) {
246
+ const queryStringTrades = "insert into trades(symbol,direction,price,size,time) values(?,?,?,?,?);"
247
+
248
+ const updateRow = await db . query ( 'UPDATE bidLimitOrders SET size = ? ORDER BY price DESC,time ASC LIMIT ?' , [ sub , 1 ] ) ;
249
+ const tradesInsert = await db . query ( queryStringTrades , [ symbol , direction , avgPrice , size , time ] ) ;
156
250
157
- // const updateRow = await db.query('UPDATE askLimitOrders SET size = ? ORDER BY price ASC,time ASC LIMIT ?',[sub,1]);
158
- // const tradesInsert = await db.query(queryStringTrades,[symbol,direction,avgPrice,size,time]);
159
251
160
- // console.log(deleteRows[0])
252
+
253
+ if ( updateRow [ 0 ] . changedRows !== 0 ) {
254
+ //Deleting market order from the database after the trade execution
255
+
256
+ const deleteRows = await db . query ( 'DELETE FROM marketOrders ORDER BY time ASC LIMIT ?;' , 1 ) ;
257
+ res . status ( 200 ) . json ( { message : "Market Order Filled Successfully" , code : 200 } )
258
+
259
+ }
161
260
162
- // }
261
+ }
163
262
164
- // console.log(index);
165
-
166
- // }
167
-
263
+
264
+ }
265
+
266
+ }
267
+
268
+
269
+
270
+
271
+ } else if ( sub === 0 ) {
272
+ const deleteRows = await db . query ( 'DELETE FROM bidLimitOrders ORDER BY price DESC,time ASC LIMIT ?;' , index ) ;
168
273
274
+ const queryStringTrades = "insert into trades(symbol,direction,price,size,time) values(?,?,?,?,?);"
275
+
276
+
277
+ if ( deleteRows [ 0 ] . affectedRows !== 0 ) {
278
+ const tradesInsert = await db . query ( queryStringTrades , [ symbol , direction , avgPrice , size , time ] )
279
+
280
+ //Deleting market order from the database after the trade execution
281
+
282
+ const deleteRows = await db . query ( 'DELETE FROM marketOrders ORDER BY time ASC LIMIT ?;' , 1 ) ;
283
+ res . status ( 200 ) . json ( { message : "Market Order Filled Successfully" , code : 200 } )
284
+
285
+ }
169
286
}
170
287
171
- //sending response to the client
288
+
172
289
173
- if ( insertTradeMarketArr . affectedRows !== 0 ) {
174
- res . sendStatus ( 201 )
290
+
291
+
175
292
}
176
- }
177
293
178
294
179
- } ) ;
295
+ }
180
296
181
- // const size = [10,5,10,100]
182
- // const marketSize = [20,1,3,100]
183
- // let limitSize = size[0];
184
- // let index = 0
185
- // let row;
186
- // for(let i = 1; i < 3 ; i++){
187
-
188
- // if(marketSize[0] > limitSize){
189
- // limitSize += size[i]
190
- // index += i
191
-
192
- // }else{
193
- // console.log('breaked');
194
- // break;
195
-
196
- // }
197
-
198
-
199
- // }
200
297
201
- // const sub = limitSize - marketSize[0]
202
- // if(sub === 0){
203
- // console.log(`delelte db rows ${index}`);
204
- // }else if(sub !== 0){
205
- // row = index - 1
206
- // console.log(`delete rows ${row} & update row ${0} with value ${sub}`)
207
- // }
298
+ } ) ;
208
299
209
300
210
301
export default router ;
@@ -213,4 +304,3 @@ export default router;
213
304
214
305
215
306
216
-
0 commit comments