Order creation verifies available assets
This commit is contained in:
parent
ab4a1035d2
commit
2eb3be4ce2
@ -45,6 +45,7 @@ class TradingEngine implements TradingAPI {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a trading engine for an asset pair
|
* Construct a trading engine for an asset pair
|
||||||
|
*
|
||||||
* @param pair The AssetPair obect to create the tradinge engine for
|
* @param pair The AssetPair obect to create the tradinge engine for
|
||||||
* @param outer Outer class - points to an Exchange object thins trading
|
* @param outer Outer class - points to an Exchange object thins trading
|
||||||
* engine belongs to.
|
* engine belongs to.
|
||||||
@ -87,7 +88,6 @@ class TradingEngine implements TradingAPI {
|
|||||||
SortedSet<Order> ul_buy = order_books.get(Order.Type.BUY);
|
SortedSet<Order> ul_buy = order_books.get(Order.Type.BUY);
|
||||||
SortedSet<Order> ul_sell = order_books.get(Order.Type.SELL);
|
SortedSet<Order> ul_sell = order_books.get(Order.Type.SELL);
|
||||||
|
|
||||||
|
|
||||||
double volume_total = 0;
|
double volume_total = 0;
|
||||||
double money_total = 0;
|
double money_total = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
@ -233,18 +233,90 @@ class TradingEngine implements TradingAPI {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Order createOrder(Account account, Order.Type type, double volume, double limit) {
|
public Order createOrder(Account account, Order.Type type, double volume, double limit) {
|
||||||
Order o = new opensesim.world.Order(this, account, type, volume, limit);
|
synchronized (account) {
|
||||||
|
|
||||||
|
// Round volume
|
||||||
|
double v = assetpair.getAsset().roundToDecimals(volume);
|
||||||
|
|
||||||
|
// Order volume must be grater than 0.0.
|
||||||
|
if (v <= 0.0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Round currency (limit)
|
||||||
|
double l = assetpair.getCurrency().roundToDecimals(limit);
|
||||||
|
|
||||||
|
double order_limit;
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case BUYLIMIT: {
|
||||||
|
// verfify available currency for a buy limit order
|
||||||
|
AbstractAsset currency = this.assetpair.getCurrency();
|
||||||
|
Double avail = account.getAvail(currency);
|
||||||
|
|
||||||
|
// return if not enough money is available
|
||||||
|
if (avail < v * l) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// reduce the available money
|
||||||
|
account.assets_avail.put(currency, avail - v * l);
|
||||||
|
order_limit = l;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case BUY: {
|
||||||
|
// For an unlimited by order there is nothing to check
|
||||||
|
// other than currency is > 0.0
|
||||||
|
AbstractAsset currency = this.assetpair.getCurrency();
|
||||||
|
Double avail = account.getAvail(currency);
|
||||||
|
if(avail <=0.0){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// All available monney is assigned to this unlimited order
|
||||||
|
account.assets_avail.put(currency, 0.0);
|
||||||
|
// we "mis"use order_limit to memorize occupied ammount \
|
||||||
|
// of currency
|
||||||
|
order_limit = avail;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case SELLLIMIT:
|
||||||
|
case SELL: {
|
||||||
|
|
||||||
|
// verfiy sell limit
|
||||||
|
AbstractAsset asset = this.assetpair.getAsset();
|
||||||
|
Double avail = account.getAvail(asset);
|
||||||
|
|
||||||
|
if (avail < v) {
|
||||||
|
// not enough items of asset (shares) available
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
account.assets_avail.put(asset, avail - v);
|
||||||
|
order_limit = l;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Order o = new opensesim.world.Order(this, account, type, v, order_limit);
|
||||||
|
|
||||||
System.out.printf("The new Order has: volume: %f limit: %f\n", o.getVolume(), o.getLimit());
|
System.out.printf("The new Order has: volume: %f limit: %f\n", o.getVolume(), o.getLimit());
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
order_books.get(o.type).add(o);
|
order_books.get(o.type).add(o);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (FiringEvent e : book_listener) {
|
for (FiringEvent e : book_listener) {
|
||||||
e.fire();
|
e.fire();
|
||||||
}
|
}
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
HashSet<FiringEvent> book_listener = new HashSet<>();
|
HashSet<FiringEvent> book_listener = new HashSet<>();
|
||||||
|
|
||||||
@ -263,7 +335,6 @@ class TradingEngine implements TradingAPI {
|
|||||||
case SELL:
|
case SELL:
|
||||||
return Collections.unmodifiableSet(askbook);
|
return Collections.unmodifiableSet(askbook);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
// return Collections.unmodifiableSet(order_books.get(type));
|
// return Collections.unmodifiableSet(order_books.get(type));
|
||||||
@ -279,7 +350,4 @@ class TradingEngine implements TradingAPI {
|
|||||||
return getOrderBook(Order.Type.SELL);
|
return getOrderBook(Order.Type.SELL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user