Skip to content

Commit 42a10ca

Browse files
committed
buy and sell market orders supported
1 parent 2f38463 commit 42a10ca

File tree

2 files changed

+168
-76
lines changed

2 files changed

+168
-76
lines changed

Mysql.session.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ CREATE TABLE trades(
1414

1515
ALTER TABLE marketOrders
1616
ALTER direction CHECK (direction = "buy" OR "sell") ;
17+
18+

routes/orders.js

Lines changed: 166 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ if(symbol !== "BTC/USD"){
3737
const insertTrade = await db.query(queryString,[symbol,type,price,size,time])
3838
const insertTradeArr = insertTrade[0]
3939
if(insertTradeArr.affectedRows !== 0){
40-
res.sendStatus(201)
40+
res.status(201).json({message : "Limit Order Posted Successfully",code : 201})
4141
}
4242
}else if(direction === "sell" && type === "limit"){
4343

@@ -47,11 +47,11 @@ if(symbol !== "BTC/USD"){
4747
const insertTrade = await db.query(queryString,[symbol,type,price,size,time])
4848
const insertTradeArr = insertTrade[0]
4949
if(insertTradeArr.affectedRows !== 0){
50-
res.sendStatus(201)
50+
res.status(201).json({message : "Limit Order Posted Successfully",code : 201})
5151
}
5252
}else if(type === "market"){
5353

54-
//inserting market order buy into the database
54+
//inserting market order into the database
5555

5656

5757
const queryString = "insert into marketOrders(symbol,direction,type,size,time) values(?,?,?,?,?)"
@@ -72,7 +72,7 @@ if(symbol !== "BTC/USD"){
7272
let index = 1;
7373
let askPriceSum = updateAsksArr[0].price;
7474
const time = Date.now()
75-
// const avgPrice = askPriceSum / index
75+
7676

7777
for(let i = 1; i < updateAsksArr.length ; i++){
7878

@@ -99,112 +99,203 @@ if(symbol !== "BTC/USD"){
9999
const queryStringTrades = "insert into trades(symbol,direction,price,size,time) values(?,?,?,?,?);"
100100

101101
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-
110102
if(deleteRows[0].affectedRows !== 0){
111-
const queryStringTrades = "insert into trades(symbol,direction,price,size,time) values(?,?,?,?,?);"
112103

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+
113117
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]);
115118

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+
117152

118153
}
119154

120-
console.log(index);
121155
}
156+
157+
158+
122159

123160
}else if(sub === 0){
124161
const deleteRows = await db.query('DELETE FROM askLimitOrders ORDER BY price ASC,time ASC LIMIT ?;',index);
125162

126-
127163
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})
130173

174+
}
131175
}
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];
134182

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;
139200

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]);
141216

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
143218

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})
144221

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+
}
148223

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+
153230

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]);
156250

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]);
159251

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+
}
161260

162-
// }
261+
}
163262

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);
168273

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+
}
169286
}
170287

171-
//sending response to the client
288+
172289

173-
if(insertTradeMarketArr.affectedRows !== 0){
174-
res.sendStatus(201)
290+
291+
175292
}
176-
}
177293

178294

179-
});
295+
}
180296

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-
// }
200297

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+
});
208299

209300

210301
export default router;
@@ -213,4 +304,3 @@ export default router;
213304

214305

215306

216-

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy