Work on stop loss

This commit is contained in:
7u83 2019-01-01 19:20:35 +01:00
parent 3f43ff6f29
commit 0cedbbc3d4
7 changed files with 274 additions and 145 deletions

View File

@ -1,4 +1,4 @@
#Mon, 31 Dec 2018 14:02:50 +0100
#Tue, 01 Jan 2019 15:33:07 +0100
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processors.list=

View File

@ -45,7 +45,22 @@ public class Account {
Trader owner;
//public Exchange exchange = null;
private World world;
private boolean unlimited = false;
public boolean isUnlimied() {
return unlimited;
}
void setUnlimied(boolean unlimied) {
this.unlimited = unlimied;
}
private double leverage = 0.0;
@ -81,10 +96,7 @@ public class Account {
return this.getFinalBalance(currency) * getLeverage()
- this.getAssetDebt(world.getDefaultExchange(), currency);
}
;
}
synchronized void add(AssetPack pack) {
assets.put(pack.asset, get(pack.asset) + pack.volume);

View File

@ -62,6 +62,11 @@ public class GodWorld implements GetJson, World {
return scheduler.currentTimeMillis();
}
@Override
public AbstractAsset getDefaultCurrency() {
return null;
}
public static final class JKEYS {
public static final String ASSETS = "assets";

View File

@ -90,4 +90,9 @@ public class RealWorld implements World {
return godworld.getAssetPair(asset, currency);
}
@Override
public AbstractAsset getDefaultCurrency() {
return godworld.getDefaultCurrency();
}
}

View File

@ -49,7 +49,7 @@ public class SimpleTrader extends AbstractTrader implements EventListener {
if (cfg == null) {
return;
}
}
public SimpleTrader() {
@ -74,8 +74,8 @@ public class SimpleTrader extends AbstractTrader implements EventListener {
return 0;
}
public Account account_s, account_b;
public Account account_10, account_1;
@Override
public void start() {
setVerbose(true);
@ -92,54 +92,50 @@ public class SimpleTrader extends AbstractTrader implements EventListener {
setStatus("Stopped.");
return;
}
AbstractAsset c,a;
AbstractAsset c, a;
AssetPair p = getWorld().getDefaultAssetPair();
account_s = new Account(getWorld());
account_b = new Account(getWorld());
account_10 = new Account(getWorld());
account_1 = new Account(getWorld());
AssetPack pack;
pack = new AssetPack(p.getAsset(),200);
account_s.add(pack);
pack = new AssetPack(p.getCurrency(),0);
account_b.add(pack);
pack = new AssetPack(p.getCurrency(),1000);
account_b.add(pack);
account_b.setLeverage(0.0);
pack = new AssetPack(p.getAsset(), 0);
account_10.add(pack);
pack = new AssetPack(p.getCurrency(), 1000);
account_10.add(pack);
account_10.setLeverage(10);
pack = new AssetPack(p.getCurrency(), 100000);
account_1.add(pack);
account_1.setLeverage(1.0);
ex = getWorld().getDefaultExchange();
api = ex.getAPI(p);
api = ex.getAPI(p);
Order oa = api.createOrder(account_b, Order.Type.BUYLIMIT, 50, 100);
// Order ob = api.createOrder(account_s, Order.Type.SELLLIMIT, 200,100);
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 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);
// Order oa = api.createOrder(account_b, Order.Type.BUYLIMIT, 100, 10.0);
// Order ob = api.createOrder(account_b, Order.Type.BUYLIMIT, 100, 9.0);
// Order oc = api.createOrder(account_b, Order.Type.BUYLIMIT, 100, 8.0);
// Order o2 = api.createOrder(account_s, Order.Type.SELLLIMIT, 300, 1.0);
//Order ou = api.createOrder(account_b, Order.Type.BUYLIMIT, 30, 10.0);
// Order o1 = api.createOrder(account, Order.Type.SELLLIMIT, 250, 278);
// Order oa = api.createOrder(account_1, Order.Type.BUYLIMIT, 100, 10.0);
// Order ob = api.createOrder(account_1, Order.Type.BUYLIMIT, 100, 9.0);
// Order oc = api.createOrder(account_1, Order.Type.BUYLIMIT, 100, 8.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 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);
// getWorld().schedule(this, delay);
// long delay = (long) (getRandom(initial_delay[0], initial_delay[1]) * 1000);
// setStatus("Inital delay: %d", delay);
@ -150,9 +146,6 @@ public class SimpleTrader extends AbstractTrader implements EventListener {
double limit = 253.871239;
@Override
public long receive(Event task) {
// System.out.printf("Here we are !!! %f\n", getWorld().randNextFloat(12f, 27f));
@ -162,14 +155,14 @@ public class SimpleTrader extends AbstractTrader implements EventListener {
System.out.printf("Here we are: %d - [%d]\n", Thread.currentThread().getId(), diff);
getWorld().schedule(this, 1000);
AssetPair p = getWorld().getDefaultAssetPair();
AssetPair p = getWorld().getDefaultAssetPair();
ex = getWorld().getDefaultExchange();
api = ex.getAPI(p);
Order o = api.createOrder(account, Order.Type.BUY, 112.987123, limit);
api = ex.getAPI(p);
Order o = api.createOrder(account, Order.Type.BUY, 112.987123, limit);
limit += 12;
return -1;
}

View File

@ -428,131 +428,244 @@ class TradingEngine implements TradingAPI {
double order_limit;
switch (type) {
case BUYLIMIT: {
// verfify available currency for a buy limit order
AbstractAsset currency = this.assetpair.getCurrency();
if (account.isUnlimied()) {
order_limit = l;
} else {
switch (type) {
case BUYLIMIT: {
Double avail = account.getMargin(assetpair.getCurrency());
// verfify available currency for a buy limit order
AbstractAsset currency = this.assetpair.getCurrency();
// return if not enough money is available
if (avail < v * l) {
// return null;
}
Double avail = account.getMargin(assetpair.getCurrency());
// reduce the available money
// return if not enough money is available
if (avail < v * l) {
// return null;
}
// reduce the available money
// account.assets_avail.put(currency, avail - v * l);
account.addAvail(currency, -(v * l));
account.addAvail(currency, -(v * l));
//account.addMarginAvail(currency, -((v * l)/account.getLeverage()));
order_limit = l;
break;
order_limit = l;
break;
}
}
case BUY: {
// For an unlimited by order there is nothing to check
// other than currency is > 0.0
AbstractAsset currency = this.assetpair.getCurrency();
Double avail = account.getAvail(currency);
case BUY: {
// For an unlimited by order there is nothing to check
// other than currency is > 0.0
AbstractAsset currency = this.assetpair.getCurrency();
Double avail = account.getAvail(currency);
if (avail <= 0.0) {
if (avail <= 0.0) {
return null;
}
// All available monney is assigned to this unlimited order
account.assets_avail.put(currency, 0.0);
// we "mis"use order_limit to memorize occupied ammount \
// of currency
order_limit = avail;
break;
}
case SELLLIMIT:
case SELL: {
// verfiy sell limit
AbstractAsset asset = this.assetpair.getAsset();
Double avail = account.getAvail(asset);
if (avail < v) {
// not enough items of asset (shares) available
// return null;
}
account.assets_avail.put(asset, avail - v);
order_limit = l;
break;
}
default:
return null;
}
// All available monney is assigned to this unlimited order
account.assets_avail.put(currency, 0.0);
// we "mis"use order_limit to memorize occupied ammount \
// of currency
order_limit = avail;
break;
}
case SELLLIMIT:
case SELL: {
// verfiy sell limit
AbstractAsset asset = this.assetpair.getAsset();
Double avail = account.getAvail(asset);
if (avail < v) {
// not enough items of asset (shares) available
// return null;
}
account.assets_avail.put(asset, avail - v);
order_limit = l;
break;
}
default:
return null;
}
o = new Order(this, account, type, v, order_limit);
o = new Order(this, account, type, v, order_limit);
//System.out.printf("The new Order has: volume: %f limit: %f\n", o.getVolume(), o.getLimit());
synchronized (this) {
order_books.get(o.type).add(o);
//System.out.printf("The new Order has: volume: %f limit: %f\n", o.getVolume(), o.getLimit());
synchronized (this) {
order_books.get(o.type).add(o);
}
}
executeOrders();
// last_quote.price = 200;
for (FiringEvent e : book_listener) {
}
executeOrders();
// last_quote.price = 200;
for (FiringEvent e : book_listener
) {
e.fire();
}
return o;
}
return o ;
HashSet<FiringEvent> book_listener = new HashSet<>();
}
HashSet
<FiringEvent
> book_listener
= new HashSet
<>();
@Override
public void addOrderBookListener(EventListener listener) {
book_listener.add(new FiringEvent(listener));
public
}
void addOrderBookListener
(EventListener
listener
) {
book_listener
.add
(new FiringEvent
(listener
));
}
@Override
public Set getOrderBook(Order.Type type) {
switch (type) {
case BUYLIMIT:
case BUY:
return Collections.unmodifiableSet(bidbook);
public Set
case SELLLIMIT:
case SELL:
return Collections.unmodifiableSet(askbook);
getOrderBook
}
(Order
.Type
type
) {
switch (type
) {
case BUYLIMIT
:
case BUY
:
return Collections
.unmodifiableSet
(bidbook
);
case SELLLIMIT
:
case SELL
:
return Collections
.unmodifiableSet
(askbook
);
}
return null;
}
}
@Override
public Set
getBidBook() {
return getOrderBook(Order.Type.BUYLIMIT
);
public Set
}
getBidBook
() {
return getOrderBook
(Order
.Type
.BUYLIMIT
);
}
@Override
public Set
getAskBook() {
return getOrderBook(Order.Type.SELL
);
public Set
}
getAskBook
() {
return getOrderBook
(Order
.Type
.SELL
);
}
@Override
public Set<Quote> getQuoteHistory() {
public Set
<Quote
> getQuoteHistory
() {
return Collections
.unmodifiableSet(quote_history
);
.unmodifiableSet
(quote_history
);
}
}

View File

@ -52,6 +52,7 @@ public interface World {
public Exchange getDefaultExchange();
public AssetPair getDefaultAssetPair();
public AbstractAsset getDefaultCurrency();
Collection<Trader> getTradersCollection();