prepared for stop loss orders

This commit is contained in:
7u83 2017-02-20 23:40:34 +01:00
parent 39d30d2d4d
commit 396279b8e6

View File

@ -77,7 +77,6 @@ public class Exchange {
return data; return data;
} }
Iterator<Quote> it = quoteHistory.iterator(); Iterator<Quote> it = quoteHistory.iterator();
while (it.hasNext()) { while (it.hasNext()) {
Quote q = it.next(); Quote q = it.next();
@ -88,15 +87,13 @@ public class Exchange {
return data; return data;
} }
public void injectMoney() {
public void injectMoney(){ accounts.forEach(new BiConsumer() {
accounts.forEach(new BiConsumer(){
@Override @Override
public void accept(Object t, Object u) { public void accept(Object t, Object u) {
Account a = (Account)u; Account a = (Account) u;
a.money+=2000.0; a.money += 20000.0;
} }
@ -104,13 +101,23 @@ public class Exchange {
} }
public void pointZero() {
accounts.forEach(new BiConsumer() {
@Override
public void accept(Object t, Object u) {
Account a = (Account) u;
a.money = 20000.0;
}
});
}
public OHLCData getOHLCdata(Integer timeFrame) { public OHLCData getOHLCdata(Integer timeFrame) {
OHLCData data; //=new OHLCData(timeFrame); OHLCData data; //=new OHLCData(timeFrame);
data = ohlc_data.get(timeFrame); data = ohlc_data.get(timeFrame);
if (data == null){ if (data == null) {
this.tradelock.lock(); this.tradelock.lock();
data = this.buildOHLCData(timeFrame); data = this.buildOHLCData(timeFrame);
@ -119,7 +126,7 @@ public class Exchange {
} }
return data; return data;
/* try { /* try {
data = ohlc_data.get(timeFrame); data = ohlc_data.get(timeFrame);
} catch (Exception e) { } catch (Exception e) {
data = null; data = null;
@ -127,7 +134,7 @@ public class Exchange {
if (data == null) { if (data == null) {
data = buildOHLCData(timeFrame); data = buildOHLCData(timeFrame);
} }
*/ */
} }
@ -286,8 +293,8 @@ public class Exchange {
private double limit; private double limit;
private double volume; private double volume;
private final double initial_volume; private final double initial_volume;
private long id; private final long id;
long created; private final long created;
private final Account account; private final Account account;
Order(Account account, OrderType type, double volume, double limit) { Order(Account account, OrderType type, double volume, double limit) {
@ -737,6 +744,22 @@ public class Exchange {
} }
} }
public void executeUnlimitedOrders() {
}
private void finishTrade(Order b, Order a, double price, double volume) {
// Transfer money and shares
transferMoneyAndShares(b.account, a.account, volume * price, -volume);
// Update volume
b.volume -= volume;
a.volume -= volume;
removeOrderIfExecuted(a);
removeOrderIfExecuted(b);
}
/** /**
* *
*/ */
@ -751,12 +774,26 @@ public class Exchange {
double volume_total = 0; double volume_total = 0;
double money_total = 0; double money_total = 0;
while (!bid.isEmpty() && !ask.isEmpty()) { // Match unlimited sell orders against unlimited buy orders
while (!ul_sell.isEmpty() && !ul_buy.isEmpty()) {
System.out.printf("Cannot match two unlimited orders!\n");
System.exit(0);
}
// Match unlimited sell orders against limited buy orders
while (!ul_sell.isEmpty() && !bid.isEmpty()) {
Order b = bid.first();
Order a = ul_sell.first();
double price = b.limit;
double volume = b.volume >= a.volume ? a.volume : b.volume;
}
while (!bid.isEmpty() && !ask.isEmpty()) {
Order b = bid.first(); Order b = bid.first();
Order a = ask.first(); Order a = ask.first();
//System.out.printf("In %f (%f) < %f (%f)\n",b.limit,b.volume,a.limit,a.volume);
if (b.limit < a.limit) { if (b.limit < a.limit) {
break; break;
} }
@ -765,25 +802,13 @@ public class Exchange {
double price = b.id < a.id ? b.limit : a.limit; double price = b.id < a.id ? b.limit : a.limit;
double volume = b.volume >= a.volume ? a.volume : b.volume; double volume = b.volume >= a.volume ? a.volume : b.volume;
//System.out.printf("Price %f Vol %f\n", price,volume); finishTrade(b, a, price, volume);
// Transfer money and shares
transferMoneyAndShares(b.account, a.account, volume * price, -volume);
// Update volume
b.volume -= volume;
a.volume -= volume;
// a.account.update(a);
// b.account.update(b);
//System.out.printf("In %f (%f) < %f (%f)\n",b.limit,b.volume,a.limit,a.volume);
volume_total += volume; volume_total += volume;
money_total += price * volume; money_total += price * volume;
num_trades++; num_trades++;
removeOrderIfExecuted(a);
removeOrderIfExecuted(b);
this.checkSLOrders(price); this.checkSLOrders(price);
} }
@ -831,7 +856,6 @@ public class Exchange {
} }
//System.out.printf("Creating Order width Volume %f %f \n",o.volume,o.limit); //System.out.printf("Creating Order width Volume %f %f \n",o.volume,o.limit);
tradelock.lock(); tradelock.lock();
num_orders++; num_orders++;