Moved getOHLCData from Exchange to Stock class

This commit is contained in:
7u83 2017-12-10 18:58:19 +01:00
parent 11fec6e022
commit 989e35a7be
3 changed files with 42 additions and 8 deletions

View File

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

View File

@ -146,7 +146,7 @@ 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(); Stock stock = getDefaultStock();
OHLCData data = new OHLCData(timeFrame); OHLCData data = new OHLCData(timeFrame);
@ -163,7 +163,8 @@ public class Exchange {
return data; return data;
} }
*/
public void injectMoney() { public void injectMoney() {
accounts.forEach(new BiConsumer() { accounts.forEach(new BiConsumer() {
@ -194,7 +195,9 @@ public class Exchange {
} }
public OHLCData getOHLCdata(Stock stock,Integer timeFrame) { public OHLCData getOHLCdata(Stock stock,Integer timeFrame) {
OHLCData data; return stock.getOHLCdata(timeFrame);
/* OHLCData data;
data = stock.ohlc_data.get(timeFrame); data = stock.ohlc_data.get(timeFrame);
if (data == null) { if (data == null) {
@ -204,6 +207,7 @@ public class Exchange {
} }
} }
return data; return data;
*/
} }
void updateOHLCData(Stock stock,Quote q) { void updateOHLCData(Stock stock,Quote q) {

View File

@ -26,6 +26,7 @@
package sesim; package sesim;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
@ -57,7 +58,7 @@ public class Stock {
} }
quoteHistory = new TreeSet(); quoteHistory = new TreeSet();
ohlc_data = new HashMap(); ohlc_data = new HashMap();
} }
String getSymbol() { String getSymbol() {
@ -74,8 +75,37 @@ public class Stock {
/** /**
* Histrory of quotes * Histrory of quotes
*/ */
public TreeSet<Quote> quoteHistory; // = new TreeSet<>(); public TreeSet<Quote> quoteHistory;
HashMap<Integer, OHLCData> ohlc_data = new HashMap<>();
private OHLCData buildOHLCData(int timeFrame) {
OHLCData data = new OHLCData(timeFrame);
if (quoteHistory == null) {
return data;
}
Iterator<Quote> 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<Integer, OHLCData> ohlc_data = new HashMap<>();
} }