mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-07-26 00:15:18 -04:00
Fix wrong ask/bid usage. add rounding
This commit is contained in:
parent
1434d37733
commit
1fecca009a
2 changed files with 24 additions and 14 deletions
|
@ -107,7 +107,13 @@ public final class Offer implements StoragePayload, RequiresOwnerIsOnlinePayload
|
||||||
private final String id;
|
private final String id;
|
||||||
private final long date;
|
private final long date;
|
||||||
private final long protocolVersion;
|
private final long protocolVersion;
|
||||||
|
// Price if fixed price is used (usePercentageBasedPrice = false)
|
||||||
private final long fiatPrice;
|
private final long fiatPrice;
|
||||||
|
// 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 double marketPriceMargin;
|
||||||
private final boolean usePercentageBasedPrice;
|
private final boolean usePercentageBasedPrice;
|
||||||
private final long amount;
|
private final long amount;
|
||||||
|
@ -331,7 +337,7 @@ public final class Offer implements StoragePayload, RequiresOwnerIsOnlinePayload
|
||||||
if (usePercentageBasedPrice && priceFeed != null) {
|
if (usePercentageBasedPrice && priceFeed != null) {
|
||||||
MarketPrice marketPrice = priceFeed.getMarketPrice(currencyCode);
|
MarketPrice marketPrice = priceFeed.getMarketPrice(currencyCode);
|
||||||
if (marketPrice != null) {
|
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 marketPriceAsDouble = marketPrice.getPrice(priceFeedType);
|
||||||
double factor = direction == Offer.Direction.BUY ? 1 - marketPriceMargin : 1 + marketPriceMargin;
|
double factor = direction == Offer.Direction.BUY ? 1 - marketPriceMargin : 1 + marketPriceMargin;
|
||||||
double targetPrice = marketPriceAsDouble * factor;
|
double targetPrice = marketPriceAsDouble * factor;
|
||||||
|
|
|
@ -243,17 +243,20 @@ class CreateOfferViewModel extends ActivatableWithDataModel<CreateOfferDataModel
|
||||||
if (!priceAsPercentageIsInput) {
|
if (!priceAsPercentageIsInput) {
|
||||||
MarketPrice marketPrice = priceFeed.getMarketPrice(dataModel.tradeCurrencyCode.get());
|
MarketPrice marketPrice = priceFeed.getMarketPrice(dataModel.tradeCurrencyCode.get());
|
||||||
if (marketPrice != null) {
|
if (marketPrice != null) {
|
||||||
double marketPriceAsDouble = marketPrice.getPrice(priceFeedType);
|
double marketPriceAsDouble = formatter.roundDouble(marketPrice.getPrice(priceFeedType), 2);
|
||||||
try {
|
try {
|
||||||
double priceAsDouble = formatter.parseNumberStringToDouble(price.get());
|
double priceAsDouble = formatter.parseNumberStringToDouble(price.get());
|
||||||
double priceFactor = priceAsDouble / marketPriceAsDouble;
|
double relation = priceAsDouble / marketPriceAsDouble;
|
||||||
priceFactor = dataModel.getDirection() == Offer.Direction.BUY ? 1 - priceFactor : 1 + priceFactor;
|
relation = formatter.roundDouble(relation, 2);
|
||||||
priceAsPercentage.set(formatter.formatToPercent(priceFactor, 2));
|
double marketPriceMargin = dataModel.getDirection() == Offer.Direction.BUY ? 1 - relation : 1 + relation;
|
||||||
|
priceAsPercentage.set(formatter.formatToPercent(marketPriceMargin, 2));
|
||||||
} catch (NumberFormatException t) {
|
} catch (NumberFormatException t) {
|
||||||
priceAsPercentage.set("");
|
priceAsPercentage.set("");
|
||||||
new Popup().warning("Your input is not a valid number.")
|
new Popup().warning("Your input is not a valid number.")
|
||||||
.show();
|
.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) {
|
if (priceAsPercentageIsInput) {
|
||||||
try {
|
try {
|
||||||
if (!newValue.isEmpty() && !newValue.equals("-")) {
|
if (!newValue.isEmpty() && !newValue.equals("-")) {
|
||||||
double percentageBasedPrice = formatter.parsePercentStringToDouble(newValue);
|
double marketPriceMargin = formatter.parsePercentStringToDouble(newValue);
|
||||||
if (percentageBasedPrice >= 1 || percentageBasedPrice <= -1) {
|
if (marketPriceMargin >= 1 || marketPriceMargin <= -1) {
|
||||||
dataModel.setPercentageBasedPrice(0);
|
dataModel.setPercentageBasedPrice(0);
|
||||||
UserThread.execute(() -> priceAsPercentage.set("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%")
|
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 {
|
} else {
|
||||||
MarketPrice marketPrice = priceFeed.getMarketPrice(dataModel.tradeCurrencyCode.get());
|
MarketPrice marketPrice = priceFeed.getMarketPrice(dataModel.tradeCurrencyCode.get());
|
||||||
if (marketPrice != null) {
|
if (marketPrice != null) {
|
||||||
percentageBasedPrice = formatter.roundDouble(percentageBasedPrice, 4);
|
marketPriceMargin = formatter.roundDouble(marketPriceMargin, 4);
|
||||||
dataModel.setPercentageBasedPrice(percentageBasedPrice);
|
dataModel.setPercentageBasedPrice(marketPriceMargin);
|
||||||
double marketPriceAsDouble = marketPrice.getPrice(priceFeedType);
|
Offer.Direction direction = dataModel.getDirection();
|
||||||
double factor = dataModel.getDirection() == Offer.Direction.BUY ? 1 - percentageBasedPrice : 1 + percentageBasedPrice;
|
double marketPriceAsDouble = formatter.roundDouble(marketPrice.getPrice(priceFeedType), 2);
|
||||||
double targetPrice = marketPriceAsDouble * factor;
|
double factor = direction == Offer.Direction.BUY ? 1 - marketPriceMargin : 1 + marketPriceMargin;
|
||||||
|
double targetPrice = formatter.roundDouble(marketPriceAsDouble * factor, 2);
|
||||||
price.set(formatter.formatToNumberString(targetPrice, 2));
|
price.set(formatter.formatToNumberString(targetPrice, 2));
|
||||||
setPriceToModel();
|
setPriceToModel();
|
||||||
calculateVolume();
|
calculateVolume();
|
||||||
|
@ -376,7 +380,7 @@ class CreateOfferViewModel extends ActivatableWithDataModel<CreateOfferDataModel
|
||||||
if (dataModel.paymentAccount != null)
|
if (dataModel.paymentAccount != null)
|
||||||
btcValidator.setMaxTradeLimitInBitcoin(dataModel.paymentAccount.getPaymentMethod().getMaxTradeLimit());
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue