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

View File

@ -123,7 +123,18 @@ public class Account {
}
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) {
@ -166,6 +177,8 @@ public class Account {
Double v = get(a) * api.last_quote.price;
Double sl = this.calcStopLoss(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);
@ -199,10 +212,15 @@ public class Account {
if (pair == null) {
continue;
}
TradingEngine api = (TradingEngine) ex.getAPI(pair);
v = get(a) * api.last_quote.price;
v = get(a);
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;
}

View File

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

View File

@ -445,9 +445,7 @@ class TradingEngine implements TradingAPI {
avail = account.getAvail(currency);
account.addAvail(currency, -(v * l));
}
else{
} else {
avail = account.getMargin(assetpair.getCurrency());
@ -526,10 +524,7 @@ class TradingEngine implements TradingAPI {
executeOrders();
last_quote.price = 150; //75-12.5;
for (FiringEvent e : book_listener
) {
for (FiringEvent e : book_listener) {
e.fire();
}
@ -538,154 +533,64 @@ class TradingEngine implements TradingAPI {
}
HashSet
<FiringEvent
> book_listener
= new HashSet
<>();
HashSet<FiringEvent> book_listener
= new HashSet<>();
@Override
public
void addOrderBookListener
(EventListener
listener
public void addOrderBookListener(EventListener listener
) {
book_listener
.add
(new FiringEvent
(listener
.add(new FiringEvent(listener
));
}
@Override
public Set
getOrderBook
(Order
.Type
type
getOrderBook(Order.Type type
) {
switch (type
) {
case BUYLIMIT
:
case BUY
:
switch (type) {
case BUYLIMIT:
case BUY:
return Collections
.unmodifiableSet
(bidbook
.unmodifiableSet(bidbook
);
case SELLLIMIT
:
case SELL
:
case SELLLIMIT:
case SELL:
return Collections
.unmodifiableSet
(askbook
.unmodifiableSet(askbook
);
}
return null;
}
@Override
public Set
getBidBook() {
return getOrderBook(Order.Type.BUYLIMIT
);
}
@Override
public Set
getBidBook
() {
return getOrderBook
(Order
.Type
.BUYLIMIT
getAskBook() {
return getOrderBook(Order.Type.SELL
);
}
@Override
public Set
getAskBook
() {
return getOrderBook
(Order
.Type
.SELL
);
public Set<Quote> getQuoteHistory() {
return Collections.unmodifiableSet(quote_history);
}
@Override
public Set
<Quote
> getQuoteHistory
() {
return Collections
.unmodifiableSet
(quote_history
);
public Quote getLastQuote() {
return this.last_quote;
}
}