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); otherTrader.sell(80, 22.70);
*/ */
/*
Account traccount = new Account(se,5500,1000000.0); Account traccount = new Account(se,5500,1000000.0);
RandomTrader rt = new RandomTrader(traccount,null); RandomTrader rt = new RandomTrader(traccount,null);
TraderRunner tr = new TraderRunner(rt); TraderRunner tr = new TraderRunner(rt);
tr.start(); 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 { try {

View File

@ -100,7 +100,7 @@ public abstract class OrderBook extends javax.swing.JPanel implements Exchange.B
private boolean desc = false; private boolean desc = false;
public OrderBookListModel() { public OrderBookListModel() {
System.out.print("CREATING A NEW MODEL\n"); // System.out.print("CREATING A NEW MODEL\n");
// update(); // update();
list = getOrderBook(); list = getOrderBook();
} }
@ -114,7 +114,7 @@ public abstract class OrderBook extends javax.swing.JPanel implements Exchange.B
this.update_calls++; this.update_calls++;
int hc = this.hashCode(); 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 @Override
public int getRowCount() { public int getRowCount() {
colcount_calls++; 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(); 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 * Bookreceiver Interface
*/ */
public interface BookReceiver { public interface BookReceiver {
void UpdateOrderBook(); void UpdateOrderBook();
} }
private ArrayList<BookReceiver> ask_bookreceivers = new ArrayList<>(); private ArrayList<BookReceiver> ask_bookreceivers = new ArrayList<>();
private ArrayList<BookReceiver> bid_bookreceivers = new ArrayList<>(); private ArrayList<BookReceiver> bid_bookreceivers = new ArrayList<>();
private ArrayList <BookReceiver> selectBookReceiver(OrderType t){ private ArrayList<BookReceiver> selectBookReceiver(OrderType t) {
switch (t){ switch (t) {
case ask: case ask:
return ask_bookreceivers; return ask_bookreceivers;
case bid: case bid:
@ -70,29 +68,28 @@ public class Exchange extends Thread {
return null; return null;
} }
public void addBookReceiver(OrderType t, BookReceiver br){ public void addBookReceiver(OrderType t, BookReceiver br) {
ArrayList <BookReceiver> bookreceivers; ArrayList<BookReceiver> bookreceivers;
bookreceivers = selectBookReceiver(t); bookreceivers = selectBookReceiver(t);
bookreceivers.add(br); bookreceivers.add(br);
} }
void updateBookReceivers(OrderType t) { void updateBookReceivers(OrderType t) {
ArrayList <BookReceiver> bookreceivers; ArrayList<BookReceiver> bookreceivers;
bookreceivers = selectBookReceiver(t); bookreceivers = selectBookReceiver(t);
Iterator<BookReceiver> i = bookreceivers.iterator(); Iterator<BookReceiver> i = bookreceivers.iterator();
while (i.hasNext()) { while (i.hasNext()) {
i.next().UpdateOrderBook(); i.next().UpdateOrderBook();
} }
/* try { try {
sleep(50); sleep(0);
} catch (InterruptedException e) { } catch (InterruptedException e) {
System.out.println("I was Interrupted"); System.out.println("I was Interrupted");
} }
*/
}
}
// Here we store the list of quote receivers // Here we store the list of quote receivers
private final TreeSet<QuoteReceiver> qrlist; private final TreeSet<QuoteReceiver> qrlist;
@ -117,7 +114,7 @@ public class Exchange extends Thread {
double theprice = 12.9; double theprice = 12.9;
long orderid = 1; long orderid = 1;
double lastprice = 300.0; double lastprice = 100.0;
long lastsvolume; long lastsvolume;
public TreeSet<Order> bid; public TreeSet<Order> bid;
@ -138,10 +135,9 @@ public class Exchange extends Thread {
available.release(); available.release();
} }
private TreeSet<Order> selectOrderBook(OrderType t) {
private TreeSet<Order> selectOrderBook(OrderType t){ switch (t) {
switch(t){
case bid: case bid:
return this.bid; return this.bid;
case ask: case ask:
@ -153,9 +149,10 @@ public class Exchange extends Thread {
public ArrayList<Order> getOrderBook(OrderType t, int depth) { public ArrayList<Order> getOrderBook(OrderType t, int depth) {
TreeSet <Order> book = selectOrderBook(t); TreeSet<Order> book = selectOrderBook(t);
if (book==null) if (book == null) {
return null; return null;
}
ArrayList<Order> ret = new ArrayList<>(); ArrayList<Order> ret = new ArrayList<>();
Iterator<Order> it = book.iterator(); Iterator<Order> it = book.iterator();
@ -170,8 +167,6 @@ public class Exchange extends Thread {
} }
public void print_current() { public void print_current() {
Order b; Order b;
@ -211,7 +206,7 @@ public class Exchange extends Thread {
public void CancelOrder(Order o) { public void CancelOrder(Order o) {
Lock(); Lock();
TreeSet <Order> book = this.selectOrderBook(o.type); TreeSet<Order> book = this.selectOrderBook(o.type);
book.remove(o); book.remove(o);
this.updateBookReceivers(o.type); this.updateBookReceivers(o.type);
o.account.pending.remove(o); o.account.pending.remove(o);
@ -222,12 +217,13 @@ public class Exchange extends Thread {
/** /**
* Transfer shares from one account to another account * Transfer shares from one account to another account
*
* @param src source account * @param src source account
* @param dst destination account * @param dst destination account
* @param volumen number of shares * @param volumen number of shares
* @param price price * @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; dst.shares += volume;
src.shares -= volume; src.shares -= volume;
dst.money -= price * volume; dst.money -= price * volume;
@ -290,11 +286,9 @@ public class Exchange extends Thread {
volume = b.volume; volume = b.volume;
} }
transferShares(a.account,b.account,volume,price); transferShares(a.account, b.account, volume, price);
// b.account.Buy(a.account, volume, price);
// b.account.Buy(a.account, volume, price);
b.volume -= volume; b.volume -= volume;
a.volume -= volume; a.volume -= volume;
@ -311,6 +305,13 @@ public class Exchange extends Thread {
this.updateBookReceivers(OrderType.bid); this.updateBookReceivers(OrderType.bid);
this.updateBookReceivers(OrderType.ask); this.updateBookReceivers(OrderType.ask);
System.out.print(
"Executed: "
+ q.price
+ " / "
+ q.size
+ "\n"
);
//quoteHistory.add(q); //quoteHistory.add(q);
continue; continue;
@ -333,22 +334,23 @@ public class Exchange extends Thread {
// Add an order to the orderbook // Add an order to the orderbook
private boolean addOrder(Order o) { private boolean addOrder(Order o) {
boolean ret=false; boolean ret = false;
switch (o.type) { switch (o.type) {
case bid: case bid:
System.out.print("Exchange adding bid oder \n"); // System.out.print("Exchange adding bid order \n");
ret = bid.add(o); ret = bid.add(o);
break; break;
case ask: case ask:
System.out.print("Exchange adding ask oder \n"); // System.out.print("Exchange adding ask order \n");
ret = ask.add(o); ret = ask.add(o);
break; break;
} }
if (ret) if (ret) {
this.updateBookReceivers(o.type); this.updateBookReceivers(o.type);
}
return ret; return ret;
} }
@ -356,8 +358,9 @@ public class Exchange extends Thread {
public Order SendOrder(Order o) { public Order SendOrder(Order o) {
boolean rc = InitOrder(o); boolean rc = InitOrder(o);
if (!rc) if (!rc) {
return null; return null;
}
Lock(); Lock();
o.timestamp = System.currentTimeMillis(); o.timestamp = System.currentTimeMillis();

View File

@ -44,11 +44,21 @@ public abstract class Trader {
/** /**
* Construct a Trader object * Construct a Trader object
* @param account Account for this trader * @param account Account for this trader
* @param config Configration for this trader
*/ */
public Trader(Account account, TraderConfig config){ public Trader(Account account, TraderConfig config){
this.account=account; this.account=account;
this.config=config; 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> * @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

@ -8,42 +8,56 @@ import SeSim.TraderConfig;
public class RandomTrader extends Trader { public class RandomTrader extends Trader {
public TraderConfig getTraderConfig(){
return new RandomTraderConfig();
}
private RandomTraderConfig myconfig;
Random rand = new Random(); Random rand = new Random();
// my current order // my current order
//private Order myorder = null; //private Order myorder = null;
public RandomTrader(Account account,TraderConfig config) { public RandomTrader(Account account,TraderConfig config) {
super(account,config); super(account,config);
if (config==null){
config = new RandomTraderConfig();
}
myconfig = (RandomTraderConfig)config;
} }
public void doBuy() { public void doBuy() {
// if (account.money <= 0) {
// return;
// }
double perc = rand.nextDouble() * 1.0; if (account.money <= 0) {
return;
}
double perc = 5.0-rand.nextDouble() * 10.0;
double lp = account.se.getlastprice(); double lp = account.se.getlastprice();
double limit = lp / 100 * perc + lp; double limit = lp / 100 * perc + lp;
long size = (int) (account.money / (limit * 1)); long volume = (int) (account.money / (limit * 1));
buy(size, limit); if (volume ==0 ){
System.out.print("Can't buy shares = 0 my money="+account.money+" shares="+account.shares+"\n");
return;
}
buy(volume, limit);
return; return;
} }
public void doSell() { public void doSell() {
/* if (myorder != null) {
return;
}
if (account.shares <= 0) { if (account.shares <= 0) {
return; return;
} }
*/
double perc = rand.nextDouble() * 1.0;
double perc = 5.0-rand.nextDouble() * 10.0;
double lp = account.se.getlastprice(); double lp = account.se.getlastprice();
double limit = lp - lp / 100 * perc; double limit = lp - lp / 100 * perc;
@ -57,7 +71,7 @@ public class RandomTrader extends Trader {
// System.out.print("RT: Monitoring trades - Pending: "+numpending+"\n"); // System.out.print("RT: Monitoring trades - Pending: "+numpending+"\n");
if (numpending == 0) { if (numpending == 0) {
System.out.print("RT: pending = 0 - return false\n"); // System.out.print("RT: pending = 0 - return false\n");
return false; return false;
} }
@ -66,9 +80,10 @@ public class RandomTrader extends Trader {
// System.out.print("RT: age is: "+age+"\n"); // 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); account.se.CancelOrder(o);
System.out.print("Age reached - canel return false\n"); // System.out.print("Age reached - canel return false\n");
return false; return false;
} }
@ -87,7 +102,11 @@ public class RandomTrader extends Trader {
// What next to do? // 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) { if (action == 1) {
doBuy(); 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);
}
}