diff --git a/nbproject/project.properties b/nbproject/project.properties index 45386f2..6037054 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -1,4 +1,4 @@ -#Fri, 04 Jan 2019 08:25:13 +0100 +#Fri, 04 Jan 2019 17:38:20 +0100 annotation.processing.enabled=true annotation.processing.enabled.in.editor=false annotation.processing.processors.list= diff --git a/src/opensesim/world/Account.java b/src/opensesim/world/Account.java index 084038a..a7dbca2 100644 --- a/src/opensesim/world/Account.java +++ b/src/opensesim/world/Account.java @@ -123,7 +123,18 @@ public class Account { } public double getAvail(AbstractAsset asset) { - return assets_avail.getOrDefault(asset, 0.0); + if (this.getLeverage()>0){ + Double margin = this.getMargin(world.getDefaultCurrency()); + + AssetPair ap = world.getAssetPair(asset, world.getDefaultCurrency()); + + + return margin / world.getDefaultExchange().getAPI(ap).getLastQuote().price; + } + + return 0.0; + + //return assets_avail.getOrDefault(asset, 0.0); } public void addAvail(AbstractAsset asset, double val) { @@ -166,6 +177,8 @@ public class Account { Double v = get(a) * api.last_quote.price; Double sl = this.calcStopLoss(a); Double n = get(a); + if (n==0.0) + continue; System.out.printf("Asset: %s - %f %f %f\n", a.getSymbol(),n, v, sl*n); @@ -199,10 +212,15 @@ public class Account { if (pair == null) { continue; } + v = get(a); + if (v==0.0) + continue; + + TradingEngine api = (TradingEngine) ex.getAPI(pair); - v = get(a) * api.last_quote.price; + //v = get(a) * api.last_quote.price; - result = result + v; + result = result + v*api.last_quote.price; } return result; } diff --git a/src/opensesim/world/TradingAPI.java b/src/opensesim/world/TradingAPI.java index 15ae05a..e3ae537 100644 --- a/src/opensesim/world/TradingAPI.java +++ b/src/opensesim/world/TradingAPI.java @@ -52,4 +52,6 @@ public interface TradingAPI { public Set getQuoteHistory(); + public Quote getLastQuote(); + } diff --git a/src/opensesim/world/TradingEngine.java b/src/opensesim/world/TradingEngine.java index e6e5e5c..4bdb44e 100644 --- a/src/opensesim/world/TradingEngine.java +++ b/src/opensesim/world/TradingEngine.java @@ -289,9 +289,9 @@ class TradingEngine implements TradingAPI { // For sellers there is no need to update. double avdiff = b.limit * volume - price * volume; b.account.addAvail(assetpair.getCurrency(), avdiff); - if (b.account.getLeverage()>0.0){ + if (b.account.getLeverage() > 0.0) { //b.account.margin_bound-=avdiff; - b.account.margin_bound-=b.limit*volume; + b.account.margin_bound -= b.limit * volume; } // b.account.addMarginAvail(assetpair.getCurrency(), avdiff/b.account.getLeverage()); @@ -438,31 +438,29 @@ class TradingEngine implements TradingAPI { switch (type) { case BUYLIMIT: { Double avail; - + // verfify available currency for a buy limit order AbstractAsset currency = this.assetpair.getCurrency(); - if (account.getLeverage()==0.0){ + if (account.getLeverage() == 0.0) { avail = account.getAvail(currency); account.addAvail(currency, -(v * l)); - } - else{ - + } else { avail = account.getMargin(assetpair.getCurrency()); - - } - - // return if not enough funds are available - if (avail < v * l) { - o = new Order(this, account, type, v, l); - o.status=Order.Status.ERROR; - - System.out.printf("Error order no funds\n"); - // return o; + } - account.margin_bound+=v*l; + // return if not enough funds are available + if (avail < v * l) { + o = new Order(this, account, type, v, l); + o.status = Order.Status.ERROR; + + System.out.printf("Error order no funds\n"); + // return o; + } + + account.margin_bound += v * l; // reduce the available money // account.assets_avail.put(currency, avail - v * l); @@ -514,178 +512,85 @@ class TradingEngine implements TradingAPI { } } - - o = new Order(this, account, type, v, order_limit); - //System.out.printf("The new Order has: volume: %f limit: %f\n", o.getVolume(), o.getLimit()); - synchronized (this) { - order_books.get(o.type).add(o); + o = new Order(this, account, type, v, order_limit); + + //System.out.printf("The new Order has: volume: %f limit: %f\n", o.getVolume(), o.getLimit()); + synchronized (this) { + order_books.get(o.type).add(o); + + } + } + + executeOrders(); + last_quote.price = 150; //75-12.5; + for (FiringEvent e : book_listener) { + e.fire(); + } + + account.notfiyListeners(); + return o; + + } + + HashSet book_listener + = new HashSet<>(); + + @Override + public void addOrderBookListener(EventListener listener + ) { + book_listener + .add(new FiringEvent(listener + )); + + } + + @Override + public Set + getOrderBook(Order.Type type + ) { + switch (type) { + case BUYLIMIT: + case BUY: + return Collections + .unmodifiableSet(bidbook + ); + + case SELLLIMIT: + case SELL: + return Collections + .unmodifiableSet(askbook + ); } - } - - executeOrders(); - last_quote.price = 150; //75-12.5; - for (FiringEvent e : book_listener - - - ) { - e.fire(); - } - - account.notfiyListeners(); - return o ; - -} - -HashSet - - book_listener - -= new HashSet - -<>(); - - @Override - public - -void addOrderBookListener - -(EventListener - -listener - -) { - book_listener - -.add - -(new FiringEvent - -(listener - -)); - - - -} - - @Override - public Set - -getOrderBook - -(Order - -.Type - -type - -) { - switch (type - -) { - case BUYLIMIT - -: - case BUY - -: - return Collections - -.unmodifiableSet - -(bidbook - -); - - - -case SELLLIMIT - -: - case SELL - -: - return Collections - -.unmodifiableSet - -(askbook - -); - - - -} return null; - -} + } @Override - public Set - + public Set + getBidBook() { + return getOrderBook(Order.Type.BUYLIMIT + ); -getBidBook - -() { - return getOrderBook - -(Order - -.Type - -.BUYLIMIT - - -); - - - -} + } @Override - public Set - + public Set + getAskBook() { + return getOrderBook(Order.Type.SELL + ); -getAskBook - -() { - return getOrderBook - -(Order - -.Type - -.SELL - - -); - - - -} + } @Override - public Set + public Set getQuoteHistory() { + return Collections.unmodifiableSet(quote_history); + } - getQuoteHistory - -() { - return Collections - - -.unmodifiableSet - -(quote_history - - -); + @Override + public Quote getLastQuote() { + return this.last_quote; } }