OpenSeSim/src/main/java/sesim/Exchange.java

800 lines
18 KiB
Java
Raw Normal View History

2017-01-05 19:20:44 +01:00
package sesim;
import java.util.*;
2017-01-15 09:28:26 +01:00
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
2017-01-26 01:52:03 +01:00
import org.json.JSONArray;
import org.json.JSONObject;
2017-01-05 19:20:44 +01:00
/**
2017-02-03 18:46:39 +01:00
* @desc Echchange class
* @author 7u83
2017-01-05 19:20:44 +01:00
*/
2017-02-03 18:46:39 +01:00
public class Exchange {
2017-01-05 19:20:44 +01:00
2017-01-31 08:04:11 +01:00
private double money_df = 10000;
2017-02-03 18:46:39 +01:00
/**
* Set the number of decimals used with money
*
* @param n number of decimals
*/
2017-01-31 08:04:11 +01:00
public void setMoneyDecimals(int n) {
money_df = Math.pow(10, n);
}
2017-02-03 18:46:39 +01:00
private double shares_df = 1;
/**
* Set the number of decimals for shares
*
* @param n number of decimals
*/
2017-01-31 08:04:11 +01:00
public void setSharesDecimals(int n) {
shares_df = Math.pow(10, n);
}
2017-02-02 18:39:55 +01:00
public double roundToDecimals(double val, double f) {
return Math.floor(val * f) / f;
2017-01-31 08:04:11 +01:00
}
2017-02-02 18:39:55 +01:00
public double roundShares(double shares) {
return roundToDecimals(shares, shares_df);
2017-01-31 08:04:11 +01:00
}
2017-02-02 18:39:55 +01:00
public double roundMoney(double money) {
return roundToDecimals(money, money_df);
}
2017-01-31 08:04:11 +01:00
2017-02-03 18:46:39 +01:00
/**
* Definition of order types
*/
2017-01-10 07:41:52 +01:00
public enum OrderType {
2017-02-10 08:12:38 +01:00
BUYLIMIT, SELLLIMIT, STOPLOSS, STOPBUY, BUY, SELL
2017-01-10 07:41:52 +01:00
}
IDGenerator account_id = new IDGenerator();
2017-01-16 21:30:10 +01:00
//public static Timer timer = new Timer();
2017-01-31 08:04:11 +01:00
2017-02-04 16:29:45 +01:00
public Scheduler timer; // = new Scheduler();
2017-02-03 18:46:39 +01:00
2017-02-11 08:36:45 +01:00
public ArrayList<AutoTraderInterface> traders;
2017-01-10 07:41:52 +01:00
2017-02-13 00:07:50 +01:00
/**
*
*/
public interface AccountListener {
public void accountUpdated(Account a, Order o);
}
2017-01-12 23:16:37 +01:00
/**
2017-01-22 09:47:51 +01:00
* Implements a trading account
2017-01-12 23:16:37 +01:00
*/
public class Account implements Comparable {
2017-01-09 17:00:05 +01:00
2017-02-13 00:07:50 +01:00
private AccountListener listener = null;
2017-01-22 09:47:51 +01:00
private final double id;
2017-01-14 14:42:00 +01:00
private double shares;
private double money;
2017-02-11 08:36:45 +01:00
protected AutoTraderInterface owner;
2017-01-12 01:44:50 +01:00
2017-01-12 23:16:37 +01:00
private final HashMap<Long, Order> orders;
2017-01-09 17:00:05 +01:00
@Override
public int compareTo(Object a) {
2017-01-10 07:41:52 +01:00
Account account = (Account) a;
2017-01-09 17:00:05 +01:00
return this.id - account.id < 0 ? -1 : 1;
}
2017-01-10 07:41:52 +01:00
Account(double money, double shares) {
2017-02-03 08:47:43 +01:00
id = (random.nextDouble() + (account_id.getNext()));
2017-01-10 07:41:52 +01:00
orders = new HashMap();
this.money = money;
this.shares = shares;
2017-01-09 17:00:05 +01:00
}
2017-01-12 23:16:37 +01:00
public double getID() {
return id;
}
public double getShares() {
return shares;
}
public double getMoney() {
return money;
}
2017-02-09 17:59:20 +01:00
2017-02-11 08:36:45 +01:00
public AutoTraderInterface getOwner() {
2017-02-06 08:14:05 +01:00
return owner;
}
2017-01-12 23:16:37 +01:00
2017-02-13 00:07:50 +01:00
public HashMap<Long, Order> getOrders() {
2017-02-09 17:59:20 +01:00
return orders;
}
2017-02-13 00:07:50 +01:00
public void setListener(AccountListener al) {
this.listener = al;
}
public void update(Order o) {
if (listener == null) {
return;
}
listener.accountUpdated(this, o);
}
2017-01-09 17:00:05 +01:00
}
2017-01-31 08:04:11 +01:00
public void createTraders(JSONArray traderdefs) {
for (int i = 0; i < traderdefs.length(); i++) {
2017-01-26 01:52:03 +01:00
JSONObject o = traderdefs.getJSONObject(i);
2017-01-31 08:04:11 +01:00
2017-01-26 01:52:03 +01:00
}
2017-01-31 08:04:11 +01:00
2017-01-26 01:52:03 +01:00
// this.traders.add(randt);
// randt.setName("Bob");
// randt.start();
}
2017-01-09 17:00:05 +01:00
2017-01-15 09:28:26 +01:00
// private final ConcurrentHashMap<Double, Account> accounts = new ConcurrentHashMap<>();
2017-02-04 16:29:45 +01:00
private ConcurrentHashMap<Double, Account> accounts;
2017-02-04 18:57:18 +01:00
2017-01-10 07:41:52 +01:00
public double createAccount(double money, double shares) {
2017-01-15 09:28:26 +01:00
2017-01-10 07:41:52 +01:00
Account a = new Account(money, shares);
accounts.put(a.id, a);
2017-01-09 17:00:05 +01:00
return a.id;
}
2017-02-13 00:07:50 +01:00
public enum OrderStatus {
OPEN,
PARTIALLY_EXECUTED,
CLOSED,
CANCELED
}
2017-01-10 07:41:52 +01:00
class OrderComparator implements Comparator<Order> {
OrderType type;
OrderComparator(OrderType type) {
this.type = type;
}
@Override
public int compare(Order left, Order right) {
double d;
switch (this.type) {
2017-02-09 17:59:20 +01:00
case BUYLIMIT:
2017-02-13 00:07:50 +01:00
case STOPBUY:
case BUY:
2017-01-10 07:41:52 +01:00
d = right.limit - left.limit;
break;
2017-02-09 17:59:20 +01:00
case SELLLIMIT:
2017-02-13 00:07:50 +01:00
case STOPLOSS:
2017-02-10 08:12:38 +01:00
case SELL:
2017-01-10 07:41:52 +01:00
d = left.limit - right.limit;
break;
default:
d = 0;
}
if (d != 0) {
return d > 0 ? 1 : -1;
}
2017-02-02 18:39:55 +01:00
d = right.initial_volume - left.initial_volume;
if (d != 0) {
2017-01-31 08:04:11 +01:00
return d > 0 ? 1 : -1;
}
2017-01-12 23:16:37 +01:00
if (left.id < right.id) {
2017-01-12 01:44:50 +01:00
return -1;
2017-01-12 23:16:37 +01:00
}
if (left.id > right.id) {
2017-01-12 01:44:50 +01:00
return 1;
2017-01-12 23:16:37 +01:00
}
2017-01-12 01:44:50 +01:00
return 0;
2017-01-10 07:41:52 +01:00
}
}
2017-02-04 16:29:45 +01:00
HashMap<OrderType, SortedSet<Order>> order_books;
2017-02-04 18:57:18 +01:00
2017-01-10 07:41:52 +01:00
IDGenerator order_id = new IDGenerator();
2017-01-12 23:16:37 +01:00
public class Order {
2017-01-10 07:41:52 +01:00
2017-02-13 00:07:50 +01:00
OrderStatus status;
2017-01-10 07:41:52 +01:00
OrderType type;
2017-01-12 23:16:37 +01:00
private double limit;
private double volume;
private final double initial_volume;
private long id;
2017-01-10 07:41:52 +01:00
long created;
2017-02-09 17:59:20 +01:00
private final Account account;
2017-01-10 07:41:52 +01:00
Order(Account account, OrderType type, double volume, double limit) {
2017-01-10 07:41:52 +01:00
id = order_id.getNext();
this.account = account;
2017-01-10 07:41:52 +01:00
this.type = type;
2017-01-31 08:04:11 +01:00
this.limit = roundMoney(limit);
this.volume = roundShares(volume);
this.initial_volume = this.volume;
2017-02-03 01:56:41 +01:00
this.created = timer.currentTimeMillis();
2017-02-13 00:07:50 +01:00
this.status=OrderStatus.OPEN;
2017-01-10 07:41:52 +01:00
}
2017-01-12 23:16:37 +01:00
public long getID() {
return id;
}
public double getVolume() {
return volume;
}
public double getLimit() {
return limit;
}
public OrderType getType() {
return type;
}
public double getExecuted() {
return initial_volume - volume;
}
public double getInitialVolume() {
return initial_volume;
}
2017-02-04 18:57:18 +01:00
public Account getAccount() {
2017-02-03 18:46:39 +01:00
return account;
}
2017-02-13 00:07:50 +01:00
public OrderStatus getOrderStatus(){
return status;
}
2017-01-12 23:16:37 +01:00
2017-01-10 07:41:52 +01:00
}
2017-01-05 19:20:44 +01:00
/**
* Histrory of quotes
*/
2017-02-04 16:29:45 +01:00
public TreeSet<Quote> quoteHistory; // = new TreeSet<>();
2017-02-04 18:57:18 +01:00
final void initExchange() {
timer = new Scheduler(); // timer = new Scheduler();
2017-02-04 16:29:45 +01:00
random = new Random(12);
2017-02-04 16:29:45 +01:00
quoteHistory = new TreeSet();
accounts = new ConcurrentHashMap<>();
traders = new ArrayList();
2017-02-04 18:57:18 +01:00
num_trades = 0;
2017-02-04 16:29:45 +01:00
// Create order books
2017-02-04 18:57:18 +01:00
order_books = new HashMap();
2017-02-04 16:29:45 +01:00
for (OrderType type : OrderType.values()) {
order_books.put(type, new TreeSet(new OrderComparator(type)));
}
2017-01-05 19:20:44 +01:00
2017-02-04 16:29:45 +01:00
}
2017-02-04 18:57:18 +01:00
2017-01-05 19:20:44 +01:00
/**
* Constructor
*/
public Exchange() {
2017-02-04 16:29:45 +01:00
qrlist = (new CopyOnWriteArrayList<>());
2017-02-03 18:46:39 +01:00
2017-02-04 16:29:45 +01:00
initExchange();
2017-01-10 07:41:52 +01:00
2017-01-05 19:20:44 +01:00
}
2017-02-03 18:46:39 +01:00
public class Statistics {
2017-02-03 01:56:41 +01:00
public long trades;
public long orders;
2017-02-03 18:46:39 +01:00
};
2017-02-04 18:57:18 +01:00
2017-02-03 18:46:39 +01:00
Statistics statistics;
long num_trades = 0;
long num_orders = 0;
public Statistics getStatistics() {
2017-02-03 01:56:41 +01:00
Statistics s = new Statistics();
2017-02-03 18:46:39 +01:00
s.trades = num_trades;
s.orders = num_orders;
2017-02-03 01:56:41 +01:00
return s;
2017-02-03 18:46:39 +01:00
2017-02-03 01:56:41 +01:00
}
2017-01-31 08:04:11 +01:00
2017-02-04 16:29:45 +01:00
/**
* Start the exchange
*/
public void start() {
2017-01-16 21:30:10 +01:00
timer.start();
}
2017-02-04 18:57:18 +01:00
public void reset() {
2017-02-04 16:29:45 +01:00
initExchange();
}
2017-02-04 18:57:18 +01:00
2017-02-04 16:29:45 +01:00
public void terminate() {
timer.terminate();
}
2017-02-04 18:57:18 +01:00
/*
2017-01-10 07:41:52 +01:00
class BidBook extends TreeSet {
2017-01-09 18:38:01 +01:00
TreeSet t = new TreeSet();
2017-01-10 07:41:52 +01:00
boolean hallo() {
2017-01-09 18:38:01 +01:00
t.comparator();
return true;
}
}
2017-02-04 18:57:18 +01:00
*/
2017-01-05 19:20:44 +01:00
public SortedSet<Quote> getQuoteHistory(long start) {
Quote s = new Quote();
2017-01-08 01:39:14 +01:00
s.time = start * 1000;
2017-01-05 23:18:19 +01:00
s.id = 0;
2017-01-05 19:20:44 +01:00
TreeSet<Quote> result = new TreeSet<>();
result.addAll(this.quoteHistory.tailSet(s));
return result;
}
2017-02-13 00:07:50 +01:00
public final String CFG_MONEY_DECIMALS = "money_decimals";
public final String CFG_SHARES_DECIMALS = "shares_decimals";
public void putConfig(JSONObject cfg) {
this.setMoneyDecimals(cfg.getInt(CFG_MONEY_DECIMALS));
this.setSharesDecimals(cfg.getInt(CFG_SHARES_DECIMALS));
}
2017-01-12 23:16:37 +01:00
public Quote getCurrentPrice() {
2017-02-13 00:07:50 +01:00
/* if (!this.quoteHistory.isEmpty()){
Quote q = this.quoteHistory.pollLast();
System.out.printf("Quote: %f\n", q.price);
return q;
}
return null;
*/
2017-02-09 17:59:20 +01:00
SortedSet<Order> bid = order_books.get(OrderType.BUYLIMIT);
SortedSet<Order> ask = order_books.get(OrderType.SELLLIMIT);
2017-01-12 23:16:37 +01:00
Quote q = null;
tradelock.lock();
if (!bid.isEmpty() && !ask.isEmpty()) {
q = new Quote();
q.price = (bid.first().limit + ask.first().limit) / 2.0;
}
tradelock.unlock();
if (q != null) {
return q;
}
if (this.quoteHistory.isEmpty()) {
return null;
}
q = this.quoteHistory.last();
return q;
}
2017-01-05 19:20:44 +01:00
// Class to describe an executed order
// QuoteReceiver has to be implemented by objects that wants
// to receive quote updates
public interface QuoteReceiver {
void UpdateQuote(Quote q);
}
/**
* Bookreceiver Interface
*/
public interface BookReceiver {
void UpdateOrderBook();
}
2017-01-08 01:39:14 +01:00
final private ArrayList<BookReceiver> ask_bookreceivers = new ArrayList<>();
final private ArrayList<BookReceiver> bid_bookreceivers = new ArrayList<>();
2017-01-05 19:20:44 +01:00
private ArrayList<BookReceiver> selectBookReceiver(OrderType t) {
2017-01-05 19:20:44 +01:00
switch (t) {
2017-02-09 17:59:20 +01:00
case SELLLIMIT:
2017-01-05 19:20:44 +01:00
return ask_bookreceivers;
2017-02-09 17:59:20 +01:00
case BUYLIMIT:
2017-01-05 19:20:44 +01:00
return bid_bookreceivers;
}
return null;
}
public void addBookReceiver(OrderType t, BookReceiver br) {
2017-01-05 19:20:44 +01:00
ArrayList<BookReceiver> bookreceivers;
bookreceivers = selectBookReceiver(t);
bookreceivers.add(br);
}
void updateBookReceivers(OrderType t) {
2017-01-05 19:20:44 +01:00
ArrayList<BookReceiver> bookreceivers;
bookreceivers = selectBookReceiver(t);
Iterator<BookReceiver> i = bookreceivers.iterator();
while (i.hasNext()) {
i.next().UpdateOrderBook();
}
2017-01-31 08:04:11 +01:00
2017-01-05 19:20:44 +01:00
}
// Here we store the list of quote receivers
2017-02-04 16:29:45 +01:00
private List<QuoteReceiver> qrlist;
2017-01-05 19:20:44 +01:00
/**
*
* @param qr
*/
public void addQuoteReceiver(QuoteReceiver qr) {
qrlist.add(qr);
}
// send updated quotes to all quote receivers
private void updateQuoteReceivers(Quote q) {
Iterator<QuoteReceiver> i = qrlist.iterator();
while (i.hasNext()) {
i.next().UpdateQuote(q);
}
}
// long time = 0;
2017-02-09 17:59:20 +01:00
//double theprice = 12.9;
// long orderid = 1;
2017-01-05 19:20:44 +01:00
double lastprice = 100.0;
long lastsvolume;
2017-01-14 14:42:00 +01:00
private final Locker tradelock = new Locker();
2017-01-05 19:20:44 +01:00
2017-01-12 23:16:37 +01:00
public ArrayList<Order> getOrderBook(OrderType type, int depth) {
2017-01-05 19:20:44 +01:00
2017-01-15 09:28:26 +01:00
SortedSet<Order> book = order_books.get(type);
2017-01-05 19:20:44 +01:00
if (book == null) {
return null;
}
2017-01-15 09:28:26 +01:00
tradelock.lock();
2017-01-12 23:16:37 +01:00
ArrayList<Order> ret = new ArrayList<>();
Iterator<Order> it = book.iterator();
2017-01-12 01:44:50 +01:00
2017-01-05 19:20:44 +01:00
for (int i = 0; i < depth && it.hasNext(); i++) {
2017-02-02 18:39:55 +01:00
Order o = it.next();
// System.out.print(o.volume);
if (o.volume <= 0) {
2017-01-31 08:54:38 +01:00
System.exit(0);
2017-02-02 18:39:55 +01:00
}
2017-01-31 08:54:38 +01:00
ret.add(o);
2017-01-05 19:20:44 +01:00
}
2017-02-02 18:39:55 +01:00
// System.out.println();
2017-01-15 09:28:26 +01:00
tradelock.unlock();
2017-01-05 19:20:44 +01:00
return ret;
2017-01-12 23:16:37 +01:00
}
2017-01-05 19:20:44 +01:00
2017-01-12 23:16:37 +01:00
public Quote getLastQuoete() {
2017-02-13 00:07:50 +01:00
if (this.quoteHistory.isEmpty()) {
return null;
}
return this.quoteHistory.pollLast();
2017-01-05 19:20:44 +01:00
}
2017-01-10 07:41:52 +01:00
private void transferMoneyAndShares(Account src, Account dst, double money, double shares) {
2017-01-09 17:00:05 +01:00
src.money -= money;
dst.money += money;
src.shares -= shares;
dst.shares += shares;
2017-02-13 00:07:50 +01:00
2017-01-09 17:00:05 +01:00
}
2017-01-12 01:44:50 +01:00
public boolean cancelOrder(double account_id, long order_id) {
2017-01-12 01:00:42 +01:00
Account a = accounts.get(account_id);
2017-01-12 01:44:50 +01:00
if (a == null) {
2017-01-12 01:00:42 +01:00
return false;
}
2017-01-12 01:44:50 +01:00
boolean ret = false;
2017-01-12 01:00:42 +01:00
tradelock.lock();
Order o = a.orders.get(order_id);
2017-01-12 23:16:37 +01:00
// System.out.print("The Order:"+o.limit+"\n");
2017-01-12 01:44:50 +01:00
if (o != null) {
2017-01-15 09:28:26 +01:00
SortedSet ob = order_books.get(o.type);
2017-01-12 23:16:37 +01:00
boolean rc = ob.remove(o);
2017-01-12 01:44:50 +01:00
a.orders.remove(o.id);
ret = true;
}
2017-01-12 01:00:42 +01:00
tradelock.unlock();
2017-02-09 17:59:20 +01:00
this.updateBookReceivers(OrderType.BUYLIMIT);
2017-01-12 01:44:50 +01:00
2017-01-12 01:00:42 +01:00
return ret;
}
2017-01-09 17:00:05 +01:00
2017-02-04 16:29:45 +01:00
Random random;
2017-02-03 18:46:39 +01:00
2017-02-02 18:39:55 +01:00
public int randNextInt() {
return random.nextInt();
2017-02-02 18:39:55 +01:00
}
public int randNextInt(int bounds) {
2017-02-02 18:39:55 +01:00
return random.nextInt(bounds);
2017-02-02 18:39:55 +01:00
}
public double randNextDouble() {
return random.nextDouble();
}
2017-01-08 13:16:00 +01:00
/**
*
* @param o
*/
2017-01-05 19:20:44 +01:00
long nextQuoteId = 0;
2017-02-02 18:39:55 +01:00
public double fairValue = 0;
private void removeOrderIfExecuted(Order o) {
if (o.volume != 0) {
2017-02-13 00:07:50 +01:00
o.status=OrderStatus.PARTIALLY_EXECUTED;
o.account.update(o);
return;
}
2017-01-31 08:04:11 +01:00
o.account.orders.remove(o.id);
2017-01-15 09:28:26 +01:00
SortedSet book = order_books.get(o.type);
2017-02-02 18:39:55 +01:00
2017-01-15 09:28:26 +01:00
book.remove(book.first());
2017-02-13 00:07:50 +01:00
o.status=OrderStatus.CLOSED;
o.account.update(o);
}
2017-02-13 00:07:50 +01:00
void checkSLOrders(double price) {
2017-02-10 08:12:38 +01:00
SortedSet<Order> sl = order_books.get(OrderType.STOPLOSS);
SortedSet<Order> ask = order_books.get(OrderType.SELLLIMIT);
2017-02-13 00:07:50 +01:00
if (sl.isEmpty()) {
2017-02-10 08:12:38 +01:00
return;
2017-02-13 00:07:50 +01:00
}
2017-02-10 08:12:38 +01:00
Order s = sl.first();
2017-02-13 00:07:50 +01:00
if (price <= s.limit) {
2017-02-10 08:12:38 +01:00
sl.remove(s);
2017-02-13 00:07:50 +01:00
s.type = OrderType.SELL;
2017-02-10 08:12:38 +01:00
addOrderToBook(s);
2017-02-13 00:07:50 +01:00
System.out.printf("Stoploss hit %f %f\n", s.volume, s.limit);
2017-02-10 08:12:38 +01:00
}
}
2017-02-04 18:57:18 +01:00
2017-01-08 13:16:00 +01:00
/**
*
*/
2017-01-10 07:41:52 +01:00
public void executeOrders() {
2017-01-05 19:20:44 +01:00
2017-02-09 17:59:20 +01:00
SortedSet<Order> bid = order_books.get(OrderType.BUYLIMIT);
SortedSet<Order> ask = order_books.get(OrderType.SELLLIMIT);
2017-02-13 00:07:50 +01:00
2017-02-10 08:12:38 +01:00
SortedSet<Order> ul_buy = order_books.get(OrderType.BUY);
SortedSet<Order> ul_sell = order_books.get(OrderType.SELL);
2017-01-12 01:44:50 +01:00
double volume_total = 0;
double money_total = 0;
2017-01-08 03:25:18 +01:00
while (!bid.isEmpty() && !ask.isEmpty()) {
2017-01-05 19:20:44 +01:00
Order b = bid.first();
Order a = ask.first();
2017-01-31 08:04:11 +01:00
//System.out.printf("In %f (%f) < %f (%f)\n",b.limit,b.volume,a.limit,a.volume);
2017-01-08 03:25:18 +01:00
if (b.limit < a.limit) {
2017-01-12 23:16:37 +01:00
break;
2017-01-08 03:25:18 +01:00
}
2017-01-09 18:38:01 +01:00
// There is a match, calculate price and volume
2017-01-08 13:16:00 +01:00
double price = b.id < a.id ? b.limit : a.limit;
2017-01-10 07:41:52 +01:00
double volume = b.volume >= a.volume ? a.volume : b.volume;
2017-01-08 13:16:00 +01:00
2017-02-02 18:39:55 +01:00
//System.out.printf("Price %f Vol %f\n", price,volume);
// Transfer money and shares
transferMoneyAndShares(b.account, a.account, volume * price, -volume);
2017-02-13 00:07:50 +01:00
// Update volume
b.volume -= volume;
a.volume -= volume;
2017-01-12 01:44:50 +01:00
2017-02-13 00:07:50 +01:00
// a.account.update(a);
// b.account.update(b);
2017-01-31 08:04:11 +01:00
//System.out.printf("In %f (%f) < %f (%f)\n",b.limit,b.volume,a.limit,a.volume);
2017-01-12 01:44:50 +01:00
volume_total += volume;
money_total += price * volume;
2017-01-08 13:16:00 +01:00
2017-02-03 01:56:41 +01:00
num_trades++;
2017-02-03 18:46:39 +01:00
removeOrderIfExecuted(a);
removeOrderIfExecuted(b);
2017-02-13 00:07:50 +01:00
2017-02-10 08:12:38 +01:00
this.checkSLOrders(price);
2017-01-08 13:16:00 +01:00
}
2017-01-12 23:16:37 +01:00
//System.out.print("Volume total is "+volume_total+"\n");
if (volume_total == 0) {
return;
}
Quote q = new Quote();
2017-01-12 01:44:50 +01:00
q.price = money_total / volume_total;
q.volume = volume_total;
2017-02-03 01:56:41 +01:00
q.time = timer.currentTimeMillis();
2017-01-12 01:44:50 +01:00
2017-01-12 23:16:37 +01:00
// System.out.print("There was a trade:"+q.price+"\n");
this.quoteHistory.add(q);
2017-01-05 19:20:44 +01:00
2017-01-15 09:28:26 +01:00
this.updateQuoteReceivers(q);
2017-01-05 19:20:44 +01:00
}
private void addOrderToBook(Order o) {
2017-01-10 07:41:52 +01:00
order_books.get(o.type).add(o);
2017-01-10 07:41:52 +01:00
}
2017-01-10 07:41:52 +01:00
/**
*
* @param account_id
* @param type
* @param volume
* @param limit
* @return
*/
public long createOrder(double account_id, OrderType type, double volume, double limit) {
2017-02-09 17:59:20 +01:00
2017-01-10 07:41:52 +01:00
Account a = accounts.get(account_id);
if (a == null) {
return -1;
}
Order o = new Order(a, type, volume, limit);
2017-02-02 18:39:55 +01:00
if (o.volume <= 0 || o.limit <= 0) {
2017-02-10 08:12:38 +01:00
//System.out.print("binweg\n");
2017-02-02 18:39:55 +01:00
return -1;
2017-01-31 08:54:38 +01:00
}
tradelock.lock();
2017-02-03 01:56:41 +01:00
num_orders++;
2017-02-03 18:46:39 +01:00
2017-01-10 07:41:52 +01:00
addOrderToBook(o);
a.orders.put(o.id, o);
2017-02-13 00:07:50 +01:00
a.update(o);
2017-01-12 01:44:50 +01:00
this.executeOrders();
2017-01-31 08:54:38 +01:00
2017-01-12 01:00:42 +01:00
tradelock.unlock();
2017-02-09 17:59:20 +01:00
this.updateBookReceivers(OrderType.SELLLIMIT);
this.updateBookReceivers(OrderType.BUYLIMIT);
2017-01-12 23:16:37 +01:00
2017-01-10 07:41:52 +01:00
return o.id;
}
2017-01-12 01:44:50 +01:00
public double getBestLimit(OrderType type) {
2017-01-11 19:11:17 +01:00
Order o = order_books.get(type).first();
2017-01-12 01:44:50 +01:00
if (o == null) {
2017-01-11 19:11:17 +01:00
return -1;
}
return o.limit;
}
2017-01-12 01:44:50 +01:00
public int getNumberOfOpenOrders(double account_id) {
Account a = accounts.get(account_id);
if (a == null) {
2017-01-11 19:11:17 +01:00
return 0;
2017-01-12 01:44:50 +01:00
}
2017-01-11 19:11:17 +01:00
return a.orders.size();
}
2017-01-12 01:44:50 +01:00
2017-01-12 23:16:37 +01:00
public Account getAccount(double account_id) {
return accounts.get(account_id);
}
2017-02-11 13:12:02 +01:00
/*public AccountData getAccountData(double account_id) {
2017-01-15 09:28:26 +01:00
tradelock.lock();
Account a = accounts.get(account_id);
2017-01-15 09:28:26 +01:00
tradelock.unlock();
if (a == null) {
return null;
}
AccountData ad = new AccountData();
ad.id = account_id;
ad.money = a.money;
ad.shares = a.shares;
2017-01-12 01:44:50 +01:00
2017-01-12 23:16:37 +01:00
ad.orders = new ArrayList<>();
2017-01-12 01:00:42 +01:00
ad.orders.iterator();
2017-01-12 01:44:50 +01:00
a.orders.values();
Set s = a.orders.keySet();
Iterator it = s.iterator();
2017-01-12 23:16:37 +01:00
2017-01-12 01:44:50 +01:00
while (it.hasNext()) {
long x = (long) it.next();
2017-01-12 23:16:37 +01:00
2017-01-12 01:44:50 +01:00
Order o = a.orders.get(x);
2017-01-12 23:16:37 +01:00
2017-01-12 01:44:50 +01:00
OrderData od = new OrderData();
od.id = o.id;
od.limit = o.limit;
od.volume = o.volume;
ad.orders.add(od);
}
//System.exit(0);
2017-01-12 01:00:42 +01:00
//a.orders.keySet();
//KeySet ks = a.orders.keySet();
return ad;
}
2017-02-13 00:07:50 +01:00
*/
2017-01-12 01:44:50 +01:00
public ArrayList<OrderData> getOpenOrders(double account_id) {
Account a = accounts.get(account_id);
2017-01-12 01:44:50 +01:00
if (a == null) {
return null;
2017-01-12 01:44:50 +01:00
}
ArrayList<OrderData> al = new ArrayList();
Iterator it = a.orders.entrySet().iterator();
2017-01-12 01:44:50 +01:00
while (it.hasNext()) {
Order o = (Order) it.next();
OrderData od = new OrderData();
2017-01-12 01:44:50 +01:00
od.limit = o.limit;
od.volume = o.initial_volume;
od.executed = o.initial_volume - o.volume;
od.id = o.id;
al.add(od);
}
2017-01-12 01:44:50 +01:00
return al;
}
2017-01-05 19:20:44 +01:00
}