Refined getBestPrice function

This commit is contained in:
7u83 2018-12-29 10:35:47 +01:00
parent cadc910f99
commit f7601aea3a
1 changed files with 99 additions and 126 deletions

View File

@ -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,134 +314,89 @@ 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;
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(-1);
if (lq // Last quote is below bid, so the best price is the
== null) { // current bid
return a.limit; if (last_quote.price < b.limit) {
}
if (lq.price
> a.limit) {
return a.limit;
}
return lq.price;
}
if (b
!= null) {
Quote q
= new Quote(-1);
if (lq
== null) {
return b.limit; return b.limit;
}
// Last price is grater ask, so return the current ask
if (last_quote.price > a.limit) {
return a.limit;
}
// Last price is somewhere between bid and ask,
// 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;
} }
if (lq.price return last_quote.price;
}
// No bid, but ask is present
// Same as a !=null like before but reversed
if (b != null) {
if (last_quote == null) {
return b.limit;
}
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;
} }
@Override @Override
public Order public Order
createOrder(Account account, createOrder(Account account,
Order.Type type, Order.Type type,
double volume, double volume,
double limit double limit
) { ) {
Order o; Order o;
@ -472,7 +445,7 @@ class TradingEngine implements TradingAPI {
// reduce the available money // reduce the available money
account.assets_avail account.assets_avail
.put(currency, .put(currency,
avail avail
- v - v
* l * l
); );
@ -503,7 +476,7 @@ class TradingEngine implements TradingAPI {
// All available monney is assigned to this unlimited order // All available monney is assigned to this unlimited order
account.assets_avail account.assets_avail
.put(currency, .put(currency,
0.0); 0.0);
// we "mis"use order_limit to memorize occupied ammount \ // we "mis"use order_limit to memorize occupied ammount \
// of currency // of currency
order_limit order_limit
@ -533,7 +506,7 @@ class TradingEngine implements TradingAPI {
} }
account.assets_avail account.assets_avail
.put(asset, .put(asset,
avail avail
- v - v
); );
order_limit order_limit
@ -550,9 +523,9 @@ class TradingEngine implements TradingAPI {
o o
= new opensesim.world.Order(this, account, = new opensesim.world.Order(this, account,
type, type,
v, v,
order_limit order_limit
); );
System.out System.out