Scheduler 0.0000001
This commit is contained in:
parent
9c60f7c521
commit
ebe3f7e6f4
@ -194,6 +194,7 @@ public class MainWin extends javax.swing.JFrame {
|
|||||||
public static void main(String args[]) {
|
public static void main(String args[]) {
|
||||||
|
|
||||||
se = new Exchange();
|
se = new Exchange();
|
||||||
|
se.timer.start();
|
||||||
|
|
||||||
|
|
||||||
//RandomTraderConfig rcfg = new RandomTraderConfig();
|
//RandomTraderConfig rcfg = new RandomTraderConfig();
|
||||||
@ -214,7 +215,7 @@ public class MainWin extends javax.swing.JFrame {
|
|||||||
// SwitchingTraderConfig cfg = new SwitchingTraderConfig();
|
// SwitchingTraderConfig cfg = new SwitchingTraderConfig();
|
||||||
RandomTraderConfig cfg= new RandomTraderConfig();
|
RandomTraderConfig cfg= new RandomTraderConfig();
|
||||||
|
|
||||||
for (int i=0; i<10; i++){
|
for (int i=0; i<1; i++){
|
||||||
AutoTrader randt = cfg.createTrader(se, 100000, 0);
|
AutoTrader randt = cfg.createTrader(se, 100000, 0);
|
||||||
|
|
||||||
randt.setName("Alice");
|
randt.setName("Alice");
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2017, tobias
|
* Copyright (c) 2017, 7u83
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
@ -27,9 +27,9 @@ package sesim;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author tobias
|
* @author 7u83
|
||||||
*/
|
*/
|
||||||
public abstract class AutoTrader {
|
public abstract class AutoTrader implements Scheduler.TimerTask {
|
||||||
|
|
||||||
protected double account_id;
|
protected double account_id;
|
||||||
protected Exchange se;
|
protected Exchange se;
|
||||||
@ -37,9 +37,6 @@ public abstract class AutoTrader {
|
|||||||
|
|
||||||
protected String name;
|
protected String name;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public AutoTrader(Exchange se, double money, double shares, AutoTraderConfig config) {
|
public AutoTrader(Exchange se, double money, double shares, AutoTraderConfig config) {
|
||||||
account_id = se.createAccount(money, shares);
|
account_id = se.createAccount(money, shares);
|
||||||
this.se = se;
|
this.se = se;
|
||||||
@ -58,5 +55,4 @@ public abstract class AutoTrader {
|
|||||||
|
|
||||||
public abstract void start();
|
public abstract void start();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,9 @@ public class Exchange { //extends Thread {
|
|||||||
}
|
}
|
||||||
|
|
||||||
IDGenerator account_id = new IDGenerator();
|
IDGenerator account_id = new IDGenerator();
|
||||||
public static Timer timer = new Timer();
|
//public static Timer timer = new Timer();
|
||||||
|
|
||||||
|
public Scheduler timer = new Scheduler();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -175,8 +177,21 @@ public class Exchange { //extends Thread {
|
|||||||
order_books.put(type, new TreeSet(new OrderComparator(type)));
|
order_books.put(type, new TreeSet(new OrderComparator(type)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*public interface TimerEvent {
|
||||||
|
|
||||||
|
long timerEvent();
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
void start(){
|
||||||
|
timer.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class BidBook extends TreeSet {
|
class BidBook extends TreeSet {
|
||||||
|
|
||||||
TreeSet t = new TreeSet();
|
TreeSet t = new TreeSet();
|
||||||
|
@ -40,21 +40,27 @@ public class Scheduler extends Thread {
|
|||||||
|
|
||||||
long multiply = 1;
|
long multiply = 1;
|
||||||
|
|
||||||
private final SortedMap<Long, SortedSet<TimerEvent>> event_queue = new TreeMap<>();
|
private final SortedMap<Long, SortedSet<TimerTask>> event_queue = new TreeMap<>();
|
||||||
private boolean stop = false;
|
private boolean halt = false;
|
||||||
|
|
||||||
|
public interface TimerTask {
|
||||||
|
|
||||||
|
long timerTask();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**\
|
||||||
|
*
|
||||||
|
*/
|
||||||
public void halt() {
|
public void halt() {
|
||||||
stop = true;
|
halt = true;
|
||||||
synchronized (event_queue) {
|
synchronized (event_queue) {
|
||||||
event_queue.notifyAll();
|
event_queue.notifyAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface TimerEvent {
|
/**
|
||||||
|
*
|
||||||
long timerEvent();
|
*/
|
||||||
}
|
|
||||||
|
|
||||||
private class ObjectComparator implements Comparator<Object> {
|
private class ObjectComparator implements Comparator<Object> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -63,41 +69,40 @@ public class Scheduler extends Thread {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getCurrentTimeMillies() {
|
public long currentTimeMillis() {
|
||||||
return System.currentTimeMillis();
|
return System.currentTimeMillis();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startEvent(TimerEvent e, long time) {
|
/**
|
||||||
long evtime = time + this.getCurrentTimeMillies();
|
*
|
||||||
|
* @param e
|
||||||
|
* @param time
|
||||||
|
*/
|
||||||
|
public void startTimerEvent(TimerTask e, long time) {
|
||||||
|
long evtime = time + this.currentTimeMillis();
|
||||||
synchronized (event_queue) {
|
synchronized (event_queue) {
|
||||||
SortedSet<TimerEvent> s = event_queue.get(evtime);
|
this.addEvent(e, time);
|
||||||
if (s == null) {
|
|
||||||
s = new TreeSet<>(new ObjectComparator());
|
|
||||||
event_queue.put(evtime, s);
|
|
||||||
}
|
|
||||||
s.add(e);
|
|
||||||
}
|
}
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
notify();
|
notify();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public long fireEvent(TimerEvent e) {
|
public long fireEvent(TimerTask e) {
|
||||||
return e.timerEvent();
|
return e.timerTask();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addEvent(TimerEvent e, long time) {
|
private boolean addEvent(TimerTask e, long time) {
|
||||||
|
|
||||||
long evtime = time + this.getCurrentTimeMillies();
|
long evtime = time + this.currentTimeMillis();
|
||||||
|
|
||||||
SortedSet<TimerEvent> s = event_queue.get(evtime);
|
SortedSet<TimerTask> s = event_queue.get(evtime);
|
||||||
if (s == null) {
|
if (s == null) {
|
||||||
s = new TreeSet<>(new ObjectComparator());
|
s = new TreeSet<>(new ObjectComparator());
|
||||||
event_queue.put(evtime, s);
|
event_queue.put(evtime, s);
|
||||||
}
|
}
|
||||||
s.add(e);
|
return s.add(e);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public long runEvents() {
|
public long runEvents() {
|
||||||
@ -106,23 +111,20 @@ public class Scheduler extends Thread {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
long t = event_queue.firstKey();
|
long t = event_queue.firstKey();
|
||||||
if (t <= this.getCurrentTimeMillies()) {
|
if (t <= this.currentTimeMillis()) {
|
||||||
SortedSet s = event_queue.get(t);
|
SortedSet s = event_queue.get(t);
|
||||||
|
|
||||||
event_queue.remove(t);
|
event_queue.remove(t);
|
||||||
Iterator<TimerEvent> it = s.iterator();
|
Iterator<TimerTask> it = s.iterator();
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
|
TimerTask e = it.next();
|
||||||
TimerEvent e = it.next();
|
|
||||||
long next_t = this.fireEvent(e);
|
long next_t = this.fireEvent(e);
|
||||||
this.addEvent(e, next_t);
|
this.addEvent(e, next_t);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return t - this.getCurrentTimeMillies();
|
return t - this.currentTimeMillis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,7 +132,7 @@ public class Scheduler extends Thread {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while (!stop) {
|
while (!halt) {
|
||||||
long wtime = runEvents();
|
long wtime = runEvents();
|
||||||
if (wtime == 0) {
|
if (wtime == 0) {
|
||||||
continue;
|
continue;
|
||||||
@ -144,6 +146,7 @@ public class Scheduler extends Thread {
|
|||||||
wait();
|
wait();
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ package traders;
|
|||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.TimerTask;
|
//import java.util.TimerTask;
|
||||||
/*import sesim.AccountData;*/
|
/*import sesim.AccountData;*/
|
||||||
|
|
||||||
import sesim.AutoTrader;
|
import sesim.AutoTrader;
|
||||||
@ -42,34 +42,15 @@ import sesim.*;
|
|||||||
*/
|
*/
|
||||||
public class RandomTrader extends AutoTrader {
|
public class RandomTrader extends AutoTrader {
|
||||||
|
|
||||||
//static Timer timer = new Timer();
|
|
||||||
|
|
||||||
/* enum Event {
|
|
||||||
CANCEL,
|
|
||||||
CREATE
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
public long megaLoop(){
|
|
||||||
long rc=0;
|
|
||||||
for (int i=0; i<1; i++){
|
|
||||||
rc+=i;
|
|
||||||
// System.out.print(rc+"\n");
|
|
||||||
}
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
long event() {
|
long event() {
|
||||||
if ("Alice".equals(this.name)){
|
|
||||||
megaLoop();
|
|
||||||
}
|
|
||||||
sesim.Exchange.Account a = se.getAccount(account_id);
|
sesim.Exchange.Account a = se.getAccount(account_id);
|
||||||
long rc = this.doTrade();
|
long rc = this.doTrade();
|
||||||
// System.out.print(String.format("%s: %.0f/%.2f\n", this.getName(),a.getShares(),a.getMoney()));
|
return rc/3;
|
||||||
return rc;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public RandomTrader(Exchange se, double money, double shares, RandomTraderConfig config) {
|
public RandomTrader(Exchange se, double money, double shares, RandomTraderConfig config) {
|
||||||
super(se, money, shares, config);
|
super(se, money, shares, config);
|
||||||
if (this.config == null) {
|
if (this.config == null) {
|
||||||
@ -78,6 +59,15 @@ public class RandomTrader extends AutoTrader {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long timerTask() {
|
||||||
|
sesim.Exchange.Account a = se.getAccount(account_id);
|
||||||
|
long rc = this.doTrade();
|
||||||
|
return rc/33;
|
||||||
|
|
||||||
|
// return this.event();
|
||||||
|
}
|
||||||
|
|
||||||
protected enum Action {
|
protected enum Action {
|
||||||
BUY, SELL, RANDOM
|
BUY, SELL, RANDOM
|
||||||
}
|
}
|
||||||
@ -85,21 +75,20 @@ public class RandomTrader extends AutoTrader {
|
|||||||
protected Action getAction() {
|
protected Action getAction() {
|
||||||
if (rand.nextInt(2) == 0) {
|
if (rand.nextInt(2) == 0) {
|
||||||
return Action.BUY;
|
return Action.BUY;
|
||||||
}
|
} else {
|
||||||
else{
|
|
||||||
return Action.SELL;
|
return Action.SELL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
double start = 0.1;
|
double start = 0.1;
|
||||||
Timer timer=new Timer();
|
//Timer timer = new Timer();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start() {
|
public void start() {
|
||||||
|
|
||||||
timer.schedule(new TimerTaskImpl(this,timer), 0);
|
// timer.schedule(new TimerTaskImpl(this, timer), 0);
|
||||||
|
se.timer.startTimerEvent(this, 0);
|
||||||
|
|
||||||
// timer.schedule(new TimerTaskImpl, date);
|
// timer.schedule(new TimerTaskImpl, date);
|
||||||
}
|
}
|
||||||
@ -109,7 +98,6 @@ public class RandomTrader extends AutoTrader {
|
|||||||
// object to generate random numbers
|
// object to generate random numbers
|
||||||
final private Random rand = new Random();
|
final private Random rand = new Random();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a (long) random number between min an max
|
* Get a (long) random number between min an max
|
||||||
*
|
*
|
||||||
@ -138,7 +126,6 @@ public class RandomTrader extends AutoTrader {
|
|||||||
return getRandom(min, max);
|
return getRandom(min, max);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public long cancelOrders() {
|
public long cancelOrders() {
|
||||||
int n = se.getNumberOfOpenOrders(account_id);
|
int n = se.getNumberOfOpenOrders(account_id);
|
||||||
// System.out.print("Open Orders: "+n+"\n");
|
// System.out.print("Open Orders: "+n+"\n");
|
||||||
@ -175,8 +162,6 @@ if (ad==null || myconfig==null) {
|
|||||||
Quote q = se.getCurrentPrice();
|
Quote q = se.getCurrentPrice();
|
||||||
double lp = q == null ? start : q.price;
|
double lp = q == null ? start : q.price;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
double limit;
|
double limit;
|
||||||
limit = lp + getRandomAmmount(lp, myconfig.buy_limit);
|
limit = lp + getRandomAmmount(lp, myconfig.buy_limit);
|
||||||
|
|
||||||
@ -185,36 +170,25 @@ if (ad==null || myconfig==null) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// System.out.print("Volume is:"+volume+"\n");
|
|
||||||
// System.out.print("My Ammount is: "+money+" My limit si:"+limit+ "\n");
|
|
||||||
|
|
||||||
|
|
||||||
se.createOrder(account_id, type, volume, limit);
|
se.createOrder(account_id, type, volume, limit);
|
||||||
|
|
||||||
return getRandom(myconfig.buy_order_wait);
|
return getRandom(myconfig.buy_order_wait);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public long doSell() {
|
public long doSell() {
|
||||||
RandomTraderConfig myconfig = (RandomTraderConfig) this.config;
|
RandomTraderConfig myconfig = (RandomTraderConfig) this.config;
|
||||||
AccountData ad = this.se.getAccountData(account_id);
|
AccountData ad = this.se.getAccountData(account_id);
|
||||||
|
|
||||||
OrderType type = OrderType.ASK;
|
OrderType type = OrderType.ASK;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// how much money we ant to envest?
|
// how much money we ant to envest?
|
||||||
double volume = (long) getRandomAmmount(ad.shares, myconfig.sell_volume);
|
double volume = (long) getRandomAmmount(ad.shares, myconfig.sell_volume);
|
||||||
|
|
||||||
|
|
||||||
// double lp = 100.0; //se.getBestLimit(type);
|
// double lp = 100.0; //se.getBestLimit(type);
|
||||||
|
|
||||||
Quote q = se.getCurrentPrice();
|
Quote q = se.getCurrentPrice();
|
||||||
double lp = q == null ? start : q.price;
|
double lp = q == null ? start : q.price;
|
||||||
|
|
||||||
|
|
||||||
double limit;
|
double limit;
|
||||||
limit = lp + getRandomAmmount(lp, myconfig.sell_limit);
|
limit = lp + getRandomAmmount(lp, myconfig.sell_limit);
|
||||||
|
|
||||||
@ -226,18 +200,12 @@ if (ad==null || myconfig==null) {
|
|||||||
|
|
||||||
// System.out.print("Volume is:"+volume+"\n");
|
// System.out.print("Volume is:"+volume+"\n");
|
||||||
// System.out.print("My Ammount is: "+volume+" My limit si:"+limit+ "\n");
|
// System.out.print("My Ammount is: "+volume+" My limit si:"+limit+ "\n");
|
||||||
|
|
||||||
|
|
||||||
se.createOrder(account_id, type, volume, limit);
|
se.createOrder(account_id, type, volume, limit);
|
||||||
|
|
||||||
return getRandom(myconfig.sell_order_wait);
|
return getRandom(myconfig.sell_order_wait);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
long doTrade() {
|
long doTrade() {
|
||||||
cancelOrders();
|
cancelOrders();
|
||||||
Action a = getAction();
|
Action a = getAction();
|
||||||
@ -247,13 +215,12 @@ if (ad==null || myconfig==null) {
|
|||||||
case SELL:
|
case SELL:
|
||||||
return doSell();
|
return doSell();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
private static class TimerTaskImpl extends TimerTask {
|
private static class TimerTaskImpl extends TimerTask {
|
||||||
|
|
||||||
RandomTrader trader;
|
RandomTrader trader;
|
||||||
@ -269,12 +236,13 @@ if (ad==null || myconfig==null) {
|
|||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
long time = trader.event();
|
long time = trader.event();
|
||||||
time/=1;
|
time /= 100;
|
||||||
|
|
||||||
this.cancel();
|
this.cancel();
|
||||||
timer.schedule(new TimerTaskImpl(trader, timer), time);
|
timer.schedule(new TimerTaskImpl(trader, timer), time);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -70,10 +70,10 @@ public class Test {
|
|||||||
Scheduler s = new Scheduler();
|
Scheduler s = new Scheduler();
|
||||||
s.start();
|
s.start();
|
||||||
|
|
||||||
class Ev implements Scheduler.TimerEvent{
|
class Ev implements Scheduler.TimerTask{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long timerEvent() {
|
public long timerTask() {
|
||||||
System.out.printf("Timer Event Occured %s\n",name);
|
System.out.printf("Timer Event Occured %s\n",name);
|
||||||
if ("Ev1".equals(this.name))
|
if ("Ev1".equals(this.name))
|
||||||
return 2000;
|
return 2000;
|
||||||
@ -92,8 +92,8 @@ public class Test {
|
|||||||
Ev e2 = new Ev("Eb2");
|
Ev e2 = new Ev("Eb2");
|
||||||
|
|
||||||
|
|
||||||
s.startEvent(e1, 0);
|
s.startTimerEvent(e1, 0);
|
||||||
s.startEvent(e2, 0);
|
s.startTimerEvent(e2, 0);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -110,7 +110,7 @@ public class Test {
|
|||||||
|
|
||||||
System.out.print("All isstopped\n");
|
System.out.print("All isstopped\n");
|
||||||
|
|
||||||
// s.startEvent(e2, 100);
|
// s.startTimerEvent(e2, 100);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user