From 1fecca009a5edf4a8a408fead43c1790745359a7 Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Sat, 16 Apr 2016 12:21:53 +0200 Subject: [PATCH] Fix wrong ask/bid usage. add rounding --- .../java/io/bitsquare/trade/offer/Offer.java | 10 +++++-- .../createoffer/CreateOfferViewModel.java | 28 +++++++++++-------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/core/src/main/java/io/bitsquare/trade/offer/Offer.java b/core/src/main/java/io/bitsquare/trade/offer/Offer.java index 6c29fb63b2..289d505f7d 100644 --- a/core/src/main/java/io/bitsquare/trade/offer/Offer.java +++ b/core/src/main/java/io/bitsquare/trade/offer/Offer.java @@ -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; diff --git a/gui/src/main/java/io/bitsquare/gui/main/offer/createoffer/CreateOfferViewModel.java b/gui/src/main/java/io/bitsquare/gui/main/offer/createoffer/CreateOfferViewModel.java index e3981b2d13..1d3ea2dce2 100644 --- a/gui/src/main/java/io/bitsquare/gui/main/offer/createoffer/CreateOfferViewModel.java +++ b/gui/src/main/java/io/bitsquare/gui/main/offer/createoffer/CreateOfferViewModel.java @@ -243,17 +243,20 @@ class CreateOfferViewModel extends ActivatableWithDataModel= 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