Work on RandomTrader
This commit is contained in:
@ -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();
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
}
|
@ -35,9 +35,14 @@ public abstract class Trader {
|
||||
public void sell(long shares, double limit){
|
||||
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 {
|
||||
|
||||
protected long sleeptime = 100;
|
||||
|
||||
public void RandomTrader(Exchange ex, long shares, double money) {
|
||||
// this.ex=ex;
|
||||
|
||||
public class TraderRunner extends Thread {
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user