Added some documentation.
This commit is contained in:
parent
8264214ab2
commit
bcaf3a2aeb
@ -9,6 +9,8 @@ import java.awt.*;
|
|||||||
import sesim.Exchange.*;
|
import sesim.Exchange.*;
|
||||||
import sesim.Quote;
|
import sesim.Quote;
|
||||||
import gui.MainWin;
|
import gui.MainWin;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -33,9 +35,9 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver {
|
|||||||
|
|
||||||
int item_width = 10;
|
int item_width = 10;
|
||||||
int items = 350;
|
int items = 350;
|
||||||
long ntime = -1;
|
long ntime = 0;
|
||||||
|
|
||||||
OHLCData data;
|
OHLCData data = new OHLCData();
|
||||||
|
|
||||||
OHLCDataItem current = null;
|
OHLCDataItem current = null;
|
||||||
|
|
||||||
@ -48,14 +50,14 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver {
|
|||||||
|
|
||||||
private void realTimeAdd(long time, float price, float volume) {
|
private void realTimeAdd(long time, float price, float volume) {
|
||||||
|
|
||||||
/* System.out.print("Diff:"
|
/*System.out.print("Diff:"
|
||||||
+(ntime-time)
|
+(ntime-time)
|
||||||
+"\n"
|
+"\n"
|
||||||
);
|
);*/
|
||||||
*/
|
|
||||||
if (time > ntime) {
|
if (time > ntime) {
|
||||||
|
|
||||||
System.out.print("new raster ----------------------------------\n");
|
// System.out.print("new raster ----------------------------------\n");
|
||||||
current = null;
|
current = null;
|
||||||
ntime = rasterTime(time) + 5000;
|
ntime = rasterTime(time) + 5000;
|
||||||
// System.out.print(ntime+"\n");
|
// System.out.print(ntime+"\n");
|
||||||
@ -95,8 +97,12 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver {
|
|||||||
this.getSize();
|
this.getSize();
|
||||||
|
|
||||||
int pwidth = item_width * items;
|
int pwidth = item_width * items;
|
||||||
|
int phight = 40;
|
||||||
|
|
||||||
this.setPreferredSize(new Dimension(pwidth, 400));
|
this.setPreferredSize(new Dimension(pwidth, phight));
|
||||||
|
|
||||||
|
Dimension dim = this.getSize();
|
||||||
|
// System.out.print("Diemension "+dim.width+" "+dim.height+"\n");
|
||||||
|
|
||||||
g.setColor(Color.RED);
|
g.setColor(Color.RED);
|
||||||
g.drawLine(0,0,100,100);
|
g.drawLine(0,0,100,100);
|
||||||
@ -109,13 +115,58 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (this.current == null) {
|
// if (this.current == null) {
|
||||||
return;
|
// return;
|
||||||
|
// }
|
||||||
|
|
||||||
|
ArrayList <OHLCDataItem> od = data.data;
|
||||||
|
|
||||||
|
System.out.print("OD S: "+od.size()+"\n");
|
||||||
|
g.setColor(Color.BLUE);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Iterator <OHLCDataItem> it = od.iterator();
|
||||||
|
int myi=0;
|
||||||
|
while (it.hasNext()){
|
||||||
|
OHLCDataItem di = it.next();
|
||||||
|
|
||||||
|
float val = di.close;
|
||||||
|
float max = data.max;
|
||||||
|
float min = data.min;
|
||||||
|
|
||||||
|
if (min==max){
|
||||||
|
min = val/2;
|
||||||
|
max = val*2;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
System.out.print("Fval: "+val+" "+min+"\n");
|
||||||
|
val -= min;
|
||||||
|
System.out.print("VAL New"+val+"\n");
|
||||||
|
|
||||||
|
//val/ ((data.max-data.min)/dim.height);
|
||||||
|
|
||||||
|
|
||||||
|
System.out.print("MINMAX "+min+" "+max+"\n");
|
||||||
|
|
||||||
|
val = dim.height*val/(data.max-data.min);
|
||||||
|
|
||||||
|
|
||||||
|
int x = myi * this.item_width;
|
||||||
|
myi++;
|
||||||
|
g.drawLine(x, 0, x, (int)val);
|
||||||
|
|
||||||
|
System.out.print("Draw Line: "+x+" "+val+"\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
g.setColor(Color.BLUE);
|
|
||||||
g.drawLine(0, 0, 100, (int) ((this.current.close-80.0)*80.0));
|
// g.drawLine(0, 0, 100, (int) ((this.current.close-80.0)*80.0));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,7 +182,7 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver {
|
|||||||
// g.get
|
// g.get
|
||||||
|
|
||||||
Rectangle bounds = g.getDeviceConfiguration().getBounds();
|
Rectangle bounds = g.getDeviceConfiguration().getBounds();
|
||||||
System.out.print(bounds.width + "\n");
|
// System.out.print(bounds.width + "\n");
|
||||||
|
|
||||||
//g.fillRect(0, 0, 100, 100);
|
//g.fillRect(0, 0, 100, 100);
|
||||||
Dimension d = this.getSize();
|
Dimension d = this.getSize();
|
||||||
@ -170,8 +221,10 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void UpdateQuote(Quote q) {
|
public void UpdateQuote(Quote q) {
|
||||||
// System.out.print("Quote Received\n");
|
// System.out.print("Quote Received\n");
|
||||||
this.realTimeAdd(q.time, (float) q.price, (float)q.volume);
|
// this.realTimeAdd(q.time, (float) q.price, (float)q.volume);
|
||||||
|
|
||||||
|
data.realTimeAdd(q.time, (float)q.price, (float)q.volume);
|
||||||
// this.invalidate();
|
// this.invalidate();
|
||||||
this.repaint();
|
this.repaint();
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,8 @@ import java.util.*;
|
|||||||
*/
|
*/
|
||||||
public class OHLCData { //extends ArrayList <OHLCDataItem> {
|
public class OHLCData { //extends ArrayList <OHLCDataItem> {
|
||||||
|
|
||||||
float max;
|
float max=0;
|
||||||
|
float min=0;
|
||||||
|
|
||||||
long time_start;
|
long time_start;
|
||||||
long time_step;
|
long time_step;
|
||||||
@ -55,19 +56,36 @@ public class OHLCData { //extends ArrayList <OHLCDataItem> {
|
|||||||
|
|
||||||
|
|
||||||
private void updateMinMax(float price){
|
private void updateMinMax(float price){
|
||||||
// if (price>max)
|
if (price > max){
|
||||||
|
|
||||||
|
max = price;
|
||||||
|
}
|
||||||
|
if (price < min){
|
||||||
|
min = price;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private long ntime = 0;
|
private long ntime = 0;
|
||||||
|
|
||||||
boolean realTimeAdd(long time, float price, float volume) {
|
boolean realTimeAdd(long time, float price, float volume) {
|
||||||
|
|
||||||
|
|
||||||
if (time > ntime) {
|
if (time > ntime) {
|
||||||
|
if (ntime==0){
|
||||||
|
System.out.print ("Setting ntimt was zero\n");
|
||||||
|
this.min=price;
|
||||||
|
this.max=price;
|
||||||
|
}
|
||||||
|
|
||||||
ntime = rasterTime(time) + 5000;
|
ntime = rasterTime(time) + 5000;
|
||||||
data.add(new OHLCDataItem(price, price, price, price, volume));
|
data.add(new OHLCDataItem(price, price, price, price, volume));
|
||||||
|
this.updateMinMax(price);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
OHLCDataItem d = data.get(data.size() - 1);
|
OHLCDataItem d = data.get(data.size() - 1);
|
||||||
|
this.updateMinMax(price);
|
||||||
boolean rc = d.update(price, volume);
|
boolean rc = d.update(price, volume);
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
*/
|
*/
|
||||||
package gui;
|
package gui;
|
||||||
|
|
||||||
import sesim.Order_old.*;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import sesim.Exchange.*;
|
import sesim.Exchange.*;
|
||||||
|
|
||||||
@ -36,7 +35,7 @@ import sesim.Exchange.*;
|
|||||||
public class AskBook extends OrderBook {
|
public class AskBook extends OrderBook {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
ArrayList getOrderBook() {
|
ArrayList <Order> getOrderBook() {
|
||||||
return MainWin.se.getOrderBook(OrderType.ASK,40);
|
return MainWin.se.getOrderBook(OrderType.ASK,40);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
*/
|
*/
|
||||||
package gui;
|
package gui;
|
||||||
|
|
||||||
import sesim.Order_old.*;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import sesim.Exchange.*;
|
import sesim.Exchange.*;
|
||||||
|
|
||||||
@ -36,7 +35,7 @@ import sesim.Exchange.*;
|
|||||||
public class BidBook extends OrderBook {
|
public class BidBook extends OrderBook {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
ArrayList getOrderBook() {
|
ArrayList <Order> getOrderBook() {
|
||||||
return MainWin.se.getOrderBook(OrderType.BID, 40);
|
return MainWin.se.getOrderBook(OrderType.BID, 40);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
*/
|
*/
|
||||||
package gui;
|
package gui;
|
||||||
|
|
||||||
|
import sesim.*;
|
||||||
import sesim.Exchange;
|
import sesim.Exchange;
|
||||||
import sesim.Exchange.*;
|
import sesim.Exchange.*;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
@ -68,6 +69,10 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver {
|
|||||||
*/
|
*/
|
||||||
public Chart() {
|
public Chart() {
|
||||||
initComponents();
|
initComponents();
|
||||||
|
|
||||||
|
sesim.IDGenerator idgen = new IDGenerator();
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
String stockSymbol = "Schliemanz Koch AG";
|
String stockSymbol = "Schliemanz Koch AG";
|
||||||
//String stockSymbol = "MSFT";
|
//String stockSymbol = "MSFT";
|
||||||
@ -192,7 +197,7 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver {
|
|||||||
data.add(d);
|
data.add(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.print(data.size() + "\n");
|
// System.out.print(data.size() + "\n");
|
||||||
// System.exit(0);
|
// System.exit(0);
|
||||||
|
|
||||||
return data.toArray(new OHLCDataItem[data.size()]);
|
return data.toArray(new OHLCDataItem[data.size()]);
|
||||||
|
@ -25,14 +25,7 @@
|
|||||||
*/
|
*/
|
||||||
package gui;
|
package gui;
|
||||||
|
|
||||||
import traders.RandomTraderConfig_old;
|
|
||||||
import traders.SwitchingTraderConfig;
|
|
||||||
import sesim.AutoTraderLIst;
|
|
||||||
import sesim.Exchange;
|
import sesim.Exchange;
|
||||||
import sesim.BuyOrder;
|
|
||||||
import javax.swing.UIManager;
|
|
||||||
import javax.swing.*;
|
|
||||||
import sesim.AccountData;
|
|
||||||
import traders.RandomTrader;
|
import traders.RandomTrader;
|
||||||
import traders.RandomTraderConfig;
|
import traders.RandomTraderConfig;
|
||||||
|
|
||||||
@ -188,15 +181,21 @@ public class MainWin extends javax.swing.JFrame {
|
|||||||
se = new Exchange();
|
se = new Exchange();
|
||||||
|
|
||||||
|
|
||||||
RandomTraderConfig rcfg = new RandomTraderConfig();
|
//RandomTraderConfig rcfg = new RandomTraderConfig();
|
||||||
RandomTrader rt = rcfg.createTrader(se, 1000, 100);
|
//RandomTrader rt = rcfg.createTrader(se, 1000, 100);
|
||||||
rt.start();
|
//rt.start();
|
||||||
|
|
||||||
RandomTraderConfig rcfg1 = new RandomTraderConfig();
|
RandomTraderConfig rcfg1 = new RandomTraderConfig();
|
||||||
RandomTrader rt1 = rcfg.createTrader(se, 1000, 100);
|
RandomTrader rt1 = rcfg1.createTrader(se, 1000000000, 0);
|
||||||
rt1.start();
|
rt1.start();
|
||||||
|
|
||||||
|
|
||||||
|
RandomTraderConfig cfg = new RandomTraderConfig();
|
||||||
|
for (int i=0; i<5000; i++){
|
||||||
|
RandomTrader randt = cfg.createTrader(se, 100, 100);
|
||||||
|
randt.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//RandomTrader rt = new RandomTrader();
|
//RandomTrader rt = new RandomTrader();
|
||||||
//rt.start();
|
//rt.start();
|
||||||
|
@ -48,7 +48,7 @@ public abstract class OrderBook extends javax.swing.JPanel implements Exchange.B
|
|||||||
|
|
||||||
OrderBookListModel model;
|
OrderBookListModel model;
|
||||||
|
|
||||||
abstract ArrayList getOrderBook();
|
abstract ArrayList <Order> getOrderBook();
|
||||||
|
|
||||||
private Color hdr_color = Color.LIGHT_GRAY;
|
private Color hdr_color = Color.LIGHT_GRAY;
|
||||||
|
|
||||||
@ -71,7 +71,7 @@ public abstract class OrderBook extends javax.swing.JPanel implements Exchange.B
|
|||||||
|
|
||||||
class Updater implements Runnable{
|
class Updater implements Runnable{
|
||||||
OrderBookListModel model;
|
OrderBookListModel model;
|
||||||
ArrayList newlist;
|
ArrayList <Order> newlist;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@ -97,7 +97,7 @@ public abstract class OrderBook extends javax.swing.JPanel implements Exchange.B
|
|||||||
|
|
||||||
protected class OrderBookListModel extends AbstractTableModel {
|
protected class OrderBookListModel extends AbstractTableModel {
|
||||||
|
|
||||||
private ArrayList <OrderBookItem> list;
|
private ArrayList <Order> list;
|
||||||
//private final boolean desc = false;
|
//private final boolean desc = false;
|
||||||
|
|
||||||
public OrderBookListModel() {
|
public OrderBookListModel() {
|
||||||
@ -146,7 +146,7 @@ public abstract class OrderBook extends javax.swing.JPanel implements Exchange.B
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getValueAt(int r, int c) {
|
public Object getValueAt(int r, int c) {
|
||||||
OrderBookItem o;
|
Order o;
|
||||||
|
|
||||||
int s = list.size();
|
int s = list.size();
|
||||||
//System.out.print("Looking for Value at" + r + ":" + c + " w size:" + s + "\n");
|
//System.out.print("Looking for Value at" + r + ":" + c + " w size:" + s + "\n");
|
||||||
@ -159,12 +159,12 @@ public abstract class OrderBook extends javax.swing.JPanel implements Exchange.B
|
|||||||
Formatter f = new Formatter();
|
Formatter f = new Formatter();
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 0:
|
case 0:
|
||||||
return String.format("#%06x", o.id);
|
return String.format("#%06x", o.getID());
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
return String.format("%.4f",o.limit);
|
return String.format("%.4f",o.getLimit());
|
||||||
case 2:
|
case 2:
|
||||||
return String.format("%.4f", o.volume);
|
return String.format("%.4f", o.getVolume());
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
@ -125,7 +125,7 @@ public class QuotePanel extends javax.swing.JPanel implements sesim.Exchange.Quo
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
u.text = String.format("%.2f\n(%d)", q.price,q.volume);
|
u.text = String.format("%.2f\n(%.0f)", q.price,q.volume);
|
||||||
|
|
||||||
SwingUtilities.invokeLater(u);
|
SwingUtilities.invokeLater(u);
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ package sesim;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author tobias
|
* @author 7u83
|
||||||
*/
|
*/
|
||||||
public abstract class AutoTraderConfig {
|
public abstract class AutoTraderConfig {
|
||||||
public abstract AutoTrader createTrader(Exchange se, double money, double shares);
|
public abstract AutoTrader createTrader(Exchange se, double money, double shares);
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package sesim;
|
package sesim;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.*;
|
|
||||||
|
|
||||||
import sesim.Order_old.OrderStatus;
|
import sesim.Order_old.OrderStatus;
|
||||||
import sesim.Order_old.OrderType_old;
|
import sesim.Order_old.OrderType_old;
|
||||||
@ -17,15 +16,18 @@ public class Exchange extends Thread {
|
|||||||
}
|
}
|
||||||
|
|
||||||
IDGenerator account_id = new IDGenerator();
|
IDGenerator account_id = new IDGenerator();
|
||||||
public static Timer timer=new Timer();
|
public static Timer timer = new Timer();
|
||||||
|
|
||||||
private class Account implements Comparable {
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class Account implements Comparable {
|
||||||
|
|
||||||
protected double id;
|
protected double id;
|
||||||
protected double shares;
|
protected double shares;
|
||||||
protected double money;
|
protected double money;
|
||||||
|
|
||||||
protected HashMap<Long, Order> orders;
|
private final HashMap<Long, Order> orders;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(Object a) {
|
public int compareTo(Object a) {
|
||||||
@ -39,6 +41,19 @@ public class Exchange extends Thread {
|
|||||||
this.money = money;
|
this.money = money;
|
||||||
this.shares = shares;
|
this.shares = shares;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double getID() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getShares() {
|
||||||
|
return shares;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getMoney() {
|
||||||
|
return money;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//private TreeSet<Account> accounts = new TreeSet<>();
|
//private TreeSet<Account> accounts = new TreeSet<>();
|
||||||
@ -76,14 +91,15 @@ public class Exchange extends Thread {
|
|||||||
return d > 0 ? 1 : -1;
|
return d > 0 ? 1 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(left.id<right.id)
|
if (left.id < right.id) {
|
||||||
return -1;
|
return -1;
|
||||||
if(left.id>right.id)
|
}
|
||||||
|
if (left.id > right.id) {
|
||||||
return 1;
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
|
||||||
// return left.id < right.id ? -1 : 1;
|
// return left.id < right.id ? -1 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,15 +111,15 @@ public class Exchange extends Thread {
|
|||||||
|
|
||||||
IDGenerator order_id = new IDGenerator();
|
IDGenerator order_id = new IDGenerator();
|
||||||
|
|
||||||
private class Order {
|
public class Order {
|
||||||
|
|
||||||
OrderType type;
|
OrderType type;
|
||||||
double limit;
|
private double limit;
|
||||||
double volume;
|
private double volume;
|
||||||
double initial_volume;
|
private final double initial_volume;
|
||||||
long id;
|
private long id;
|
||||||
long created;
|
long created;
|
||||||
Account account;
|
private Account account;
|
||||||
|
|
||||||
Order(Account account, OrderType type, double volume, double limit) {
|
Order(Account account, OrderType type, double volume, double limit) {
|
||||||
id = order_id.getNext();
|
id = order_id.getNext();
|
||||||
@ -114,6 +130,31 @@ public class Exchange extends Thread {
|
|||||||
this.initial_volume = volume;
|
this.initial_volume = volume;
|
||||||
this.created = System.currentTimeMillis();
|
this.created = System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long getID() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getVolume() {
|
||||||
|
return volume;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getLimit() {
|
||||||
|
return limit;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OrderType getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getExecuted() {
|
||||||
|
return initial_volume - volume;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getInitialVolume() {
|
||||||
|
return initial_volume;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -168,6 +209,36 @@ public class Exchange extends Thread {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Quote getCurrentPrice() {
|
||||||
|
|
||||||
|
TreeSet<Order> bid = order_books.get(OrderType.BID);
|
||||||
|
TreeSet<Order> ask = order_books.get(OrderType.ASK);
|
||||||
|
|
||||||
|
Quote q = null;
|
||||||
|
|
||||||
|
tradelock.lock();
|
||||||
|
if (!bid.isEmpty() && !ask.isEmpty()) {
|
||||||
|
q = new Quote();
|
||||||
|
q.price = (bid.first().limit + ask.first().limit) / 2.0;
|
||||||
|
|
||||||
|
}
|
||||||
|
tradelock.unlock();
|
||||||
|
|
||||||
|
if (q != null) {
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.quoteHistory.isEmpty()) {
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
q = this.quoteHistory.last();
|
||||||
|
|
||||||
|
return q;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/* public SortedSet<Quote> getQuoteHistory(int seconds) {
|
/* public SortedSet<Quote> getQuoteHistory(int seconds) {
|
||||||
Quote last = quoteHistory.last();
|
Quote last = quoteHistory.last();
|
||||||
return this.getQuoteHistory(seconds, last.time);
|
return this.getQuoteHistory(seconds, last.time);
|
||||||
@ -283,38 +354,25 @@ public class Exchange extends Thread {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class OrderBookItem {
|
public ArrayList<Order> getOrderBook(OrderType type, int depth) {
|
||||||
|
|
||||||
public long id;
|
|
||||||
public double limit;
|
|
||||||
public double volume;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayList<OrderBookItem> getOrderBook(OrderType type, int depth) {
|
|
||||||
|
|
||||||
TreeSet<Order> book = order_books.get(type);
|
TreeSet<Order> book = order_books.get(type);
|
||||||
if (book == null) {
|
if (book == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayList<OrderBookItem> ret = new ArrayList<>();
|
ArrayList<Order> ret = new ArrayList<>();
|
||||||
|
|
||||||
Iterator<Order> it = book.iterator();
|
Iterator<Order> it = book.iterator();
|
||||||
|
|
||||||
for (int i = 0; i < depth && it.hasNext(); i++) {
|
for (int i = 0; i < depth && it.hasNext(); i++) {
|
||||||
|
ret.add(it.next());
|
||||||
Order o = it.next();
|
|
||||||
OrderBookItem n = new OrderBookItem();
|
|
||||||
n.id = o.id;
|
|
||||||
n.limit = o.limit;
|
|
||||||
n.volume = o.volume;
|
|
||||||
|
|
||||||
ret.add(n);
|
|
||||||
//System.out.print("Order_old" + o.limit);
|
|
||||||
//System.out.println();
|
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Quote getLastQuoete() {
|
||||||
|
return this.quoteHistory.first();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void print_current() {
|
public void print_current() {
|
||||||
@ -372,21 +430,12 @@ public class Exchange extends Thread {
|
|||||||
tradelock.lock();
|
tradelock.lock();
|
||||||
Order o = a.orders.get(order_id);
|
Order o = a.orders.get(order_id);
|
||||||
|
|
||||||
// System.out.print("The Order:"+o.limit+"\n");
|
// System.out.print("The Order:"+o.limit+"\n");
|
||||||
|
|
||||||
if (o != null) {
|
if (o != null) {
|
||||||
TreeSet ob =order_books.get(o.type);
|
TreeSet ob = order_books.get(o.type);
|
||||||
|
|
||||||
System.out.print("We have the orderbook"+ob.size()+"\n");
|
boolean rc = ob.remove(o);
|
||||||
|
|
||||||
System.out.print("Want to remove:"+o.limit+" "+o.volume+" "+o.id+"\n");
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
boolean rc = ob.remove(o);
|
|
||||||
|
|
||||||
|
|
||||||
System.out.print("My first rc = :" + rc);
|
|
||||||
a.orders.remove(o.id);
|
a.orders.remove(o.id);
|
||||||
ret = true;
|
ret = true;
|
||||||
}
|
}
|
||||||
@ -456,9 +505,7 @@ public class Exchange extends Thread {
|
|||||||
Order a = ask.first();
|
Order a = ask.first();
|
||||||
|
|
||||||
if (b.limit < a.limit) {
|
if (b.limit < a.limit) {
|
||||||
System.out.print("No match\n");
|
break;
|
||||||
// no match, nothing to do
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// There is a match, calculate price and volume
|
// There is a match, calculate price and volume
|
||||||
@ -467,7 +514,7 @@ public class Exchange extends Thread {
|
|||||||
|
|
||||||
// Transfer money and shares
|
// Transfer money and shares
|
||||||
transferMoneyAndShares(b.account, a.account, volume * price, -volume);
|
transferMoneyAndShares(b.account, a.account, volume * price, -volume);
|
||||||
|
//System.out.print("Transfer Shares was called with volume "+volume+"\n");
|
||||||
// Update volume
|
// Update volume
|
||||||
b.volume -= volume;
|
b.volume -= volume;
|
||||||
a.volume -= volume;
|
a.volume -= volume;
|
||||||
@ -479,14 +526,20 @@ public class Exchange extends Thread {
|
|||||||
removeOrderIfExecuted(b);
|
removeOrderIfExecuted(b);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
//System.out.print("Volume total is "+volume_total+"\n");
|
||||||
|
if (volume_total == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
Quote q = new Quote();
|
Quote q = new Quote();
|
||||||
q.price = money_total / volume_total;
|
q.price = money_total / volume_total;
|
||||||
q.volume = volume_total;
|
q.volume = volume_total;
|
||||||
q.time = System.currentTimeMillis();
|
q.time = System.currentTimeMillis();
|
||||||
|
|
||||||
System.out.print("Price" + q.price + "," + q.volume + "\n");
|
// System.out.print("There was a trade:"+q.price+"\n");
|
||||||
//this.updateQuoteReceivers(q);
|
|
||||||
|
this.quoteHistory.add(q);
|
||||||
|
this.updateQuoteReceivers(q);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void executeOrders_old() {
|
private void executeOrders_old() {
|
||||||
@ -681,6 +734,10 @@ public class Exchange extends Thread {
|
|||||||
return a.orders.size();
|
return a.orders.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Account getAccount(double account_id) {
|
||||||
|
return accounts.get(account_id);
|
||||||
|
}
|
||||||
|
|
||||||
public AccountData getAccountData(double account_id) {
|
public AccountData getAccountData(double account_id) {
|
||||||
Account a = accounts.get(account_id);
|
Account a = accounts.get(account_id);
|
||||||
if (a == null) {
|
if (a == null) {
|
||||||
@ -692,18 +749,18 @@ public class Exchange extends Thread {
|
|||||||
ad.money = a.money;
|
ad.money = a.money;
|
||||||
ad.shares = a.shares;
|
ad.shares = a.shares;
|
||||||
|
|
||||||
ad.orders = new ArrayList<OrderData>();
|
ad.orders = new ArrayList<>();
|
||||||
ad.orders.iterator();
|
ad.orders.iterator();
|
||||||
|
|
||||||
a.orders.values();
|
a.orders.values();
|
||||||
Set s = a.orders.keySet();
|
Set s = a.orders.keySet();
|
||||||
Iterator it = s.iterator();
|
Iterator it = s.iterator();
|
||||||
System.out.print("Keys list" + s.size() + "\n");
|
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
long x = (long) it.next();
|
long x = (long) it.next();
|
||||||
System.out.print("X" + x + "\n");
|
|
||||||
Order o = a.orders.get(x);
|
Order o = a.orders.get(x);
|
||||||
System.out.print("oGot: " + o.limit + " " + o.volume + "\n");
|
|
||||||
OrderData od = new OrderData();
|
OrderData od = new OrderData();
|
||||||
od.id = o.id;
|
od.id = o.id;
|
||||||
od.limit = o.limit;
|
od.limit = o.limit;
|
||||||
|
@ -26,37 +26,40 @@
|
|||||||
package sesim;
|
package sesim;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Implementation of a simple ID generator to create uniqe IDs of type long
|
||||||
*
|
*
|
||||||
* @author 7u83 <7u83@mail.ru>
|
* @author 7u83 <7u83@mail.ru>
|
||||||
*/
|
*/
|
||||||
class IDGenerator{
|
public class IDGenerator {
|
||||||
private final Locker ID_LOCKER = new Locker();
|
|
||||||
private long next_id;
|
|
||||||
|
|
||||||
/**
|
private final Locker ID_LOCKER = new Locker();
|
||||||
* Initialize the ID generator
|
private long next_id;
|
||||||
* @param start ID value to start with
|
|
||||||
*/
|
|
||||||
public IDGenerator(long start){
|
|
||||||
next_id=start;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize ID Generator with start ID = 0
|
* Initialize the ID generator
|
||||||
*/
|
*
|
||||||
public IDGenerator(){
|
* @param start ID value to start with
|
||||||
this(0);
|
*/
|
||||||
}
|
public IDGenerator(long start) {
|
||||||
|
next_id = start;
|
||||||
/**
|
|
||||||
* Get the next ID
|
|
||||||
* @return the next generated ID
|
|
||||||
*/
|
|
||||||
public long getNext(){
|
|
||||||
ID_LOCKER.lock();
|
|
||||||
long id = next_id++;
|
|
||||||
ID_LOCKER.unlock();
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize ID Generator with start ID = 0
|
||||||
|
*/
|
||||||
|
public IDGenerator() {
|
||||||
|
this(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the next ID
|
||||||
|
*
|
||||||
|
* @return the next generated ID
|
||||||
|
*/
|
||||||
|
public long getNext() {
|
||||||
|
ID_LOCKER.lock();
|
||||||
|
long id = next_id++;
|
||||||
|
ID_LOCKER.unlock();
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -36,7 +36,7 @@ public class Locker {
|
|||||||
private final Semaphore AVAIL = new Semaphore(1, true);
|
private final Semaphore AVAIL = new Semaphore(1, true);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Acquire a lock
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public boolean lock() {
|
public boolean lock() {
|
||||||
|
@ -52,7 +52,7 @@ public class RandomTrader extends AutoTrader {
|
|||||||
|
|
||||||
long event(){
|
long event(){
|
||||||
|
|
||||||
System.out.print("Hello world Iam a trader\n");
|
// System.out.print("Hello world Iam a trader\n");
|
||||||
return this.doTrade();
|
return this.doTrade();
|
||||||
// doBuy();
|
// doBuy();
|
||||||
|
|
||||||
@ -129,16 +129,16 @@ public class RandomTrader extends AutoTrader {
|
|||||||
|
|
||||||
public long cancelOrders(){
|
public long cancelOrders(){
|
||||||
int n = se.getNumberOfOpenOrders(account_id);
|
int n = se.getNumberOfOpenOrders(account_id);
|
||||||
System.out.print("Open Orders: "+n+"\n");
|
// System.out.print("Open Orders: "+n+"\n");
|
||||||
if (n>0){
|
if (n>0){
|
||||||
System.out.print("Want to killń\n");
|
// System.out.print("Want to killń\n");
|
||||||
AccountData ad = se.getAccountData(account_id);
|
AccountData ad = se.getAccountData(account_id);
|
||||||
Iterator <OrderData> it = ad.orders.iterator();
|
Iterator <OrderData> it = ad.orders.iterator();
|
||||||
while (it.hasNext()){
|
while (it.hasNext()){
|
||||||
OrderData od=it.next();
|
OrderData od=it.next();
|
||||||
boolean rc = se.cancelOrder(account_id, od.id);
|
boolean rc = se.cancelOrder(account_id, od.id);
|
||||||
System.out.print("killer rc "+rc+"\n");
|
// System.out.print("killer rc "+rc+"\n");
|
||||||
System.out.print("Killing: "+od.id+"\n");
|
// System.out.print("Killing: "+od.id+"\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,8 +155,10 @@ public class RandomTrader extends AutoTrader {
|
|||||||
// how much money we ant to envest?
|
// how much money we ant to envest?
|
||||||
double money = getRandomAmmount(ad.money, myconfig.buy_volume);
|
double money = getRandomAmmount(ad.money, myconfig.buy_volume);
|
||||||
|
|
||||||
|
Quote q = se.getCurrentPrice();
|
||||||
|
double lp = q == null ? 100.0 : q.price;
|
||||||
|
|
||||||
|
|
||||||
double lp = 100.0; //se.getBestLimit(type);
|
|
||||||
double limit;
|
double limit;
|
||||||
limit = lp + getRandomAmmount(lp, myconfig.buy_limit);
|
limit = lp + getRandomAmmount(lp, myconfig.buy_limit);
|
||||||
|
|
||||||
@ -165,8 +167,8 @@ public class RandomTrader extends AutoTrader {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.print("Volume is:"+volume+"\n");
|
// System.out.print("Volume is:"+volume+"\n");
|
||||||
System.out.print("My Ammount is: "+money+" My limit si:"+limit+ "\n");
|
// System.out.print("My Ammount is: "+money+" My limit si:"+limit+ "\n");
|
||||||
|
|
||||||
|
|
||||||
se.createOrder(account_id, type, volume, limit);
|
se.createOrder(account_id, type, volume, limit);
|
||||||
@ -187,7 +189,12 @@ public class RandomTrader extends AutoTrader {
|
|||||||
double volume = (long)getRandomAmmount(ad.shares, myconfig.sell_volume);
|
double volume = (long)getRandomAmmount(ad.shares, myconfig.sell_volume);
|
||||||
|
|
||||||
|
|
||||||
double lp = 100.0; //se.getBestLimit(type);
|
// double lp = 100.0; //se.getBestLimit(type);
|
||||||
|
|
||||||
|
Quote q = se.getCurrentPrice();
|
||||||
|
double lp = q == null ? 100.0 : q.price;
|
||||||
|
|
||||||
|
|
||||||
double limit;
|
double limit;
|
||||||
limit = lp + getRandomAmmount(lp, myconfig.sell_limit);
|
limit = lp + getRandomAmmount(lp, myconfig.sell_limit);
|
||||||
|
|
||||||
@ -196,8 +203,8 @@ public class RandomTrader extends AutoTrader {
|
|||||||
// return false;
|
// return false;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
System.out.print("Volume is:"+volume+"\n");
|
// System.out.print("Volume is:"+volume+"\n");
|
||||||
System.out.print("My Ammount is: "+volume+" My limit si:"+limit+ "\n");
|
// System.out.print("My Ammount is: "+volume+" My limit si:"+limit+ "\n");
|
||||||
|
|
||||||
|
|
||||||
se.createOrder(account_id, type, volume, limit);
|
se.createOrder(account_id, type, volume, limit);
|
||||||
|
Loading…
Reference in New Issue
Block a user