Work on stop loss
This commit is contained in:
parent
3f43ff6f29
commit
0cedbbc3d4
@ -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=true
|
||||||
annotation.processing.enabled.in.editor=false
|
annotation.processing.enabled.in.editor=false
|
||||||
annotation.processing.processors.list=
|
annotation.processing.processors.list=
|
||||||
|
@ -45,8 +45,23 @@ public class Account {
|
|||||||
|
|
||||||
Trader owner;
|
Trader owner;
|
||||||
//public Exchange exchange = null;
|
//public Exchange exchange = null;
|
||||||
|
|
||||||
|
|
||||||
private World world;
|
private World world;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private boolean unlimited = false;
|
||||||
|
|
||||||
|
public boolean isUnlimied() {
|
||||||
|
return unlimited;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setUnlimied(boolean unlimied) {
|
||||||
|
this.unlimited = unlimied;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private double leverage = 0.0;
|
private double leverage = 0.0;
|
||||||
|
|
||||||
public double getLeverage() {
|
public double getLeverage() {
|
||||||
@ -83,9 +98,6 @@ public class Account {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
;
|
|
||||||
|
|
||||||
|
|
||||||
synchronized void add(AssetPack pack) {
|
synchronized void add(AssetPack pack) {
|
||||||
assets.put(pack.asset, get(pack.asset) + pack.volume);
|
assets.put(pack.asset, get(pack.asset) + pack.volume);
|
||||||
assets_avail.put(pack.asset, getAvail(pack.asset) + pack.volume);
|
assets_avail.put(pack.asset, getAvail(pack.asset) + pack.volume);
|
||||||
|
@ -62,6 +62,11 @@ public class GodWorld implements GetJson, World {
|
|||||||
return scheduler.currentTimeMillis();
|
return scheduler.currentTimeMillis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AbstractAsset getDefaultCurrency() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public static final class JKEYS {
|
public static final class JKEYS {
|
||||||
|
|
||||||
public static final String ASSETS = "assets";
|
public static final String ASSETS = "assets";
|
||||||
|
@ -90,4 +90,9 @@ public class RealWorld implements World {
|
|||||||
return godworld.getAssetPair(asset, currency);
|
return godworld.getAssetPair(asset, currency);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AbstractAsset getDefaultCurrency() {
|
||||||
|
return godworld.getDefaultCurrency();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -74,7 +74,7 @@ public class SimpleTrader extends AbstractTrader implements EventListener {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Account account_s, account_b;
|
public Account account_10, account_1;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start() {
|
public void start() {
|
||||||
@ -92,51 +92,47 @@ public class SimpleTrader extends AbstractTrader implements EventListener {
|
|||||||
setStatus("Stopped.");
|
setStatus("Stopped.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
AbstractAsset c,a;
|
AbstractAsset c, a;
|
||||||
|
|
||||||
AssetPair p = getWorld().getDefaultAssetPair();
|
AssetPair p = getWorld().getDefaultAssetPair();
|
||||||
|
|
||||||
account_s = new Account(getWorld());
|
account_10 = new Account(getWorld());
|
||||||
account_b = new Account(getWorld());
|
account_1 = new Account(getWorld());
|
||||||
|
|
||||||
AssetPack pack;
|
AssetPack pack;
|
||||||
pack = new AssetPack(p.getAsset(),200);
|
pack = new AssetPack(p.getAsset(), 0);
|
||||||
account_s.add(pack);
|
account_10.add(pack);
|
||||||
|
|
||||||
pack = new AssetPack(p.getCurrency(),0);
|
pack = new AssetPack(p.getCurrency(), 1000);
|
||||||
account_b.add(pack);
|
account_10.add(pack);
|
||||||
|
account_10.setLeverage(10);
|
||||||
|
|
||||||
pack = new AssetPack(p.getCurrency(),1000);
|
pack = new AssetPack(p.getCurrency(), 100000);
|
||||||
account_b.add(pack);
|
account_1.add(pack);
|
||||||
account_b.setLeverage(0.0);
|
account_1.setLeverage(1.0);
|
||||||
|
|
||||||
ex = getWorld().getDefaultExchange();
|
ex = getWorld().getDefaultExchange();
|
||||||
api = ex.getAPI(p);
|
api = ex.getAPI(p);
|
||||||
|
|
||||||
|
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 oa = api.createOrder(account_b, Order.Type.BUYLIMIT, 50, 100);
|
//Order oax = mapi.createOrder(account_1, Order.Type.BUYLIMIT, 100, 100);
|
||||||
// Order ob = api.createOrder(account_s, Order.Type.SELLLIMIT, 200,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 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);
|
// Order o1 = api.createOrder(account, Order.Type.SELLLIMIT, 250, 278);
|
||||||
|
|
||||||
|
|
||||||
long delay = (long) (1000.0f * getWorld().randNextFloat(3.0f, 12.7f));
|
long delay = (long) (1000.0f * getWorld().randNextFloat(3.0f, 12.7f));
|
||||||
setStatus(String.format("Initial delay: Sleeping for %d seconds.", delay));
|
setStatus(String.format("Initial delay: Sleeping for %d seconds.", delay));
|
||||||
// getWorld().schedule(this, delay);
|
// getWorld().schedule(this, delay);
|
||||||
@ -150,9 +146,6 @@ public class SimpleTrader extends AbstractTrader implements EventListener {
|
|||||||
|
|
||||||
double limit = 253.871239;
|
double limit = 253.871239;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long receive(Event task) {
|
public long receive(Event task) {
|
||||||
// System.out.printf("Here we are !!! %f\n", getWorld().randNextFloat(12f, 27f));
|
// System.out.printf("Here we are !!! %f\n", getWorld().randNextFloat(12f, 27f));
|
||||||
|
@ -428,8 +428,12 @@ class TradingEngine implements TradingAPI {
|
|||||||
|
|
||||||
double order_limit;
|
double order_limit;
|
||||||
|
|
||||||
|
if (account.isUnlimied()) {
|
||||||
|
order_limit = l;
|
||||||
|
} else {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case BUYLIMIT: {
|
case BUYLIMIT: {
|
||||||
|
|
||||||
// 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();
|
||||||
|
|
||||||
@ -491,6 +495,7 @@ class TradingEngine implements TradingAPI {
|
|||||||
return null;
|
return null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
o = new Order(this, account, type, v, order_limit);
|
o = new Order(this, account, type, v, order_limit);
|
||||||
|
|
||||||
@ -500,59 +505,167 @@ class TradingEngine implements TradingAPI {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
executeOrders();
|
executeOrders();
|
||||||
// last_quote.price = 200;
|
// last_quote.price = 200;
|
||||||
for (FiringEvent e : book_listener) {
|
for (FiringEvent e : book_listener
|
||||||
|
|
||||||
|
|
||||||
|
) {
|
||||||
e.fire();
|
e.fire();
|
||||||
}
|
}
|
||||||
return o;
|
return o ;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
HashSet<FiringEvent> book_listener = new HashSet<>();
|
HashSet
|
||||||
|
|
||||||
|
<FiringEvent
|
||||||
|
|
||||||
|
> book_listener
|
||||||
|
|
||||||
|
= new HashSet
|
||||||
|
|
||||||
|
<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addOrderBookListener(EventListener listener) {
|
public
|
||||||
book_listener.add(new FiringEvent(listener));
|
|
||||||
|
|
||||||
}
|
void addOrderBookListener
|
||||||
|
|
||||||
@Override
|
(EventListener
|
||||||
public Set getOrderBook(Order.Type type) {
|
|
||||||
switch (type) {
|
|
||||||
case BUYLIMIT:
|
|
||||||
case BUY:
|
|
||||||
return Collections.unmodifiableSet(bidbook);
|
|
||||||
|
|
||||||
case SELLLIMIT:
|
listener
|
||||||
case SELL:
|
|
||||||
return Collections.unmodifiableSet(askbook);
|
|
||||||
|
|
||||||
}
|
) {
|
||||||
return null;
|
book_listener
|
||||||
}
|
|
||||||
|
.add
|
||||||
|
|
||||||
|
(new FiringEvent
|
||||||
|
|
||||||
|
(listener
|
||||||
|
|
||||||
|
));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set
|
public Set
|
||||||
getBidBook() {
|
|
||||||
return getOrderBook(Order.Type.BUYLIMIT
|
|
||||||
);
|
|
||||||
|
|
||||||
}
|
getOrderBook
|
||||||
|
|
||||||
@Override
|
(Order
|
||||||
public Set
|
|
||||||
getAskBook() {
|
|
||||||
return getOrderBook(Order.Type.SELL
|
|
||||||
);
|
|
||||||
|
|
||||||
}
|
.Type
|
||||||
|
|
||||||
@Override
|
type
|
||||||
public Set<Quote> getQuoteHistory() {
|
|
||||||
|
) {
|
||||||
|
switch (type
|
||||||
|
|
||||||
|
) {
|
||||||
|
case BUYLIMIT
|
||||||
|
|
||||||
|
:
|
||||||
|
case BUY
|
||||||
|
|
||||||
|
:
|
||||||
return Collections
|
return Collections
|
||||||
.unmodifiableSet(quote_history
|
|
||||||
);
|
.unmodifiableSet
|
||||||
|
|
||||||
|
(bidbook
|
||||||
|
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
case SELLLIMIT
|
||||||
|
|
||||||
|
:
|
||||||
|
case SELL
|
||||||
|
|
||||||
|
:
|
||||||
|
return Collections
|
||||||
|
|
||||||
|
.unmodifiableSet
|
||||||
|
|
||||||
|
(askbook
|
||||||
|
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set
|
||||||
|
|
||||||
|
|
||||||
|
getBidBook
|
||||||
|
|
||||||
|
() {
|
||||||
|
return getOrderBook
|
||||||
|
|
||||||
|
(Order
|
||||||
|
|
||||||
|
.Type
|
||||||
|
|
||||||
|
.BUYLIMIT
|
||||||
|
|
||||||
|
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set
|
||||||
|
|
||||||
|
|
||||||
|
getAskBook
|
||||||
|
|
||||||
|
() {
|
||||||
|
return getOrderBook
|
||||||
|
|
||||||
|
(Order
|
||||||
|
|
||||||
|
.Type
|
||||||
|
|
||||||
|
.SELL
|
||||||
|
|
||||||
|
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set
|
||||||
|
|
||||||
|
<Quote
|
||||||
|
|
||||||
|
> getQuoteHistory
|
||||||
|
|
||||||
|
() {
|
||||||
|
return Collections
|
||||||
|
|
||||||
|
|
||||||
|
.unmodifiableSet
|
||||||
|
|
||||||
|
(quote_history
|
||||||
|
|
||||||
|
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -52,6 +52,7 @@ public interface World {
|
|||||||
|
|
||||||
public Exchange getDefaultExchange();
|
public Exchange getDefaultExchange();
|
||||||
public AssetPair getDefaultAssetPair();
|
public AssetPair getDefaultAssetPair();
|
||||||
|
public AbstractAsset getDefaultCurrency();
|
||||||
|
|
||||||
Collection<Trader> getTradersCollection();
|
Collection<Trader> getTradersCollection();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user