Work on RandomTrader
This commit is contained in:
parent
e699e376ac
commit
41cb87c9d4
@ -52,10 +52,10 @@ public class Chart extends javax.swing.JPanel implements Exchange.QuoteReceiver
|
||||
|
||||
private DefaultHighLowDataset createDataset() {
|
||||
|
||||
System.out.print("Making Data");
|
||||
// System.out.print("Making Data");
|
||||
int s = se.quoteHistory.size();
|
||||
System.out.print(("SIZE"));
|
||||
System.out.println(s);
|
||||
// System.out.print(("SIZE"));
|
||||
// System.out.println(s);
|
||||
|
||||
int serice = 115;
|
||||
|
||||
|
@ -84,15 +84,11 @@ public class ControlPanel extends javax.swing.JPanel {
|
||||
}// </editor-fold>//GEN-END:initComponents
|
||||
|
||||
private void SellButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SellButtonActionPerformed
|
||||
System.out.print("SellPressed\n");
|
||||
// MainWin.myAccount.Sell(100, 520.0, MainWin.se);
|
||||
MainWin.myAccount.sell(100, 13.0);
|
||||
MainWin.myAccount.sell(81, 55.0);
|
||||
}//GEN-LAST:event_SellButtonActionPerformed
|
||||
|
||||
private void BuyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BuyButtonActionPerformed
|
||||
System.out.print("Buy Pressed\n");
|
||||
//MainWin.myAccount.Buy(100, 120.0, MainWin.se);
|
||||
MainWin.myAccount.buy(100, 1.0);
|
||||
MainWin.myAccount.buy(44, 66.0);
|
||||
}//GEN-LAST:event_BuyButtonActionPerformed
|
||||
|
||||
|
||||
|
@ -48,20 +48,9 @@ public class MainWin extends javax.swing.JFrame {
|
||||
*/
|
||||
public MainWin() {
|
||||
|
||||
myTrader.sell(100, 230);
|
||||
myTrader.sell(10, 123.12);
|
||||
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
|
||||
|
||||
private void FileNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FileNewActionPerformed
|
||||
System.out.print("Menu 0 called\n");
|
||||
|
||||
|
||||
}//GEN-LAST:event_FileNewActionPerformed
|
||||
|
||||
@ -164,26 +153,23 @@ public class MainWin extends javax.swing.JFrame {
|
||||
myAccount = new Account(se,1000,100000000.0);
|
||||
myTrader = new Traders.ManTrader(myAccount);
|
||||
|
||||
/* Account otherAccount = new Account(se,1000,1000);
|
||||
Traders.ManTrader otherTrader = new Traders.ManTrader(otherAccount);
|
||||
otherTrader.sell(80, 22.70);
|
||||
|
||||
*/
|
||||
Account traccount = new Account(se,5500,1000000.0);
|
||||
RandomTrader rt = new RandomTrader(traccount);
|
||||
TraderRunner tr = new TraderRunner(rt);
|
||||
tr.start();
|
||||
|
||||
/* SeSim.SellOrder so = new SeSim.SellOrder();
|
||||
so.limit = 20.0;
|
||||
so.volume = 12;
|
||||
so.timestamp = 12;
|
||||
se.SendOrder(so);
|
||||
*/
|
||||
|
||||
try {
|
||||
// Set cross-platform Java L&F (also called "Metal")
|
||||
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
|
||||
} catch (UnsupportedLookAndFeelException | ClassNotFoundException |
|
||||
InstantiationException | IllegalAccessException e) {
|
||||
System.out.print("Alles muell\n");
|
||||
}
|
||||
// handle exception
|
||||
// handle exception
|
||||
// handle exception
|
||||
|
||||
|
||||
/* Set the Nimbus look and feel */
|
||||
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
|
||||
|
@ -140,7 +140,7 @@ public abstract class OrderBook extends javax.swing.JPanel implements Exchange.B
|
||||
* Creates new form OrderBook
|
||||
*/
|
||||
public OrderBook() {
|
||||
System.out.print("init Orderbook]\n");
|
||||
//System.out.print("init Orderbook]\n");
|
||||
initComponents();
|
||||
|
||||
this.setBorder(BorderFactory.createEmptyBorder());
|
||||
|
@ -49,7 +49,7 @@ public class OrderBookPanel extends javax.swing.JPanel {
|
||||
if (this.se == null) {
|
||||
return;
|
||||
}
|
||||
System.out.print("Order boo init\n");
|
||||
// System.out.print("Order boo init\n");
|
||||
|
||||
}
|
||||
|
||||
|
@ -2,28 +2,32 @@ package SeSim;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
||||
final public class Account {
|
||||
|
||||
/**
|
||||
* Exchange this account belongs to
|
||||
*/
|
||||
protected Exchange se;
|
||||
public Exchange se;
|
||||
|
||||
/**
|
||||
* Number of shares in this account
|
||||
*/
|
||||
protected long shares = 0;
|
||||
public long shares = 0;
|
||||
|
||||
/**
|
||||
* Ammount of money in this account
|
||||
*/
|
||||
protected double money = 0;
|
||||
public double money = 0;
|
||||
|
||||
/**
|
||||
* Name of this account
|
||||
*/
|
||||
public String name = "";
|
||||
|
||||
|
||||
public ArrayList <Order> pending;
|
||||
|
||||
public boolean orderpending = false;
|
||||
|
||||
|
||||
@ -31,7 +35,7 @@ final public class Account {
|
||||
this.shares=shares;
|
||||
this.money=money;
|
||||
this.se=se;
|
||||
pending = new TreeSet();
|
||||
pending = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Account(){
|
||||
@ -48,7 +52,7 @@ final public class Account {
|
||||
);
|
||||
}
|
||||
|
||||
TreeSet pending;
|
||||
|
||||
|
||||
public Order sell(long volume, double limit) {
|
||||
SellOrder o = new SellOrder();
|
||||
|
@ -76,7 +76,7 @@ public class Exchange extends Thread {
|
||||
bookreceivers.add(br);
|
||||
}
|
||||
|
||||
void UpdateBookReceivers(OrderType t) {
|
||||
void updateBookReceivers(OrderType t) {
|
||||
ArrayList <BookReceiver> bookreceivers;
|
||||
bookreceivers = selectBookReceiver(t);
|
||||
|
||||
@ -121,7 +121,7 @@ public class Exchange extends Thread {
|
||||
try {
|
||||
available.acquire();
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("Interrupted");
|
||||
System.out.println("Interrupted\n");
|
||||
}
|
||||
|
||||
}
|
||||
@ -155,8 +155,8 @@ public class Exchange extends Thread {
|
||||
Order o;
|
||||
o = it.next();
|
||||
ret.add(o);
|
||||
System.out.print("Order" + o.limit);
|
||||
System.out.println();
|
||||
//System.out.print("Order" + o.limit);
|
||||
//System.out.println();
|
||||
}
|
||||
return ret;
|
||||
|
||||
@ -203,9 +203,10 @@ public class Exchange extends Thread {
|
||||
|
||||
public void CancelOrder(Order o) {
|
||||
Lock();
|
||||
// System.out.println("Cancel BuyOrder");
|
||||
bid.remove((BuyOrder) o);
|
||||
ask.remove((SellOrder) o);
|
||||
TreeSet <Order> book = this.selectOrderBook(o.type);
|
||||
book.remove(o);
|
||||
this.updateBookReceivers(o.type);
|
||||
o.account.pending.remove(o);
|
||||
o.status = OrderStatus.canceled;
|
||||
Unlock();
|
||||
|
||||
@ -241,7 +242,11 @@ public class Exchange extends Thread {
|
||||
// This order is fully executed, remove
|
||||
a.account.orderpending = false;
|
||||
a.status = OrderStatus.executed;
|
||||
|
||||
a.account.pending.remove(a);
|
||||
|
||||
ask.pollFirst();
|
||||
this.updateBookReceivers(OrderType.ask);
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -249,7 +254,9 @@ public class Exchange extends Thread {
|
||||
// This order is fully executed, remove
|
||||
b.account.orderpending = false;
|
||||
b.status = OrderStatus.executed;
|
||||
b.account.pending.remove(b);
|
||||
bid.pollFirst();
|
||||
this.updateBookReceivers(OrderType.bid);
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -293,6 +300,9 @@ public class Exchange extends Thread {
|
||||
q.time = System.currentTimeMillis();
|
||||
|
||||
this.UpdateQuoteReceivers(q);
|
||||
this.updateBookReceivers(OrderType.bid);
|
||||
this.updateBookReceivers(OrderType.ask);
|
||||
|
||||
|
||||
//quoteHistory.add(q);
|
||||
continue;
|
||||
@ -330,7 +340,7 @@ public class Exchange extends Thread {
|
||||
}
|
||||
|
||||
if (ret)
|
||||
this.UpdateBookReceivers(o.type);
|
||||
this.updateBookReceivers(o.type);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@ -343,9 +353,10 @@ public class Exchange extends Thread {
|
||||
|
||||
Lock();
|
||||
o.timestamp = System.currentTimeMillis();
|
||||
System.out.print(o.timestamp + " TS:\n");
|
||||
//System.out.print(o.timestamp + " TS:\n");
|
||||
o.id = orderid++;
|
||||
addOrder(o);
|
||||
o.account.pending.add(o);
|
||||
OrderMatching();
|
||||
Unlock();
|
||||
|
||||
@ -386,7 +397,7 @@ public class Exchange extends Thread {
|
||||
* SendOrder(bo);
|
||||
*/
|
||||
|
||||
return theprice;
|
||||
return lastprice;
|
||||
}
|
||||
|
||||
public double sendOrder(Account o) {
|
||||
|
@ -60,10 +60,10 @@ public abstract class Order implements Comparable<Order> {
|
||||
if (r!=0)
|
||||
return r;
|
||||
|
||||
if (o.timestamp< timestamp)
|
||||
if (o.timestamp> timestamp)
|
||||
return -1;
|
||||
|
||||
if (o.timestamp>timestamp)
|
||||
if (o.timestamp<timestamp)
|
||||
return 1;
|
||||
|
||||
|
||||
|
@ -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();
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
}
|
@ -36,8 +36,13 @@ public abstract class Trader {
|
||||
account.sell(shares, limit);
|
||||
}
|
||||
|
||||
public void buy(long shares, double limit){
|
||||
account.buy(shares, limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a Trader object
|
||||
* @param account Account for this trader
|
||||
*/
|
||||
public Trader(Account account){
|
||||
this.account=account;
|
||||
|
@ -1,13 +1,13 @@
|
||||
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() {
|
||||
@ -18,8 +18,7 @@ public abstract class ThreadedTrader extends Thread {
|
||||
System.out.println("Interrupted");
|
||||
return;
|
||||
}
|
||||
// trade();
|
||||
trader.trade();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
122
src/Traders/RandomTrader.java
Normal file
122
src/Traders/RandomTrader.java
Normal 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();
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
}
|
Loading…
Reference in New Issue
Block a user