-
-
Notifications
You must be signed in to change notification settings - Fork 140
/
Order.java
335 lines (265 loc) · 7.19 KB
/
Order.java
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package com.univocity.trader.account;
import com.univocity.trader.indicators.base.*;
import java.util.*;
import static com.univocity.trader.account.Balance.*;
import static com.univocity.trader.account.Order.Status.*;
import static com.univocity.trader.account.Order.TriggerCondition.*;
import static com.univocity.trader.candles.Candle.*;
public class Order extends OrderRequest implements Comparable<Order> {
private final long id;
private String orderId;
private double executedQuantity = 0.0;
private Status status;
private double feesPaid = 0.0;
private double averagePrice = 0.0;
private List<Order> attachments;
private Order parent;
private double partialFillPrice = 0.0;
private double partialFillQuantity = 0.0;
private Trade trade;
public boolean processed = false;
public Order(long id, String assetSymbol, String fundSymbol, Side side, Trade.Side tradeSide, long time) {
super(assetSymbol, fundSymbol, side, tradeSide, time, null);
this.id = id;
this.orderId = String.valueOf(id);
}
public Order(long id, OrderRequest request) {
this(id, request.getAssetsSymbol(), request.getFundsSymbol(), request.getSide(), request.getTradeSide(), request.getTime());
}
public void setParent(Order parent) {
this.parent = parent;
if (parent.attachments == null) {
parent.attachments = new ArrayList<>();
}
parent.attachments.add(this);
}
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public double getExecutedQuantity() {
return executedQuantity;
}
public double getTotalOrderAmountAtAveragePrice() {
if (averagePrice == 0) {
return getPrice() * getQuantity();
}
return averagePrice * getQuantity();
}
public void setExecutedQuantity(double executedQuantity) {
this.executedQuantity = executedQuantity;
}
@Override
public void setPrice(double price) {
super.setPrice(price);
}
@Override
public void setQuantity(double quantity) {
super.setQuantity(quantity);
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public void cancel() {
if (this.status != Status.FILLED) {
this.status = Status.CANCELLED;
}
}
public boolean isCancelled() {
return this.status == Status.CANCELLED;
}
public double getFeesPaid() {
return feesPaid;
}
public void setFeesPaid(double feesPaid) {
this.feesPaid = feesPaid;
}
@Override
public String toString() {
return print(0);
}
public void setAveragePrice(double averagePrice) {
this.averagePrice = averagePrice;
}
public final double getAveragePrice() {
return averagePrice;
}
public final List<Order> getAttachments() {
return attachments == null ? null : Collections.unmodifiableList(attachments);
}
public final Order getParent() {
return parent;
}
public final String getParentOrderId() {
return parent == null ? "" : parent.getOrderId();
}
public double getQuantity() { //TODO: check this implementation in live trading.
double out = super.getQuantity();
if (parent != null && parent.isFinalized()) {
double p = parent.getExecutedQuantity();
if (out > p || p == 0) {
return p;
}
}
return out;
}
public boolean hasPartialFillDetails() {
return partialFillQuantity != 0.0 && partialFillQuantity > 0;
}
public void clearPartialFillDetails() {
partialFillQuantity = 0.0;
partialFillPrice = 0.0;
}
public double getPartialFillTotalPrice() {
return partialFillQuantity * partialFillPrice;
}
public double getPartialFillPrice() {
return partialFillPrice;
}
public double getPartialFillQuantity() {
return partialFillQuantity;
}
public void setPartialFillDetails(double filledQuantity, double fillPrice) {
this.partialFillPrice = fillPrice;
this.partialFillQuantity = filledQuantity;
}
public long getInternalId() {
return id;
}
@Override
public int compareTo(Order o) {
return Long.compare(this.id, o.id);
}
public Trade getTrade() {
return trade;
}
public void setTrade(Trade trade) {
this.trade = trade;
}
public long getTimeElapsed() {
return System.currentTimeMillis() - getTime();
}
public long getTimeElapsed(long latestClose) {
return latestClose - getTime();
}
public String getFormattedTimeElapsed() {
return TimeInterval.getFormattedDuration(getTimeElapsed());
}
public String getFormattedTimeElapsed(long latestClose) {
return TimeInterval.getFormattedDuration(getTimeElapsed(latestClose));
}
public double getRemainingQuantity() {
return getQuantity() - getExecutedQuantity();
}
public double getTotalTraded() {
return getExecutedQuantity() * getAveragePrice();
}
public boolean isFinalized() {
return getStatus() == FILLED || getStatus() == Status.CANCELLED;
}
public String print(long latestClose) {
StringBuilder description = new StringBuilder();
description.append(getStatus()).append(' ');
if (getTriggerCondition() != NONE) {
description
.append(getTriggerCondition())
.append("[")
.append(roundStr(getTriggerPrice()))
.append(']').append(' ');
}
description.append(getType()).append(' ');
if (isShort()) {
description.append(getTradeSide()).append(' ');
}
description
.append(getSide()).append(' ')
.append(roundStr(getQuantity())).append(' ')
.append(getAssetsSymbol());
if (getType() == Type.LIMIT) {
description
.append(" @ ")
.append(roundStr(getPrice())).append(' ');
description
.append(" (Total: ")
.append(roundStr(getTotalOrderAmount()))
.append(')');
description.append(' ').append(getFundsSymbol());
} else if (getType() == Type.MARKET) {
description.append(getFundsSymbol());
}
if (getType() == Type.MARKET) {
description
.append(" @ ")
.append(roundStr(getPrice())).append(' ')
.append(getFundsSymbol());
}
if (getStatus() == Status.PARTIALLY_FILLED || isFinalized() && getExecutedQuantity() > 0) {
description
.append(" - filled: ")
.append(roundStr(getExecutedQuantity()));
description
.append(", worth ~")
.append(roundStr(getTotalTraded())).append(' ')
.append(getFundsSymbol());
}
if (latestClose > 0) {
description
.append(". Open for ")
.append(TimeInterval.getFormattedDuration(getTimeElapsed(latestClose)));
}
description.append('.');
return description.toString();
}
public String getFormattedFillPct() {
String out = CHANGE_FORMAT.get().format(getFillPct() / 100.0);
//adjust display of mostly filled order so it's less confusing.
if (getStatus() != FILLED && out.equals("100.00%")) {
return "99.99%";
}
return out;
}
public double getFillPct() {
if (getQuantity() == 0.0) {
return 0.0;
}
return getExecutedQuantity() / getQuantity() * 100.0;
}
public String sideDescription() {
if (isShort()) {
if (isSell()) {
return "SHORT";
} else if (isBuy()) {
return "COVER";
}
}
return getSide().toString();
}
public enum Side {
BUY,
SELL
}
public enum Type {
LIMIT,
MARKET
}
public enum TriggerCondition {
NONE(""),
STOP_LOSS("SL"),
STOP_GAIN("SG");
public final String shortName;
TriggerCondition(String shortName) {
this.shortName = shortName;
}
}
public enum Status {
NEW,
PARTIALLY_FILLED,
FILLED,
CANCELLED
}
}