unilimited buy order implemented

This commit is contained in:
7u83 2017-02-25 06:24:26 +01:00
parent 88331b262c
commit 65ca4d9e21
5 changed files with 122 additions and 17 deletions

View File

@ -449,12 +449,11 @@ public class Exchange {
}
public Quote getCurrentPrice() {
public Double getBestPrice() {
SortedSet<Order> bid = order_books.get(OrderType.BUYLIMIT);
SortedSet<Order> ask = order_books.get(OrderType.SELLLIMIT);
tradelock.lock();
Quote lq = this.getLastQuoete();
Order b = null, a = null;
if (!bid.isEmpty()) {
@ -463,18 +462,99 @@ public class Exchange {
if (!ask.isEmpty()) {
a = ask.first();
}
tradelock.unlock();
// If there is neither bid nor ask and no last quote
// we can't return a quote
if (lq == null && b == null && a == null) {
return null;
}
// there is bid and ask
if (a != null && b != null) {
Quote q = new Quote();
// if there is no last quote calculate from bid and ask
if (lq == null) {
return (bid.first().limit + ask.first().limit) / 2.0;
}
if (lq.price < b.limit) {
return b.limit;
}
if (lq.price > a.limit) {
return a.limit;
}
return lq.price;
}
if (a != null) {
Quote q = new Quote();
if (lq == null) {
return a.limit;
}
if (lq.price > a.limit) {
return a.limit;
}
return lq.price;
}
if (b != null) {
Quote q = new Quote();
if (lq == null) {
return b.limit;
}
if (lq.price < b.limit) {
return b.limit;
}
return lq.price;
}
if (lq == null) {
return null;
}
return lq.price;
}
public Quote getBestPrice_0() {
SortedSet<Order> bid = order_books.get(OrderType.BUYLIMIT);
SortedSet<Order> ask = order_books.get(OrderType.SELLLIMIT);
Quote lq = this.getLastQuoete();
Order b = null, a = null;
if (!bid.isEmpty()) {
b = bid.first();
}
if (!ask.isEmpty()) {
a = ask.first();
}
// If there is neither bid nor ask and no last quote
// we can't return a quote
if (lq == null && b == null && a == null) {
return null;
}
// there is bid and ask
if (a != null && b != null) {
Quote q = new Quote();
// if there is no last quote calculate from bid and ask
if (lq == null) {
q.price = (bid.first().limit + ask.first().limit) / 2.0;
return q;
}
if (lq.price < b.limit) {
q.price = b.limit;
return q;
@ -774,13 +854,38 @@ public class Exchange {
double volume_total = 0;
double money_total = 0;
while (true) {
// 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);
if (!ul_sell.isEmpty() && !ul_buy.isEmpty()) {
Order a = ul_sell.first();
Order b = ul_buy.first();
Double price = getBestPrice();
if (price == null) {
break;
}
double volume = b.volume >= a.volume ? a.volume : b.volume;
finishTrade(b, a, price, volume);
volume_total += volume;
money_total += price * volume;
this.checkSLOrders(price);
//System.out.printf("Cannot match two unlimited orders!\n");
//System.exit(0);
}
while (!ul_buy.isEmpty() && !ask.isEmpty()) {
Order a = ask.first();
Order b = ul_buy.first();
double price = a.limit;
double volume = b.volume >= a.volume ? a.volume : b.volume;
finishTrade(b, a, price, volume);
volume_total += volume;
money_total += price * volume;
this.checkSLOrders(price);
}
@ -830,7 +935,6 @@ public class Exchange {
q.volume = volume_total;
q.time = timer.currentTimeMillis();
// System.out.print("There was a trade:"+q.price+"\n");
this.quoteHistory.add(q);
this.updateOHLCData(q);

View File

@ -91,10 +91,11 @@
<Component class="javax.swing.JComboBox" name="typeComboBox">
<Properties>
<Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor">
<StringArray count="3">
<StringArray count="4">
<StringItem index="0" value="Buy Limit"/>
<StringItem index="1" value="Sell Limit"/>
<StringItem index="2" value="Sell"/>
<StringItem index="3" value="Buy"/>
</StringArray>
</Property>
</Properties>

View File

@ -53,8 +53,8 @@ public class CreateOrderDialog extends javax.swing.JDialog {
public CreateOrderDialog(java.awt.Frame parent, boolean modal, Account account, OrderType type) {
this(parent, modal);
this.account = account;
typeComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[]{"Buy Lim", "Sell Lim", "Sell"}));
typeList = new OrderType[]{OrderType.BUYLIMIT, OrderType.SELLLIMIT, OrderType.SELL};
typeComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[]{"Buy Lim", "Sell Lim", "Buy", "Sell"}));
typeList = new OrderType[]{OrderType.BUYLIMIT, OrderType.SELLLIMIT, OrderType.BUY,OrderType.SELL};
for (int i = 0; i < typeList.length; i++) {
if (typeList[i] == type) {
this.typeComboBox.setSelectedIndex(i);
@ -72,7 +72,7 @@ public class CreateOrderDialog extends javax.swing.JDialog {
public void initDialog() {
OrderType t = getOrderType();
Quote q = Globals.se.getCurrentPrice();
Quote q = Globals.se.getBestPrice_0();
Double price = q == null ? 0.0 : q.price;
if (t == OrderType.BUYLIMIT) {
@ -123,7 +123,7 @@ public class CreateOrderDialog extends javax.swing.JDialog {
}
});
typeComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Buy Limit", "Sell Limit", "Sell" }));
typeComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Buy Limit", "Sell Limit", "Sell", "Buy" }));
limitSpinner.setModel(new javax.swing.SpinnerNumberModel(0.0d, 0.0d, null, 0.1d));

View File

@ -283,7 +283,7 @@ public class RandomTraderA extends AutoTraderBase {
// how much money we ant to invest?
double money = getRandomAmmount(ad.getMoney(), buy_volume);
Quote q = se.getCurrentPrice();
Quote q = se.getBestPrice_0();
//q=se.getLastQuoete();
double lp = q == null ? getStart() : q.price;
@ -323,7 +323,7 @@ public class RandomTraderA extends AutoTraderBase {
// double lp = 100.0; //se.getBestLimit(type);
Quote q = se.getCurrentPrice();
Quote q = se.getBestPrice_0();
// q=se.getLastQuoete();
double lp = q == null ? getStart() : q.price;

View File

@ -283,7 +283,7 @@ public class RandomTraderB extends AutoTraderBase {
// how much money we ant to invest?
double money = getRandomAmmount(ad.getMoney(), buy_volume);
Quote q = se.getCurrentPrice();
Quote q = se.getBestPrice_0();
//q=se.getLastQuoete();
double lp = q == null ? getStart() : q.price;
@ -323,7 +323,7 @@ public class RandomTraderB extends AutoTraderBase {
// double lp = 100.0; //se.getBestLimit(type);
Quote q = se.getCurrentPrice();
Quote q = se.getBestPrice_0();
// q=se.getLastQuoete();
double lp = q == null ? getStart() : q.price;