diff --git a/nbproject/project.properties b/nbproject/project.properties index 4d81600..689a93a 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -1,4 +1,4 @@ -#Tue, 08 Jan 2019 09:22:34 +0100 +#Tue, 08 Jan 2019 13:27:57 +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 5be2fb7..5d5f337 100644 --- a/src/opensesim/world/Account.java +++ b/src/opensesim/world/Account.java @@ -109,6 +109,8 @@ public class Account { System.out.printf("Debth %f - Final: %f Return margin %f\n", d,f, f-d); return f-d;*/ + if (!this.isLeveraged()) + return 0.0; return this.getFinalBalance(currency) * getLeverage() + this.getFinalBalance(currency) - this.getAssetDebt(world.getDefaultExchange(), currency); @@ -222,7 +224,7 @@ public class Account { for (AbstractAsset a : assets.keySet()) { Double v; if (a.equals(currency)) { - v = get(a,bound); + v = get(a, bound); result += v; continue; } @@ -230,7 +232,7 @@ public class Account { if (pair == null) { continue; } - v = get(a,bound); + v = get(a, bound); if (v == 0.0) { continue; @@ -253,7 +255,7 @@ public class Account { } void addBound(AbstractAsset asset, Double vol) { - assets.put(asset, get(asset,false)); + assets.put(asset, get(asset, false)); assets_bound.put(asset, getBound(asset) + vol); } @@ -280,6 +282,13 @@ public class Account { return getFinalBalance(world.getDefaultCurrency()); } + /** + * + * @param ex + * @param asset + * @param currency + * @return + */ public Double calcStopLoss(Exchange ex, AbstractAsset asset, AbstractAsset currency) { Double e = (get(currency)); for (AbstractAsset a : assets.keySet()) { @@ -318,11 +327,27 @@ public class Account { return getLeverage() > 0.0; } + /** + * Bind asset which will be locked in an order. + * + * @param pair + * @param volume + * @param limit + * @return true if asset could be bound, false if assets couldn't be bound + */ boolean bind(AssetPair pair, double volume, double limit) { + + // Bind asset and currecy + this.addBound(pair.getAsset(), volume); + this.addBound(pair.getCurrency(), -(volume * limit)); + if (this.isUnlimied()) { + // in case it is an unlimited account we can return + // true without further checks return true; } + // checks for leveraged account if (!this.isLeveraged()) { if (limit == 0.0) { // an unlimited order is always considered to be @@ -333,32 +358,35 @@ public class Account { if (volume < 0) { // It's a limited sell order, we have just to check // if a sufficient amount of assets is available - return get(pair.getAsset()) - volume >= 0; + + if (get(pair.getAsset()) >= 0) { + return true; + } + + // unbind and return false + this.addBound(pair.getAsset(), -volume); + this.addBound(pair.getCurrency(), (volume * limit)); + return false; + } // Check if enough money is available to cover the // entiere volume to by - return get(pair.getCurrency()) >= limit * volume; + if (get(pair.getCurrency()) >= 0) { + return true; + } + // unbind and return false + this.addBound(pair.getAsset(), -volume); + this.addBound(pair.getCurrency(), (volume * limit)); + return false; + } - // we are dealing here with a leveraged account - - System.out.printf("Add %f %f\n",volume,-volume*limit); - - // bind asset and currecy - this.addBound(pair.getAsset(), volume); - this.addBound(pair.getCurrency(), -(volume * limit)); - - Double fb = this.getFinalBalance(); - - System.out.printf("FB: %f\n",fb); - - - Double margin = this.getMargin(pair.getCurrency()); - if (margin >= 0) + if (margin >= 0) { return true; - + } + // Unbind asset and currency this.addBound(pair.getAsset(), -volume); this.addBound(pair.getCurrency(), (volume * limit)); diff --git a/src/opensesim/world/SimpleTrader.java b/src/opensesim/world/SimpleTrader.java index 7397c5a..bed505e 100644 --- a/src/opensesim/world/SimpleTrader.java +++ b/src/opensesim/world/SimpleTrader.java @@ -105,9 +105,9 @@ public class SimpleTrader extends AbstractTrader implements EventListener { pack = new AssetPack(p.getCurrency(), 1000); account_b.add(pack); - account_b.setLeverage(9.0); + account_b.setLeverage(0.1); - pack = new AssetPack(p.getCurrency(), 1234567890); + pack = new AssetPack(p.getCurrency(), 10000); account_1.add(pack); account_1.setLeverage(0.0); account_1.setUnlimied(true); @@ -142,8 +142,10 @@ public class SimpleTrader extends AbstractTrader implements EventListener { getWorld().getAssetBySymbol("EUR")); TradingAPI mapi = ex.getAPI(msftp); - Order ob = mapi.createOrder(account_b, Order.Type.SELLLIMIT, 50, 100); - Order oba = api.createOrder(account_b, Order.Type.BUYLIMIT, 50, 100); + Order ob = api.createOrder(account_b, Order.Type.SELLLIMIT, 10, 100); + + +Order oba = api.createOrder(account_1, Order.Type.BUYLIMIT, 10, 100); diff --git a/src/opensesim/world/TradingEngine.java b/src/opensesim/world/TradingEngine.java index 1956b97..cbb5e4e 100644 --- a/src/opensesim/world/TradingEngine.java +++ b/src/opensesim/world/TradingEngine.java @@ -289,11 +289,19 @@ 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) { - //b.account.margin_bound-=avdiff; - b.account.margin_bound -= b.limit * volume; - } + // Unbind + + double bound = b.account.getBound(assetpair.getCurrency()); + double addbound = volume*b.limit; + + b.account.addBound(assetpair.getCurrency(), volume*b.limit); + b.account.addBound(assetpair.getAsset(), -volume); + + a.account.addBound(assetpair.getCurrency(), -volume*b.limit); + a.account.addBound(assetpair.getAsset(), volume); + + // b.account.addMarginAvail(assetpair.getCurrency(), avdiff/b.account.getLeverage()); finishTrade(b, a, price, volume);