Work on RandomTrader

This commit is contained in:
7u83 2016-12-28 04:28:20 +01:00
parent e699e376ac
commit 41cb87c9d4
12 changed files with 187 additions and 195 deletions

View File

@ -52,10 +52,10 @@ public class Chart extends javax.swing.JPanel implements Exchange.QuoteReceiver
private DefaultHighLowDataset createDataset() { private DefaultHighLowDataset createDataset() {
System.out.print("Making Data"); // System.out.print("Making Data");
int s = se.quoteHistory.size(); int s = se.quoteHistory.size();
System.out.print(("SIZE")); // System.out.print(("SIZE"));
System.out.println(s); // System.out.println(s);
int serice = 115; int serice = 115;

View File

@ -84,15 +84,11 @@ public class ControlPanel extends javax.swing.JPanel {
}// </editor-fold>//GEN-END:initComponents }// </editor-fold>//GEN-END:initComponents
private void SellButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SellButtonActionPerformed private void SellButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SellButtonActionPerformed
System.out.print("SellPressed\n"); MainWin.myAccount.sell(81, 55.0);
// MainWin.myAccount.Sell(100, 520.0, MainWin.se);
MainWin.myAccount.sell(100, 13.0);
}//GEN-LAST:event_SellButtonActionPerformed }//GEN-LAST:event_SellButtonActionPerformed
private void BuyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BuyButtonActionPerformed private void BuyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BuyButtonActionPerformed
System.out.print("Buy Pressed\n"); MainWin.myAccount.buy(44, 66.0);
//MainWin.myAccount.Buy(100, 120.0, MainWin.se);
MainWin.myAccount.buy(100, 1.0);
}//GEN-LAST:event_BuyButtonActionPerformed }//GEN-LAST:event_BuyButtonActionPerformed

View File

