Removed Order Id
This commit is contained in:
parent
c3812b7660
commit
e3ed6d1043
@ -1,4 +1,4 @@
|
||||
#Sun, 10 Dec 2017 23:09:50 +0100
|
||||
#Mon, 11 Dec 2017 16:05:46 +0100
|
||||
annotation.processing.enabled=true
|
||||
annotation.processing.enabled.in.editor=false
|
||||
annotation.processing.processors.list=
|
||||
|
@ -227,9 +227,11 @@ public class OpenOrdersList extends javax.swing.JPanel {
|
||||
private void ctxMenuCancelOrderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ctxMenuCancelOrderActionPerformed
|
||||
int r = table.getSelectedRow();
|
||||
Long id = (Long) model.getValueAt(r, 0);
|
||||
|
||||
Order co = account.getOrders().get(id);
|
||||
|
||||
System.out.printf("Should cancel %d\n", id);
|
||||
Globals.se.cancelOrder(account.getID(), id);
|
||||
Globals.se.cancelOrder(account.getID(), co);
|
||||
|
||||
}//GEN-LAST:event_ctxMenuCancelOrderActionPerformed
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -28,6 +28,7 @@ package traders.ManTrader;
|
||||
import gui.OpenOrdersList;
|
||||
|
||||
import sesim.Exchange;
|
||||
import sesim.Order;
|
||||
import sesim.Order.OrderType;
|
||||
|
||||
/**
|
||||
@ -176,7 +177,7 @@ public class ManTraderConsole extends javax.swing.JPanel {
|
||||
|
||||
System.out.printf("Should buy: %f %f\n",volume,limit);
|
||||
|
||||
long createOrder = trader.getSE().createOrder(trader.getAccount().getID(),
|
||||
Order createOrder = trader.getSE().createOrder(trader.getAccount().getID(),
|
||||
trader.getSE().getDefaultStockSymbol(),
|
||||
OrderType.BUYLIMIT, volume, limit);
|
||||
System.out.printf("The retval is %d",createOrder);
|
||||
@ -193,7 +194,7 @@ public class ManTraderConsole extends javax.swing.JPanel {
|
||||
|
||||
System.out.printf("Should sell: %f %f\n",volume,limit);
|
||||
|
||||
long createOrder = trader.getSE().createOrder(trader.getAccount().getID(),
|
||||
Order createOrder = trader.getSE().createOrder(trader.getAccount().getID(),
|
||||
trader.getSE().getDefaultStockSymbol(),
|
||||
OrderType.SELLLIMIT, volume, limit);
|
||||
System.out.printf("The retval is %d",createOrder);
|
||||
@ -205,7 +206,7 @@ public class ManTraderConsole extends javax.swing.JPanel {
|
||||
|
||||
System.out.printf("Should stoploss: %f %f\n",volume,limit);
|
||||
|
||||
long createOrder = trader.getSE().createOrder(trader.getAccount().getID(),
|
||||
Order createOrder = trader.getSE().createOrder(trader.getAccount().getID(),
|
||||
trader.getSE().getDefaultStockSymbol(),
|
||||
OrderType.STOPLOSS, volume, limit);
|
||||
System.out.printf("The retval is %d",createOrder);
|
||||
|
@ -183,14 +183,16 @@ public class RandomTraderA extends AutoTraderBase implements AccountListener {
|
||||
int n = se.getNumberOfOpenOrders(account_id);
|
||||
if (n > 0) {
|
||||
Account ad = se.getAccount(account_id);
|
||||
|
||||
|
||||
|
||||
Set<Long> keys = ad.getOrders().keySet();
|
||||
|
||||
Iterator<Long> it = keys.iterator();
|
||||
while (it.hasNext()) {
|
||||
// Order od = it.next();
|
||||
boolean rc = se.cancelOrder(account_id, it.next());
|
||||
Order od = ad.getOrders().get(it.next());
|
||||
boolean rc = se.cancelOrder(account_id, od);
|
||||
}
|
||||
|
||||
}
|
||||
return n;
|
||||
|
||||
@ -405,9 +407,9 @@ public class RandomTraderA extends AutoTraderBase implements AccountListener {
|
||||
// System.out.printf("Buy Order wont work\n");
|
||||
// return false;
|
||||
// }
|
||||
long rc = se.createOrder(account_id, se.getDefaultStockSymbol(),type, volume, limit);
|
||||
Order rc = se.createOrder(account_id, se.getDefaultStockSymbol(),type, volume, limit);
|
||||
|
||||
if (rc == -1) {
|
||||
if (rc == null) {
|
||||
|
||||
// System.out.printf("Buy failed %f, %f / %f (%f)\n", volume, money, limit, ad.getMoney());
|
||||
return false;
|
||||
@ -445,8 +447,8 @@ public class RandomTraderA extends AutoTraderBase implements AccountListener {
|
||||
// return false;
|
||||
// }
|
||||
// System.out.printf("Create a Sell Order %f %f!!!!\n", volume, limit);
|
||||
long rc = se.createOrder(account_id, se.getDefaultStockSymbol(), type, volume, limit);
|
||||
return rc != -1;
|
||||
Order rc = se.createOrder(account_id, se.getDefaultStockSymbol(), type, volume, limit);
|
||||
return rc != null;
|
||||
|
||||
}
|
||||
|
||||
|
@ -36,6 +36,7 @@ import org.json.JSONObject;
|
||||
import sesim.AutoTraderBase;
|
||||
import sesim.AutoTraderGui;
|
||||
import sesim.Account;
|
||||
import sesim.Order;
|
||||
import sesim.Order.OrderType;
|
||||
import sesim.Quote;
|
||||
|
||||
@ -166,7 +167,8 @@ public class RandomTraderB extends AutoTraderBase {
|
||||
Iterator<Long> it = keys.iterator();
|
||||
while (it.hasNext()) {
|
||||
// Order od = it.next();
|
||||
boolean rc = se.cancelOrder(account_id, it.next());
|
||||
Order od = ad.getOrders().get(it.next());
|
||||
boolean rc = se.cancelOrder(account_id, od);
|
||||
}
|
||||
}
|
||||
return n;
|
||||
|
Loading…
Reference in New Issue
Block a user