more work on stop loss

This commit is contained in:
7u83 2019-01-04 19:09:20 +01:00
parent b5c6d45f66
commit a2e5143c64
4 changed files with 105 additions and 180 deletions

View File

@ -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=true
annotation.processing.enabled.in.editor=false annotation.processing.enabled.in.editor=false
annotation.processing.processors.list= annotation.processing.processors.list=

View File

@ -123,7 +123,18 @@ public class Account {
} }
public double getAvail(AbstractAsset asset) { 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) { public void addAvail(AbstractAsset asset, double val) {
@ -166,6 +177,8 @@ public class Account {
Double v = get(a) * api.last_quote.price; Double v = get(a) * api.last_quote.price;
Double sl = this.calcStopLoss(a); Double sl = this.calcStopLoss(a);
Double n = get(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); 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) { if (pair == null) {
continue; continue;
} }
TradingEngine api = (TradingEngine) ex.getAPI(pair); v = get(a);
v = get(a) * api.last_quote.price; if (v==0.0)
continue;
result = result + v;
TradingEngine api = (TradingEngine) ex.getAPI(pair);
//v = get(a) * api.last_quote.price;
result = result + v*api.last_quote.price;
} }
return result; return result;
} }

View File

@ -52,4 +52,6 @@ public interface TradingAPI {
public Set<Quote> getQuoteHistory(); public Set<Quote> getQuoteHistory();
public Quote getLastQuote();
} }

View File

@ -289,9 +289,9 @@ class TradingEngine implements TradingAPI {
// For sellers there is no need to update. // For sellers there is no need to update.
double avdiff = b.limit * volume - price * volume; double avdiff = b.limit * volume - price * volume;
b.account.addAvail(assetpair.getCurrency(), avdiff); 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-=avdiff;
b.account.margin_bound-=b.limit*volume; b.account.margin_bound -= b.limit * volume;
} }
// b.account.addMarginAvail(assetpair.getCurrency(), avdiff/b.account.getLeverage()); // b.account.addMarginAvail(assetpair.getCurrency(), avdiff/b.account.getLeverage());
@ -441,13 +441,11 @@ class TradingEngine implements TradingAPI {
// verfify available currency for a buy limit order // verfify available currency for a buy limit order
AbstractAsset currency = this.assetpair.getCurrency(); AbstractAsset currency = this.assetpair.getCurrency();
if (account.getLeverage()==0.0){ if (account.getLeverage() == 0.0) {
avail = account.getAvail(currency); avail = account.getAvail(currency);
account.addAvail(currency, -(v * l)); account.addAvail(currency, -(v * l));
} } else {
else{
avail = account.getMargin(assetpair.getCurrency()); avail = account.getMargin(assetpair.getCurrency());
@ -456,13 +454,13 @@ class TradingEngine implements TradingAPI {
// return if not enough funds are available // return if not enough funds are available
if (avail < v * l) { if (avail < v * l) {
o = new Order(this, account, type, v, l); o = new Order(this, account, type, v, l);
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;
// reduce the available money // reduce the available money
// account.assets_avail.put(currency, avail - v * l); // account.assets_avail.put(currency, avail - v * l);
@ -526,166 +524,73 @@ class TradingEngine implements TradingAPI {
executeOrders(); executeOrders();
last_quote.price = 150; //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(); account.notfiyListeners();
return o ; return o;
} }
HashSet HashSet<FiringEvent> book_listener
= new HashSet<>();
<FiringEvent
> book_listener
= new HashSet
<>();
@Override @Override
public public void addOrderBookListener(EventListener listener
) {
void addOrderBookListener
(EventListener
listener
) {
book_listener book_listener
.add(new FiringEvent(listener
));
.add }
(new FiringEvent
(listener
));
}
@Override @Override
public Set public Set
getOrderBook(Order.Type type
getOrderBook ) {
switch (type) {
(Order case BUYLIMIT:
case BUY:
.Type
type
) {
switch (type
) {
case BUYLIMIT
:
case BUY
:
return Collections return Collections
.unmodifiableSet(bidbook
);
.unmodifiableSet case SELLLIMIT:
case SELL:
(bidbook
);
case SELLLIMIT
:
case SELL
:
return Collections return Collections
.unmodifiableSet(askbook
);
.unmodifiableSet }
(askbook
);
}
return null; return null;
}
}
@Override @Override
public Set public Set
getBidBook() {
return getOrderBook(Order.Type.BUYLIMIT
);
}
getBidBook
() {
return getOrderBook
(Order
.Type
.BUYLIMIT
);
}
@Override @Override
public Set public Set
getAskBook() {
return getOrderBook(Order.Type.SELL
);
}
getAskBook
() {
return getOrderBook
(Order
.Type
.SELL
);
}
@Override @Override
public Set public Set<Quote> getQuoteHistory() {
return Collections.unmodifiableSet(quote_history);
}
<Quote @Override
public Quote getLastQuote() {
> getQuoteHistory return this.last_quote;
() {
return Collections
.unmodifiableSet
(quote_history
);
} }
} }