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

@ -445,9 +445,7 @@ class TradingEngine implements TradingAPI {
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());
@ -526,10 +524,7 @@ 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();
} }
@ -538,154 +533,64 @@ class TradingEngine implements TradingAPI {
} }
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
(Order
.Type
type
) { ) {
switch (type switch (type) {
case BUYLIMIT:
) { case BUY:
case BUYLIMIT
:
case BUY
:
return Collections return Collections
.unmodifiableSet(bidbook
.unmodifiableSet
(bidbook
); );
case SELLLIMIT:
case SELL:
case SELLLIMIT
:
case SELL
:
return Collections return Collections
.unmodifiableSet(askbook
.unmodifiableSet
(askbook
); );
} }
return null; return null;
}
@Override
public Set
getBidBook() {
return getOrderBook(Order.Type.BUYLIMIT
);
} }
@Override @Override
public Set public Set
getAskBook() {
return getOrderBook(Order.Type.SELL
getBidBook
() {
return getOrderBook
(Order
.Type
.BUYLIMIT
); );
} }
@Override @Override
public Set public Set<Quote> getQuoteHistory() {
return Collections.unmodifiableSet(quote_history);
getAskBook
() {
return getOrderBook
(Order
.Type
.SELL
);
} }
@Override @Override
public Set public Quote getLastQuote() {
return this.last_quote;
<Quote
> getQuoteHistory
() {
return Collections
.unmodifiableSet
(quote_history
);
} }
} }