More work on stop loss
This commit is contained in:
parent
fcdae01a8b
commit
b193d69f52
@ -1,4 +1,4 @@
|
||||
#Tue, 01 Jan 2019 15:33:07 +0100
|
||||
#Thu, 03 Jan 2019 17:08:09 +0100
|
||||
annotation.processing.enabled=true
|
||||
annotation.processing.enabled.in.editor=false
|
||||
annotation.processing.processors.list=
|
||||
|
@ -610,7 +610,7 @@ public class SeSimApplication extends javax.swing.JFrame {
|
||||
t.start();
|
||||
|
||||
AccountDialog.runDialog(this, ((SimpleTrader)t).account_1);
|
||||
AccountDialog.runDialog(this, ((SimpleTrader)t).account_10);
|
||||
AccountDialog.runDialog(this, ((SimpleTrader)t).account_b);
|
||||
|
||||
updateGodWorld(godworld);
|
||||
|
||||
|
@ -63,8 +63,8 @@ public class AccountPanel extends javax.swing.JPanel implements EventListener {
|
||||
String astr = val.toString() + "/" + avail.toString();
|
||||
|
||||
Double mval = account.getMargin(a);
|
||||
Double mavail = account.getAvail(a);
|
||||
String mastr = mval.toString(); // + "/" + mavail.toString();
|
||||
Double mavail = account.getMargin(a)-account.margin_bound;
|
||||
String mastr = mval.toString()+ "/" + mavail.toString();
|
||||
|
||||
|
||||
Double sl = account.calcStopLoss(a);
|
||||
|
@ -42,6 +42,7 @@ public class Account {
|
||||
|
||||
HashMap<AbstractAsset, Double> assets = new HashMap<>();
|
||||
HashMap<AbstractAsset, Double> assets_avail = new HashMap<>();
|
||||
HashMap<AbstractAsset, Double> stop_los = new HashMap<>();
|
||||
|
||||
public double margin_bound=0.0;
|
||||
|
||||
@ -95,7 +96,7 @@ public class Account {
|
||||
}
|
||||
|
||||
public Double getMargin(AbstractAsset currency) {
|
||||
return this.getFinalBalance(currency) * getLeverage() + this.getFinalBalance(currency)
|
||||
return this.getFinalBalance(currency) * getLeverage() //+ this.getFinalBalance(currency)
|
||||
- this.getAssetDebt(world.getDefaultExchange(), currency);
|
||||
|
||||
}
|
||||
|
@ -64,7 +64,7 @@ public class GodWorld implements GetJson, World {
|
||||
|
||||
@Override
|
||||
public AbstractAsset getDefaultCurrency() {
|
||||
return null;
|
||||
return getDefaultAssetPair().getCurrency();
|
||||
}
|
||||
|
||||
public static final class JKEYS {
|
||||
@ -261,6 +261,9 @@ public class GodWorld implements GetJson, World {
|
||||
}
|
||||
|
||||
public void add(AssetPair pair) {
|
||||
if (pair.getAsset()==null || pair.getCurrency()==null){
|
||||
return;
|
||||
}
|
||||
asset_pairs.put(pair.getSymbol(), pair);
|
||||
if (default_asset_pair == null) {
|
||||
default_asset_pair = pair;
|
||||
|
@ -69,9 +69,21 @@ public class Order implements Comparable<Order> {
|
||||
* Definition of order status
|
||||
*/
|
||||
public static enum Status {
|
||||
OPEN, PARTIALLY_EXECUTED, CLOSED, CANCELED
|
||||
OPEN, PARTIALLY_EXECUTED, CLOSED, CANCELED, ERROR
|
||||
}
|
||||
|
||||
String message="";
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
private void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Definition of order types
|
||||
*/
|
||||
|
@ -74,7 +74,7 @@ public class SimpleTrader extends AbstractTrader implements EventListener {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public Account account_10, account_1;
|
||||
public Account account_b, account_1;
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
@ -96,20 +96,21 @@ public class SimpleTrader extends AbstractTrader implements EventListener {
|
||||
|
||||
AssetPair p = getWorld().getDefaultAssetPair();
|
||||
|
||||
account_10 = new Account(getWorld());
|
||||
account_b = new Account(getWorld());
|
||||
account_1 = new Account(getWorld());
|
||||
|
||||
AssetPack pack;
|
||||
pack = new AssetPack(p.getAsset(), 0);
|
||||
account_10.add(pack);
|
||||
account_b.add(pack);
|
||||
|
||||
pack = new AssetPack(p.getCurrency(), 1000);
|
||||
account_10.add(pack);
|
||||
account_10.setLeverage(10);
|
||||
account_b.add(pack);
|
||||
account_b.setLeverage(10.0);
|
||||
|
||||
pack = new AssetPack(p.getCurrency(), 100000);
|
||||
pack = new AssetPack(p.getCurrency(), 1234567890);
|
||||
account_1.add(pack);
|
||||
account_1.setLeverage(1.0);
|
||||
account_1.setLeverage(0.0);
|
||||
account_1.setUnlimied(true);
|
||||
|
||||
ex = getWorld().getDefaultExchange();
|
||||
api = ex.getAPI(p);
|
||||
@ -118,11 +119,12 @@ AssetPair msftp = getWorld().getAssetPair(getWorld().getAssetBySymbol("MSFT"),
|
||||
getWorld().getAssetBySymbol("EUR"));
|
||||
TradingAPI mapi = ex.getAPI(msftp);
|
||||
|
||||
Order ob = api.createOrder(account_10, Order.Type.BUYLIMIT, 20, 100);
|
||||
Order mob = mapi.createOrder(account_10, Order.Type.SELLLIMIT, 80, 100);
|
||||
Order ob = api.createOrder(account_b, Order.Type.BUYLIMIT, 20, 100);
|
||||
|
||||
//Order mob = mapi.createOrder(account_b, Order.Type.SELLLIMIT, 80, 100);
|
||||
|
||||
Order oa = api.createOrder(account_1, Order.Type.SELLLIMIT, 100, 100);
|
||||
Order oaaa = mapi.createOrder(account_1, Order.Type.BUYLIMIT, 100, 100);
|
||||
Order oa = api.createOrder(account_1, Order.Type.SELLLIMIT, 100, 100);
|
||||
//Order oaaa = mapi.createOrder(account_1, Order.Type.BUYLIMIT, 100, 100);
|
||||
|
||||
|
||||
//Order oax = mapi.createOrder(account_1, Order.Type.BUYLIMIT, 100, 100);
|
||||
|
@ -289,6 +289,10 @@ 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;
|
||||
}
|
||||
|
||||
// b.account.addMarginAvail(assetpair.getCurrency(), avdiff/b.account.getLeverage());
|
||||
finishTrade(b, a, price, volume);
|
||||
@ -433,20 +437,34 @@ class TradingEngine implements TradingAPI {
|
||||
} else {
|
||||
switch (type) {
|
||||
case BUYLIMIT: {
|
||||
|
||||
Double avail;
|
||||
|
||||
// verfify available currency for a buy limit order
|
||||
AbstractAsset currency = this.assetpair.getCurrency();
|
||||
if (account.getLeverage()==0.0){
|
||||
avail = account.getAvail(currency);
|
||||
account.addAvail(currency, -(v * l));
|
||||
|
||||
Double avail = account.getMargin(assetpair.getCurrency());
|
||||
}
|
||||
else{
|
||||
|
||||
// return if not enough money is available
|
||||
|
||||
avail = account.getMargin(assetpair.getCurrency());
|
||||
|
||||
}
|
||||
|
||||
// return if not enough funds are available
|
||||
if (avail < v * l) {
|
||||
// return null;
|
||||
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);
|
||||
account.addAvail(currency, -(v * l));
|
||||
|
||||
//account.addMarginAvail(currency, -((v * l)/account.getLeverage()));
|
||||
order_limit = l;
|
||||
@ -507,7 +525,7 @@ class TradingEngine implements TradingAPI {
|
||||
}
|
||||
|
||||
executeOrders();
|
||||
// last_quote.price = 200;
|
||||
last_quote.price = 200; //75-12.5;
|
||||
for (FiringEvent e : book_listener
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user