-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductBook.java
More file actions
101 lines (91 loc) · 3.8 KB
/
Copy pathProductBook.java
File metadata and controls
101 lines (91 loc) · 3.8 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
package book;
import market.*;
import price.*;
import price.exceptions.InvalidPriceOperation;
import user.*;
import tradable.*;
public class ProductBook {
private String product;
private ProductBookSide buySide;
private ProductBookSide sellSide;
public ProductBook(String product) throws InvalidInput {
setProduct(product);
buySide = new ProductBookSide(BookSide.BUY);
sellSide = new ProductBookSide(BookSide.SELL);
}
private void setProduct(String product) throws InvalidInput {
if(product.length() < 1 || product.length() > 5) {
throw new InvalidInput("Invalid product length");
}
for(char c: product.toCharArray()) {
if(!Character.isLetter(c) || c == '.') {
throw new InvalidInput("Invalid product character");
}
}
this.product = product;
}
public TradableDTO add(Tradable t) throws user.DataValidationException, InvalidPriceOperation {
TradableDTO result = null;
if(t.getSide() == BookSide.BUY){
result = buySide.add(t);
}else if(t.getSide() == BookSide.SELL){
result = sellSide.add(t);
}
tryTrade();
updateMarket();
return result;
}
public TradableDTO[] add(Quote qte) throws user.DataValidationException {
TradableDTO buyDTO = buySide.add(qte.getQuoteSide(BookSide.BUY));
TradableDTO sellDTO = sellSide.add(qte.getQuoteSide(BookSide.SELL));
tryTrade();
return new TradableDTO[]{buyDTO,sellDTO};
}
public TradableDTO cancel(BookSide side, String orderId) throws user.DataValidationException, InvalidPriceOperation {
TradableDTO result = null;
if(side == BookSide.BUY){
result = buySide.cancel(orderId);
updateMarket();
}else if(side == BookSide.SELL){
result = sellSide.cancel(orderId);
updateMarket();
}
return result;
}
public void tryTrade() throws user.DataValidationException {
Price topBuy = buySide.topOfBookPrice();
Price topSell = sellSide.topOfBookPrice();
while(topBuy != null && topSell != null && topBuy.compareTo(topSell) >= 0){
int topBuyVol = buySide.topOfBookVolume();
int topSellVol = sellSide.topOfBookVolume();
int tradingVol = Math.min(topBuyVol, topSellVol);
sellSide.tradeOut(topSell, tradingVol);
buySide.tradeOut(topBuy, tradingVol);
topBuy = buySide.topOfBookPrice();
topSell = sellSide.topOfBookPrice();
}
}
public TradableDTO[] removeQuotesForUser(String userName) throws user.DataValidationException, InvalidPriceOperation {
TradableDTO buyDTO = buySide.removeQuotesForUser(userName);
TradableDTO sellDTO = sellSide.removeQuotesForUser(userName);
UserManager.getInstance().getUser(userName).addTradable(buyDTO);
UserManager.getInstance().getUser(userName).addTradable(sellDTO);
updateMarket();
return new TradableDTO[]{buyDTO,sellDTO};
}
public String toString(){
String result = "--------------------------------------------\n"
+ "Product Book: " + product + "\n"
+ buySide.toString()
+ sellSide.toString()
+ "--------------------------------------------";
return result;
}
private void updateMarket() throws InvalidPriceOperation {
Price topBuyPrice = buySide.topOfBookPrice();
Price topSellPrice = sellSide.topOfBookPrice();
int topBuyVol = buySide.topOfBookVolume();
int topSellVol = sellSide.topOfBookVolume();
CurrentMarketTracker.getInstance().updateMarket(product, topBuyPrice, topBuyVol, topSellPrice, topSellVol);
}
}