Refined getBestPrice function
This commit is contained in:
parent
cadc910f99
commit
f7601aea3a
@ -136,6 +136,21 @@ class TradingEngine implements TradingAPI {
|
|||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
b.cost += price * volume;
|
||||||
|
a.cost += price * volume;
|
||||||
|
|
||||||
|
removeOrderIfExecuted(a);
|
||||||
|
removeOrderIfExecuted(b);
|
||||||
|
}
|
||||||
|
|
||||||
private void removeOrderIfExecuted(Order o) {
|
private void removeOrderIfExecuted(Order o) {
|
||||||
|
|
||||||
if (o.volume != 0) {
|
if (o.volume != 0) {
|
||||||
@ -165,20 +180,24 @@ class TradingEngine implements TradingAPI {
|
|||||||
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
|
||||||
if (!ul_sell.isEmpty() && !ul_buy.isEmpty()) {
|
if (!ul_sell.isEmpty() && !ul_buy.isEmpty()) {
|
||||||
Order a = ul_sell.first();
|
Order a = ul_sell.first();
|
||||||
Order b = ul_buy.first();
|
Order b = ul_buy.first();
|
||||||
Double price = getBestPrice(stock);
|
Double price = getBestPrice();
|
||||||
if (price == null) {
|
|
||||||
break;
|
// if (price == null) {
|
||||||
}
|
// break;
|
||||||
double volume = b.volume >= a.volume ? a.volume : b.volume;
|
// }
|
||||||
finishTrade(b, a, price, volume);
|
// double volume = b.volume >= a.volume ? a.volume : b.volume;
|
||||||
volume_total += volume;
|
// finishTrade(b, a, price, volume);
|
||||||
money_total += price * volume;
|
// volume_total += volume;
|
||||||
this.checkSLOrders(price);
|
// money_total += price * volume;
|
||||||
|
//this.checkSLOrders(price);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
while (!ul_buy.isEmpty() && !ask.isEmpty()) {
|
while (!ul_buy.isEmpty() && !ask.isEmpty()) {
|
||||||
Order a = ask.first();
|
Order a = ask.first();
|
||||||
Order b = ul_buy.first();
|
Order b = ul_buy.first();
|
||||||
@ -201,7 +220,6 @@ class TradingEngine implements TradingAPI {
|
|||||||
this.checkSLOrders(price);
|
this.checkSLOrders(price);
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//
|
//
|
||||||
// Match limited orders against limited orders
|
// Match limited orders against limited orders
|
||||||
//
|
//
|
||||||
@ -231,7 +249,7 @@ class TradingEngine implements TradingAPI {
|
|||||||
type = Order.Type.SELL;
|
type = Order.Type.SELL;
|
||||||
} else {
|
} else {
|
||||||
price = a.limit;
|
price = a.limit;
|
||||||
type=Order.Type.BUY;
|
type = Order.Type.BUY;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The volume is calculated by best fit
|
// The volume is calculated by best fit
|
||||||
@ -263,7 +281,7 @@ class TradingEngine implements TradingAPI {
|
|||||||
q.price = price;
|
q.price = price;
|
||||||
q.volume = volume;
|
q.volume = volume;
|
||||||
q.time = outer.world.currentTimeMillis();
|
q.time = outer.world.currentTimeMillis();
|
||||||
q.type=type;
|
q.type = type;
|
||||||
addQuoteToHistory(q);
|
addQuoteToHistory(q);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -296,125 +314,80 @@ class TradingEngine implements TradingAPI {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
public Double getBestPrice() {
|
||||||
public Double
|
|
||||||
getBestPrice() {
|
|
||||||
SortedSet<Order> bid
|
|
||||||
= order_books
|
|
||||||
.get(Order.Type.BUYLIMIT
|
|
||||||
);
|
|
||||||
SortedSet<Order> ask
|
|
||||||
= order_books
|
|
||||||
.get(Order.Type.SELLLIMIT
|
|
||||||
);
|
|
||||||
|
|
||||||
Quote lq
|
Order b;
|
||||||
= null; //this.getLastQuoete();
|
Order a;
|
||||||
Order b
|
|
||||||
= null;
|
|
||||||
Order a
|
|
||||||
= null;
|
|
||||||
|
|
||||||
if (!bid
|
// Get first limited orders from bid and ask,
|
||||||
.isEmpty()) {
|
// assign null if no order is present
|
||||||
b
|
b = !bidbook.isEmpty() ? bidbook.first() : null;
|
||||||
= bid
|
a = !askbook.isEmpty() ? askbook.first() : null;
|
||||||
.first();
|
|
||||||
|
|
||||||
}
|
// If there is neither bid nor ask and also no last quote
|
||||||
if (!ask
|
// we can't return a price
|
||||||
.isEmpty()) {
|
if (last_quote == null && b == null && a == null) {
|
||||||
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;
|
return null;
|
||||||
|
|
||||||
}
|
}
|
||||||
// there is bid and ask
|
|
||||||
if (a
|
|
||||||
!= null && b
|
|
||||||
!= null) {
|
|
||||||
Quote q
|
|
||||||
= new Quote(-1);
|
|
||||||
System.out
|
|
||||||
.printf("aaaaa bbbbb %f %f \n", a.limit,
|
|
||||||
b.limit
|
|
||||||
);
|
|
||||||
// if there is no last quote calculate from bid and ask
|
|
||||||
//if (lq == null) {
|
|
||||||
|
|
||||||
double rc
|
// Both limited bid and ask are present
|
||||||
= (bid
|
if (a != null && b != null) {
|
||||||
.first().limit
|
|
||||||
+ ask
|
|
||||||
.first().limit) / 2.0;
|
|
||||||
System.out
|
|
||||||
.printf("RCRC2.0: %f\n", rc
|
|
||||||
);
|
|
||||||
|
|
||||||
return rc;
|
// if there is no last quote, we calculate the prpice
|
||||||
// }
|
// from bid and ask by simply averaging the limits
|
||||||
|
if (last_quote == null) {
|
||||||
|
return (bidbook.first().limit + askbook.first().limit) / 2.0;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
// Last quote is below bid, so the best price is the
|
||||||
if (lq.price < b.limit) {
|
// current bid
|
||||||
|
if (last_quote.price < b.limit) {
|
||||||
return b.limit;
|
return b.limit;
|
||||||
}
|
}
|
||||||
if (lq.price > a.limit) {
|
|
||||||
|
// Last price is grater ask, so return the current ask
|
||||||
|
if (last_quote.price > a.limit) {
|
||||||
return a.limit;
|
return a.limit;
|
||||||
}
|
}
|
||||||
return lq.price;
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
if (a
|
|
||||||
!= null) {
|
|
||||||
Quote q
|
|
||||||
= new Quote(-1);
|
|
||||||
|
|
||||||
if (lq
|
// Last price is somewhere between bid and ask,
|
||||||
== null) {
|
// we return the last price
|
||||||
|
return last_quote.price;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// There is no limited ask, but limited bid
|
||||||
|
if (a != null) {
|
||||||
|
|
||||||
|
// retrun last quote if present or lower than ask,
|
||||||
|
// otherwise return the current ask
|
||||||
|
if (last_quote == null) {
|
||||||
|
return a.limit;
|
||||||
|
}
|
||||||
|
if (last_quote.price > a.limit) {
|
||||||
return a.limit;
|
return a.limit;
|
||||||
|
|
||||||
}
|
}
|
||||||
if (lq.price
|
return last_quote.price;
|
||||||
> a.limit) {
|
|
||||||
return a.limit;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
return lq.price;
|
|
||||||
|
|
||||||
}
|
// No bid, but ask is present
|
||||||
if (b
|
// Same as a !=null like before but reversed
|
||||||
!= null) {
|
if (b != null) {
|
||||||
Quote q
|
if (last_quote == null) {
|
||||||
= new Quote(-1);
|
|
||||||
|
|
||||||
if (lq
|
|
||||||
== null) {
|
|
||||||
return b.limit;
|
return b.limit;
|
||||||
|
|
||||||
}
|
}
|
||||||
if (lq.price
|
if (last_quote.price
|
||||||
< b.limit) {
|
< b.limit) {
|
||||||
return b.limit;
|
return b.limit;
|
||||||
|
|
||||||
}
|
}
|
||||||
return lq.price;
|
return last_quote.price;
|
||||||
|
|
||||||
}
|
}
|
||||||
if (lq
|
|
||||||
== null) {
|
|
||||||
return null;
|
|
||||||
|
|
||||||
}
|
// Both bid and ask are not present, return last quote.
|
||||||
return lq.price;
|
// The case that last_quote is null can never happen here.
|
||||||
|
return last_quote.price;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user