Reformatted and commented code.

This commit is contained in:
7u83 2016-12-25 22:15:54 +01:00
parent ae82904244
commit 8b22549f4d
6 changed files with 156 additions and 169 deletions

View File

@ -3,14 +3,22 @@ package StockExchange;
public abstract class Order implements Comparable<Order> { public abstract class Order implements Comparable<Order> {
/** /**
* when * When the order was created
*/ */
public long timestamp = 0; public long timestamp = 0;
/**
* Number of shares
*/
public long size; public long size;
/**
* Limit price
*/
public double limit; public double limit;
// long time;
double money = 0; // double money = 0;
// public long shares=0;
public long id = 0; public long id = 0;
public Account account = null; public Account account = null;

View File

@ -3,127 +3,111 @@ package StockExchange;
import java.util.Random; import java.util.Random;
import StockExchange.Order.OrderStatus; import StockExchange.Order.OrderStatus;
public class RandomTrader extends ThreadedTrader{ public class RandomTrader extends ThreadedTrader {
// public Account account=new Account(); // public Account account=new Account();
Exchange ex=null; Exchange ex = null;
Random rand = new Random(); Random rand = new Random();
public String name; public String name;
// my current order // my current order
private Order myorder=null; private Order myorder = null;
public RandomTrader(Exchange ex, long shares, double money) {
account.money = money;
account.shares = shares;
this.ex = ex;
}
public RandomTrader (Exchange ex, long shares, double money){ public void DoBuy() {
account.money=money;
account.shares=shares;
this.ex=ex;
}
public void DoBuy() if (myorder != null) {
{ return;
}
if (myorder!=null) if (account.money <= 0) {
return; return;
}
if (account.money <= 0) double perc = rand.nextDouble() * 1.0;
return; double lp = ex.lastprice;
double limit = lp / 100 * perc + lp;
double perc = rand.nextDouble() * 1.0; long size = (int) (account.money / (limit * 1));
double lp = ex.lastprice;
double limit = lp/100 * perc + lp;
myorder = account.Buy(size, limit, ex);
return;
}
long size = (int)(account.money/(limit*1)); public void DoSell() {
if (myorder != null) {
return;
}
if (account.shares <= 0) {
return;
}
myorder = account.Buy(size, limit, ex); double perc = rand.nextDouble() * 1.0;
return; double lp = ex.lastprice;
} double limit = lp - lp / 100 * perc;
public void DoSell() long size = (int) (account.shares);
{
if (myorder!=null)
return;
if (account.shares<=0) myorder = account.Sell(size, limit, ex);
return; }
double perc = rand.nextDouble() * 1.0; public void trade() {
double lp = ex.lastprice;
double limit = lp - lp/100 * perc;
long size = (int)(account.shares); 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;
}
myorder= account.Sell(size, limit, ex); return;
} }
// What to do?
int action = rand.nextInt(3);
/* System.out.print(name);
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.println("---------------------------");
System.out.print("Action:"); System.out.print("Action:");
System.out.println(action); System.out.println(action);
*/ */
/* if (action==0) /* if (action==0)
{ {
DoSell(); DoSell();
return; return;
} }
*/ */
if (action == 1) if (action == 1) {
{ DoBuy();
DoBuy(); return;
return; }
}
if (action == 2) if (action == 2) {
{ DoSell();
DoSell(); return;
return; }
}
}
} /* public void run(){
/* public void run(){
while (true) while (true)
{ {
try{ try{
@ -140,7 +124,5 @@ public class RandomTrader extends ThreadedTrader{
} }
} }
*/ */
} }

View File

@ -2,15 +2,15 @@ package StockExchange;
public class SellOrder extends Order { public class SellOrder extends Order {
@Override @Override
public int compareTo(Order o) { public int compareTo(Order o) {
if (o.limit < limit) { if (o.limit < limit) {
return 1; return 1;
} }
if (o.limit > limit) { if (o.limit > limit) {
return -1; return -1;
} }
return 0; return 0;
} }
} }

View File

@ -1,26 +1,23 @@
package StockExchange; package StockExchange;
public abstract class ThreadedTrader extends Thread implements Trader { public abstract class ThreadedTrader extends Thread implements Trader {
protected long sleeptime=100; protected long sleeptime = 100;
public void RandomTrader (Exchange ex, long shares, double money){ public void RandomTrader(Exchange ex, long shares, double money) {
// this.ex=ex; // this.ex=ex;
} }
public void run(){
while (true)
{
try{
sleep(sleeptime);
}
catch(InterruptedException e) {
System.out.println("Interrupted");
}
trade();
}
}
public void run() {
while (true) {
try {
sleep(sleeptime);
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
trade();
}
}
} }

View File

@ -1,10 +1,11 @@
package StockExchange; package StockExchange;
public interface Trader {
public interface Trader{ String name = null;
String name = null;
public void trade(); public void trade();
public Account account=new Account(); public Account account = new Account();
// public Exchange ex=null; // public Exchange ex=null;
}; };

View File

@ -1,20 +1,20 @@
package StockExchange; package StockExchange;
public class TraderRun extends Thread{ public class TraderRun extends Thread {
public String tname = "";
public Exchange ex;
public void run(){ public String tname = "";
while (true) public Exchange ex;
{
try{
sleep(100);
}
catch(InterruptedException e) {
System.out.println("Interrupted");
}
/* @Override
public void run() {
while (true) {
try {
sleep(100);
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
/*
System.out.printf("%s locking\n", tname); System.out.printf("%s locking\n", tname);
ex.Lock(); ex.Lock();
System.out.printf("%s locked\n", tname); System.out.printf("%s locked\n", tname);
@ -29,9 +29,8 @@ public class TraderRun extends Thread{
System.out.printf("%s unlocking\n", tname); System.out.printf("%s unlocking\n", tname);
// ex.Free(); // ex.Free();
System.out.printf("%s unlocked\n", tname); System.out.printf("%s unlocked\n", tname);
*/ */
}
} }
}
} }