Removed Order Id
This commit is contained in:
@ -31,9 +31,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
* Implements a trading account
|
||||
*/
|
||||
public class Account implements Comparable {
|
||||
|
||||
static String default_symbold = "DFLT";
|
||||
|
||||
|
||||
private Exchange.AccountListener listener = null;
|
||||
|
||||
protected final double id;
|
||||
|
@ -640,42 +640,9 @@ public class Exchange {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param stock
|
||||
* @param type
|
||||
* @param depth
|
||||
* @return
|
||||
*/
|
||||
public ArrayList<Order> getOrderBook(Stock stock, OrderType type, int depth) {
|
||||
|
||||
SortedSet<Order> book = stock.order_books.get(type);
|
||||
if (book == null) {
|
||||
return null;
|
||||
}
|
||||
ArrayList<Order> ret;
|
||||
synchronized (stock) {
|
||||
|
||||
ret = new ArrayList<>();
|
||||
|
||||
Iterator<Order> it = book.iterator();
|
||||
|
||||
for (int i = 0; i < depth && it.hasNext(); i++) {
|
||||
Order o = it.next();
|
||||
// System.out.print(o.volume);
|
||||
if (o.volume <= 0) {
|
||||
System.out.printf("Volume < 0\n");
|
||||
System.exit(0);
|
||||
}
|
||||
ret.add(o);
|
||||
}
|
||||
// System.out.println();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public ArrayList<Order> getOrderBook(OrderType type, int depth) {
|
||||
return getOrderBook(getDefaultStock(), type, depth);
|
||||
return getDefaultStock().getOrderBook(type, depth);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -740,8 +707,9 @@ public class Exchange {
|
||||
return ret;
|
||||
}
|
||||
|
||||
public boolean cancelOrder(double account_id, long order_id) {
|
||||
return cancelOrder(getDefaultStock(), account_id, order_id);
|
||||
public boolean cancelOrder(double account_id, Order order) {
|
||||
|
||||
return cancelOrder(getDefaultStock(), account_id, order.getID());
|
||||
}
|
||||
|
||||
Random random;
|
||||
@ -972,23 +940,21 @@ public class Exchange {
|
||||
* @param limit
|
||||
* @return order_id
|
||||
*/
|
||||
public long createOrder(double account_id,
|
||||
public Order createOrder(double account_id,
|
||||
String stocksymbol, OrderType type, double volume, double limit) {
|
||||
|
||||
Stock stock = getStock(stocksymbol);
|
||||
|
||||
Account a = accounts.get(account_id);
|
||||
if (a == null) {
|
||||
return -1;
|
||||
return null;
|
||||
}
|
||||
|
||||
Order o = new Order(order_id_generator.getNext(),
|
||||
timer.currentTimeMillis(),
|
||||
a, type, roundShares(volume), roundMoney(limit));
|
||||
a, stock, type, roundShares(volume), roundMoney(limit));
|
||||
|
||||
o.stock = stock;
|
||||
|
||||
if (o.volume <= 0 || o.limit <= 0) {
|
||||
if (o.volume <= 0 || o.limit <= 0) {
|
||||
|
||||
switch (o.type) {
|
||||
case SELL:
|
||||
@ -1001,7 +967,7 @@ public class Exchange {
|
||||
break;
|
||||
}
|
||||
|
||||
return -1;
|
||||
return o;
|
||||
}
|
||||
|
||||
synchronized (stock) {
|
||||
@ -1022,7 +988,7 @@ public class Exchange {
|
||||
// executor.notify();
|
||||
}
|
||||
// a.update(o);
|
||||
return o.getID();
|
||||
return o;
|
||||
}
|
||||
|
||||
public double getBestLimit(Stock stock, OrderType type) {
|
||||
|
@ -45,9 +45,9 @@ public class Order {
|
||||
BUYLIMIT, SELLLIMIT, STOPLOSS, STOPBUY, BUY, SELL
|
||||
}
|
||||
|
||||
Stock stock;
|
||||
OrderStatus status;
|
||||
OrderType type;
|
||||
protected final Stock stock;
|
||||
protected OrderStatus status;
|
||||
protected OrderType type;
|
||||
protected double limit;
|
||||
protected double volume;
|
||||
|
||||
@ -59,8 +59,8 @@ public class Order {
|
||||
|
||||
double cost;
|
||||
|
||||
Order(long id, long created, Account account, OrderType type, double volume, double limit) {
|
||||
//id = order_id_generator.getNext();
|
||||
Order(long id, long created, Account account, Stock stock, OrderType type,
|
||||
double volume, double limit) {
|
||||
this.id = id;
|
||||
this.account = account;
|
||||
this.type = type;
|
||||
@ -69,6 +69,7 @@ public class Order {
|
||||
this.initial_volume = this.volume;
|
||||
this.created = created;
|
||||
this.status = OrderStatus.OPEN;
|
||||
this.stock=stock;
|
||||
this.cost = 0;
|
||||
}
|
||||
|
||||
@ -119,4 +120,13 @@ public class Order {
|
||||
public long getCreated() {
|
||||
return created;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the stock symbol that this order belongs to
|
||||
* @return Stock symbol
|
||||
*/
|
||||
public String getStockSymbol(){
|
||||
return stock.getSymbol();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -25,6 +25,7 @@
|
||||
*/
|
||||
package sesim;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.SortedSet;
|
||||
@ -131,4 +132,38 @@ public class Stock {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param stock
|
||||
* @param type
|
||||
* @param depth
|
||||
* @return
|
||||
*/
|
||||
public ArrayList<Order> getOrderBook(Order.OrderType type, int depth) {
|
||||
|
||||
SortedSet<Order> book = order_books.get(type);
|
||||
if (book == null) {
|
||||
return null;
|
||||
}
|
||||
ArrayList<Order> ret;
|
||||
synchronized (this) {
|
||||
|
||||
ret = new ArrayList<>();
|
||||
|
||||
Iterator<Order> it = book.iterator();
|
||||
|
||||
for (int i = 0; i < depth && it.hasNext(); i++) {
|
||||
Order o = it.next();
|
||||
|
||||
if (o.volume <= 0) {
|
||||
// throw an exception here
|
||||
}
|
||||
ret.add(o);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user