Addad Autotrades object to let thousands of trades trade

This commit is contained in:
7u83 2016-12-28 20:08:55 +01:00
parent 2b575d87dd
commit 811cc0636d
8 changed files with 216 additions and 94 deletions

View File

@ -158,10 +158,19 @@ public class MainWin extends javax.swing.JFrame {
otherTrader.sell(80, 22.70);
*/
/*
Account traccount = new Account(se,5500,1000000.0);
RandomTrader rt = new RandomTrader(traccount,null);
TraderRunner tr = new TraderRunner(rt);
tr.start();
*/
AutoTraders at = new AutoTraders();
RandomTraderConfig rcfg = new RandomTraderConfig();
at.add(1000, rcfg, se, 1000, 10000);
// at.add(10, rcfg, se, 1000000, 0);
try {

View File

@ -100,7 +100,7 @@ public abstract class OrderBook extends javax.swing.JPanel implements Exchange.B
private boolean desc = false;
public OrderBookListModel() {
System.out.print("CREATING A NEW MODEL\n");
// System.out.print("CREATING A NEW MODEL\n");
// update();
list = getOrderBook();
}
@ -114,7 +114,7 @@ public abstract class OrderBook extends javax.swing.JPanel implements Exchange.B
this.update_calls++;
int hc = this.hashCode();
System.out.print("Update/ColCalls = " + update_calls + "/" + colcount_calls + " HC: " + hc + "\n");
//System.out.print("Update/ColCalls = " + update_calls + "/" + colcount_calls + " HC: " + hc + "\n");
}
@ -134,7 +134,7 @@ public abstract class OrderBook extends javax.swing.JPanel implements Exchange.B
@Override
public int getRowCount() {
colcount_calls++;
System.out.print("Update/ColCalls = " + update_calls + "/" + colcount_calls + "\n");
// System.out.print("Update/ColCalls = " + update_calls + "/" + colcount_calls + "\n");
return list.size();
}

View File

@ -0,0 +1,43 @@
/*
* 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 AutoTraders {
public void add(int n, TraderConfig config, Exchange se, long shares, double money) {
for (int i = 0; i < n; i++) {
Trader trader = config.createTrader(se, shares, money);
TraderRunner tr = new TraderRunner(trader);
tr.start();
}
}
}

View File

@ -51,17 +51,15 @@ public class Exchange extends Thread {
* 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){
private ArrayList<BookReceiver> selectBookReceiver(OrderType t) {
switch (t) {
case ask:
return ask_bookreceivers;
case bid:
@ -69,30 +67,29 @@ public class Exchange extends Thread {
}
return null;
}
public void addBookReceiver(OrderType t, BookReceiver br){
ArrayList <BookReceiver> bookreceivers;
public void addBookReceiver(OrderType t, BookReceiver br) {
ArrayList<BookReceiver> bookreceivers;
bookreceivers = selectBookReceiver(t);
bookreceivers.add(br);
}
void updateBookReceivers(OrderType t) {
ArrayList <BookReceiver> bookreceivers;
ArrayList<BookReceiver> bookreceivers;
bookreceivers = selectBookReceiver(t);
Iterator<BookReceiver> i = bookreceivers.iterator();
while (i.hasNext()) {
i.next().UpdateOrderBook();
}
/* try {
sleep(50);
try {
sleep(0);
} catch (InterruptedException e) {
System.out.println("I was Interrupted");
}
*/
}
}
// Here we store the list of quote receivers
private final TreeSet<QuoteReceiver> qrlist;
@ -117,7 +114,7 @@ public class Exchange extends Thread {
double theprice = 12.9;
long orderid = 1;
double lastprice = 300.0;
double lastprice = 100.0;
long lastsvolume;
public TreeSet<Order> bid;
@ -138,25 +135,25 @@ public class Exchange extends Thread {
available.release();
}
private TreeSet<Order> selectOrderBook(OrderType t){
private TreeSet<Order> selectOrderBook(OrderType t) {
switch(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)
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++) {
@ -170,8 +167,6 @@ public class Exchange extends Thread {
}
public void print_current() {
Order b;
@ -211,7 +206,7 @@ public class Exchange extends Thread {
public void CancelOrder(Order o) {
Lock();
TreeSet <Order> book = this.selectOrderBook(o.type);
TreeSet<Order> book = this.selectOrderBook(o.type);
book.remove(o);
this.updateBookReceivers(o.type);
o.account.pending.remove(o);
@ -219,19 +214,20 @@ public class Exchange extends Thread {
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){
protected void transferShares(Account src, Account dst, long volume, double price) {
dst.shares += volume;
src.shares -= volume;
src.shares -= volume;
dst.money -= price * volume;
src.money += price * volume;
src.money += price * volume;
}
public void OrderMatching() {
@ -250,9 +246,9 @@ public class Exchange extends Thread {
// 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;
@ -264,7 +260,7 @@ public class Exchange extends Thread {
b.status = OrderStatus.executed;
b.account.pending.remove(b);
bid.pollFirst();
this.updateBookReceivers(OrderType.bid);
this.updateBookReceivers(OrderType.bid);
continue;
}
@ -290,11 +286,9 @@ public class Exchange extends Thread {
volume = b.volume;
}
transferShares(a.account,b.account,volume,price);
// b.account.Buy(a.account, volume, price);
transferShares(a.account, b.account, volume, price);
// b.account.Buy(a.account, volume, price);
b.volume -= volume;
a.volume -= volume;
@ -310,7 +304,14 @@ public class Exchange extends Thread {
this.UpdateQuoteReceivers(q);
this.updateBookReceivers(OrderType.bid);
this.updateBookReceivers(OrderType.ask);
System.out.print(
"Executed: "
+ q.price
+ " / "
+ q.size
+ "\n"
);
//quoteHistory.add(q);
continue;
@ -333,41 +334,43 @@ public class Exchange extends Thread {
// Add an order to the orderbook
private boolean addOrder(Order o) {
boolean ret=false;
boolean ret = false;
switch (o.type) {
case bid:
System.out.print("Exchange adding bid oder \n");
// System.out.print("Exchange adding bid order \n");
ret = bid.add(o);
break;
case ask:
System.out.print("Exchange adding ask oder \n");
// System.out.print("Exchange adding ask order \n");
ret = ask.add(o);
break;
}
if (ret)
if (ret) {
this.updateBookReceivers(o.type);
}
return ret;
}
public Order SendOrder(Order o) {
boolean rc = InitOrder(o);
if (!rc)
return null;
Lock();
boolean rc = InitOrder(o);
if (!rc) {
return null;
}
Lock();
o.timestamp = System.currentTimeMillis();
//System.out.print(o.timestamp + " TS:\n");
o.id = orderid++;
addOrder(o);
o.account.pending.add(o);
OrderMatching();
Unlock();
Unlock();
return o;
}

View File

@ -44,11 +44,21 @@ public abstract class Trader {
/**
* 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);
}
};

View File

@ -29,6 +29,8 @@ package SeSim;
*
* @author 7u83 <7u83@mail.ru>
*/
public abstract class TraderConfig {
public abstract class TraderConfig{
String name;
public abstract Trader createTrader(Exchange se, long shares, double money);
}

View File

@ -7,6 +7,13 @@ import SeSim.Trader;
import SeSim.TraderConfig;
public class RandomTrader extends Trader {
public TraderConfig getTraderConfig(){
return new RandomTraderConfig();
}
private RandomTraderConfig myconfig;
Random rand = new Random();
@ -14,36 +21,43 @@ public class RandomTrader extends Trader {
//private Order myorder = null;
public RandomTrader(Account account,TraderConfig config) {
super(account,config);
if (config==null){
config = new RandomTraderConfig();
}
myconfig = (RandomTraderConfig)config;
}
public void doBuy() {
// if (account.money <= 0) {
// return;
// }
if (account.money <= 0) {
return;
}
double perc = rand.nextDouble() * 1.0;
double perc = 5.0-rand.nextDouble() * 10.0;
double lp = account.se.getlastprice();
double limit = lp / 100 * perc + lp;
long size = (int) (account.money / (limit * 1));
long volume = (int) (account.money / (limit * 1));
if (volume ==0 ){
System.out.print("Can't buy shares = 0 my money="+account.money+" shares="+account.shares+"\n");
return;
}
buy(size, limit);
buy(volume, limit);
return;
}
public void doSell() {
/* if (myorder != null) {
return;
}
if (account.shares <= 0) {
return;
}
*/
double perc = rand.nextDouble() * 1.0;
double perc = 5.0-rand.nextDouble() * 10.0;
double lp = account.se.getlastprice();
double limit = lp - lp / 100 * perc;
@ -57,7 +71,7 @@ public class RandomTrader extends Trader {
// System.out.print("RT: Monitoring trades - Pending: "+numpending+"\n");
if (numpending == 0) {
System.out.print("RT: pending = 0 - return false\n");
// System.out.print("RT: pending = 0 - return false\n");
return false;
}
@ -66,9 +80,10 @@ public class RandomTrader extends Trader {
// System.out.print("RT: age is: "+age+"\n");
if (age > 10000) {
if (age > myconfig.maxage) {
// System.out.print("MaxAge is"+myconfig.maxage+"\n");
account.se.CancelOrder(o);
System.out.print("Age reached - canel return false\n");
// System.out.print("Age reached - canel return false\n");
return false;
}
@ -87,7 +102,11 @@ public class RandomTrader extends Trader {
// What next to do?
int action = rand.nextInt(3);
int action = rand.nextInt(5);
if (account.money<10 && account.shares<5){
System.out.print("I'm almost ruined\n");
}
if (action == 1) {
doBuy();
@ -101,22 +120,5 @@ public class RandomTrader extends Trader {
}
/* 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,53 @@
/*
* 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 Traders;
import SeSim.*;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class RandomTraderConfig extends TraderConfig {
public long maxage = 1000 * 10 * 1;
/**
*
* @param se
* @param shares
* @param money
* @return
*/
@Override
public Trader createTrader(Exchange se, long shares, double money) {
Account a = new Account(se, shares, money);
return new RandomTrader(a, this);
}
}