Added some stuff.

This commit is contained in:
7u83 2017-01-14 14:42:00 +01:00
parent 4973868db5
commit 7bf55870b1
6 changed files with 65 additions and 44 deletions

View File

@ -42,7 +42,8 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver {
OHLCDataItem current = null; OHLCDataItem current = null;
int min;
int max;
int getY(float Y){ int getY(float Y){
return 0; return 0;
@ -200,12 +201,12 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver {
// max = 5; // max = 5;
// min = 0; // min = 0;
System.out.print("Fval: " + y + " " + min + "\n"); // System.out.print("Fval: " + y + " " + min + "\n");
y -= min; y -= min;
System.out.print("VAL New" + y + "\n"); // System.out.print("VAL New" + y + "\n");
//val/ ((data.max-data.min)/dim.height); //val/ ((data.max-data.min)/dim.height);
System.out.print("MINMAX " + min + " " + max + " " + dim.height + "\n"); // System.out.print("MINMAX " + min + " " + max + " " + dim.height + "\n");
y = dim.height - (dim.height * y / (max - min)); y = dim.height - (dim.height * y / (max - min));
@ -217,7 +218,7 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver {
lastx = x; lastx = x;
lasty = (int) y; lasty = (int) y;
System.out.print("Draw Line: " + x + " " + y + "\n"); // System.out.print("Draw Line: " + x + " " + y + "\n");
} }

View File

@ -183,7 +183,7 @@ public class MainWin extends javax.swing.JFrame {
}//GEN-LAST:event_jButton1ActionPerformed }//GEN-LAST:event_jButton1ActionPerformed
private void FileRunActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FileRunActionPerformed private void FileRunActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FileRunActionPerformed
se.start(); // se.start();
}//GEN-LAST:event_FileRunActionPerformed }//GEN-LAST:event_FileRunActionPerformed
private void helpAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpAboutActionPerformed private void helpAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpAboutActionPerformed
@ -206,14 +206,16 @@ public class MainWin extends javax.swing.JFrame {
// SwitchingTraderConfig rcfg1 = new SwitchingTraderConfig(); // SwitchingTraderConfig rcfg1 = new SwitchingTraderConfig();
RandomTraderConfig rcfg1 = new RandomTraderConfig(); RandomTraderConfig rcfg1 = new RandomTraderConfig();
AutoTrader rt1 = rcfg1.createTrader(se, 1000000, 0); AutoTrader rt1 = rcfg1.createTrader(se, 1000000, 0);
rt1.setName("Bob");
rt1.start(); rt1.start();
//AutoTrader rt2 = rcfg1.createTrader(se, 1, 100); //AutoTrader rt2 = rcfg1.createTrader(se, 1, 100);
//rt2.start(); //rt2.start();
RandomTraderConfig cfg = new RandomTraderConfig(); RandomTraderConfig cfg = new RandomTraderConfig();
for (int i=0; i<100; i++){ for (int i=0; i<10000; i++){
AutoTrader randt = cfg.createTrader(se, 100, 100); AutoTrader randt = cfg.createTrader(se, 100, 100);
randt.setName("Alice");
randt.start(); randt.start();
} }

View File

@ -35,6 +35,8 @@ public abstract class AutoTrader {
protected Exchange se; protected Exchange se;
protected AutoTraderConfig config; protected AutoTraderConfig config;
protected String name;
@ -42,12 +44,17 @@ public abstract class AutoTrader {
account_id = se.createAccount(money, shares); account_id = se.createAccount(money, shares);
this.se=se; this.se=se;
this.config=config; this.config=config;
this.name="";
} }
/*public AutoTrader(Exchange se, double money, double shares){ public void setName(String name){
this(se,money,shares,null); this.name=name;
}
public String getName(){
return name;
} }
*/
public abstract void start(); public abstract void start();

View File

@ -9,7 +9,7 @@ import sesim.Order_old.OrderType_old;
* *
* @author tube * @author tube
*/ */
public class Exchange extends Thread { public class Exchange { //extends Thread {
public enum OrderType { public enum OrderType {
BID, ASK BID, ASK
@ -23,9 +23,9 @@ public class Exchange extends Thread {
*/ */
public class Account implements Comparable { public class Account implements Comparable {
protected double id; private double id;
protected double shares; private double shares;
protected double money; private double money;
private final HashMap<Long, Order> orders; private final HashMap<Long, Order> orders;
@ -287,12 +287,13 @@ public class Exchange extends Thread {
while (i.hasNext()) { while (i.hasNext()) {
i.next().UpdateOrderBook(); i.next().UpdateOrderBook();
} }
try { /* try {
sleep(10); sleep(10);
} catch (InterruptedException e) { } catch (InterruptedException e) {
System.out.println("I was Interrupted"); System.out.println("I was Interrupted");
} }
*/
} }
// Here we store the list of quote receivers // Here we store the list of quote receivers
@ -324,7 +325,7 @@ public class Exchange extends Thread {
public TreeSet<Order_old> bid; public TreeSet<Order_old> bid;
public TreeSet<Order_old> ask; public TreeSet<Order_old> ask;
private Locker tradelock = new Locker(); private final Locker tradelock = new Locker();
/* /*
private final Semaphore available = new Semaphore(1, true); private final Semaphore available = new Semaphore(1, true);
@ -704,12 +705,12 @@ public class Exchange extends Thread {
if (a == null) { if (a == null) {
return -1; return -1;
} }
tradelock.lock();
Order o = new Order(a, type, volume, limit); Order o = new Order(a, type, volume, limit);
addOrderToBook(o); addOrderToBook(o);
a.orders.put(o.id, o); a.orders.put(o.id, o);
tradelock.lock();
this.executeOrders(); this.executeOrders();
tradelock.unlock(); tradelock.unlock();
this.updateBookReceivers(OrderType.ASK); this.updateBookReceivers(OrderType.ASK);
@ -842,9 +843,9 @@ public class Exchange extends Thread {
/** /**
* *
*/ */
@Override // @Override
public void run() { public void run() {
while (true) { /* while (true) {
try { try {
sleep(1500); sleep(1500);
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -853,6 +854,7 @@ public class Exchange extends Thread {
print_current(); print_current();
} }
*/
} }
} }

View File

@ -44,34 +44,32 @@ public class RandomTrader extends AutoTrader {
//static Timer timer = new Timer(); //static Timer timer = new Timer();
enum Event { /* enum Event {
CANCEL, CANCEL,
CREATE CREATE
} }
*/
public long megaLoop(){
long rc=0;
for (int i=0; i<1; i++){
rc+=i;
// System.out.print(rc+"\n");
}
return rc;
}
long event(){ long event(){
if ("Alice".equals(this.name)){
// System.out.print("Hello world Iam a trader\n"); megaLoop();
return this.doTrade(); }
// doBuy(); sesim.Exchange.Account a = se.getAccount(account_id);
long rc = this.doTrade();
// System.out.print(String.format("%s: %.0f/%.2f\n", this.getName(),a.getShares(),a.getMoney()));
return rc;
} }
class NextEvent {
public Event event;
public long time;
/* NextEvent(Event e, long time) {
this.event = e;
this.time = time;
}
*/
}
public RandomTrader(Exchange se, double money, double shares, RandomTraderConfig config) { public RandomTrader(Exchange se, double money, double shares, RandomTraderConfig config) {
super(se, money, shares, config); super(se, money, shares, config);
if (this.config == null) { if (this.config == null) {
@ -96,11 +94,12 @@ public class RandomTrader extends AutoTrader {
} }
double start = 0.1; double start = 0.1;
Timer timer=new Timer();
@Override @Override
public void start() { public void start() {
Exchange.timer.schedule(new TimerTaskImpl(this), 0); timer.schedule(new TimerTaskImpl(this,timer), 0);
// timer.schedule(new TimerTaskImpl, date); // timer.schedule(new TimerTaskImpl, date);
} }
@ -256,10 +255,11 @@ public class RandomTrader extends AutoTrader {
private static class TimerTaskImpl extends TimerTask { private static class TimerTaskImpl extends TimerTask {
RandomTrader trader; RandomTrader trader;
NextEvent nextevent; Timer timer;
public TimerTaskImpl(RandomTrader trader) { public TimerTaskImpl(RandomTrader trader, Timer timer) {
this.trader = trader; this.trader = trader;
this.timer=timer;
} }
@ -267,10 +267,10 @@ public class RandomTrader extends AutoTrader {
public void run() { public void run() {
long time = trader.event(); long time = trader.event();
time/=50; time/=1;
this.cancel(); this.cancel();
Exchange.timer.schedule(new TimerTaskImpl(trader), time); timer.schedule(new TimerTaskImpl(trader,timer), time);
} }
} }

View File

@ -51,6 +51,14 @@ public class Test {
double aid1 = se.createAccount(100, 100); double aid1 = se.createAccount(100, 100);
double aid2 = se.createAccount(100, 100); double aid2 = se.createAccount(100, 100);
sesim.Exchange.Account a = se.getAccount(aid1);
System.out.print(a.getMoney());
System.exit(0);
/*
AccountData a1 = se.getAccountData(aid1); AccountData a1 = se.getAccountData(aid1);
AccountData a2 = se.getAccountData(aid2); AccountData a2 = se.getAccountData(aid2);
@ -76,6 +84,7 @@ public class Test {
//S/ystem.out.print(aid); //S/ystem.out.print(aid);
//System.out.print("\n"); //System.out.print("\n");
*/
} }
} }