diff --git a/nbproject/project.properties b/nbproject/project.properties index 923f673..98daf9a 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -1,4 +1,4 @@ -#Sun, 10 Dec 2017 18:46:05 +0100 +#Sun, 10 Dec 2017 18:56:38 +0100 annotation.processing.enabled=true annotation.processing.enabled.in.editor=false annotation.processing.processors.list= diff --git a/src/sesim/Exchange.java b/src/sesim/Exchange.java index 8f3661f..4c93157 100644 --- a/src/sesim/Exchange.java +++ b/src/sesim/Exchange.java @@ -146,7 +146,7 @@ public class Exchange { //HashMap ohlc_data = new HashMap<>(); - public OHLCData buildOHLCData(int timeFrame) { +/* public OHLCData buildOHLCData(int timeFrame) { Stock stock = getDefaultStock(); OHLCData data = new OHLCData(timeFrame); @@ -163,7 +163,8 @@ public class Exchange { return data; } - +*/ + public void injectMoney() { accounts.forEach(new BiConsumer() { @@ -194,7 +195,9 @@ public class Exchange { } public OHLCData getOHLCdata(Stock stock,Integer timeFrame) { - OHLCData data; + return stock.getOHLCdata(timeFrame); + +/* OHLCData data; data = stock.ohlc_data.get(timeFrame); if (data == null) { @@ -204,6 +207,7 @@ public class Exchange { } } return data; +*/ } void updateOHLCData(Stock stock,Quote q) { diff --git a/src/sesim/Stock.java b/src/sesim/Stock.java index ed38b1e..e8e530c 100644 --- a/src/sesim/Stock.java +++ b/src/sesim/Stock.java @@ -26,6 +26,7 @@ package sesim; import java.util.HashMap; +import java.util.Iterator; import java.util.SortedSet; import java.util.TreeSet; import java.util.concurrent.ConcurrentLinkedQueue; @@ -57,7 +58,7 @@ public class Stock { } quoteHistory = new TreeSet(); - ohlc_data = new HashMap(); + ohlc_data = new HashMap(); } String getSymbol() { @@ -74,8 +75,37 @@ public class Stock { /** * Histrory of quotes */ - public TreeSet quoteHistory; // = new TreeSet<>(); + public TreeSet quoteHistory; + + HashMap ohlc_data = new HashMap<>(); + + private OHLCData buildOHLCData(int timeFrame) { + OHLCData data = new OHLCData(timeFrame); + if (quoteHistory == null) { + return data; + } + + Iterator it = quoteHistory.iterator(); + while (it.hasNext()) { + Quote q = it.next(); + data.realTimeAdd(q.time, (float) q.price, (float) q.volume); + + } + + return data; + } + + public OHLCData getOHLCdata(Integer timeFrame) { + OHLCData data; + data = ohlc_data.get(timeFrame); + if (data == null) { + + synchronized (this) { + data = buildOHLCData(timeFrame); + ohlc_data.put(timeFrame, data); + } + } + return data; + } - HashMap ohlc_data = new HashMap<>(); - }