-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTradingSim.java
More file actions
181 lines (137 loc) · 7.05 KB
/
TradingSim.java
File metadata and controls
181 lines (137 loc) · 7.05 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
package sim;
import book.BookSide;
import book.ProductManager;
import exceptions.DataValidationException;
import gui.Gui;
import price.Price;
import price.PriceFactory;
import market.CurrentMarketPublisher;
import tradable.InvalidInput;
import tradable.Order;
import tradable.TradableDTO;
import user.User;
import user.UserManager;
import java.util.HashMap;
import static book.BookSide.BUY;
import static book.BookSide.SELL;
public class TradingSim {
public static boolean OUTPUT = true;
public static final HashMap<String, Double> basePrices = new HashMap<>();
public static void runSim() throws Exception {
UserManager.getInstance().init(
new String[]{"ANN", "BOB", "CAT", "DOG", "EGG"});
User u1 = UserManager.getInstance().getUser("ANN");
User u2 = UserManager.getInstance().getUser("BOB");
User u3 = UserManager.getInstance().getUser("CAT");
User u4 = UserManager.getInstance().getUser("DOG");
User u5 = UserManager.getInstance().getUser("EGG");
CurrentMarketPublisher.getInstance().
subscribeCurrentMarket("WMT", UserManager.getInstance().getUser("ANN"));
CurrentMarketPublisher.getInstance().
subscribeCurrentMarket("TGT", UserManager.getInstance().getUser("ANN"));
CurrentMarketPublisher.getInstance().
subscribeCurrentMarket("TGT", UserManager.getInstance().getUser("BOB"));
CurrentMarketPublisher.getInstance().
subscribeCurrentMarket("TSLA", UserManager.getInstance().getUser("BOB"));
CurrentMarketPublisher.getInstance().
subscribeCurrentMarket("AMZN", UserManager.getInstance().getUser("CAT"));
CurrentMarketPublisher.getInstance().
subscribeCurrentMarket("TGT", UserManager.getInstance().getUser("CAT"));
CurrentMarketPublisher.getInstance().
subscribeCurrentMarket("WMT", UserManager.getInstance().getUser("CAT"));
CurrentMarketPublisher.getInstance().
subscribeCurrentMarket("TSLA", UserManager.getInstance().getUser("DOG"));
CurrentMarketPublisher.getInstance().
subscribeCurrentMarket("WMT", UserManager.getInstance().getUser("EGG"));
ProductManager.getInstance().addProduct("WMT");
ProductManager.getInstance().addProduct("TGT");
ProductManager.getInstance().addProduct("AMZN");
ProductManager.getInstance().addProduct("TSLA");
ProductManager.getInstance().addProduct("AAPL");
ProductManager.getInstance().addProduct("MSFT");
ProductManager.getInstance().addProduct("PYPL");
Gui gui = new Gui();
CurrentMarketPublisher.getInstance().subscribeCurrentMarket("AAPL", gui);
CurrentMarketPublisher.getInstance().subscribeCurrentMarket("AMZN", gui);
CurrentMarketPublisher.getInstance().subscribeCurrentMarket("TGT", gui);
CurrentMarketPublisher.getInstance().subscribeCurrentMarket("WMT", gui);
CurrentMarketPublisher.getInstance().subscribeCurrentMarket("TSLA", gui);
CurrentMarketPublisher.getInstance().subscribeCurrentMarket("MSFT", gui);
CurrentMarketPublisher.getInstance().subscribeCurrentMarket("PYPL", gui);
basePrices.put("WMT", 60.17);
basePrices.put("TGT", 177.21);
basePrices.put("AMZN", 180.38);
basePrices.put("TSLA", 175.79);
basePrices.put("AAPL", 171.48);
basePrices.put("MSFT", 420.72);
basePrices.put("PYPL", 66.99);
CurrentMarketPublisher.getInstance().unSubscribeCurrentMarket("TGT", u2);
long start = System.currentTimeMillis();
System.out.println("START");
for (int i = 0; i < 150; i++) {
Thread.sleep(100);
if (i % 1_000 == 0)
System.out.println(i);
User u = UserManager.getInstance().getRandomUser();
if (Math.random() < 0.9) {
String p = ProductManager.getInstance().getRandomProduct();
BookSide side = (Math.random() < 0.5 ? BUY : SELL);
int qty = (int) (25 + (Math.random() * 300));
int v = (int) Math.round(qty / 5.0) * 5;
Price price = getPrice(p, side);
try {
Order order = new Order(u.getUserId(), p, price, v, side);
TradableDTO orderDTO = ProductManager.getInstance().addTradable(order);
u.addTradable(orderDTO);
} catch (InvalidInput e) {
throw new RuntimeException(e);
}
} else {
if (u.hasTradableWithRemainingQty()) {
TradableDTO odto = u.getTradableWithRemainingQty();
TradableDTO orderDTO = ProductManager.getInstance().cancel(odto);
if (orderDTO != null)
u.addTradable(orderDTO);
else
if (OUTPUT)
System.out.println("*** " + odto);
}
}
}
long end = System.currentTimeMillis();
System.out.println(u1.getUserId() + ":\n" + u1.getCurrentMarkets());
System.out.println(u2.getUserId() + ":\n" + u2.getCurrentMarkets());
System.out.println(u3.getUserId() + ":\n" + u3.getCurrentMarkets());
System.out.println(u4.getUserId() + ":\n" + u4.getCurrentMarkets());
System.out.println(u5.getUserId() + ":\n" + u5.getCurrentMarkets());
CurrentMarketPublisher.getInstance().unSubscribeCurrentMarket("WMT", u1);
CurrentMarketPublisher.getInstance().unSubscribeCurrentMarket("TGT", u1);
CurrentMarketPublisher.getInstance().unSubscribeCurrentMarket("TSLA", u2);
CurrentMarketPublisher.getInstance().unSubscribeCurrentMarket("AMZN", u3);
CurrentMarketPublisher.getInstance().unSubscribeCurrentMarket("TGT", u3);
CurrentMarketPublisher.getInstance().unSubscribeCurrentMarket("WMT", u3);
CurrentMarketPublisher.getInstance().unSubscribeCurrentMarket("TSLA", u4);
CurrentMarketPublisher.getInstance().unSubscribeCurrentMarket("WMT", u5);
System.out.println((end - start) + " millis");
gui.shutdown();
}
private static Price getPrice(String symbol, BookSide side) {
Double basePrice = basePrices.get(symbol); //100.00
double priceWidth = 0.02;
double startPoint = 0.01;
double tickSize = 0.1;
double gapFromBase = basePrice * priceWidth; // 5.0
double priceVariance = gapFromBase * (Math.random());
if (side == BUY) {
double priceToUse = basePrice * (1 - startPoint);
priceToUse += priceVariance;
double priceToTick = Math.round(priceToUse * 1/tickSize) / 10.0;
return PriceFactory.makePrice((int) (priceToTick * 100));
} else {
double priceToUse = basePrice * (1 + startPoint);
priceToUse -= priceVariance;
double priceToTick = Math.round(priceToUse * 1/tickSize) / 10.0;
return PriceFactory.makePrice((int) (priceToTick * 100));
}
}
}