Package naming
This commit is contained in:
100
src/sesim/Account.java
Normal file
100
src/sesim/Account.java
Normal file
@ -0,0 +1,100 @@
|
||||
package sesim;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
||||
final public class Account {
|
||||
|
||||
/**
|
||||
* 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> pending;
|
||||
|
||||
public boolean orderpending = false;
|
||||
|
||||
|
||||
public Account(Exchange se, long shares, double money ) {
|
||||
this.shares=shares;
|
||||
this.money=money;
|
||||
this.se=se;
|
||||
pending = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Account(){
|
||||
//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: "
|
||||
+money
|
||||
+" / "
|
||||
+shares
|
||||
+"\n"
|
||||
);
|
||||
*/
|
||||
return this.money<=se.lastprice && this.shares<=0;
|
||||
}
|
||||
|
||||
public Order 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 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 a, long size, double price) {
|
||||
shares += size;
|
||||
money -= price * size;
|
||||
a.shares -= size;
|
||||
a.money += price * size;
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
63
src/sesim/AutoTrader.java
Normal file
63
src/sesim/AutoTrader.java
Normal file
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2016, 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 static java.lang.Thread.sleep;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public abstract class AutoTrader extends Trader implements Runnable {
|
||||
|
||||
public AutoTrader(Account account, TraderConfig config) {
|
||||
super(account, config);
|
||||
}
|
||||
|
||||
protected void doSleep(int seconds) {
|
||||
try {
|
||||
sleep(seconds*1000);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public void start(){
|
||||
System.out.print("Starting AutoTrader\n");
|
||||
class Runner extends Thread{
|
||||
AutoTrader trader;
|
||||
@Override
|
||||
public void run(){
|
||||
trader.run();
|
||||
}
|
||||
}
|
||||
Runner r = new Runner();
|
||||
r.trader=this;
|
||||
r.start();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
44
src/sesim/AutoTraderLIst.java
Normal file
44
src/sesim/AutoTraderLIst.java
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (c) 2016, 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;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class AutoTraderLIst {
|
||||
|
||||
public void add(int n, TraderConfig config, Exchange se, long shares, double money) {
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
AutoTrader trader = config.createTrader(se, shares, money);
|
||||
// TraderRunner tr = new TraderRunner(trader);
|
||||
|
||||
trader.start();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
9
src/sesim/BuyOrder.java
Normal file
9
src/sesim/BuyOrder.java
Normal file
@ -0,0 +1,9 @@
|
||||
package sesim;
|
||||
|
||||
public class BuyOrder extends Order implements Comparable<Order> {
|
||||
|
||||
public BuyOrder(){
|
||||
type=OrderType.bid;
|
||||
}
|
||||
|
||||
}
|
457
src/sesim/Exchange.java
Normal file
457
src/sesim/Exchange.java
Normal file
@ -0,0 +1,457 @@
|
||||
package sesim;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import sesim.Order.OrderStatus;
|
||||
import sesim.Order.OrderType;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author tube
|
||||
*/
|
||||
public class Exchange extends Thread {
|
||||
|
||||
/**
|
||||
* Histrory of quotes
|
||||
*/
|
||||
public TreeSet<Quote> quoteHistory = new TreeSet<>();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public Exchange() {
|
||||
this.ask = new TreeSet<>();
|
||||
this.bid = new TreeSet<>();
|
||||
this.qrlist = new ArrayList<>();
|
||||
|
||||
}
|
||||
|
||||
public static long getCurrentTimeSeconds(long div) {
|
||||
long ct = System.currentTimeMillis() / 1000*div;
|
||||
return ct * div;
|
||||
}
|
||||
|
||||
public static long getCurrentTimeSeconds(){
|
||||
return getCurrentTimeSeconds(1);
|
||||
}
|
||||
|
||||
public SortedSet<Quote> getQuoteHistory(long start) {
|
||||
|
||||
Quote s = new Quote();
|
||||
s.time = start;
|
||||
s.time = 2;
|
||||
s.id = 2;
|
||||
|
||||
TreeSet<Quote> result = new TreeSet<>();
|
||||
result.addAll(this.quoteHistory.tailSet(s));
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
/* public SortedSet<Quote> getQuoteHistory(int seconds) {
|
||||
Quote last = quoteHistory.last();
|
||||
return this.getQuoteHistory(seconds, last.time);
|
||||
}
|
||||
*/
|
||||
|
||||
// Class to describe an executed order
|
||||
// QuoteReceiver has to be implemented by objects that wants
|
||||
// to receive quote updates
|
||||
public interface QuoteReceiver {
|
||||
|
||||
void UpdateQuote(Quote q);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bookreceiver Interface
|
||||
*/
|
||||
public interface BookReceiver {
|
||||
|
||||
void UpdateOrderBook();
|
||||
}
|
||||
|
||||
private ArrayList<BookReceiver> ask_bookreceivers = new ArrayList<>();
|
||||
private ArrayList<BookReceiver> bid_bookreceivers = new ArrayList<>();
|
||||
|
||||
private ArrayList<BookReceiver> selectBookReceiver(OrderType t) {
|
||||
switch (t) {
|
||||
case ask:
|
||||
return ask_bookreceivers;
|
||||
case bid:
|
||||
return bid_bookreceivers;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addBookReceiver(OrderType t, BookReceiver br) {
|
||||
ArrayList<BookReceiver> bookreceivers;
|
||||
bookreceivers = selectBookReceiver(t);
|
||||
bookreceivers.add(br);
|
||||
}
|
||||
|
||||
void updateBookReceivers(OrderType t) {
|
||||
ArrayList<BookReceiver> bookreceivers;
|
||||
bookreceivers = selectBookReceiver(t);
|
||||
|
||||
Iterator<BookReceiver> i = bookreceivers.iterator();
|
||||
while (i.hasNext()) {
|
||||
i.next().UpdateOrderBook();
|
||||
}
|
||||
try {
|
||||
sleep(10);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("I was Interrupted");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Here we store the list of quote receivers
|
||||
private final ArrayList<QuoteReceiver> qrlist;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param qr
|
||||
*/
|
||||
public void addQuoteReceiver(QuoteReceiver qr) {
|
||||
qrlist.add(qr);
|
||||
}
|
||||
|
||||
// send updated quotes to all quote receivers
|
||||
private void updateQuoteReceivers(Quote q) {
|
||||
Iterator<QuoteReceiver> i = qrlist.iterator();
|
||||
while (i.hasNext()) {
|
||||
i.next().UpdateQuote(q);
|
||||
}
|
||||
}
|
||||
|
||||
// long time = 0;
|
||||
double theprice = 12.9;
|
||||
long orderid = 1;
|
||||
|
||||
double lastprice = 100.0;
|
||||
long lastsvolume;
|
||||
|
||||
public TreeSet<Order> bid;
|
||||
public TreeSet<Order> ask;
|
||||
|
||||
private Locker tradelock = new Locker();
|
||||
|
||||
/*
|
||||
private final Semaphore available = new Semaphore(1, true);
|
||||
|
||||
private void Lock() {
|
||||
try {
|
||||
available.acquire();
|
||||
} catch (InterruptedException s) {
|
||||
System.out.println("Interrupted\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void Unlock() {
|
||||
available.release();
|
||||
}
|
||||
*/
|
||||
private TreeSet<Order> selectOrderBook(OrderType t) {
|
||||
|
||||
switch (t) {
|
||||
case bid:
|
||||
return this.bid;
|
||||
case ask:
|
||||
return this.ask;
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
public ArrayList<Order> getOrderBook(OrderType t, int depth) {
|
||||
|
||||
TreeSet<Order> book = selectOrderBook(t);
|
||||
if (book == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ArrayList<Order> ret = new ArrayList<>();
|
||||
Iterator<Order> it = book.iterator();
|
||||
for (int i = 0; i < depth && it.hasNext(); i++) {
|
||||
Order o;
|
||||
o = it.next();
|
||||
ret.add(o);
|
||||
//System.out.print("Order" + o.limit);
|
||||
//System.out.println();
|
||||
}
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
public void print_current() {
|
||||
|
||||
Order b;
|
||||
Order 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 src, Account dst, double money) {
|
||||
src.money -= money;
|
||||
dst.money += money;
|
||||
|
||||
}
|
||||
|
||||
public void cancelOrder(Order o) {
|
||||
tradelock.lock();
|
||||
TreeSet<Order> 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 src, Account dst, long volume, double price) {
|
||||
dst.shares += volume;
|
||||
src.shares -= volume;
|
||||
dst.money -= price * volume;
|
||||
src.money += price * volume;
|
||||
}
|
||||
|
||||
long nextQuoteId = 0;
|
||||
|
||||
public void OrderMatching() {
|
||||
|
||||
while (true) {
|
||||
|
||||
if (bid.isEmpty() || ask.isEmpty()) {
|
||||
// nothing to do
|
||||
return;
|
||||
}
|
||||
|
||||
Order b = bid.first();
|
||||
Order a = ask.first();
|
||||
|
||||
if (a.volume == 0) {
|
||||
// This order is fully executed, remove
|
||||
a.account.orderpending = false;
|
||||
a.status = OrderStatus.executed;
|
||||
|
||||
a.account.pending.remove(a);
|
||||
|
||||
ask.pollFirst();
|
||||
this.updateBookReceivers(OrderType.ask);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (b.volume == 0) {
|
||||
// This order is fully executed, remove
|
||||
b.account.orderpending = false;
|
||||
b.status = OrderStatus.executed;
|
||||
b.account.pending.remove(b);
|
||||
bid.pollFirst();
|
||||
this.updateBookReceivers(OrderType.bid);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (b.limit < a.limit) {
|
||||
// no match, nothing to do
|
||||
return;
|
||||
}
|
||||
|
||||
if (b.limit >= a.limit) {
|
||||
double price;
|
||||
|
||||
if (b.id < a.id) {
|
||||
price = b.limit;
|
||||
} else {
|
||||
price = a.limit;
|
||||
}
|
||||
|
||||
long volume;
|
||||
|
||||
if (b.volume >= a.volume) {
|
||||
volume = a.volume;
|
||||
} else {
|
||||
volume = b.volume;
|
||||
}
|
||||
|
||||
transferShares(a.account, b.account, volume, price);
|
||||
|
||||
// b.account.Buy(a.account, volume, price);
|
||||
b.volume -= volume;
|
||||
a.volume -= volume;
|
||||
|
||||
lastprice = price;
|
||||
lastsvolume = volume;
|
||||
|
||||
Quote q = new Quote();
|
||||
|
||||
q.volume = volume;
|
||||
q.price = price;
|
||||
q.time = System.currentTimeMillis();
|
||||
|
||||
q.ask = a.limit;
|
||||
q.bid = b.limit;
|
||||
q.id = nextQuoteId++;
|
||||
|
||||
this.updateQuoteReceivers(q);
|
||||
this.updateBookReceivers(OrderType.bid);
|
||||
this.updateBookReceivers(OrderType.ask);
|
||||
|
||||
/* System.out.print(
|
||||
"Executed: "
|
||||
+ q.price
|
||||
+ " / "
|
||||
+ q.volume
|
||||
+ "\n"
|
||||
);
|
||||
*/
|
||||
quoteHistory.add(q);
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void ExecuteOrder(BuyOrder o) {
|
||||
// SellOrder op = ask.peek();
|
||||
|
||||
}
|
||||
|
||||
private boolean InitOrder(Order o) {
|
||||
double moneyNeeded = o.volume * o.limit;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Add an order to the orderbook
|
||||
private boolean addOrder(Order o) {
|
||||
boolean ret = false;
|
||||
switch (o.type) {
|
||||
case bid:
|
||||
|
||||
// System.out.print("Exchange adding bid order \n");
|
||||
ret = bid.add(o);
|
||||
break;
|
||||
|
||||
case ask:
|
||||
// System.out.print("Exchange adding ask order \n");
|
||||
ret = ask.add(o);
|
||||
break;
|
||||
}
|
||||
|
||||
if (ret) {
|
||||
this.updateBookReceivers(o.type);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public Order SendOrder(Order o) {
|
||||
|
||||
boolean rc = InitOrder(o);
|
||||
if (!rc) {
|
||||
return null;
|
||||
}
|
||||
|
||||
tradelock.lock();
|
||||
o.timestamp = System.currentTimeMillis();
|
||||
o.id = orderid++;
|
||||
addOrder(o);
|
||||
o.account.pending.add(o);
|
||||
OrderMatching();
|
||||
tradelock.unlock();
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
/*
|
||||
public void SendOrder(BuyOrder o) {
|
||||
//System.out.println("EX Buyorder");
|
||||
Lock();
|
||||
o.timestamp = System.currentTimeMillis();
|
||||
o.id = orderid++;
|
||||
bid.add(o);
|
||||
|
||||
Unlock();
|
||||
Lock();
|
||||
// OrderMatching();
|
||||
Unlock();
|
||||
|
||||
}
|
||||
*/
|
||||
/*
|
||||
* public void SendOrder(Order o){
|
||||
*
|
||||
*
|
||||
* if ( o.getClass() == BuyOrder.class){ bid.add((BuyOrder)o); }
|
||||
*
|
||||
* if ( o.getClass() == SellOrder.class){ ask.add((SellOrder)o); }
|
||||
*
|
||||
* }
|
||||
*/
|
||||
public double getlastprice() {
|
||||
/*
|
||||
* SellOrder so = new SellOrder(); so.limit=1000.0; so.volume=500;
|
||||
* SendOrder(so);
|
||||
*
|
||||
* BuyOrder bo = new BuyOrder(); bo.limit=1001.0; bo.volume=300;
|
||||
* SendOrder(bo);
|
||||
*/
|
||||
|
||||
return lastprice;
|
||||
}
|
||||
|
||||
/* public double sendOrder(Account o) {
|
||||
return 0.7;
|
||||
}
|
||||
*/
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
try {
|
||||
sleep(1500);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("I was Interrupted");
|
||||
}
|
||||
print_current();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
51
src/sesim/Locker.java
Normal file
51
src/sesim/Locker.java
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2016, 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.concurrent.Semaphore;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class Locker {
|
||||
|
||||
private final Semaphore avail = new Semaphore(1, true);
|
||||
|
||||
public boolean lock() {
|
||||
try {
|
||||
avail.acquire();
|
||||
} catch (InterruptedException e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void unlock() {
|
||||
avail.release();
|
||||
}
|
||||
|
||||
}
|
23
src/sesim/Logger.java
Normal file
23
src/sesim/Logger.java
Normal file
@ -0,0 +1,23 @@
|
||||
package sesim;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
110
src/sesim/Order.java
Normal file
110
src/sesim/Order.java
Normal file
@ -0,0 +1,110 @@
|
||||
package sesim;
|
||||
|
||||
public abstract class Order implements Comparable<Order> {
|
||||
|
||||
/**
|
||||
* When the order was created
|
||||
*/
|
||||
public long timestamp = 0;
|
||||
|
||||
/**
|
||||
* Number of shares
|
||||
*/
|
||||
public long volume;
|
||||
|
||||
/**
|
||||
* Limit price
|
||||
*/
|
||||
public double limit;
|
||||
|
||||
/**
|
||||
* Order ID
|
||||
*/
|
||||
public long id = 0;
|
||||
|
||||
/**
|
||||
* Type of order
|
||||
*/
|
||||
public OrderType type;
|
||||
|
||||
public Account account = null;
|
||||
|
||||
|
||||
protected int compareLimit(Order o){
|
||||
int r=0;
|
||||
if (o.limit < limit) {
|
||||
r=-1;
|
||||
}
|
||||
if (o.limit > limit) {
|
||||
r=1;
|
||||
}
|
||||
|
||||
if (type==OrderType.ask)
|
||||
return -r;
|
||||
|
||||
return r;
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
public int compareTo(Order o) {
|
||||
|
||||
if (o.type!=type){
|
||||
System.out.print("OrderType Missmatch\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int r = compareLimit(o);
|
||||
if (r!=0)
|
||||
return r;
|
||||
|
||||
/* if (o.timestamp> timestamp)
|
||||
return -1;
|
||||
|
||||
if (o.timestamp<timestamp)
|
||||
return 1;
|
||||
*/
|
||||
|
||||
if (o.id>id)
|
||||
return -1;
|
||||
|
||||
if (o.id<id)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
enum OrderStatus {
|
||||
open, executed, canceled
|
||||
}
|
||||
|
||||
public enum OrderType {
|
||||
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() {
|
||||
|
||||
}
|
||||
}
|
76
src/sesim/Quote.java
Normal file
76
src/sesim/Quote.java
Normal file
@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright (c) 2016, 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;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class Quote implements Comparable {
|
||||
|
||||
public double bid;
|
||||
public double bid_volume;
|
||||
public double ask;
|
||||
public double ask_volume;
|
||||
|
||||
public double price;
|
||||
public long volume;
|
||||
public long time;
|
||||
|
||||
Locker lock = new Locker();
|
||||
|
||||
public void print() {
|
||||
System.out.print("Quote ("
|
||||
+ time
|
||||
+ ") :"
|
||||
+ price
|
||||
+ " / "
|
||||
+ volume
|
||||
+ "\n"
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
public long id;
|
||||
|
||||
@Override
|
||||
public int compareTo(Object o) {
|
||||
int ret;
|
||||
Quote q = (Quote)o;
|
||||
|
||||
ret = (int)(this.time-q.time);
|
||||
if (ret !=0)
|
||||
return ret;
|
||||
|
||||
return (int)(this.id-q.id);
|
||||
}
|
||||
|
||||
/* Quote (){
|
||||
lock.lock();
|
||||
id=nextid++;
|
||||
lock.unlock();
|
||||
}*/
|
||||
}
|
8
src/sesim/SellOrder.java
Normal file
8
src/sesim/SellOrder.java
Normal file
@ -0,0 +1,8 @@
|
||||
package sesim;
|
||||
|
||||
public class SellOrder extends Order {
|
||||
|
||||
public SellOrder(){
|
||||
type=OrderType.ask;
|
||||
}
|
||||
}
|
69
src/sesim/Trader.java
Normal file
69
src/sesim/Trader.java
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (c) 2016, 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 static java.lang.Thread.sleep;
|
||||
|
||||
public abstract class Trader {
|
||||
|
||||
public String name = null;
|
||||
|
||||
|
||||
public Account account;
|
||||
public TraderConfig config;
|
||||
|
||||
public void sell(long shares, double limit){
|
||||
account.sell(shares, limit);
|
||||
}
|
||||
|
||||
public void buy(long shares, double limit){
|
||||
account.buy(shares, limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a Trader object
|
||||
* @param account Account for this trader
|
||||
* @param config Configration for this trader
|
||||
*/
|
||||
public Trader(Account account, TraderConfig config){
|
||||
this.account=account;
|
||||
this.config=config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a Trader object w/o config
|
||||
* The Trader object shoul initialize a default config
|
||||
* @param account
|
||||
*/
|
||||
public Trader(Account account){
|
||||
this(account,null);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
36
src/sesim/TraderConfig.java
Normal file
36
src/sesim/TraderConfig.java
Normal file
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (c) 2016, 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;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public abstract class TraderConfig{
|
||||
String name;
|
||||
|
||||
public abstract AutoTrader createTrader(Exchange se, long shares, double money);
|
||||
}
|
Reference in New Issue
Block a user