diff --git a/src/main/java/chart/Chart.java b/src/main/java/chart/Chart.java index 5f20a2a..144840a 100644 --- a/src/main/java/chart/Chart.java +++ b/src/main/java/chart/Chart.java @@ -5,6 +5,8 @@ */ package chart; +import sesim.OHLCDataItem; +import sesim.OHLCData; import java.awt.*; import sesim.Exchange.*; import sesim.Quote; @@ -41,13 +43,12 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver { int items = 350; long ntime = 0; - OHLCData data = new OHLCData(); + OHLCData data = new OHLCData(2000); OHLCDataItem current = null; - int min; - int max; - + // int min; + // int max; int getY(float Y) { return 0; @@ -62,25 +63,28 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver { double unit_width = 1; int big_tick = 10; long start; - - - XLegendDef(){ - + + XLegendDef() { + } - - String getAt(int unit){ - Date date = new Date(start); - DateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS"); + + String getAt(int unit) { + Date date = new Date(sesim.Scheduler.timeStart + unit * 5000); +// DateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS"); + DateFormat formatter = new SimpleDateFormat("HH:mm:ss"); String dateFormatted = formatter.format(date); return dateFormatted; } } - void drawXLegend(Graphics2D g,XLegendDef xld) { + void drawOHLC(Graphics2D g, int x, OHLCDataItem di) { + + } + + void drawXLegend(Graphics2D g, XLegendDef xld) { //XLegendDef xld = new XLegendDef(); - g = (Graphics2D) g.create(); int xl_height = 30; @@ -93,122 +97,176 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver { g.drawLine(0, y, dim.width, y); - int n = 0; - for (double x = 0; x < dim.width; x += em_width * xld.unit_width) { + int n; + double x; + + for (n = 0, x = 0; x < dim.width; x += em_width * xld.unit_width) { if (n % xld.big_tick == 0) { g.drawLine((int) x, y, (int) x, y + em_height); } else { g.drawLine((int) x, y, (int) x, y + em_height / 2); } - - if (n % xld.big_tick == 0){ - String text = "Hello"; - - text = xld.getAt(n); - int swidth = g.getFontMetrics().stringWidth(text); - - - g.drawString(text, (int)x - swidth / 2, y + em_height * 2); + if (n % xld.big_tick == 0) { + String text = "Hello"; + + text = xld.getAt(n); + int swidth = g.getFontMetrics().stringWidth(text); + + g.drawString(text, (int) x - swidth / 2, y + em_height * 2); } - + OHLCDataItem d; try { d = data.data.get(n); } catch (Exception e) { d = null; } - + n++; } - - /* - for (int i = 0; i < items; i ++) { - int x = i * this.item_width; - - if (i%5==0) - g.drawLine(x, y, x, y + 6); - else - g.drawLine(x, y, x, y + 3); - - OHLCDataItem d; - try { - d = data.data.get(i); - } catch (Exception e) { - d = null; - } - - String text; - if (d != null) { - text = "A"; - } else { - text = "x"; - } - - - - int swidth = g.getFontMetrics().stringWidth(text); - - g.drawString(text, x - swidth / 2, y + em_height * 2); - - - } - */ - //for(int x=0; x) - } - - private void realTimeAdd(long time, float price, float volume) { - - /*System.out.print("Diff:" - +(ntime-time) - +"\n" - );*/ - if (time > ntime) { - -// System.out.print("new raster ----------------------------------\n"); - current = null; -// ntime = rasterTime(time) + 5000; - // System.out.print(ntime+"\n"); - // System.out.print((time)+"\n"); - // System.exit(0); - } - - if (current == null) { - current = new OHLCDataItem(price, price, price, price, volume); - return; - } - - boolean rc = current.update(price, volume); - - if (rc) { - System.out.print("Updated -" - + " High:" - + current.high - + " Low:" - + current.low - + " Volume" - + current.volume - + "(" - + time - + ")" - + "\n" - ); - } - } private void getData() { } + class RenderCtx { + + Rectangle rect; + float scaling; + float min; + Graphics2D g; + float iwidth; + + float getY(float y) { + return rect.height - ((y - min) * scaling); + } + } + + float getY(float y, float min, float s, Rectangle r) { + + return r.height - ((y - min) * s); + } + + private void old_drawItem(Graphics2D g, Rectangle r, int prevx, int x, OHLCDataItem prev, OHLCDataItem item, float s, float min) { + + if (prev == null) { + prev = item; + } + + g.drawLine(prevx, (int) getY(prev.close, min, s, r), x, (int) getY(item.close, min, s, r)); + g.drawLine(r.x, r.height + r.y, r.x + r.width, r.height + r.y); + } + + private void drawItem_l(RenderCtx ctx, int prevx, int x, OHLCDataItem prev, OHLCDataItem item) { + + if (prev == null) { + prev = item; + } + Graphics2D g = ctx.g; + + Rectangle r = ctx.rect; + g.drawLine(prevx, (int) ctx.getY(prev.close), x, (int) ctx.getY(item.close)); + g.drawLine(r.x, r.height + r.y, r.x + r.width, r.height + r.y); + } + + private void drawItem(RenderCtx ctx, int prevx, int x, OHLCDataItem prev, OHLCDataItem i) { + + if (prev == null) { + prev = i; + } + Graphics2D g = ctx.g; + + Rectangle r = ctx.rect; +// g.drawLine(prevx, (int) ctx.getY(prev.close), x, (int) ctx.getY(item.close)); + +// g.drawLine(x,(int)ctx.getY(i.high),x,(int)ctx.getY(i.low)); + if (i.open < i.close) { + + g.setColor(Color.BLACK); + g.drawLine(x, (int) ctx.getY(i.close), x, (int) ctx.getY(i.high)); + g.drawLine(x, (int) ctx.getY(i.low), x, (int) ctx.getY(i.open)); + + float w = ctx.iwidth; + float h = (int) (ctx.getY(i.open) - ctx.getY(i.close)); + + System.out.printf("CLO: %f %f \n", w, h); + g.setColor(Color.GREEN); + g.fillRect((int) (x - w / 2), (int) ctx.getY(i.close), (int) w, (int) h); + g.setColor(Color.BLACK); + g.drawRect((int) (x - w / 2), (int) ctx.getY(i.close), (int) w, (int) h); + + } else { + + g.setColor(Color.RED); + g.drawLine(x, (int) ctx.getY(i.high), x, (int) ctx.getY(i.close)); + g.drawLine(x, (int) ctx.getY(i.open), x, (int) ctx.getY(i.low)); + + float w = ctx.iwidth; + float h = (int) (ctx.getY(i.close) - ctx.getY(i.open)); + + g.fillRect((int) (x - w / 2), (int) ctx.getY(i.open), (int) w, (int) h); + g.setColor(Color.BLACK); + g.drawRect((int) (x - w / 2), (int) ctx.getY(i.open), (int) w, (int) h); + + } + + g.drawLine(r.x, r.height + r.y, r.x + r.width, r.height + r.y); + } + private void draw(Graphics2D g) { + OHLCDataItem di0 = data.get(0); + XLegendDef xld = new XLegendDef(); + this.drawXLegend(g, xld); + + int em_height = g.getFontMetrics().getHeight(); + int em_width = g.getFontMetrics().stringWidth("M"); + + this.getSize(); + + int pwidth = em_width * items; + int phight = 400; + + this.setPreferredSize(new Dimension(pwidth, phight)); + + Dimension dim = this.getSize(); + + Iterator it = data.iterator(); + OHLCDataItem prev = null; + int myi = 0; + + RenderCtx ctx = new RenderCtx(); + + Rectangle r = new Rectangle(0, 2 * em_width, pwidth, dim.height - 6 *em_width ); + ctx.rect = r; + ctx.scaling = (float) r.height / (data.getMax() - data.getMin()); + ctx.min = data.getMin(); + ctx.g = g; + ctx.iwidth = em_width - em_width / 5f; + + //System.out.printf("Scaling: %f %f %f %f %f\n",diff,(float)r.height,data.getMin(),data.getMax(),yscaling); + while (it.hasNext()) { + OHLCDataItem di = it.next(); + + int x = myi * em_width; + this.drawItem(ctx, x - em_width, x, prev, di); //, ctx.scaling, data.getMin()); + + myi++; + prev = di; + + } + + } + + private void draw_old(Graphics2D g) { + OHLCDataItem di0 = data.data.get(0); - XLegendDef xld= new XLegendDef(); - //xld.start=di. - this.drawXLegend(g,xld); + XLegendDef xld = new XLegendDef(); + this.drawXLegend(g, xld); this.getSize(); @@ -222,21 +280,8 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver { g.setColor(Color.RED); -// g.drawLine(0, 0, 100, 100); - - /* for (int i = 0; i < items; i++) { - int x = i * this.item_width; - g.drawLine(x, 0, x, 50); - - } - */ - // if (this.current == null) { - // return; - // } ArrayList od = data.data; - - // System.out.print("OD S: " + od.size() + "\n"); g.setColor(Color.BLUE); g.setStroke(new BasicStroke(3)); @@ -250,8 +295,8 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver { OHLCDataItem di = it.next(); float y = di.close; - float max = data.max; - float min = data.min; + float max = data.getMax(); + float min = data.getMin(); max = max / 10.0f + max; min = min - min / 10.0f; @@ -262,14 +307,7 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver { } - // max = 5; - // min = 0; - // System.out.print("Fval: " + y + " " + min + "\n"); y -= min; - // System.out.print("VAL New" + y + "\n"); - - //val/ ((data.max-data.min)/dim.height); - // System.out.print("MINMAX " + min + " " + max + " " + dim.height + "\n"); y = dim.height - (dim.height * y / (max - min)); int x = myi * this.item_width; @@ -280,10 +318,8 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver { lastx = x; lasty = (int) y; -// System.out.print("Draw Line: " + x + " " + y + "\n"); } - // g.drawLine(0, 0, 100, (int) ((this.current.close-80.0)*80.0)); } @Override diff --git a/src/main/java/gui/MainWin.java b/src/main/java/gui/MainWin.java index 632e46f..b083d29 100644 --- a/src/main/java/gui/MainWin.java +++ b/src/main/java/gui/MainWin.java @@ -240,7 +240,7 @@ public class MainWin extends javax.swing.JFrame { // SwitchingTraderConfig cfg = new SwitchingTraderConfig(); RandomTraderConfig cfg= new RandomTraderConfig(); - for (int i=0; i<100; i++){ + for (int i=0; i<30; i++){ AutoTrader randt = cfg.createTrader(se, 100000, 100000); se.traders.add(randt); diff --git a/src/main/java/sesim/Exchange.java b/src/main/java/sesim/Exchange.java index 6c120cb..be2d0dc 100644 --- a/src/main/java/sesim/Exchange.java +++ b/src/main/java/sesim/Exchange.java @@ -183,6 +183,8 @@ public class Exchange { //extends Thread { } + + /*public interface TimerEvent { long timerEvent(); diff --git a/src/main/java/chart/OHLCData.java b/src/main/java/sesim/OHLCData.java similarity index 58% rename from src/main/java/chart/OHLCData.java rename to src/main/java/sesim/OHLCData.java index af347c1..2344277 100644 --- a/src/main/java/chart/OHLCData.java +++ b/src/main/java/sesim/OHLCData.java @@ -23,7 +23,7 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ -package chart; +package sesim; import java.util.*; @@ -33,29 +33,56 @@ import java.util.*; */ public class OHLCData { //extends ArrayList { - float max=0; - float min=0; + private float max=0; + private float min=0; - int ras=20000/10; + private int frame_size=60000; + int max_size=100; + + public OHLCData(){ - long time_start; - long time_step; + } + + public OHLCData(int frame_size){ + + this.frame_size=frame_size; + } + + // long time_start; + // long time_step; public float getMax() { return max; } + public float getMin(){ + return min; + } + + public int size(){ + return data.size(); + } + + + public int getFrameSize(){ + return this.frame_size; + } + - long rasterTime(long time) { + long getFrameStart(long time) { - long rt = time / ras; - return rt * ras; + long rt = time / frame_size; + return rt * frame_size; } - ArrayList data = new ArrayList<>(); + public ArrayList data = new ArrayList<>(); + + public OHLCDataItem get(int n){ + return data.get(n); + } private void updateMinMax(float price){ @@ -68,27 +95,42 @@ public class OHLCData { //extends ArrayList { } } + + public Iterator iterator(){ + return data.iterator(); + } - private long ntime = 0; + // Start and end of current frame + private long current_frame_end = 0; + private long current_frame_start =0; - boolean realTimeAdd(long time, float price, float volume) { + public boolean realTimeAdd(long time, float price, float volume) { - if (time > ntime) { - if (ntime==0){ - System.out.print ("Setting ntimt was zero\n"); + if (time >= current_frame_end) { + if (current_frame_end==0){ + this.min=price; this.max=price; } - ntime = rasterTime(time) + ras; - data.add(new OHLCDataItem(price, price, price, price, volume)); + long last_frame_start=current_frame_start; + current_frame_start = getFrameStart(time); + + + + current_frame_end = current_frame_start + frame_size; + + System.out.printf("TA %d TE %d\n",this.current_frame_start,this.current_frame_end); + + data.add(new OHLCDataItem(this.current_frame_start, price, volume)); this.updateMinMax(price); return true; } - OHLCDataItem d = data.get(data.size() - 1); this.updateMinMax(price); + + OHLCDataItem d = data.get(data.size() - 1); boolean rc = d.update(price, volume); return rc; } diff --git a/src/main/java/chart/OHLCDataItem.java b/src/main/java/sesim/OHLCDataItem.java similarity index 86% rename from src/main/java/chart/OHLCDataItem.java rename to src/main/java/sesim/OHLCDataItem.java index d5ebdac..b8b06d0 100644 --- a/src/main/java/chart/OHLCDataItem.java +++ b/src/main/java/sesim/OHLCDataItem.java @@ -23,7 +23,7 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ -package chart; +package sesim; /** * @@ -36,17 +36,13 @@ public class OHLCDataItem { public float low; public float close; public float volume; - - public OHLCDataItem(float open, float high, float low, float close, float volume) { - this.open = open; - this.high = high; - this.low = low; - this.close = close; - this.volume = volume; - } - - public OHLCDataItem() { - this(0, 0, 0, 0, 0); + public long time; + + + public OHLCDataItem(long time,float price, float volume){ + this.time=time; + open=low=high=close=price; + this.volume=volume; } public boolean update(float price, float volume) { diff --git a/src/main/java/sesim/OrderData.java b/src/main/java/sesim/OrderData.java index 9aeb989..e3e8398 100644 --- a/src/main/java/sesim/OrderData.java +++ b/src/main/java/sesim/OrderData.java @@ -36,3 +36,4 @@ public class OrderData { public double executed; } + diff --git a/src/main/java/sesim/Scheduler.java b/src/main/java/sesim/Scheduler.java index 7cbfe1d..262d91a 100644 --- a/src/main/java/sesim/Scheduler.java +++ b/src/main/java/sesim/Scheduler.java @@ -69,10 +69,18 @@ public class Scheduler extends Thread { } } - public long currentTimeMillis() { + /** + * + * @return + */ + public static long currentTimeMillis() { return System.currentTimeMillis(); } + + public static long timeStart=Scheduler.currentTimeMillis(); + + /** * @@ -80,7 +88,7 @@ public class Scheduler extends Thread { * @param time */ public void startTimerEvent(TimerTask e, long time) { - long evtime = time + this.currentTimeMillis(); + long evtime = time + currentTimeMillis(); synchronized (event_queue) { this.addEvent(e, time); } @@ -95,7 +103,7 @@ public class Scheduler extends Thread { private boolean addEvent(TimerTask e, long time) { - long evtime = time + this.currentTimeMillis(); + long evtime = time + currentTimeMillis(); SortedSet s = event_queue.get(evtime); if (s == null) { @@ -112,7 +120,7 @@ public class Scheduler extends Thread { } long t = event_queue.firstKey(); - if (t <= this.currentTimeMillis()) { + if (t <= currentTimeMillis()) { SortedSet s = event_queue.get(t); event_queue.remove(t); Iterator it = s.iterator(); @@ -124,7 +132,7 @@ public class Scheduler extends Thread { return 0; } else { - return t - this.currentTimeMillis(); + return t - currentTimeMillis(); } } diff --git a/src/main/java/traders/RandomTrader.java b/src/main/java/traders/RandomTrader.java index 56e912f..b0e654f 100644 --- a/src/main/java/traders/RandomTrader.java +++ b/src/main/java/traders/RandomTrader.java @@ -46,7 +46,7 @@ public class RandomTrader extends AutoTrader { sesim.Exchange.Account a = se.getAccount(account_id); long rc = this.doTrade(); - return rc / 600; + return rc ; } @@ -66,7 +66,7 @@ public class RandomTrader extends AutoTrader { public long timerTask() { sesim.Exchange.Account a = se.getAccount(account_id); long rc = this.doTrade(); - return rc / 28; + return rc / 80; // return this.event(); } diff --git a/src/main/java/traders/RandomTraderConfig.java b/src/main/java/traders/RandomTraderConfig.java index 4961235..2bf8c42 100644 --- a/src/main/java/traders/RandomTraderConfig.java +++ b/src/main/java/traders/RandomTraderConfig.java @@ -36,13 +36,13 @@ import sesim.Exchange; public class RandomTraderConfig extends AutoTraderConfig { public float[] sell_volume = {100, 100}; - public float[] sell_limit = {-3, 3}; - public int[] sell_order_wait = {500, 1500}; + public float[] sell_limit = {-1.5f, 1.5f}; + public int[] sell_order_wait = {1000, 5000}; public int[] wait_after_sell = {10, 30}; public float[] buy_volume = {100, 100}; - public float[] buy_limit = {-1, 3}; - public int[] buy_order_wait = {500, 1500}; + public float[] buy_limit = {-1.0f, 1.5f}; + public int[] buy_order_wait = {1000, 5000}; public int[] wait_after_buy = {10, 30}; @Override diff --git a/src/test/java/sesim/Test.java b/src/test/java/sesim/Test.java index ff4d2a5..ac98da3 100644 --- a/src/test/java/sesim/Test.java +++ b/src/test/java/sesim/Test.java @@ -28,27 +28,23 @@ package sesim; import java.util.SortedMap; import java.util.TreeMap; - - - - /** * * @author tobias */ public class Test { - - static void tube(){ - try{ - System.out.printf("Hello %s\n", "args"); - if (0==0) - return; - } - finally { - System.out.printf("Always %s\n", "the end"); - } - System.out.print("haha\n"); - } + + static void tube() { + try { + System.out.printf("Hello %s\n", "args"); + if (0 == 0) { + return; + } + } finally { + System.out.printf("Always %s\n", "the end"); + } + System.out.print("haha\n"); + } static void print_account(AccountData ad) { System.out.print( @@ -66,56 +62,66 @@ public class Test { * @param args the command line arguments */ public static void main(String[] args) throws InterruptedException { + + OHLCData od = new OHLCData(1000); + + od.realTimeAdd(12, 100, 10); + + od.realTimeAdd(5000, 100, 10); + //od.realTimeAdd(12, 100, 10); + + System.out.printf("Size: %d\n", od.size()); + + OHLCDataItem di; + di = od.get(2); + + System.exit(0); + Scheduler s = new Scheduler(); s.start(); - - class Ev implements Scheduler.TimerTask{ - @Override - public long timerTask() { - System.out.printf("Timer Event Occured %s\n",name); - if ("Ev1".equals(this.name)) + class Ev implements Scheduler.TimerTask { + + @Override + public long timerTask() { + System.out.printf("Timer Event Occured %s\n", name); + if ("Ev1".equals(this.name)) { return 2000; - else - return 4000; - } - - String name; - Ev(String name){ - this.name=name; - } - + } else { + return 4000; + } + } + + String name; + + Ev(String name) { + this.name = name; + } + } - + Ev e1 = new Ev("Ev1"); Ev e2 = new Ev("Eb2"); - - - s.startTimerEvent(e1, 0); - s.startTimerEvent(e2, 0); - - try - { - Thread.sleep(90000); - } - catch(Exception e) { - - } - - s.halt(); - while (s.isAlive()){ - - } - - System.out.print("All isstopped\n"); - // s.startTimerEvent(e2, 100); - - - - - /* long starttime=System.currentTimeMillis(); + s.startTimerEvent(e1, 0); + s.startTimerEvent(e2, 0); + + try { + Thread.sleep(90000); + } catch (Exception e) { + + } + + s.halt(); + while (s.isAlive()) { + + } + + System.out.print("All isstopped\n"); + + // s.startTimerEvent(e2, 100); + /* long starttime=System.currentTimeMillis(); while (s.isAlive()){ if (System.currentTimeMillis()>starttime+6650){ s.stop(); @@ -128,9 +134,7 @@ public class Test { } System.out.print("All isstopped\n"); - */ - - + */ } }