Initial commit

This commit is contained in:
7u83 2016-12-25 18:11:23 +01:00
parent a7858e2959
commit c1e3e2162c
10 changed files with 463 additions and 0 deletions

View File

@ -0,0 +1,58 @@
package StockExchange;
public class Account {
public long shares=0; // number of shares
public double money=0; // amount of money
public String name="";
public boolean orderpending=false;
private double bound_money;
public void print_current(){
System.out.printf("%s shares: %d credit: %.2f\n",
name, shares,money
);
}
public SellOrder Sell (long size, double limit, Exchange ex){
SellOrder o = new SellOrder();
o.account=this;
o.limit=limit;
o.size=size;
orderpending=true;
ex.SendOrder(o);
return o;
}
public BuyOrder Buy (long size, double limit, Exchange ex ){
if (size * limit > money)
return null;
BuyOrder o = new BuyOrder();
o.limit=limit;
o.size=size;
o.account=this;
orderpending=true;
ex.SendOrder(o);
return o;
}
public void Buy( Account a,long size, double price)
{
shares+=size;
money-= price * size;
a.shares-=size;
a.money+=price * size;
}
}

View File

@ -0,0 +1,21 @@
package StockExchange;
public class BuyOrder extends Order implements Comparable<Order>{
public int compareTo(Order o){
if (o.limit < limit)
{
//System.out.println("return 1");
return -1;
}
if (o.limit > limit){
//System.out.println("return -1");
return +1;
}
// System.out.println("0000000000000000000000");
return 0;
}
}

View File

@ -0,0 +1,21 @@
package StockExchange;
public class Logger {
static boolean dbg=true;
static boolean info=true;
static void dbg(String s){
if (!dbg)
return;
System.out.print("DBG: ");
System.out.println(s);
}
static void info(String s){
if (!info)
return;
System.out.print("INFO: ");
System.out.println(s);
}
}

View File

@ -0,0 +1,88 @@
package StockExchange;
import java.util.Random;
public class MTrader implements Trader {
Exchange ex;
Random rand;
public MTrader (Exchange ex1, long shares, double money){
account.money=money;
account.shares=shares;
this.ex=ex;
rand = new Random();
}
public void DoBuy()
{
// System.out.println("AAA");
if (account.orderpending)
return;
if (account.money <= 0)
return;
double perc = rand.nextDouble() * 1.0;
double lp = ex.lastprice;
double limit = lp/100 * perc + lp;
// System.out.println("HW");
long size = (int)(account.money/limit);
account.Buy(size, limit, ex);
return;
}
public void DoSell()
{
// System.out.println("SoSell");
if (account.orderpending)
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);
account.Sell(size, limit, ex);
return;
}
public void trade(){
// What to do?
int action = rand.nextInt(3);
// System.out.print("Action");
// System.out.println(action);
if (action==0)
return;
if (action == 1)
{
DoBuy();
return;
}
if (action == 2)
{
DoSell();
return;
}
//System.out.printf("MyPrice: %.2f\n",price);
}
}

View File

@ -0,0 +1,41 @@
package StockExchange;
public abstract class Order implements Comparable<Order>{
public long timestamp=0;
public long size;
public double limit;
// long time;
double money=0;
// public long shares=0;
public long id=0;
public Account account=null;
enum OrderStatus {
open,executed,canceled
}
OrderStatus status=OrderStatus.open;
public long getAge(){
if (timestamp==0)
return 0;
return System.currentTimeMillis()-timestamp;
}
String format_limit(){
if (limit <0.0){
return "n.a.";
}
return String.format("%.2f",limit);
}
String format_size(){
return String.format("%d", size);
}
Order(){
}
}

View File

@ -0,0 +1,146 @@
package StockExchange;
import java.util.Random;
import StockExchange.Order.OrderStatus;
public class RandomTrader extends ThreadedTrader{
// public Account account=new Account();
Exchange ex=null;
Random rand = new Random();
public String name;
// my current order
private Order myorder=null;
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, ex);
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, ex);
}
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

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

View File

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

View File

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

View File

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