binding function works now for short and long
with and without leverage
This commit is contained in:
parent
8f35e767bd
commit
236138a822
@ -1,4 +1,4 @@
|
|||||||
#Tue, 08 Jan 2019 09:22:34 +0100
|
#Tue, 08 Jan 2019 13:27:57 +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=
|
||||||
|
@ -109,6 +109,8 @@ public class Account {
|
|||||||
System.out.printf("Debth %f - Final: %f Return margin %f\n", d,f, f-d);
|
System.out.printf("Debth %f - Final: %f Return margin %f\n", d,f, f-d);
|
||||||
|
|
||||||
return f-d;*/
|
return f-d;*/
|
||||||
|
if (!this.isLeveraged())
|
||||||
|
return 0.0;
|
||||||
|
|
||||||
return this.getFinalBalance(currency) * getLeverage() + this.getFinalBalance(currency)
|
return this.getFinalBalance(currency) * getLeverage() + this.getFinalBalance(currency)
|
||||||
- this.getAssetDebt(world.getDefaultExchange(), currency);
|
- this.getAssetDebt(world.getDefaultExchange(), currency);
|
||||||
@ -222,7 +224,7 @@ public class Account {
|
|||||||
for (AbstractAsset a : assets.keySet()) {
|
for (AbstractAsset a : assets.keySet()) {
|
||||||
Double v;
|
Double v;
|
||||||
if (a.equals(currency)) {
|
if (a.equals(currency)) {
|
||||||
v = get(a,bound);
|
v = get(a, bound);
|
||||||
result += v;
|
result += v;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -230,7 +232,7 @@ public class Account {
|
|||||||
if (pair == null) {
|
if (pair == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
v = get(a,bound);
|
v = get(a, bound);
|
||||||
|
|
||||||
if (v == 0.0) {
|
if (v == 0.0) {
|
||||||
continue;
|
continue;
|
||||||
@ -253,7 +255,7 @@ public class Account {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void addBound(AbstractAsset asset, Double vol) {
|
void addBound(AbstractAsset asset, Double vol) {
|
||||||
assets.put(asset, get(asset,false));
|
assets.put(asset, get(asset, false));
|
||||||
assets_bound.put(asset, getBound(asset) + vol);
|
assets_bound.put(asset, getBound(asset) + vol);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -280,6 +282,13 @@ public class Account {
|
|||||||
return getFinalBalance(world.getDefaultCurrency());
|
return getFinalBalance(world.getDefaultCurrency());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param ex
|
||||||
|
* @param asset
|
||||||
|
* @param currency
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public Double calcStopLoss(Exchange ex, AbstractAsset asset, AbstractAsset currency) {
|
public Double calcStopLoss(Exchange ex, AbstractAsset asset, AbstractAsset currency) {
|
||||||
Double e = (get(currency));
|
Double e = (get(currency));
|
||||||
for (AbstractAsset a : assets.keySet()) {
|
for (AbstractAsset a : assets.keySet()) {
|
||||||
@ -318,11 +327,27 @@ public class Account {
|
|||||||
return getLeverage() > 0.0;
|
return getLeverage() > 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bind asset which will be locked in an order.
|
||||||
|
*
|
||||||
|
* @param pair
|
||||||
|
* @param volume
|
||||||
|
* @param limit
|
||||||
|
* @return true if asset could be bound, false if assets couldn't be bound
|
||||||
|
*/
|
||||||
boolean bind(AssetPair pair, double volume, double limit) {
|
boolean bind(AssetPair pair, double volume, double limit) {
|
||||||
|
|
||||||
|
// Bind asset and currecy
|
||||||
|
this.addBound(pair.getAsset(), volume);
|
||||||
|
this.addBound(pair.getCurrency(), -(volume * limit));
|
||||||
|
|
||||||
if (this.isUnlimied()) {
|
if (this.isUnlimied()) {
|
||||||
|
// in case it is an unlimited account we can return
|
||||||
|
// true without further checks
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// checks for leveraged account
|
||||||
if (!this.isLeveraged()) {
|
if (!this.isLeveraged()) {
|
||||||
if (limit == 0.0) {
|
if (limit == 0.0) {
|
||||||
// an unlimited order is always considered to be
|
// an unlimited order is always considered to be
|
||||||
@ -333,32 +358,35 @@ public class Account {
|
|||||||
if (volume < 0) {
|
if (volume < 0) {
|
||||||
// It's a limited sell order, we have just to check
|
// It's a limited sell order, we have just to check
|
||||||
// if a sufficient amount of assets is available
|
// if a sufficient amount of assets is available
|
||||||
return get(pair.getAsset()) - volume >= 0;
|
|
||||||
|
if (get(pair.getAsset()) >= 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// unbind and return false
|
||||||
|
this.addBound(pair.getAsset(), -volume);
|
||||||
|
this.addBound(pair.getCurrency(), (volume * limit));
|
||||||
|
return false;
|
||||||
|
|
||||||
}
|
}
|
||||||
// Check if enough money is available to cover the
|
// Check if enough money is available to cover the
|
||||||
// entiere volume to by
|
// entiere volume to by
|
||||||
return get(pair.getCurrency()) >= limit * volume;
|
if (get(pair.getCurrency()) >= 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// unbind and return false
|
||||||
|
this.addBound(pair.getAsset(), -volume);
|
||||||
|
this.addBound(pair.getCurrency(), (volume * limit));
|
||||||
|
return false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// we are dealing here with a leveraged account
|
// we are dealing here with a leveraged account
|
||||||
|
|
||||||
System.out.printf("Add %f %f\n",volume,-volume*limit);
|
|
||||||
|
|
||||||
// bind asset and currecy
|
|
||||||
this.addBound(pair.getAsset(), volume);
|
|
||||||
this.addBound(pair.getCurrency(), -(volume * limit));
|
|
||||||
|
|
||||||
Double fb = this.getFinalBalance();
|
|
||||||
|
|
||||||
System.out.printf("FB: %f\n",fb);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Double margin = this.getMargin(pair.getCurrency());
|
Double margin = this.getMargin(pair.getCurrency());
|
||||||
if (margin >= 0)
|
if (margin >= 0) {
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// Unbind asset and currency
|
// Unbind asset and currency
|
||||||
this.addBound(pair.getAsset(), -volume);
|
this.addBound(pair.getAsset(), -volume);
|
||||||
this.addBound(pair.getCurrency(), (volume * limit));
|
this.addBound(pair.getCurrency(), (volume * limit));
|
||||||
|
@ -105,9 +105,9 @@ public class SimpleTrader extends AbstractTrader implements EventListener {
|
|||||||
|
|
||||||
pack = new AssetPack(p.getCurrency(), 1000);
|
pack = new AssetPack(p.getCurrency(), 1000);
|
||||||
account_b.add(pack);
|
account_b.add(pack);
|
||||||
account_b.setLeverage(9.0);
|
account_b.setLeverage(0.1);
|
||||||
|
|
||||||
pack = new AssetPack(p.getCurrency(), 1234567890);
|
pack = new AssetPack(p.getCurrency(), 10000);
|
||||||
account_1.add(pack);
|
account_1.add(pack);
|
||||||
account_1.setLeverage(0.0);
|
account_1.setLeverage(0.0);
|
||||||
account_1.setUnlimied(true);
|
account_1.setUnlimied(true);
|
||||||
@ -142,8 +142,10 @@ public class SimpleTrader extends AbstractTrader implements EventListener {
|
|||||||
getWorld().getAssetBySymbol("EUR"));
|
getWorld().getAssetBySymbol("EUR"));
|
||||||
TradingAPI mapi = ex.getAPI(msftp);
|
TradingAPI mapi = ex.getAPI(msftp);
|
||||||
|
|
||||||
Order ob = mapi.createOrder(account_b, Order.Type.SELLLIMIT, 50, 100);
|
Order ob = api.createOrder(account_b, Order.Type.SELLLIMIT, 10, 100);
|
||||||
Order oba = api.createOrder(account_b, Order.Type.BUYLIMIT, 50, 100);
|
|
||||||
|
|
||||||
|
Order oba = api.createOrder(account_1, Order.Type.BUYLIMIT, 10, 100);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -289,11 +289,19 @@ 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) {
|
|
||||||
//b.account.margin_bound-=avdiff;
|
|
||||||
b.account.margin_bound -= b.limit * volume;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Unbind
|
||||||
|
|
||||||
|
double bound = b.account.getBound(assetpair.getCurrency());
|
||||||
|
double addbound = volume*b.limit;
|
||||||
|
|
||||||
|
b.account.addBound(assetpair.getCurrency(), volume*b.limit);
|
||||||
|
b.account.addBound(assetpair.getAsset(), -volume);
|
||||||
|
|
||||||
|
a.account.addBound(assetpair.getCurrency(), -volume*b.limit);
|
||||||
|
a.account.addBound(assetpair.getAsset(), volume);
|
||||||
|
|
||||||
|
|
||||||
// b.account.addMarginAvail(assetpair.getCurrency(), avdiff/b.account.getLeverage());
|
// b.account.addMarginAvail(assetpair.getCurrency(), avdiff/b.account.getLeverage());
|
||||||
finishTrade(b, a, price, volume);
|
finishTrade(b, a, price, volume);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user