Fix wrong ask/bid usage. add rounding

This commit is contained in:
Manfred Karrer 2016-04-16 12:21:53 +02:00
parent 1434d37733
commit 1fecca009a
2 changed files with 24 additions and 14 deletions

View File

@ -107,8 +107,14 @@ public final class Offer implements StoragePayload, RequiresOwnerIsOnlinePayload
private final String id;
private final long date;
private final long protocolVersion;
// Price if fixed price is used (usePercentageBasedPrice = false)
private final long fiatPrice;
private final double marketPriceMargin;
// Distance form market price if percentage based price is used (usePercentageBasedPrice = true).
// E.g. 0.1 -> 10%. Can be negative as well. Depending on direction the marketPriceMargin is above or below the market price.
// Positive values is always the usual case where you want a better price as the market.
// E.g. Buy offer with market price 400.- leads to a 360.- price.
// Sell offer with market price 400.- leads to a 440.- price.
private final double marketPriceMargin;
private final boolean usePercentageBasedPrice;
private final long amount;
private final long minAmount;
@ -331,7 +337,7 @@ public final class Offer implements StoragePayload, RequiresOwnerIsOnlinePayload
if (usePercentageBasedPrice && priceFeed != null) {
MarketPrice marketPrice = priceFeed.getMarketPrice(currencyCode);
if (marketPrice != null) {
PriceFeed.Type priceFeedType = direction == Direction.SELL ? PriceFeed.Type.ASK : PriceFeed.Type.BID;
PriceFeed.Type priceFeedType = direction == Direction.BUY ? PriceFeed.Type.ASK : PriceFeed.Type.BID;
double marketPriceAsDouble = marketPrice.getPrice(priceFeedType);
double factor = direction == Offer.Direction.BUY ? 1 - marketPriceMargin : 1 + marketPriceMargin;
double targetPrice = marketPriceAsDouble * factor;

View File

@ -243,17 +243,20 @@ class CreateOfferViewModel extends ActivatableWithDataModel<CreateOfferDataModel
if (!priceAsPercentageIsInput) {
MarketPrice marketPrice = priceFeed.getMarketPrice(dataModel.tradeCurrencyCode.get());
if (marketPrice != null) {
double marketPriceAsDouble = marketPrice.getPrice(priceFeedType);
double marketPriceAsDouble = formatter.roundDouble(marketPrice.getPrice(priceFeedType), 2);
try {
double priceAsDouble = formatter.parseNumberStringToDouble(price.get());
double priceFactor = priceAsDouble / marketPriceAsDouble;
priceFactor = dataModel.getDirection() == Offer.Direction.BUY ? 1 - priceFactor : 1 + priceFactor;
priceAsPercentage.set(formatter.formatToPercent(priceFactor, 2));
double relation = priceAsDouble / marketPriceAsDouble;
relation = formatter.roundDouble(relation, 2);
double marketPriceMargin = dataModel.getDirection() == Offer.Direction.BUY ? 1 - relation : 1 + relation;
priceAsPercentage.set(formatter.formatToPercent(marketPriceMargin, 2));
} catch (NumberFormatException t) {
priceAsPercentage.set("");
new Popup().warning("Your input is not a valid number.")
.show();
}
} else {
log.warn("We don't have a market price. We use the static price instead.");
}
}
}
@ -263,8 +266,8 @@ class CreateOfferViewModel extends ActivatableWithDataModel<CreateOfferDataModel
if (priceAsPercentageIsInput) {
try {
if (!newValue.isEmpty() && !newValue.equals("-")) {
double percentageBasedPrice = formatter.parsePercentStringToDouble(newValue);
if (percentageBasedPrice >= 1 || percentageBasedPrice <= -1) {
double marketPriceMargin = formatter.parsePercentStringToDouble(newValue);
if (marketPriceMargin >= 1 || marketPriceMargin <= -1) {
dataModel.setPercentageBasedPrice(0);
UserThread.execute(() -> priceAsPercentage.set("0"));
new Popup().warning("You cannot set a percentage of 100% or larger. Please enter a percentage number like \"5.4\" for 5.4%")
@ -272,11 +275,12 @@ class CreateOfferViewModel extends ActivatableWithDataModel<CreateOfferDataModel
} else {
MarketPrice marketPrice = priceFeed.getMarketPrice(dataModel.tradeCurrencyCode.get());
if (marketPrice != null) {
percentageBasedPrice = formatter.roundDouble(percentageBasedPrice, 4);
dataModel.setPercentageBasedPrice(percentageBasedPrice);
double marketPriceAsDouble = marketPrice.getPrice(priceFeedType);
double factor = dataModel.getDirection() == Offer.Direction.BUY ? 1 - percentageBasedPrice : 1 + percentageBasedPrice;
double targetPrice = marketPriceAsDouble * factor;
marketPriceMargin = formatter.roundDouble(marketPriceMargin, 4);
dataModel.setPercentageBasedPrice(marketPriceMargin);
Offer.Direction direction = dataModel.getDirection();
double marketPriceAsDouble = formatter.roundDouble(marketPrice.getPrice(priceFeedType), 2);
double factor = direction == Offer.Direction.BUY ? 1 - marketPriceMargin : 1 + marketPriceMargin;
double targetPrice = formatter.roundDouble(marketPriceAsDouble * factor, 2);
price.set(formatter.formatToNumberString(targetPrice, 2));
setPriceToModel();
calculateVolume();
@ -376,7 +380,7 @@ class CreateOfferViewModel extends ActivatableWithDataModel<CreateOfferDataModel
if (dataModel.paymentAccount != null)
btcValidator.setMaxTradeLimitInBitcoin(dataModel.paymentAccount.getPaymentMethod().getMaxTradeLimit());
priceFeedType = direction == Offer.Direction.SELL ? PriceFeed.Type.ASK : PriceFeed.Type.BID;
priceFeedType = direction == Offer.Direction.BUY ? PriceFeed.Type.ASK : PriceFeed.Type.BID;
return result;
}