@ -47,21 +47,10 @@ public class MainWin extends javax.swing.JFrame {
* Creates new form MainWin * Creates new form MainWin
*/ */
public MainWin() { public MainWin() {
myTrader.sell(100, 230);
myTrader.sell(10, 123.12);
initComponents(); initComponents();
//myAccount.money=1500.70;
//myAccount.shares=250;
/* class x extends Account{
public void x(){
tube=13;
}
}
*/
} }
@ -143,7 +132,7 @@ public class MainWin extends javax.swing.JFrame {
}// </editor-fold>//GEN-END:initComponents }// </editor-fold>//GEN-END:initComponents
private void FileNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FileNewActionPerformed private void FileNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FileNewActionPerformed
System.out.print("Menu 0 called\n");
}//GEN-LAST:event_FileNewActionPerformed }//GEN-LAST:event_FileNewActionPerformed
@ -164,26 +153,23 @@ public class MainWin extends javax.swing.JFrame {
myAccount = new Account(se,1000,100000000.0); myAccount = new Account(se,1000,100000000.0);
myTrader = new Traders.ManTrader(myAccount); myTrader = new Traders.ManTrader(myAccount);
/* Account otherAccount = new Account(se,1000,1000);
Traders.ManTrader otherTrader = new Traders.ManTrader(otherAccount);
otherTrader.sell(80, 22.70);
*/
/* SeSim.SellOrder so = new SeSim.SellOrder(); Account traccount = new Account(se,5500,1000000.0);
so.limit = 20.0; RandomTrader rt = new RandomTrader(traccount);
so.volume = 12; TraderRunner tr = new TraderRunner(rt);
so.timestamp = 12; tr.start();
se.SendOrder(so);
*/
try { try {
// Set cross-platform Java L&F (also called "Metal") // Set cross-platform Java L&F (also called "Metal")
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
} catch (UnsupportedLookAndFeelException | ClassNotFoundException | } catch (UnsupportedLookAndFeelException | ClassNotFoundException |
InstantiationException | IllegalAccessException e) { InstantiationException | IllegalAccessException e) {
System.out.print("Alles muell\n");
} }
// handle exception
// handle exception
// handle exception
/* Set the Nimbus look and feel */ /* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

View File

@ -140,7 +140,7 @@ public abstract class OrderBook extends javax.swing.JPanel implements Exchange.B
* Creates new form OrderBook * Creates new form OrderBook
*/ */
public OrderBook() { public OrderBook() {
System.out.print("init Orderbook]\n"); //System.out.print("init Orderbook]\n");
initComponents(); initComponents();
this.setBorder(BorderFactory.createEmptyBorder()); this.setBorder(BorderFactory.createEmptyBorder());

View File

@ -49,7 +49,7 @@ public class OrderBookPanel extends javax.swing.JPanel {
if (this.se == null) { if (this.se == null) {
return; return;
} }
System.out.print("Order boo init\n"); // System.out.print("Order boo init\n");
} }

View File

@ -2,28 +2,32 @@ package SeSim;
import java.util.*; import java.util.*;
final public class Account { final public class Account {
/** /**
* Exchange this account belongs to * Exchange this account belongs to
*/ */
protected Exchange se; public Exchange se;
/** /**
* Number of shares in this account * Number of shares in this account
*/ */
protected long shares = 0; public long shares = 0;
/** /**
* Ammount of money in this account * Ammount of money in this account
*/ */
protected double money = 0; public double money = 0;
/** /**
* Name of this account * Name of this account
*/ */
public String name = ""; public String name = "";
public ArrayList <Order> pending;
public boolean orderpending = false; public boolean orderpending = false;
@ -31,7 +35,7 @@ final public class Account {
this.shares=shares; this.shares=shares;
this.money=money; this.money=money;
this.se=se; this.se=se;
pending = new TreeSet(); pending = new ArrayList<>();
} }
public Account(){ public Account(){
@ -48,7 +52,7 @@ final public class Account {
); );
} }
TreeSet pending;
public Order sell(long volume, double limit) { public Order sell(long volume, double limit) {
SellOrder o = new SellOrder(); SellOrder o = new SellOrder();

View File

@ -76,7 +76,7 @@ public class Exchange extends Thread {
bookreceivers.add(br); bookreceivers.add(br);
} }
void UpdateBookReceivers(OrderType t) { void updateBookReceivers(OrderType t) {
ArrayList <BookReceiver> bookreceivers; ArrayList <BookReceiver> bookreceivers;
bookreceivers = selectBookReceiver(t); bookreceivers = selectBookReceiver(t);
@ -121,7 +121,7 @@ public class Exchange extends Thread {
try { try {
available.acquire(); available.acquire();
} catch (InterruptedException e) { } catch (InterruptedException e) {
System.out.println("Interrupted"); System.out.println("Interrupted\n");
} }
} }
@ -155,8 +155,8 @@ public class Exchange extends Thread {
Order o; Order o;
o = it.next(); o = it.next();
ret.add(o); ret.add(o);
System.out.print("Order" + o.limit); //System.out.print("Order" + o.limit);
System.out.println(); //System.out.println();
} }
return ret; return ret;
@ -203,9 +203,10 @@ public class Exchange extends Thread {
public void CancelOrder(Order o) { public void CancelOrder(Order o) {
Lock(); Lock();
// System.out.println("Cancel BuyOrder"); TreeSet <Order> book = this.selectOrderBook(o.type);
bid.remove((BuyOrder) o); book.remove(o);
ask.remove((SellOrder) o); this.updateBookReceivers(o.type);
o.account.pending.remove(o);
o.status = OrderStatus.canceled; o.status = OrderStatus.canceled;
Unlock(); Unlock();
@ -241,7 +242,11 @@ public class Exchange extends Thread {
// This order is fully executed, remove // This order is fully executed, remove
a.account.orderpending = false; a.account.orderpending = false;
a.status = OrderStatus.executed; a.status = OrderStatus.executed;
a.account.pending.remove(a);
ask.pollFirst(); ask.pollFirst();
this.updateBookReceivers(OrderType.ask);
continue; continue;
} }
@ -249,7 +254,9 @@ public class Exchange extends Thread {
// This order is fully executed, remove // This order is fully executed, remove
b.account.orderpending = false; b.account.orderpending = false;
b.status = OrderStatus.executed; b.status = OrderStatus.executed;
b.account.pending.remove(b);
bid.pollFirst(); bid.pollFirst();
this.updateBookReceivers(OrderType.bid);
continue; continue;
} }
@ -293,6 +300,9 @@ public class Exchange extends Thread {
q.time = System.currentTimeMillis(); q.time = System.currentTimeMillis();
this.UpdateQuoteReceivers(q); this.UpdateQuoteReceivers(q);
this.updateBookReceivers(OrderType.bid);
this.updateBookReceivers(OrderType.ask);
//quoteHistory.add(q); //quoteHistory.add(q);
continue; continue;
@ -330,7 +340,7 @@ public class Exchange extends Thread {
} }
if (ret) if (ret)
this.UpdateBookReceivers(o.type); this.updateBookReceivers(o.type);
return ret; return ret;
} }
@ -343,9 +353,10 @@ public class Exchange extends Thread {
Lock(); Lock();
o.timestamp = System.currentTimeMillis(); o.timestamp = System.currentTimeMillis();
System.out.print(o.timestamp + " TS:\n"); //System.out.print(o.timestamp + " TS:\n");
o.id = orderid++; o.id = orderid++;
addOrder(o); addOrder(o);
o.account.pending.add(o);
OrderMatching(); OrderMatching();
Unlock(); Unlock();
@ -386,7 +397,7 @@ public class Exchange extends Thread {
* SendOrder(bo); * SendOrder(bo);
*/ */
return theprice; return lastprice;
} }
public double sendOrder(Account o) { public double sendOrder(Account o) {

View File

@ -60,10 +60,10 @@ public abstract class Order implements Comparable<Order> {
if (r!=0) if (r!=0)
return r; return r;
if (o.timestamp< timestamp) if (o.timestamp> timestamp)
return -1; return -1;
if (o.timestamp>timestamp) if (o.timestamp<timestamp)
return 1; return 1;

View File

@ -1,131 +0,0 @@
package SeSim;
import java.util.Random;
import SeSim.Order.OrderStatus;
public class RandomTrader extends Trader {
// public Account account=new Account();
Exchange ex = null;
Random rand = new Random();
// my current order
private Order myorder = null;
public RandomTrader(Account account) {
super(account);
}
/* public RandomTrader(Exchange ex, long shares, double money) {
account.money = money;
account.shares = shares;
this.ex = ex;
}
*/
public void DoBuy() {
if (myorder != null) {
return;
}
if (account.money <= 0) {
return;
}
double perc = rand.nextDouble() * 1.0;
double lp = ex.lastprice;
double limit = lp / 100 * perc + lp;
long size = (int) (account.money / (limit * 1));
myorder = account.buy(size, limit);
return;
}
public void DoSell() {
if (myorder != null) {
return;
}
if (account.shares <= 0) {
return;
}
double perc = rand.nextDouble() * 1.0;
double lp = ex.lastprice;
double limit = lp - lp / 100 * perc;
long size = (int) (account.shares);
myorder = account.sell(size, limit);
}
public void trade() {
if (myorder != null) {
long age = myorder.getAge();
if (myorder.status == OrderStatus.executed) {
myorder = null;
// System.out.println(name);
// System.out.println("----------------------");
// account.print_current();
return;
}
if (myorder.getAge() > 10) {
//System.out.println("Shall cancel now");
//System.out.println(myorder.status);
ex.CancelOrder(myorder);
myorder = null;
return;
}
return;
}
// What to do?
int action = rand.nextInt(3);
/* System.out.print(name);
System.out.println("---------------------------");
System.out.print("Action:");
System.out.println(action);
*/
/* if (action==0)
{
DoSell();
return;
}
*/
if (action == 1) {
DoBuy();
return;
}
if (action == 2) {
DoSell();
return;
}
}
/* public void run(){
while (true)
{
try{
sleep(200);
}
catch(InterruptedException e) {
System.out.println("Interrupted");
}
// System.out.println("Trader has slept");
trade();
}
}
*/
}

View File

@ -35,9 +35,14 @@ public abstract class Trader {
public void sell(long shares, double limit){ public void sell(long shares, double limit){
account.sell(shares, limit); account.sell(shares, limit);
} }
public void buy(long shares, double limit){
account.buy(shares, limit);
}
/** /**
* Construct a Trader object * Construct a Trader object
* @param account Account for this trader
*/ */
public Trader(Account account){ public Trader(Account account){
this.account=account; this.account=account;

View File

@ -1,13 +1,13 @@
package SeSim; package SeSim;
public abstract class ThreadedTrader extends Thread { public class TraderRunner extends Thread {
protected long sleeptime = 100;
public void RandomTrader(Exchange ex, long shares, double money) {
// this.ex=ex;
protected long sleeptime = 1000;
Trader trader;
public TraderRunner(Trader trader){
this.trader=trader;
} }
public void run() { public void run() {
@ -18,8 +18,7 @@ public abstract class ThreadedTrader extends Thread {
System.out.println("Interrupted"); System.out.println("Interrupted");
return; return;
} }
// trade(); trader.trade();
} }
} }
} }

View File

@ -0,0 +1,122 @@
package Traders;
import SeSim.Account;
import SeSim.Exchange;
import SeSim.Order;
import java.util.Random;
import SeSim.Trader;
public class RandomTrader extends Trader {
Random rand = new Random();
// my current order
//private Order myorder = null;
public RandomTrader(Account account) {
super(account);
}
public void doBuy() {
// if (account.money <= 0) {
// return;
// }
double perc = rand.nextDouble() * 1.0;
double lp = account.se.getlastprice();
double limit = lp / 100 * perc + lp;
long size = (int) (account.money / (limit * 1));
buy(size, limit);
return;
}
public void doSell() {
/* if (myorder != null) {
return;
}
if (account.shares <= 0) {
return;
}
*/
double perc = rand.nextDouble() * 1.0;
double lp = account.se.getlastprice();
double limit = lp - lp / 100 * perc;
long size = (int) (account.shares);
sell(size, limit);
}
private boolean monitorTrades() {
int numpending = account.pending.size();
System.out.print("RT: Monitoring trades - Pending: "+numpending+"\n");
if (numpending == 0) {
System.out.print("RT: pending = 0 - return false\n");
return false;
}
Order o = account.pending.get(0);
long age = o.getAge();
System.out.print("RT: age is: "+age+"\n");
if (age > 10000) {
account.se.CancelOrder(o);
System.out.print("Age reached - canel return false\n");
return false;
}
System.out.print("RT: monitor return true\n");
return true;
}
public void trade() {
System.out.print("RT: Now trading\n");
if (monitorTrades()) {
return;
}
// What next to do?
int action = rand.nextInt(3);
if (action == 1) {
doBuy();
return;
}
if (action == 2) {
doSell();
return;
}
}
/* public void run(){
while (true)
{
try{
sleep(200);
}
catch(InterruptedException e) {
System.out.println("Interrupted");
}
// System.out.println("Trader has slept");
trade();
}
}
*/
}