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

@ -14,41 +14,40 @@ public class RandomTrader extends ThreadedTrader{
// my current order // my current order
private Order myorder = null; private Order myorder = null;
public RandomTrader(Exchange ex, long shares, double money) { public RandomTrader(Exchange ex, long shares, double money) {
account.money = money; account.money = money;
account.shares = shares; account.shares = shares;
this.ex = ex; this.ex = ex;
} }
public void DoBuy() public void DoBuy() {
{
if (myorder!=null) if (myorder != null) {
return; return;
}
if (account.money <= 0) if (account.money <= 0) {
return; return;
}
double perc = rand.nextDouble() * 1.0; double perc = rand.nextDouble() * 1.0;
double lp = ex.lastprice; double lp = ex.lastprice;
double limit = lp / 100 * perc + lp; double limit = lp / 100 * perc + lp;
long size = (int) (account.money / (limit * 1)); long size = (int) (account.money / (limit * 1));
myorder = account.Buy(size, limit, ex); myorder = account.Buy(size, limit, ex);
return; return;
} }
public void DoSell() public void DoSell() {
{ if (myorder != null) {
if (myorder!=null)
return; return;
}
if (account.shares<=0) if (account.shares <= 0) {
return; return;
}
double perc = rand.nextDouble() * 1.0; double perc = rand.nextDouble() * 1.0;
double lp = ex.lastprice; double lp = ex.lastprice;
@ -56,20 +55,14 @@ public class RandomTrader extends ThreadedTrader{
long size = (int) (account.shares); long size = (int) (account.shares);
myorder = account.Sell(size, limit, ex); myorder = account.Sell(size, limit, ex);
} }
public void trade() { public void trade() {
if (myorder != null) if (myorder != null) {
{
long age = myorder.getAge(); long age = myorder.getAge();
if (myorder.status == OrderStatus.executed) if (myorder.status == OrderStatus.executed) {
{
myorder = null; myorder = null;
// System.out.println(name); // System.out.println(name);
// System.out.println("----------------------"); // System.out.println("----------------------");
@ -77,7 +70,6 @@ public class RandomTrader extends ThreadedTrader{
return; return;
} }
if (myorder.getAge() > 10) { if (myorder.getAge() > 10) {
//System.out.println("Shall cancel now"); //System.out.println("Shall cancel now");
//System.out.println(myorder.status); //System.out.println(myorder.status);
@ -89,11 +81,6 @@ public class RandomTrader extends ThreadedTrader{
return; return;
} }
// What to do? // What to do?
int action = rand.nextInt(3); int action = rand.nextInt(3);
/* System.out.print(name); /* System.out.print(name);
@ -108,19 +95,16 @@ public class RandomTrader extends ThreadedTrader{
} }
*/ */
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(){
@ -141,6 +125,4 @@ public class RandomTrader extends ThreadedTrader{
} }
*/ */
} }

View File

@ -10,17 +10,14 @@ public abstract class ThreadedTrader extends Thread implements Trader {
} }
public void run() { public void run() {
while (true) while (true) {
{
try { try {
sleep(sleeptime); sleep(sleeptime);
} } catch (InterruptedException e) {
catch(InterruptedException e) {
System.out.println("Interrupted"); System.out.println("Interrupted");
} }
trade(); trade();
} }
} }
} }

View File

@ -1,8 +1,9 @@
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,16 +1,16 @@
package StockExchange; package StockExchange;
public class TraderRun extends Thread { public class TraderRun extends Thread {
public String tname = ""; public String tname = "";
public Exchange ex; public Exchange ex;
@Override
public void run() { public void run() {
while (true) while (true) {
{
try { try {
sleep(100); sleep(100);
} } catch (InterruptedException e) {
catch(InterruptedException e) {
System.out.println("Interrupted"); System.out.println("Interrupted");
} }
@ -30,7 +30,6 @@ public class TraderRun extends Thread{
// ex.Free(); // ex.Free();
System.out.printf("%s unlocked\n", tname); System.out.printf("%s unlocked\n", tname);
*/ */
} }
} }