Scheduler 0.0000001
This commit is contained in:
@ -1,101 +0,0 @@
|
||||
package sesim;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
||||
final public class Account_old {
|
||||
|
||||
/**
|
||||
* Exchange this account belongs to
|
||||
*/
|
||||
public Exchange se;
|
||||
|
||||
/**
|
||||
* Number of shares in this account
|
||||
*/
|
||||
public long shares = 0;
|
||||
|
||||
/**
|
||||
* Ammount of money in this account
|
||||
*/
|
||||
public double money = 0;
|
||||
|
||||
/**
|
||||
* Name of this account
|
||||
*/
|
||||
public String name = "";
|
||||
|
||||
|
||||
public ArrayList <Order_old> pending;
|
||||
|
||||
public boolean orderpending = false;
|
||||
|
||||
|
||||
public Account_old(Exchange se, long shares, double money ) {
|
||||
this.shares=shares;
|
||||
this.money=money;
|
||||
this.se=se;
|
||||
pending = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Account_old(){
|
||||
//this(,0.0);
|
||||
}
|
||||
|
||||
// private double bound_money;
|
||||
|
||||
|
||||
|
||||
public void print_current() {
|
||||
System.out.printf("%s shares: %d credit: %.2f\n",
|
||||
name, shares, money
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public boolean isRuined(){
|
||||
|
||||
|
||||
/* System.out.print(
|
||||
"Account_old: "
|
||||
+money
|
||||
+" / "
|
||||
+shares
|
||||
+"\n"
|
||||
);
|
||||
*/
|
||||
return this.money<=se.lastprice && this.shares<=0;
|
||||
}
|
||||
|
||||
/*
|
||||
public Order_old sell(long volume, double limit) {
|
||||
SellOrder o = new SellOrder();
|
||||
o.account = this;
|
||||
o.limit = limit;
|
||||
o.volume = volume;
|
||||
orderpending = true;
|
||||
return se.SendOrder(o);
|
||||
}
|
||||
*/
|
||||
/* public Order_old buy(long volume, double limit) {
|
||||
if (volume * limit > money) {
|
||||
return null;
|
||||
}
|
||||
BuyOrder o = new BuyOrder();
|
||||
o.limit = limit;
|
||||
o.volume = volume;
|
||||
o.account = this;
|
||||
orderpending = true;
|
||||
return se.SendOrder(o);
|
||||
}
|
||||
/*
|
||||
/*
|
||||
public void Buy(Account_old a, long size, double price) {
|
||||
shares += size;
|
||||
money -= price * size;
|
||||
a.shares -= size;
|
||||
a.money += price * size;
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
@ -25,11 +25,26 @@
|
||||
*/
|
||||
package sesim;
|
||||
|
||||
import java.util.SortedSet;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class AutoTraderLIst {
|
||||
public class AutoTraderList {
|
||||
|
||||
SortedSet <AutoTrader> traders;
|
||||
|
||||
|
||||
AutoTraderList(){
|
||||
|
||||
}
|
||||
|
||||
List getTraders(){
|
||||
|
||||
|
||||
return new ArrayList();
|
||||
}
|
||||
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package sesim;
|
||||
|
||||
public class BuyOrder extends Order_old implements Comparable<Order_old> {
|
||||
|
||||
public BuyOrder(){
|
||||
super(OrderType_old.bid);
|
||||
}
|
||||
|
||||
}
|
@ -2,10 +2,8 @@ package sesim;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import sesim.Order_old.OrderType_old;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -168,9 +166,7 @@ public class Exchange { //extends Thread {
|
||||
* Constructor
|
||||
*/
|
||||
public Exchange() {
|
||||
this.ask = new TreeSet<>();
|
||||
this.bid = new TreeSet<>();
|
||||
// this.qrlist = new ArrayList<>();
|
||||
|
||||
this.qrlist = (new CopyOnWriteArrayList<>());
|
||||
|
||||
// Create order books
|
||||
@ -316,23 +312,12 @@ public class Exchange { //extends Thread {
|
||||
double lastprice = 100.0;
|
||||
long lastsvolume;
|
||||
|
||||
public TreeSet<Order_old> bid;
|
||||
public TreeSet<Order_old> ask;
|
||||
|
||||
|
||||
private final Locker tradelock = new Locker();
|
||||
|
||||
|
||||
private TreeSet<Order_old> selectOrderBook(OrderType_old t) {
|
||||
|
||||
switch (t) {
|
||||
case bid:
|
||||
return this.bid;
|
||||
case ask:
|
||||
return this.ask;
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public ArrayList<Order> getOrderBook(OrderType type, int depth) {
|
||||
|
||||
@ -356,43 +341,9 @@ public class Exchange { //extends Thread {
|
||||
return this.quoteHistory.first();
|
||||
}
|
||||
|
||||
public void print_current() {
|
||||
|
||||
Order_old b;
|
||||
Order_old a;
|
||||
|
||||
//String BID;
|
||||
if (bid.isEmpty()) {
|
||||
b = new BuyOrder();
|
||||
b.limit = -1;
|
||||
b.volume = 0;
|
||||
} else {
|
||||
b = bid.first();
|
||||
}
|
||||
|
||||
if (ask.isEmpty()) {
|
||||
a = new SellOrder();
|
||||
a.limit = -1;
|
||||
a.volume = 0;
|
||||
|
||||
} else {
|
||||
a = ask.first();
|
||||
}
|
||||
|
||||
Logger.info(String.format("BID: %s(%s) LAST: %.2f(%d) ASK: %s(%s)\n",
|
||||
b.format_limit(), b.format_volume(),
|
||||
lastprice, lastsvolume,
|
||||
a.format_limit(), a.format_volume())
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
public void transferMoney(Account_old src, Account_old dst, double money) {
|
||||
src.money -= money;
|
||||
dst.money += money;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void transferMoneyAndShares(Account src, Account dst, double money, double shares) {
|
||||
src.money -= money;
|
||||
dst.money += money;
|
||||
@ -431,33 +382,9 @@ public class Exchange { //extends Thread {
|
||||
*
|
||||
* @param o
|
||||
*/
|
||||
public void cancelOrder_old(Order_old o) {
|
||||
tradelock.lock();
|
||||
TreeSet<Order_old> book = this.selectOrderBook(o.type);
|
||||
book.remove(o);
|
||||
/* this.updateBookReceivers(o.type);
|
||||
o.account.pending.remove(o);
|
||||
o.status = OrderStatus.canceled;
|
||||
tradelock.unlock();
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfer shares from one account to another account
|
||||
*
|
||||
* @param src source account
|
||||
* @param dst destination account
|
||||
* @param volumen number of shares
|
||||
* @param price price
|
||||
*/
|
||||
protected void transferShares(Account_old src, Account_old dst, long volume, double price) {
|
||||
dst.shares += volume;
|
||||
src.shares -= volume;
|
||||
dst.money -= price * volume;
|
||||
src.money += price * volume;
|
||||
}
|
||||
|
||||
|
||||
|
||||
long nextQuoteId = 0;
|
||||
|
||||
private void removeOrderIfExecuted(Order o) {
|
||||
|
@ -27,7 +27,9 @@ package sesim;
|
||||
|
||||
import java.util.concurrent.Semaphore;
|
||||
|
||||
|
||||
/**
|
||||
* A locker object
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
@ -37,6 +39,7 @@ public class Locker {
|
||||
|
||||
/**
|
||||
* Acquire a lock
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean lock() {
|
||||
@ -48,6 +51,9 @@ public class Locker {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Release a lock
|
||||
*/
|
||||
public void unlock() {
|
||||
AVAIL.release();
|
||||
}
|
||||
|
@ -1,105 +0,0 @@
|
||||
package sesim;
|
||||
|
||||
public abstract class Order_old implements Comparable<Order_old> {
|
||||
|
||||
/**
|
||||
* When the order was created
|
||||
*/
|
||||
public long timestamp = 0;
|
||||
|
||||
/**
|
||||
* Number of shares
|
||||
*/
|
||||
public long volume;
|
||||
|
||||
/**
|
||||
* Limit price
|
||||
*/
|
||||
public double limit;
|
||||
|
||||
/**
|
||||
* Order_old ID
|
||||
*/
|
||||
public long id = 0;
|
||||
|
||||
/**
|
||||
* Type of order
|
||||
*/
|
||||
public final OrderType_old type;
|
||||
|
||||
public Account_old account = null;
|
||||
|
||||
|
||||
protected int compareLimit(Order_old o){
|
||||
|
||||
int r=0;
|
||||
if (o.limit < limit) {
|
||||
r=-1;
|
||||
}
|
||||
if (o.limit > limit) {
|
||||
r=1;
|
||||
}
|
||||
|
||||
if (type==OrderType_old.ask)
|
||||
return -r;
|
||||
|
||||
return r;
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
public int compareTo(Order_old o) {
|
||||
|
||||
if (o.type!=type){
|
||||
System.out.print("OrderType Missmatch\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int r = compareLimit(o);
|
||||
if (r!=0)
|
||||
return r;
|
||||
|
||||
|
||||
if (o.id>id)
|
||||
return -1;
|
||||
|
||||
if (o.id<id)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
enum OrderStatus {
|
||||
open, executed, canceled
|
||||
}
|
||||
|
||||
public enum OrderType_old {
|
||||
bid,ask
|
||||
}
|
||||
|
||||
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_volume() {
|
||||
return String.format("%d", volume);
|
||||
}
|
||||
|
||||
Order_old(OrderType_old type) {
|
||||
this.type=type;
|
||||
}
|
||||
}
|
231
src/main/java/sesim/Scheduler.java
Normal file
231
src/main/java/sesim/Scheduler.java
Normal file
@ -0,0 +1,231 @@
|
||||
/*
|
||||
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package sesim;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.SortedMap;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
import java.util.concurrent.ConcurrentSkipListSet;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class Scheduler extends Thread {
|
||||
|
||||
long multiply = 1;
|
||||
final SortedMap<Long, SortedSet<TimerEvent>> event_queue = new TreeMap<>();
|
||||
|
||||
private boolean stop = false;
|
||||
|
||||
public class Runner extends Thread {
|
||||
|
||||
Runner() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (!stop) {
|
||||
synchronized (event_queue) {
|
||||
try {
|
||||
event_queue.wait(5000);
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
System.out.printf("500 mllies waited\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class Event implements Comparable {
|
||||
|
||||
long time;
|
||||
|
||||
@Override
|
||||
public int compareTo(Object o) {
|
||||
if (((Event) o).time < time) {
|
||||
return -1;
|
||||
}
|
||||
if (((Event) o).time > time) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Runner queues[];
|
||||
Runner runners[];
|
||||
|
||||
/**
|
||||
*
|
||||
* @param num_runners
|
||||
*/
|
||||
public Scheduler(int num_runners) {
|
||||
// event_queue = new ConcurrentSkipListSet(new TreeSet<>());
|
||||
runners = new Runner[num_runners];
|
||||
|
||||
for (int i = 0; i < num_runners; i++) {
|
||||
Runner r = new Runner();
|
||||
System.out.print("Making runner: " + r + "\n");
|
||||
|
||||
runners[i] = new Runner();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void start() {
|
||||
|
||||
/* for (Runner runner : runners) {
|
||||
runner.start();
|
||||
}
|
||||
*/
|
||||
super.start();
|
||||
}
|
||||
|
||||
/*
|
||||
public boolean isAlive() {
|
||||
|
||||
for (Runner runner : runners) {
|
||||
if (runner.isAlive()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
public void halt() {
|
||||
stop = true;
|
||||
synchronized (event_queue) {
|
||||
event_queue.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
public interface TimerEvent {
|
||||
|
||||
long timerEvent();
|
||||
|
||||
}
|
||||
|
||||
class ObjectComparator implements Comparator<Object> {
|
||||
|
||||
@Override
|
||||
public int compare(Object o1, Object o2) {
|
||||
return System.identityHashCode(o1) - System.identityHashCode(o1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public long getCurrentTimeMillies() {
|
||||
return System.currentTimeMillis();
|
||||
|
||||
}
|
||||
|
||||
public void startEvent(TimerEvent e, long time) {
|
||||
long evtime = time + this.getCurrentTimeMillies();
|
||||
synchronized (event_queue) {
|
||||
SortedSet<TimerEvent> s = event_queue.get(evtime);
|
||||
if (s == null) {
|
||||
s = new TreeSet<>(new ObjectComparator());
|
||||
event_queue.put(evtime, s);
|
||||
}
|
||||
s.add(e);
|
||||
}
|
||||
synchronized(this){
|
||||
notify();
|
||||
}
|
||||
}
|
||||
|
||||
public long fireEvent(TimerEvent e) {
|
||||
return e.timerEvent();
|
||||
}
|
||||
|
||||
private void addEvent(TimerEvent e, long time) {
|
||||
long evtime = time + this.getCurrentTimeMillies();
|
||||
SortedSet<TimerEvent> s = event_queue.get(evtime);
|
||||
if (s == null) {
|
||||
s = new TreeSet<>(new ObjectComparator());
|
||||
event_queue.put(evtime, s);
|
||||
}
|
||||
s.add(e);
|
||||
}
|
||||
|
||||
public long runEvents() {
|
||||
synchronized (event_queue) {
|
||||
if (event_queue.isEmpty()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
long t = event_queue.firstKey();
|
||||
if (t >= this.getCurrentTimeMillies()) {
|
||||
SortedSet s = event_queue.get(t);
|
||||
event_queue.remove(t);
|
||||
Iterator<TimerEvent> it = s.iterator();
|
||||
while (it.hasNext()) {
|
||||
long next_t = this.fireEvent(it.next());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (!stop) {
|
||||
long wtime = runEvents();
|
||||
|
||||
synchronized (this) {
|
||||
try {
|
||||
if (wtime != -1) {
|
||||
wait(wtime);
|
||||
} else {
|
||||
wait();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.print("Running the scheduler\n");
|
||||
}
|
||||
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
package sesim;
|
||||
|
||||
public class SellOrder extends Order_old {
|
||||
|
||||
public SellOrder(){
|
||||
super(OrderType_old.ask);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user