Better RandomTrader + Chart (still just a demo chart)
This commit is contained in:
@ -53,6 +53,19 @@ final public class Account {
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
|
47
src/SeSim/AutoTrader.java
Normal file
47
src/SeSim/AutoTrader.java
Normal file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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) {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -29,12 +29,12 @@ package SeSim;
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class AutoTraders {
|
||||
public class AutoTraderLIst {
|
||||
|
||||
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);
|
||||
AutoTrader trader = config.createTrader(se, shares, money);
|
||||
TraderRunner tr = new TraderRunner(trader);
|
||||
tr.start();
|
||||
}
|
@ -23,7 +23,7 @@ public class Exchange extends Thread {
|
||||
public Exchange() {
|
||||
this.ask = new TreeSet<>();
|
||||
this.bid = new TreeSet<>();
|
||||
this.qrlist = new TreeSet<>();
|
||||
this.qrlist = new ArrayList<>();
|
||||
this.quoteHistory = new ArrayList<>();
|
||||
}
|
||||
|
||||
@ -31,13 +31,27 @@ public class Exchange extends Thread {
|
||||
public class Quote {
|
||||
|
||||
double bid;
|
||||
double bid_size;
|
||||
double bid_volume;
|
||||
double ask;
|
||||
double ask_size;
|
||||
double ask_volume;
|
||||
|
||||
public double price;
|
||||
public long size;
|
||||
public long volume;
|
||||
public long time;
|
||||
|
||||
public void print(){
|
||||
System.out.print("Quite ("
|
||||
+time
|
||||
+") :"
|
||||
+price
|
||||
+" / "
|
||||
+volume
|
||||
+"\n"
|
||||
);
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// QuoteReceiver has to be implemented by objects that wants
|
||||
@ -92,7 +106,7 @@ public class Exchange extends Thread {
|
||||
}
|
||||
|
||||
// Here we store the list of quote receivers
|
||||
private final TreeSet<QuoteReceiver> qrlist;
|
||||
private final ArrayList<QuoteReceiver> qrlist;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -297,7 +311,7 @@ public class Exchange extends Thread {
|
||||
|
||||
Quote q = new Quote();
|
||||
|
||||
q.size = volume;
|
||||
q.volume = volume;
|
||||
q.price = price;
|
||||
q.time = System.currentTimeMillis();
|
||||
|
||||
@ -305,13 +319,14 @@ public class Exchange extends Thread {
|
||||
this.updateBookReceivers(OrderType.bid);
|
||||
this.updateBookReceivers(OrderType.ask);
|
||||
|
||||
System.out.print(
|
||||
/* System.out.print(
|
||||
"Executed: "
|
||||
+ q.price
|
||||
+ " / "
|
||||
+ q.size
|
||||
+ q.volume
|
||||
+ "\n"
|
||||
);
|
||||
*/
|
||||
|
||||
//quoteHistory.add(q);
|
||||
continue;
|
||||
|
@ -1,87 +0,0 @@
|
||||
package SeSim;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class MTrader extends Trader {
|
||||
|
||||
Exchange ex;
|
||||
Random rand;
|
||||
|
||||
public MTrader(Account account,TraderConfig config) {
|
||||
super(account,config);
|
||||
}
|
||||
|
||||
/* public MTrader(Exchange ex1, long shares, double money) {
|
||||
account.money = money;
|
||||
account.shares = shares;
|
||||
this.ex = ex;
|
||||
rand = new Random();
|
||||
}
|
||||
*/
|
||||
public void DoBuy() {
|
||||
// System.out.println("AAA");
|
||||
|
||||
if (account.orderpending) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (account.money <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
double perc = rand.nextDouble() * 1.0;
|
||||
double lp = ex.lastprice;
|
||||
double limit = lp / 100 * perc + lp;
|
||||
|
||||
// System.out.println("HW");
|
||||
long size = (int) (account.money / limit);
|
||||
|
||||
account.buy(size, limit);
|
||||
|
||||
}
|
||||
|
||||
public void DoSell() {
|
||||
// System.out.println("SoSell");
|
||||
|
||||
if (account.orderpending) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (account.shares <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
double perc = rand.nextDouble() * 1.0;
|
||||
double lp = ex.lastprice;
|
||||
double limit = lp - lp / 100 * perc;
|
||||
|
||||
long size = (int) (account.shares);
|
||||
|
||||
account.sell(size, limit);
|
||||
return;
|
||||
}
|
||||
|
||||
public void trade() {
|
||||
|
||||
// What to do?
|
||||
int action = rand.nextInt(3);
|
||||
// System.out.print("Action");
|
||||
// System.out.println(action);
|
||||
if (action == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (action == 1) {
|
||||
DoBuy();
|
||||
return;
|
||||
}
|
||||
|
||||
if (action == 2) {
|
||||
DoSell();
|
||||
return;
|
||||
}
|
||||
|
||||
//System.out.printf("MyPrice: %.2f\n",theprice);
|
||||
}
|
||||
|
||||
}
|
@ -25,11 +25,13 @@
|
||||
*/
|
||||
package SeSim;
|
||||
|
||||
import static java.lang.Thread.sleep;
|
||||
|
||||
public abstract class Trader {
|
||||
|
||||
public String name = null;
|
||||
|
||||
public abstract void trade();
|
||||
|
||||
public Account account;
|
||||
public TraderConfig config;
|
||||
|
||||
@ -59,6 +61,9 @@ public abstract class Trader {
|
||||
public Trader(Account account){
|
||||
this(account,null);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
@ -32,5 +32,5 @@ package SeSim;
|
||||
public abstract class TraderConfig{
|
||||
String name;
|
||||
|
||||
public abstract Trader createTrader(Exchange se, long shares, double money);
|
||||
public abstract AutoTrader createTrader(Exchange se, long shares, double money);
|
||||
}
|
||||
|
@ -4,21 +4,15 @@ public class TraderRunner extends Thread {
|
||||
|
||||
protected long sleeptime = 1000;
|
||||
|
||||
Trader trader;
|
||||
AutoTrader trader;
|
||||
|
||||
public TraderRunner(Trader trader){
|
||||
public TraderRunner(AutoTrader trader){
|
||||
this.trader=trader;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
while (true) {
|
||||
try {
|
||||
sleep(sleeptime);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("Interrupted");
|
||||
return;
|
||||
}
|
||||
trader.trade();
|
||||
}
|
||||
trader.run();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user