diff --git a/nbproject/project.properties b/nbproject/project.properties index 0eca3ac..45386f2 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -1,4 +1,4 @@ -#Thu, 03 Jan 2019 17:08:09 +0100 +#Fri, 04 Jan 2019 08:25:13 +0100 annotation.processing.enabled=true annotation.processing.enabled.in.editor=false annotation.processing.processors.list= diff --git a/src/opensesim/gui/SeSimApplication.java b/src/opensesim/gui/SeSimApplication.java index 68555bc..5d6c05a 100644 --- a/src/opensesim/gui/SeSimApplication.java +++ b/src/opensesim/gui/SeSimApplication.java @@ -607,11 +607,12 @@ public class SeSimApplication extends javax.swing.JFrame { + "strategy: opensesim.world.SimpleTrader" + "}"); Trader t = godworld.createTrader(cfg); - t.start(); + t.start(); AccountDialog.runDialog(this, ((SimpleTrader)t).account_1); AccountDialog.runDialog(this, ((SimpleTrader)t).account_b); - + + updateGodWorld(godworld); AssetPair p = godworld.getDefaultAssetPair(); diff --git a/src/opensesim/gui/account/AccountDialog.java b/src/opensesim/gui/account/AccountDialog.java index 3709519..0488bad 100644 --- a/src/opensesim/gui/account/AccountDialog.java +++ b/src/opensesim/gui/account/AccountDialog.java @@ -47,6 +47,7 @@ public class AccountDialog extends javax.swing.JDialog { d=new AccountDialog(parent, false); d.accountPanel1.account=account; d.accountPanel1.update(); + account.addListener(d.accountPanel1); d.setVisible(true); } diff --git a/src/opensesim/gui/account/AccountPanel.java b/src/opensesim/gui/account/AccountPanel.java index 2018a25..3d276d0 100644 --- a/src/opensesim/gui/account/AccountPanel.java +++ b/src/opensesim/gui/account/AccountPanel.java @@ -72,12 +72,18 @@ public class AccountPanel extends javax.swing.JPanel implements EventListener { // model.setValueAt(ob1.getAccount().getOwner().getName(), row, 0); model.setValueAt(a.getSymbol(), row, 0); model.setValueAt(astr, row, 1); - model.setValueAt(mastr, row, 2); + // model.setValueAt(mastr, row, 2); model.setValueAt(sl.toString(), row, 3); row++; } - this.finalbalance.setText(account.getFinalBalance().toString()); + String fb; + Double margin = account.getMargin(account.getWorld().getDefaultCurrency()); + fb = account.getFinalBalance().toString() + " Margin: " + + margin.toString(); + + this.finalbalance.setText(fb); + } /** diff --git a/src/opensesim/world/Account.java b/src/opensesim/world/Account.java index dc791eb..084038a 100644 --- a/src/opensesim/world/Account.java +++ b/src/opensesim/world/Account.java @@ -96,7 +96,14 @@ public class Account { } public Double getMargin(AbstractAsset currency) { - return this.getFinalBalance(currency) * getLeverage() //+ this.getFinalBalance(currency) + /* Double d = this.getAssetDebt(world.getDefaultExchange(), currency); + + Double f = this.getFinalBalance(currency) * getLeverage() ; + System.out.printf("Debth %f - Final: %f Return margin %f\n", d,f, f-d); + + return f-d;*/ + + return this.getFinalBalance(currency) * getLeverage() + this.getFinalBalance(currency) - this.getAssetDebt(world.getDefaultExchange(), currency); } @@ -145,6 +152,7 @@ public class Account { public Double getAssetDebt(Exchange ex, AbstractAsset currency) { Double result = 0.0; + System.out.printf("Enter depth rechner %f\n", result); for (AbstractAsset a : assets.keySet()) { if (a.equals(currency)) { continue; @@ -156,10 +164,17 @@ public class Account { TradingEngine api = (TradingEngine) ex.getAPI(pair); Double v = get(a) * api.last_quote.price; - - result = result + Math.abs(v); + Double sl = this.calcStopLoss(a); + Double n = get(a); + + System.out.printf("Asset: %s - %f %f %f\n", a.getSymbol(),n, v, sl*n); + + + result = result + (v-sl*n); + System.out.printf("Result is now %f\n", result); } + System.out.printf("Return Dresult %f\n", result); return result; } @@ -239,4 +254,12 @@ public class Account { public Double calcStopLoss(AbstractAsset asset){ return calcStopLoss(world.getDefaultExchange(),asset,world.getDefaultAssetPair().getCurrency()); } + + /** + * Return the world this account belongs to + * @return world + */ + public World getWorld(){ + return world; + } } diff --git a/src/opensesim/world/SimpleTrader.java b/src/opensesim/world/SimpleTrader.java index a48d8b5..e55c6ec 100644 --- a/src/opensesim/world/SimpleTrader.java +++ b/src/opensesim/world/SimpleTrader.java @@ -112,10 +112,35 @@ public class SimpleTrader extends AbstractTrader implements EventListener { account_1.setLeverage(0.0); account_1.setUnlimied(true); - ex = getWorld().getDefaultExchange(); + long delay = (long) (1000.0f * getWorld().randNextFloat(15.0f, 15.7f)); + setStatus(String.format("Initial delay: Sleeping for %d seconds.", delay)); + + + getWorld().schedule(this, delay); + + // long delay = (long) (getRandom(initial_delay[0], initial_delay[1]) * 1000); + // setStatus("Inital delay: %d", delay); + // timerTask = se.timer.startTimerTask(this, delay); + } + + long last_time = 0; + + double limit = 253.871239; + + @Override + public long receive(Event task) { + // System.out.printf("Here we are !!! %f\n", getWorld().randNextFloat(12f, 27f)); + + long diff = getWorld().currentTimeMillis() - last_time; + last_time = getWorld().currentTimeMillis(); + + AssetPair p = getWorld().getDefaultAssetPair(); + + + ex = getWorld().getDefaultExchange(); api = ex.getAPI(p); -AssetPair msftp = getWorld().getAssetPair(getWorld().getAssetBySymbol("MSFT"), + AssetPair msftp = getWorld().getAssetPair(getWorld().getAssetBySymbol("MSFT"), getWorld().getAssetBySymbol("EUR")); TradingAPI mapi = ex.getAPI(msftp); @@ -135,25 +160,10 @@ AssetPair msftp = getWorld().getAssetPair(getWorld().getAssetBySymbol("MSFT"), // Order o2 = api.createOrder(account_10, Order.Type.SELLLIMIT, 300, 1.0); //Order ou = api.createOrder(account_1, Order.Type.BUYLIMIT, 30, 10.0); // Order o1 = api.createOrder(account, Order.Type.SELLLIMIT, 250, 278); - long delay = (long) (1000.0f * getWorld().randNextFloat(3.0f, 12.7f)); - setStatus(String.format("Initial delay: Sleeping for %d seconds.", delay)); - // getWorld().schedule(this, delay); - // long delay = (long) (getRandom(initial_delay[0], initial_delay[1]) * 1000); - // setStatus("Inital delay: %d", delay); - // timerTask = se.timer.startTimerTask(this, delay); - } - - long last_time = 0; - - double limit = 253.871239; - - @Override - public long receive(Event task) { - // System.out.printf("Here we are !!! %f\n", getWorld().randNextFloat(12f, 27f)); - - long diff = getWorld().currentTimeMillis() - last_time; - last_time = getWorld().currentTimeMillis(); + + + /* System.out.printf("Here we are: %d - [%d]\n", Thread.currentThread().getId(), diff); getWorld().schedule(this, 1000); @@ -164,7 +174,7 @@ AssetPair msftp = getWorld().getAssetPair(getWorld().getAssetBySymbol("MSFT"), api = ex.getAPI(p); Order o = api.createOrder(account, Order.Type.BUY, 112.987123, limit); limit += 12; - +*/ return -1; } diff --git a/src/opensesim/world/TradingEngine.java b/src/opensesim/world/TradingEngine.java index e15a156..e6e5e5c 100644 --- a/src/opensesim/world/TradingEngine.java +++ b/src/opensesim/world/TradingEngine.java @@ -459,7 +459,7 @@ class TradingEngine implements TradingAPI { o.status=Order.Status.ERROR; System.out.printf("Error order no funds\n"); - return o; + // return o; } account.margin_bound+=v*l; @@ -525,13 +525,15 @@ class TradingEngine implements TradingAPI { } executeOrders(); - last_quote.price = 200; //75-12.5; + last_quote.price = 150; //75-12.5; for (FiringEvent e : book_listener ) { e.fire(); } + + account.notfiyListeners(); return o ; }