Fixed stop loss calculation
This commit is contained in:
parent
b193d69f52
commit
b5c6d45f66
@ -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=true
|
||||||
annotation.processing.enabled.in.editor=false
|
annotation.processing.enabled.in.editor=false
|
||||||
annotation.processing.processors.list=
|
annotation.processing.processors.list=
|
||||||
|
@ -607,11 +607,12 @@ public class SeSimApplication extends javax.swing.JFrame {
|
|||||||
+ "strategy: opensesim.world.SimpleTrader"
|
+ "strategy: opensesim.world.SimpleTrader"
|
||||||
+ "}");
|
+ "}");
|
||||||
Trader t = godworld.createTrader(cfg);
|
Trader t = godworld.createTrader(cfg);
|
||||||
t.start();
|
|
||||||
|
|
||||||
|
t.start();
|
||||||
AccountDialog.runDialog(this, ((SimpleTrader)t).account_1);
|
AccountDialog.runDialog(this, ((SimpleTrader)t).account_1);
|
||||||
AccountDialog.runDialog(this, ((SimpleTrader)t).account_b);
|
AccountDialog.runDialog(this, ((SimpleTrader)t).account_b);
|
||||||
|
|
||||||
|
|
||||||
updateGodWorld(godworld);
|
updateGodWorld(godworld);
|
||||||
|
|
||||||
AssetPair p = godworld.getDefaultAssetPair();
|
AssetPair p = godworld.getDefaultAssetPair();
|
||||||
|
@ -47,6 +47,7 @@ public class AccountDialog extends javax.swing.JDialog {
|
|||||||
d=new AccountDialog(parent, false);
|
d=new AccountDialog(parent, false);
|
||||||
d.accountPanel1.account=account;
|
d.accountPanel1.account=account;
|
||||||
d.accountPanel1.update();
|
d.accountPanel1.update();
|
||||||
|
account.addListener(d.accountPanel1);
|
||||||
d.setVisible(true);
|
d.setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,12 +72,18 @@ public class AccountPanel extends javax.swing.JPanel implements EventListener {
|
|||||||
// model.setValueAt(ob1.getAccount().getOwner().getName(), row, 0);
|
// model.setValueAt(ob1.getAccount().getOwner().getName(), row, 0);
|
||||||
model.setValueAt(a.getSymbol(), row, 0);
|
model.setValueAt(a.getSymbol(), row, 0);
|
||||||
model.setValueAt(astr, row, 1);
|
model.setValueAt(astr, row, 1);
|
||||||
model.setValueAt(mastr, row, 2);
|
// model.setValueAt(mastr, row, 2);
|
||||||
model.setValueAt(sl.toString(), row, 3);
|
model.setValueAt(sl.toString(), row, 3);
|
||||||
row++;
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -96,7 +96,14 @@ public class Account {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Double getMargin(AbstractAsset currency) {
|
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);
|
- this.getAssetDebt(world.getDefaultExchange(), currency);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -145,6 +152,7 @@ public class Account {
|
|||||||
|
|
||||||
public Double getAssetDebt(Exchange ex, AbstractAsset currency) {
|
public Double getAssetDebt(Exchange ex, AbstractAsset currency) {
|
||||||
Double result = 0.0;
|
Double result = 0.0;
|
||||||
|
System.out.printf("Enter depth rechner %f\n", result);
|
||||||
for (AbstractAsset a : assets.keySet()) {
|
for (AbstractAsset a : assets.keySet()) {
|
||||||
if (a.equals(currency)) {
|
if (a.equals(currency)) {
|
||||||
continue;
|
continue;
|
||||||
@ -156,10 +164,17 @@ public class Account {
|
|||||||
|
|
||||||
TradingEngine api = (TradingEngine) ex.getAPI(pair);
|
TradingEngine api = (TradingEngine) ex.getAPI(pair);
|
||||||
Double v = get(a) * api.last_quote.price;
|
Double v = get(a) * api.last_quote.price;
|
||||||
|
Double sl = this.calcStopLoss(a);
|
||||||
result = result + Math.abs(v);
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -239,4 +254,12 @@ public class Account {
|
|||||||
public Double calcStopLoss(AbstractAsset asset){
|
public Double calcStopLoss(AbstractAsset asset){
|
||||||
return calcStopLoss(world.getDefaultExchange(),asset,world.getDefaultAssetPair().getCurrency());
|
return calcStopLoss(world.getDefaultExchange(),asset,world.getDefaultAssetPair().getCurrency());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the world this account belongs to
|
||||||
|
* @return world
|
||||||
|
*/
|
||||||
|
public World getWorld(){
|
||||||
|
return world;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -112,10 +112,35 @@ public class SimpleTrader extends AbstractTrader implements EventListener {
|
|||||||
account_1.setLeverage(0.0);
|
account_1.setLeverage(0.0);
|
||||||
account_1.setUnlimied(true);
|
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);
|
api = ex.getAPI(p);
|
||||||
|
|
||||||
AssetPair msftp = getWorld().getAssetPair(getWorld().getAssetBySymbol("MSFT"),
|
AssetPair msftp = getWorld().getAssetPair(getWorld().getAssetBySymbol("MSFT"),
|
||||||
getWorld().getAssetBySymbol("EUR"));
|
getWorld().getAssetBySymbol("EUR"));
|
||||||
TradingAPI mapi = ex.getAPI(msftp);
|
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 o2 = api.createOrder(account_10, Order.Type.SELLLIMIT, 300, 1.0);
|
||||||
//Order ou = api.createOrder(account_1, Order.Type.BUYLIMIT, 30, 10.0);
|
//Order ou = api.createOrder(account_1, Order.Type.BUYLIMIT, 30, 10.0);
|
||||||
// Order o1 = api.createOrder(account, Order.Type.SELLLIMIT, 250, 278);
|
// 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);
|
System.out.printf("Here we are: %d - [%d]\n", Thread.currentThread().getId(), diff);
|
||||||
getWorld().schedule(this, 1000);
|
getWorld().schedule(this, 1000);
|
||||||
@ -164,7 +174,7 @@ AssetPair msftp = getWorld().getAssetPair(getWorld().getAssetBySymbol("MSFT"),
|
|||||||
api = ex.getAPI(p);
|
api = ex.getAPI(p);
|
||||||
Order o = api.createOrder(account, Order.Type.BUY, 112.987123, limit);
|
Order o = api.createOrder(account, Order.Type.BUY, 112.987123, limit);
|
||||||
limit += 12;
|
limit += 12;
|
||||||
|
*/
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -459,7 +459,7 @@ class TradingEngine implements TradingAPI {
|
|||||||
o.status=Order.Status.ERROR;
|
o.status=Order.Status.ERROR;
|
||||||
|
|
||||||
System.out.printf("Error order no funds\n");
|
System.out.printf("Error order no funds\n");
|
||||||
return o;
|
// return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
account.margin_bound+=v*l;
|
account.margin_bound+=v*l;
|
||||||
@ -525,13 +525,15 @@ class TradingEngine implements TradingAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
executeOrders();
|
executeOrders();
|
||||||
last_quote.price = 200; //75-12.5;
|
last_quote.price = 150; //75-12.5;
|
||||||
for (FiringEvent e : book_listener
|
for (FiringEvent e : book_listener
|
||||||
|
|
||||||
|
|
||||||
) {
|
) {
|
||||||
e.fire();
|
e.fire();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
account.notfiyListeners();
|
||||||
return o ;
|
return o ;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user