Moved quoteHistory from Exchange to Stock class
This commit is contained in:
parent
9b86599f03
commit
6b2d382f8b
@ -1,4 +1,4 @@
|
|||||||
#Sun, 10 Dec 2017 12:50:27 +0100
|
#Sun, 10 Dec 2017 18:25:56 +0100
|
||||||
annotation.processing.enabled=true
|
annotation.processing.enabled=true
|
||||||
annotation.processing.enabled.in.editor=false
|
annotation.processing.enabled.in.editor=false
|
||||||
annotation.processing.processors.list=
|
annotation.processing.processors.list=
|
||||||
|
@ -45,26 +45,22 @@ import sesim.Order.OrderType;
|
|||||||
public class Exchange {
|
public class Exchange {
|
||||||
//private HashMap<Order.OrderType, SortedSet<Order>> order_books;
|
//private HashMap<Order.OrderType, SortedSet<Order>> order_books;
|
||||||
|
|
||||||
|
|
||||||
HashMap<String, Stock> stocks;
|
HashMap<String, Stock> stocks;
|
||||||
|
|
||||||
final String DEFAULT_STOCK="def";
|
final String DEFAULT_STOCK = "def";
|
||||||
|
|
||||||
public Stock getStock(String symbol) {
|
public Stock getStock(String symbol) {
|
||||||
return stocks.get(symbol);
|
return stocks.get(symbol);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Stock getDefaultStock(){
|
public Stock getDefaultStock() {
|
||||||
return getStock(DEFAULT_STOCK);
|
return getStock(DEFAULT_STOCK);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDefaultStockSymbol(){
|
public String getDefaultStockSymbol() {
|
||||||
return DEFAULT_STOCK;
|
return DEFAULT_STOCK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ConcurrentLinkedQueue<Order> order_queue = new ConcurrentLinkedQueue();
|
|
||||||
|
|
||||||
private double money_df = 10000;
|
private double money_df = 10000;
|
||||||
private int money_decimals = 2;
|
private int money_decimals = 2;
|
||||||
DecimalFormat money_formatter;
|
DecimalFormat money_formatter;
|
||||||
@ -151,12 +147,14 @@ public class Exchange {
|
|||||||
HashMap<Integer, OHLCData> ohlc_data = new HashMap<>();
|
HashMap<Integer, OHLCData> ohlc_data = new HashMap<>();
|
||||||
|
|
||||||
public OHLCData buildOHLCData(int timeFrame) {
|
public OHLCData buildOHLCData(int timeFrame) {
|
||||||
|
Stock stock = getDefaultStock();
|
||||||
|
|
||||||
OHLCData data = new OHLCData(timeFrame);
|
OHLCData data = new OHLCData(timeFrame);
|
||||||
if (this.quoteHistory == null) {
|
if (stock.quoteHistory == null) {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
Iterator<Quote> it = quoteHistory.iterator();
|
Iterator<Quote> it = stock.quoteHistory.iterator();
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
Quote q = it.next();
|
Quote q = it.next();
|
||||||
data.realTimeAdd(q.time, (float) q.price, (float) q.volume);
|
data.realTimeAdd(q.time, (float) q.price, (float) q.volume);
|
||||||
@ -208,8 +206,6 @@ public class Exchange {
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void updateOHLCData(Quote q) {
|
void updateOHLCData(Quote q) {
|
||||||
Iterator<OHLCData> it = ohlc_data.values().iterator();
|
Iterator<OHLCData> it = ohlc_data.values().iterator();
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
@ -218,18 +214,16 @@ public class Exchange {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayList <Indicator> indicators;
|
ArrayList<Indicator> indicators;
|
||||||
void updateIndicators(Quote q){
|
|
||||||
|
void updateIndicators(Quote q) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addIndicator(Indicator i) {
|
||||||
public void addIndicator(Indicator i){
|
|
||||||
this.indicators.add(i);
|
this.indicators.add(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void createTraders(JSONArray traderdefs) {
|
public void createTraders(JSONArray traderdefs) {
|
||||||
for (int i = 0; i < traderdefs.length(); i++) {
|
for (int i = 0; i < traderdefs.length(); i++) {
|
||||||
JSONObject o = traderdefs.getJSONObject(i);
|
JSONObject o = traderdefs.getJSONObject(i);
|
||||||
@ -301,22 +295,23 @@ public class Exchange {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
IDGenerator order_id_generator = new IDGenerator();
|
IDGenerator order_id_generator = new IDGenerator();
|
||||||
|
|
||||||
/**
|
|
||||||
* Histrory of quotes
|
|
||||||
*/
|
|
||||||
public TreeSet<Quote> quoteHistory; // = new TreeSet<>();
|
|
||||||
|
|
||||||
final void initExchange() {
|
final void initExchange() {
|
||||||
|
|
||||||
|
Stock defstock = new Stock(DEFAULT_STOCK);
|
||||||
|
stocks = new HashMap();
|
||||||
|
stocks.put(defstock.getSymbol(), defstock);
|
||||||
|
|
||||||
buy_orders = 0;
|
buy_orders = 0;
|
||||||
sell_orders = 0;
|
sell_orders = 0;
|
||||||
timer = new Scheduler(); // timer = new Scheduler();
|
timer = new Scheduler(); // timer = new Scheduler();
|
||||||
// random = new Random(12);
|
// random = new Random(12);
|
||||||
random = new Random();
|
random = new Random();
|
||||||
|
|
||||||
quoteHistory = new TreeSet();
|
// quoteHistory = new TreeSet();
|
||||||
|
getDefaultStock().reset();
|
||||||
|
|
||||||
accounts = new ConcurrentHashMap<>();
|
accounts = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
traders = new ArrayList();
|
traders = new ArrayList();
|
||||||
@ -328,14 +323,11 @@ public class Exchange {
|
|||||||
|
|
||||||
// Create order books
|
// Create order books
|
||||||
|
|
||||||
/* order_books = new HashMap();
|
/* order_books = new HashMap();
|
||||||
for (OrderType type : OrderType.values()) {
|
for (OrderType type : OrderType.values()) {
|
||||||
order_books.put(type, new TreeSet(new OrderComparator(type)));
|
order_books.put(type, new TreeSet(new OrderComparator(type)));
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
Stock defstock = new Stock(DEFAULT_STOCK);
|
|
||||||
stocks = new HashMap();
|
|
||||||
stocks.put(defstock.getSymbol(), defstock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -345,7 +337,7 @@ public class Exchange {
|
|||||||
qrlist = (new CopyOnWriteArrayList<>());
|
qrlist = (new CopyOnWriteArrayList<>());
|
||||||
|
|
||||||
initExchange();
|
initExchange();
|
||||||
executor.start();
|
//executor.start();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -387,6 +379,8 @@ public class Exchange {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
/* Stock stock = getDefaultStock();
|
||||||
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
try {
|
try {
|
||||||
while (true) {
|
while (true) {
|
||||||
@ -394,7 +388,7 @@ public class Exchange {
|
|||||||
this.wait();
|
this.wait();
|
||||||
|
|
||||||
Order o;
|
Order o;
|
||||||
while (null != (o = order_queue.poll())) {
|
while (null != (o = stock.order_queue.poll())) {
|
||||||
addOrderToBook(o);
|
addOrderToBook(o);
|
||||||
Account a = o.account;
|
Account a = o.account;
|
||||||
a.orders.put(o.id, o);
|
a.orders.put(o.id, o);
|
||||||
@ -411,6 +405,7 @@ public class Exchange {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -444,7 +439,7 @@ public class Exchange {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
/* public SortedSet<Quote> getQuoteHistory(long start) {
|
/* public SortedSet<Quote> getQuoteHistory(long start) {
|
||||||
|
|
||||||
Quote s = new Quote();
|
Quote s = new Quote();
|
||||||
s.time = start * 1000;
|
s.time = start * 1000;
|
||||||
@ -456,8 +451,7 @@ public class Exchange {
|
|||||||
return result;
|
return result;
|
||||||
|
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public final String CFG_MONEY_DECIMALS = "money_decimals";
|
public final String CFG_MONEY_DECIMALS = "money_decimals";
|
||||||
public final String CFG_SHARES_DECIMALS = "shares_decimals";
|
public final String CFG_SHARES_DECIMALS = "shares_decimals";
|
||||||
|
|
||||||
@ -624,7 +618,7 @@ public class Exchange {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Quote getBestPrice_0(){
|
public Quote getBestPrice_0() {
|
||||||
return getBestPrice_0(getDefaultStock());
|
return getBestPrice_0(getDefaultStock());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -703,7 +697,6 @@ public class Exchange {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param stock
|
* @param stock
|
||||||
@ -738,8 +731,8 @@ public class Exchange {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Order> getOrderBook(OrderType type, int depth){
|
public ArrayList<Order> getOrderBook(OrderType type, int depth) {
|
||||||
return getOrderBook(getDefaultStock(),type,depth);
|
return getOrderBook(getDefaultStock(), type, depth);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -747,11 +740,13 @@ public class Exchange {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Quote getLastQuoete() {
|
public Quote getLastQuoete() {
|
||||||
if (this.quoteHistory.isEmpty()) {
|
Stock stock = getDefaultStock();
|
||||||
|
|
||||||
|
if (stock.quoteHistory.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.quoteHistory.last();
|
return stock.quoteHistory.last();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void transferMoneyAndShares(Account src, Account dst, double money, double shares) {
|
private void transferMoneyAndShares(Account src, Account dst, double money, double shares) {
|
||||||
@ -768,7 +763,7 @@ public class Exchange {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean cancelOrder(Stock stock,double account_id, long order_id) {
|
public boolean cancelOrder(Stock stock, double account_id, long order_id) {
|
||||||
Account a = accounts.get(account_id);
|
Account a = accounts.get(account_id);
|
||||||
if (a == null) {
|
if (a == null) {
|
||||||
return false;
|
return false;
|
||||||
@ -802,8 +797,8 @@ public class Exchange {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean cancelOrder(double account_id, long order_id){
|
public boolean cancelOrder(double account_id, long order_id) {
|
||||||
return cancelOrder(getDefaultStock(),account_id,order_id);
|
return cancelOrder(getDefaultStock(), account_id, order_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
Random random;
|
Random random;
|
||||||
@ -829,10 +824,9 @@ public class Exchange {
|
|||||||
* @param o
|
* @param o
|
||||||
*/
|
*/
|
||||||
// long nextQuoteId = 0;
|
// long nextQuoteId = 0;
|
||||||
|
|
||||||
public double fairValue = 0;
|
public double fairValue = 0;
|
||||||
|
|
||||||
private void removeOrderIfExecuted(Stock stock,Order o) {
|
private void removeOrderIfExecuted(Stock stock, Order o) {
|
||||||
if (o.getAccount().getOwner().getName().equals("Tobias0")) {
|
if (o.getAccount().getOwner().getName().equals("Tobias0")) {
|
||||||
// System.out.printf("Tobias 0 test\n");
|
// System.out.printf("Tobias 0 test\n");
|
||||||
}
|
}
|
||||||
@ -863,11 +857,10 @@ public class Exchange {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeOrderIfExecuted(Order o){
|
private void removeOrderIfExecuted(Order o) {
|
||||||
removeOrderIfExecuted(getDefaultStock(),o);
|
removeOrderIfExecuted(getDefaultStock(), o);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void checkSLOrders(Stock stock, double price) {
|
void checkSLOrders(Stock stock, double price) {
|
||||||
SortedSet<Order> sl = stock.order_books.get(OrderType.STOPLOSS);
|
SortedSet<Order> sl = stock.order_books.get(OrderType.STOPLOSS);
|
||||||
SortedSet<Order> ask = stock.order_books.get(OrderType.SELLLIMIT);
|
SortedSet<Order> ask = stock.order_books.get(OrderType.SELLLIMIT);
|
||||||
@ -887,11 +880,10 @@ public class Exchange {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void checkSLOrders(double price){
|
void checkSLOrders(double price) {
|
||||||
checkSLOrders(getDefaultStock(),price);
|
checkSLOrders(getDefaultStock(), price);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void executeUnlimitedOrders() {
|
public void executeUnlimitedOrders() {
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -923,7 +915,8 @@ public class Exchange {
|
|||||||
statistics.low = q.price;
|
statistics.low = q.price;
|
||||||
}
|
}
|
||||||
|
|
||||||
quoteHistory.add(q);
|
Stock stock = getDefaultStock();
|
||||||
|
stock.quoteHistory.add(q);
|
||||||
updateOHLCData(q);
|
updateOHLCData(q);
|
||||||
updateQuoteReceivers(q);
|
updateQuoteReceivers(q);
|
||||||
}
|
}
|
||||||
@ -1050,8 +1043,8 @@ public class Exchange {
|
|||||||
// System.out.printf("B/S %d/%d Failed B/S: %d/%d\n", buy_orders, sell_orders,buy_failed,sell_failed);
|
// System.out.printf("B/S %d/%d Failed B/S: %d/%d\n", buy_orders, sell_orders,buy_failed,sell_failed);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addOrderToBook(Order o){
|
private void addOrderToBook(Order o) {
|
||||||
addOrderToBook(getDefaultStock(),o);
|
addOrderToBook(getDefaultStock(), o);
|
||||||
}
|
}
|
||||||
|
|
||||||
long buy_failed = 0;
|
long buy_failed = 0;
|
||||||
@ -1068,7 +1061,6 @@ public class Exchange {
|
|||||||
public long createOrder(double account_id,
|
public long createOrder(double account_id,
|
||||||
String stocksymbol, OrderType type, double volume, double limit) {
|
String stocksymbol, OrderType type, double volume, double limit) {
|
||||||
|
|
||||||
|
|
||||||
Stock stock = this.getStock(stocksymbol);
|
Stock stock = this.getStock(stocksymbol);
|
||||||
|
|
||||||
Account a = accounts.get(account_id);
|
Account a = accounts.get(account_id);
|
||||||
@ -1080,7 +1072,7 @@ public class Exchange {
|
|||||||
timer.currentTimeMillis(),
|
timer.currentTimeMillis(),
|
||||||
a, type, roundShares(volume), roundMoney(limit));
|
a, type, roundShares(volume), roundMoney(limit));
|
||||||
|
|
||||||
o.stock=stock;
|
o.stock = stock;
|
||||||
|
|
||||||
if (o.volume <= 0 || o.limit <= 0) {
|
if (o.volume <= 0 || o.limit <= 0) {
|
||||||
|
|
||||||
@ -1119,17 +1111,17 @@ public class Exchange {
|
|||||||
return o.getID();
|
return o.getID();
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getBestLimit(Stock stock,OrderType type) {
|
public double getBestLimit(Stock stock, OrderType type) {
|
||||||
Order o = stock.order_books.get(type).first();
|
Order o = stock.order_books.get(type).first();
|
||||||
if (o == null) {
|
if (o == null) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return o.limit;
|
return o.limit;
|
||||||
}
|
}
|
||||||
public double getBestLimit(OrderType type){
|
|
||||||
return getBestLimit(getDefaultStock(),type);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public double getBestLimit(OrderType type) {
|
||||||
|
return getBestLimit(getDefaultStock(), type);
|
||||||
|
}
|
||||||
|
|
||||||
public int getNumberOfOpenOrders(double account_id) {
|
public int getNumberOfOpenOrders(double account_id) {
|
||||||
Account a = accounts.get(account_id);
|
Account a = accounts.get(account_id);
|
||||||
|
@ -28,6 +28,7 @@ package sesim;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.SortedSet;
|
import java.util.SortedSet;
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -39,14 +40,23 @@ public class Stock {
|
|||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
Stock(String symbol) {
|
Stock(String symbol) {
|
||||||
this.symbol=symbol;
|
this.symbol = symbol;
|
||||||
|
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public final void reset() {
|
||||||
order_books = new HashMap();
|
order_books = new HashMap();
|
||||||
|
|
||||||
// Create an order book for each order type
|
// Create an order book for each order type
|
||||||
for (Order.OrderType type : Order.OrderType.values()) {
|
for (Order.OrderType type : Order.OrderType.values()) {
|
||||||
this.order_books.put(type, new TreeSet(new Exchange.OrderComparator(type)));
|
this.order_books.put(type, new TreeSet(new Exchange.OrderComparator(type)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
quoteHistory = new TreeSet();
|
||||||
}
|
}
|
||||||
|
|
||||||
String getSymbol() {
|
String getSymbol() {
|
||||||
@ -57,8 +67,12 @@ public class Stock {
|
|||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final HashMap<Order.OrderType, SortedSet<Order>> order_books;
|
protected HashMap<Order.OrderType, SortedSet<Order>> order_books;
|
||||||
|
// protected ConcurrentLinkedQueue<Order> order_queue = new ConcurrentLinkedQueue();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Histrory of quotes
|
||||||
|
*/
|
||||||
|
public TreeSet<Quote> quoteHistory; // = new TreeSet<>();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user