Moved quoteHistory from Exchange to Stock class

This commit is contained in:
7u83 2017-12-10 18:26:32 +01:00
parent 9b86599f03
commit 6b2d382f8b
3 changed files with 99 additions and 93 deletions

View File

@ -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.in.editor=false
annotation.processing.processors.list=

View File

@ -45,7 +45,6 @@ import sesim.Order.OrderType;
public class Exchange {
//private HashMap<Order.OrderType, SortedSet<Order>> order_books;
HashMap<String, Stock> stocks;
final String DEFAULT_STOCK = "def";
@ -62,9 +61,6 @@ public class Exchange {
return DEFAULT_STOCK;
}
ConcurrentLinkedQueue<Order> order_queue = new ConcurrentLinkedQueue();
private double money_df = 10000;
private int money_decimals = 2;
DecimalFormat money_formatter;
@ -151,12 +147,14 @@ public class Exchange {
HashMap<Integer, OHLCData> ohlc_data = new HashMap<>();
public OHLCData buildOHLCData(int timeFrame) {
Stock stock = getDefaultStock();
OHLCData data = new OHLCData(timeFrame);
if (this.quoteHistory == null) {
if (stock.quoteHistory == null) {
return data;
}
Iterator<Quote> it = quoteHistory.iterator();
Iterator<Quote> it = stock.quoteHistory.iterator();
while (it.hasNext()) {
Quote q = it.next();
data.realTimeAdd(q.time, (float) q.price, (float) q.volume);
@ -208,8 +206,6 @@ public class Exchange {
return data;
}
void updateOHLCData(Quote q) {
Iterator<OHLCData> it = ohlc_data.values().iterator();
while (it.hasNext()) {
@ -219,17 +215,15 @@ public class Exchange {
}
ArrayList<Indicator> indicators;
void updateIndicators(Quote q) {
}
public void addIndicator(Indicator i) {
this.indicators.add(i);
}
public void createTraders(JSONArray traderdefs) {
for (int i = 0; i < traderdefs.length(); i++) {
JSONObject o = traderdefs.getJSONObject(i);
@ -301,22 +295,23 @@ public class Exchange {
}
IDGenerator order_id_generator = new IDGenerator();
/**
* Histrory of quotes
*/
public TreeSet<Quote> quoteHistory; // = new TreeSet<>();
final void initExchange() {
Stock defstock = new Stock(DEFAULT_STOCK);
stocks = new HashMap();
stocks.put(defstock.getSymbol(), defstock);
buy_orders = 0;
sell_orders = 0;
timer = new Scheduler(); // timer = new Scheduler();
// random = new Random(12);
random = new Random();
quoteHistory = new TreeSet();
// quoteHistory = new TreeSet();
getDefaultStock().reset();
accounts = new ConcurrentHashMap<>();
traders = new ArrayList();
@ -333,9 +328,6 @@ public class Exchange {
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<>());
initExchange();
executor.start();
//executor.start();
}
@ -387,6 +379,8 @@ public class Exchange {
@Override
public void run() {
/* Stock stock = getDefaultStock();
synchronized (this) {
try {
while (true) {
@ -394,7 +388,7 @@ public class Exchange {
this.wait();
Order o;
while (null != (o = order_queue.poll())) {
while (null != (o = stock.order_queue.poll())) {
addOrderToBook(o);
Account a = o.account;
a.orders.put(o.id, o);
@ -411,6 +405,7 @@ public class Exchange {
}
}
*/
}
}
@ -457,7 +452,6 @@ public class Exchange {
}
*/
public final String CFG_MONEY_DECIMALS = "money_decimals";
public final String CFG_SHARES_DECIMALS = "shares_decimals";
@ -703,7 +697,6 @@ public class Exchange {
}
}
/**
*
* @param stock
@ -747,11 +740,13 @@ public class Exchange {
* @return
*/
public Quote getLastQuoete() {
if (this.quoteHistory.isEmpty()) {
Stock stock = getDefaultStock();
if (stock.quoteHistory.isEmpty()) {
return null;
}
return this.quoteHistory.last();
return stock.quoteHistory.last();
}
private void transferMoneyAndShares(Account src, Account dst, double money, double shares) {
@ -829,7 +824,6 @@ public class Exchange {
* @param o
*/
// long nextQuoteId = 0;
public double fairValue = 0;
private void removeOrderIfExecuted(Stock stock, Order o) {
@ -867,7 +861,6 @@ public class Exchange {
removeOrderIfExecuted(getDefaultStock(), o);
}
void checkSLOrders(Stock stock, double price) {
SortedSet<Order> sl = stock.order_books.get(OrderType.STOPLOSS);
SortedSet<Order> ask = stock.order_books.get(OrderType.SELLLIMIT);
@ -891,7 +884,6 @@ public class Exchange {
checkSLOrders(getDefaultStock(), price);
}
public void executeUnlimitedOrders() {
}
@ -923,7 +915,8 @@ public class Exchange {
statistics.low = q.price;
}
quoteHistory.add(q);
Stock stock = getDefaultStock();
stock.quoteHistory.add(q);
updateOHLCData(q);
updateQuoteReceivers(q);
}
@ -1068,7 +1061,6 @@ public class Exchange {
public long createOrder(double account_id,
String stocksymbol, OrderType type, double volume, double limit) {
Stock stock = this.getStock(stocksymbol);
Account a = accounts.get(account_id);
@ -1126,11 +1118,11 @@ public class Exchange {
}
return o.limit;
}
public double getBestLimit(OrderType type) {
return getBestLimit(getDefaultStock(), type);
}
public int getNumberOfOpenOrders(double account_id) {
Account a = accounts.get(account_id);
if (a == null) {

View File

@ -28,6 +28,7 @@ package sesim;
import java.util.HashMap;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
*
@ -41,12 +42,21 @@ public class Stock {
Stock(String symbol) {
this.symbol = symbol;
reset();
}
/**
*
*/
public final void reset() {
order_books = new HashMap();
// Create an order book for each order type
for (Order.OrderType type : Order.OrderType.values()) {
this.order_books.put(type, new TreeSet(new Exchange.OrderComparator(type)));
}
quoteHistory = new TreeSet();
}
String getSymbol() {
@ -57,8 +67,12 @@ public class Stock {
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<>();
}