Some changes ...

This commit is contained in:
7u83
2016-12-31 21:18:28 +01:00
parent 4f30bcbc30
commit 456be84413
10 changed files with 201 additions and 38 deletions

View File

@ -42,6 +42,22 @@ public abstract class AutoTrader extends Trader implements Runnable {
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();
}
}

View File

@ -35,8 +35,9 @@ public class AutoTraderLIst {
for (int i = 0; i < n; i++) {
AutoTrader trader = config.createTrader(se, shares, money);
TraderRunner tr = new TraderRunner(trader);
tr.start();
// TraderRunner tr = new TraderRunner(trader);
trader.start();
}
}

View File

@ -27,14 +27,18 @@ public class Exchange extends Thread {
}
public SortedSet <Quote> getQuoteHistory(int seconds){
long ct = System.currentTimeMillis() - seconds * 1000;
public TreeSet <Quote> getQuoteHistory(int seconds){
Quote last = quoteHistory.last();
long ct = last.time - seconds * 1000;
Quote e = new Quote();
e.time=ct;
SortedSet<Quote> l = quoteHistory.tailSet(e);
return l;
return (TreeSet)l;
}
// Class to describe an executed order
@ -102,7 +106,7 @@ public class Exchange extends Thread {
}
// send updated quotes to all quote receivers
void UpdateQuoteReceivers(Quote q) {
private void updateQuoteReceivers(Quote q) {
Iterator<QuoteReceiver> i = qrlist.iterator();
while (i.hasNext()) {
i.next().UpdateQuote(q);
@ -197,13 +201,13 @@ public class Exchange extends Thread {
}
public void TransferMoney(Account src, Account dst, double money) {
public void transferMoney(Account src, Account dst, double money) {
src.money -= money;
dst.money += money;
}
public void CancelOrder(Order o) {
public void cancelOrder(Order o) {
Lock();
TreeSet<Order> book = this.selectOrderBook(o.type);
book.remove(o);
@ -229,6 +233,8 @@ public class Exchange extends Thread {
src.money += price * volume;
}
long nextQuoteId=0;
public void OrderMatching() {
while (true) {
@ -300,11 +306,14 @@ public class Exchange extends Thread {
q.price = price;
q.time = System.currentTimeMillis();
q.ask=a.limit;
q.bid=b.limit;
q.id = nextQuoteId++;
this.UpdateQuoteReceivers(q);
this.updateQuoteReceivers(q);
this.updateBookReceivers(OrderType.bid);
this.updateBookReceivers(OrderType.ask);

51
src/SeSim/Locker.java Normal file
View 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();
}
}

View File

@ -30,7 +30,7 @@ package SeSim;
* @author 7u83 <7u83@mail.ru>
*/
public class Quote implements Comparable {
public double bid;
public double bid_volume;
public double ask;
@ -39,6 +39,8 @@ public class Quote implements Comparable {
public double price;
public long volume;
public long time;
Locker lock = new Locker();
public void print() {
System.out.print("Quote ("
@ -51,10 +53,23 @@ public class Quote implements Comparable {
);
}
public long id;
@Override
public int compareTo(Object o) {
int ret;
Quote q = (Quote)o;
return (int)(this.time-q.time);
ret = (int)(this.time-q.time);
if (ret !=0)
return ret;
return (int)(this.id-q.id);
}
/* Quote (){
lock.lock();
id=nextid++;
lock.unlock();
}*/
}

View File

@ -1,18 +0,0 @@
package SeSim;
public class TraderRunner extends Thread {
protected long sleeptime = 1000;
AutoTrader trader;
public TraderRunner(AutoTrader trader){
this.trader=trader;
}
public void run() {
trader.run();
}
}