unilimited buy order implemented
This commit is contained in:
parent
88331b262c
commit
65ca4d9e21
@ -449,12 +449,11 @@ public class Exchange {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Quote getCurrentPrice() {
|
public Double getBestPrice() {
|
||||||
|
|
||||||
SortedSet<Order> bid = order_books.get(OrderType.BUYLIMIT);
|
SortedSet<Order> bid = order_books.get(OrderType.BUYLIMIT);
|
||||||
SortedSet<Order> ask = order_books.get(OrderType.SELLLIMIT);
|
SortedSet<Order> ask = order_books.get(OrderType.SELLLIMIT);
|
||||||
|
|
||||||
tradelock.lock();
|
|
||||||
Quote lq = this.getLastQuoete();
|
Quote lq = this.getLastQuoete();
|
||||||
Order b = null, a = null;
|
Order b = null, a = null;
|
||||||
if (!bid.isEmpty()) {
|
if (!bid.isEmpty()) {
|
||||||
@ -463,18 +462,99 @@ public class Exchange {
|
|||||||
if (!ask.isEmpty()) {
|
if (!ask.isEmpty()) {
|
||||||
a = ask.first();
|
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) {
|
if (lq == null && b == null && a == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// there is bid and ask
|
||||||
if (a != null && b != null) {
|
if (a != null && b != null) {
|
||||||
Quote q = new Quote();
|
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) {
|
if (lq == null) {
|
||||||
q.price = (bid.first().limit + ask.first().limit) / 2.0;
|
q.price = (bid.first().limit + ask.first().limit) / 2.0;
|
||||||
return q;
|
return q;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lq.price < b.limit) {
|
if (lq.price < b.limit) {
|
||||||
q.price = b.limit;
|
q.price = b.limit;
|
||||||
return q;
|
return q;
|
||||||
@ -774,13 +854,38 @@ public class Exchange {
|
|||||||
double volume_total = 0;
|
double volume_total = 0;
|
||||||
double money_total = 0;
|
double money_total = 0;
|
||||||
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
|
||||||
// Match unlimited sell orders against unlimited buy orders
|
// Match unlimited sell orders against unlimited buy orders
|
||||||
while (!ul_sell.isEmpty() && !ul_buy.isEmpty()) {
|
if (!ul_sell.isEmpty() && !ul_buy.isEmpty()) {
|
||||||
System.out.printf("Cannot match two unlimited orders!\n");
|
Order a = ul_sell.first();
|
||||||
System.exit(0);
|
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.volume = volume_total;
|
||||||
q.time = timer.currentTimeMillis();
|
q.time = timer.currentTimeMillis();
|
||||||
|
|
||||||
// System.out.print("There was a trade:"+q.price+"\n");
|
|
||||||
this.quoteHistory.add(q);
|
this.quoteHistory.add(q);
|
||||||
this.updateOHLCData(q);
|
this.updateOHLCData(q);
|
||||||
|
|
||||||
|
@ -91,10 +91,11 @@
|
|||||||
<Component class="javax.swing.JComboBox" name="typeComboBox">
|
<Component class="javax.swing.JComboBox" name="typeComboBox">
|
||||||
<Properties>
|
<Properties>
|
||||||
<Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor">
|
<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="0" value="Buy Limit"/>
|
||||||
<StringItem index="1" value="Sell Limit"/>
|
<StringItem index="1" value="Sell Limit"/>
|
||||||
<StringItem index="2" value="Sell"/>
|
<StringItem index="2" value="Sell"/>
|
||||||
|
<StringItem index="3" value="Buy"/>
|
||||||
</StringArray>
|
</StringArray>
|
||||||
</Property>
|
</Property>
|
||||||
</Properties>
|
</Properties>
|
||||||
|
@ -53,8 +53,8 @@ public class CreateOrderDialog extends javax.swing.JDialog {
|
|||||||
public CreateOrderDialog(java.awt.Frame parent, boolean modal, Account account, OrderType type) {
|
public CreateOrderDialog(java.awt.Frame parent, boolean modal, Account account, OrderType type) {
|
||||||
this(parent, modal);
|
this(parent, modal);
|
||||||
this.account = account;
|
this.account = account;
|
||||||
typeComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[]{"Buy Lim", "Sell Lim", "Sell"}));
|
typeComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[]{"Buy Lim", "Sell Lim", "Buy", "Sell"}));
|
||||||
typeList = new OrderType[]{OrderType.BUYLIMIT, OrderType.SELLLIMIT, OrderType.SELL};
|
typeList = new OrderType[]{OrderType.BUYLIMIT, OrderType.SELLLIMIT, OrderType.BUY,OrderType.SELL};
|
||||||
for (int i = 0; i < typeList.length; i++) {
|
for (int i = 0; i < typeList.length; i++) {
|
||||||
if (typeList[i] == type) {
|
if (typeList[i] == type) {
|
||||||
this.typeComboBox.setSelectedIndex(i);
|
this.typeComboBox.setSelectedIndex(i);
|
||||||
@ -72,7 +72,7 @@ public class CreateOrderDialog extends javax.swing.JDialog {
|
|||||||
|
|
||||||
public void initDialog() {
|
public void initDialog() {
|
||||||
OrderType t = getOrderType();
|
OrderType t = getOrderType();
|
||||||
Quote q = Globals.se.getCurrentPrice();
|
Quote q = Globals.se.getBestPrice_0();
|
||||||
Double price = q == null ? 0.0 : q.price;
|
Double price = q == null ? 0.0 : q.price;
|
||||||
|
|
||||||
if (t == OrderType.BUYLIMIT) {
|
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));
|
limitSpinner.setModel(new javax.swing.SpinnerNumberModel(0.0d, 0.0d, null, 0.1d));
|
||||||
|
|
||||||
|
@ -283,7 +283,7 @@ public class RandomTraderA extends AutoTraderBase {
|
|||||||
// how much money we ant to invest?
|
// how much money we ant to invest?
|
||||||
double money = getRandomAmmount(ad.getMoney(), buy_volume);
|
double money = getRandomAmmount(ad.getMoney(), buy_volume);
|
||||||
|
|
||||||
Quote q = se.getCurrentPrice();
|
Quote q = se.getBestPrice_0();
|
||||||
//q=se.getLastQuoete();
|
//q=se.getLastQuoete();
|
||||||
double lp = q == null ? getStart() : q.price;
|
double lp = q == null ? getStart() : q.price;
|
||||||
|
|
||||||
@ -323,7 +323,7 @@ public class RandomTraderA extends AutoTraderBase {
|
|||||||
|
|
||||||
|
|
||||||
// double lp = 100.0; //se.getBestLimit(type);
|
// double lp = 100.0; //se.getBestLimit(type);
|
||||||
Quote q = se.getCurrentPrice();
|
Quote q = se.getBestPrice_0();
|
||||||
// q=se.getLastQuoete();
|
// q=se.getLastQuoete();
|
||||||
double lp = q == null ? getStart() : q.price;
|
double lp = q == null ? getStart() : q.price;
|
||||||
|
|
||||||
|
@ -283,7 +283,7 @@ public class RandomTraderB extends AutoTraderBase {
|
|||||||
// how much money we ant to invest?
|
// how much money we ant to invest?
|
||||||
double money = getRandomAmmount(ad.getMoney(), buy_volume);
|
double money = getRandomAmmount(ad.getMoney(), buy_volume);
|
||||||
|
|
||||||
Quote q = se.getCurrentPrice();
|
Quote q = se.getBestPrice_0();
|
||||||
//q=se.getLastQuoete();
|
//q=se.getLastQuoete();
|
||||||
double lp = q == null ? getStart() : q.price;
|
double lp = q == null ? getStart() : q.price;
|
||||||
|
|
||||||
@ -323,7 +323,7 @@ public class RandomTraderB extends AutoTraderBase {
|
|||||||
|
|
||||||
|
|
||||||
// double lp = 100.0; //se.getBestLimit(type);
|
// double lp = 100.0; //se.getBestLimit(type);
|
||||||
Quote q = se.getCurrentPrice();
|
Quote q = se.getBestPrice_0();
|
||||||
// q=se.getLastQuoete();
|
// q=se.getLastQuoete();
|
||||||
double lp = q == null ? getStart() : q.price;
|
double lp = q == null ? getStart() : q.price;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user