use of io.bitsquare.btc.pricefeed.MarketPrice in project bitsquare by bitsquare.
the class MainViewModel method setMarketPriceInItems.
private void setMarketPriceInItems() {
priceFeedComboBoxItems.stream().forEach(item -> {
String currencyCode = item.currencyCode;
MarketPrice marketPrice = priceFeedService.getMarketPrice(currencyCode);
String priceString;
if (marketPrice != null) {
double price = marketPrice.getPrice(priceFeedService.getType());
if (price != 0) {
priceString = formatter.formatMarketPrice(price, currencyCode);
item.setIsPriceAvailable(true);
} else {
priceString = "N/A";
item.setIsPriceAvailable(false);
}
} else {
priceString = "N/A";
item.setIsPriceAvailable(false);
}
item.setDisplayString(formatter.getCurrencyPair(currencyCode) + ": " + priceString);
});
}
use of io.bitsquare.btc.pricefeed.MarketPrice in project bitsquare by bitsquare.
the class Offer method getPrice.
@Nullable
public Fiat getPrice() {
if (useMarketBasedPrice) {
checkNotNull(priceFeedService, "priceFeed must not be null");
MarketPrice marketPrice = priceFeedService.getMarketPrice(currencyCode);
if (marketPrice != null) {
PriceFeedService.Type priceFeedType;
double factor;
if (CurrencyUtil.isCryptoCurrency(currencyCode)) {
priceFeedType = direction == Direction.BUY ? PriceFeedService.Type.ASK : PriceFeedService.Type.BID;
factor = direction == Offer.Direction.SELL ? 1 - marketPriceMargin : 1 + marketPriceMargin;
} else {
priceFeedType = direction == Direction.SELL ? PriceFeedService.Type.ASK : PriceFeedService.Type.BID;
factor = direction == Offer.Direction.BUY ? 1 - marketPriceMargin : 1 + marketPriceMargin;
}
double marketPriceAsDouble = marketPrice.getPrice(priceFeedType);
double targetPrice = marketPriceAsDouble * factor;
if (CurrencyUtil.isCryptoCurrency(currencyCode))
targetPrice = targetPrice != 0 ? 1d / targetPrice : 0;
try {
final double rounded = MathUtils.roundDouble(targetPrice, Fiat.SMALLEST_UNIT_EXPONENT);
return Fiat.parseFiat(currencyCode, decimalFormat.format(rounded).replace(",", "."));
} catch (Exception e) {
log.error("Exception at getPrice / parseToFiat: " + e.toString() + "\n" + "That case should never happen.");
return null;
}
} else {
log.debug("We don't have a market price.\n" + "That case could only happen if you don't have a price feed.");
return null;
}
} else {
return Fiat.valueOf(currencyCode, fiatPrice);
}
}
use of io.bitsquare.btc.pricefeed.MarketPrice in project bitsquare by bitsquare.
the class SpreadViewModel method update.
private void update(ObservableList<OfferBookListItem> offerBookListItems) {
Map<String, List<Offer>> offersByCurrencyMap = new HashMap<>();
for (OfferBookListItem offerBookListItem : offerBookListItems) {
Offer offer = offerBookListItem.getOffer();
String currencyCode = offer.getCurrencyCode();
if (!offersByCurrencyMap.containsKey(currencyCode))
offersByCurrencyMap.put(currencyCode, new ArrayList<>());
offersByCurrencyMap.get(currencyCode).add(offer);
}
spreadItems.clear();
for (String currencyCode : offersByCurrencyMap.keySet()) {
List<Offer> offers = offersByCurrencyMap.get(currencyCode);
List<Offer> buyOffers = offers.stream().filter(e -> e.getDirection().equals(Offer.Direction.BUY)).sorted((o1, o2) -> {
long a = o1.getPrice() != null ? o1.getPrice().value : 0;
long b = o2.getPrice() != null ? o2.getPrice().value : 0;
if (a != b)
return a < b ? 1 : -1;
return 0;
}).collect(Collectors.toList());
List<Offer> sellOffers = offers.stream().filter(e -> e.getDirection().equals(Offer.Direction.SELL)).sorted((o1, o2) -> {
long a = o1.getPrice() != null ? o1.getPrice().value : 0;
long b = o2.getPrice() != null ? o2.getPrice().value : 0;
if (a != b)
return a > b ? 1 : -1;
return 0;
}).collect(Collectors.toList());
Fiat spread = null;
String percentage = "";
Fiat bestSellOfferPrice = sellOffers.isEmpty() ? null : sellOffers.get(0).getPrice();
Fiat bestBuyOfferPrice = buyOffers.isEmpty() ? null : buyOffers.get(0).getPrice();
if (bestBuyOfferPrice != null && bestSellOfferPrice != null) {
MarketPrice marketPrice = priceFeedService.getMarketPrice(currencyCode);
// happens again
try {
spread = bestSellOfferPrice.subtract(bestBuyOfferPrice);
if (spread != null && marketPrice != null) {
double marketPriceAsDouble = marketPrice.getPrice(PriceFeedService.Type.LAST);
if (CurrencyUtil.isFiatCurrency(currencyCode)) {
double result = ((double) spread.value / 10000D) / marketPriceAsDouble;
percentage = " (" + formatter.formatPercentagePrice(result) + ")";
} else {
final double spreadAsDouble = spread.value != 0 ? 10000D / spread.value : 0;
double result = marketPriceAsDouble / spreadAsDouble;
percentage = " (" + formatter.formatPercentagePrice(result) + ")";
}
}
} catch (Throwable t) {
try {
String msg = "An error occurred at the spread calculation.\n" + "Error msg: " + t.toString() + "\n" + "Details of offer data: \n" + "bestSellOfferPrice: " + bestSellOfferPrice.value + "\n" + "bestBuyOfferPrice: " + bestBuyOfferPrice.value + "\n" + "sellOffer getCurrencyCode: " + sellOffers.get(0).getCurrencyCode() + "\n" + "buyOffer getCurrencyCode: " + buyOffers.get(0).getCurrencyCode() + "\n\n" + "Please copy and paste this data and send it to the developers so they can investigate the issue.";
new Popup().error(msg).show();
log.error(t.toString());
t.printStackTrace();
} catch (Throwable t2) {
log.error(t2.toString());
t2.printStackTrace();
}
}
}
Coin totalAmount = Coin.valueOf(offers.stream().mapToLong(offer -> offer.getAmount().getValue()).sum());
spreadItems.add(new SpreadItem(currencyCode, buyOffers.size(), sellOffers.size(), offers.size(), spread, percentage, totalAmount));
}
}
use of io.bitsquare.btc.pricefeed.MarketPrice in project bitsquare by bitsquare.
the class CreateOfferViewModel method createListeners.
private void createListeners() {
amountStringListener = (ov, oldValue, newValue) -> {
if (!ignoreAmountStringListener) {
if (isBtcInputValid(newValue).isValid) {
setAmountToModel();
dataModel.calculateVolume();
dataModel.calculateTotalToPay();
}
updateButtonDisableState();
}
};
minAmountStringListener = (ov, oldValue, newValue) -> {
if (isBtcInputValid(newValue).isValid)
setMinAmountToModel();
updateButtonDisableState();
};
priceStringListener = (ov, oldValue, newValue) -> {
if (!ignorePriceStringListener) {
if (isPriceInputValid(newValue).isValid) {
setPriceToModel();
dataModel.calculateVolume();
dataModel.calculateTotalToPay();
if (!inputIsMarketBasedPrice) {
final String currencyCode = dataModel.tradeCurrencyCode.get();
MarketPrice marketPrice = priceFeedService.getMarketPrice(currencyCode);
if (marketPrice != null) {
double marketPriceAsDouble = marketPrice.getPrice(getPriceFeedType());
try {
double priceAsDouble = formatter.parseNumberStringToDouble(price.get());
double relation = priceAsDouble / marketPriceAsDouble;
double percentage;
if (CurrencyUtil.isCryptoCurrency(currencyCode))
percentage = dataModel.getDirection() == Offer.Direction.SELL ? 1 - relation : relation - 1;
else
percentage = dataModel.getDirection() == Offer.Direction.BUY ? 1 - relation : relation - 1;
percentage = MathUtils.roundDouble(percentage, 4);
marketPriceMargin.set(formatter.formatToPercent(percentage));
} catch (NumberFormatException t) {
marketPriceMargin.set("");
new Popup().warning("Input is not a valid number.").show();
}
} else {
log.debug("We don't have a market price. We use the static price instead.");
}
}
}
updateButtonDisableState();
}
};
marketPriceMarginStringListener = (ov, oldValue, newValue) -> {
if (inputIsMarketBasedPrice) {
try {
if (!newValue.isEmpty() && !newValue.equals("-")) {
double percentage = formatter.parsePercentStringToDouble(newValue);
if (percentage >= 1 || percentage <= -1) {
new Popup().warning("You cannot set a percentage of 100% or larger. Please enter a percentage number like \"5.4\" for 5.4%").show();
} else {
final String currencyCode = dataModel.tradeCurrencyCode.get();
MarketPrice marketPrice = priceFeedService.getMarketPrice(currencyCode);
if (marketPrice != null) {
percentage = MathUtils.roundDouble(percentage, 4);
dataModel.setMarketPriceMargin(percentage);
double marketPriceAsDouble = marketPrice.getPrice(getPriceFeedType());
double factor;
if (CurrencyUtil.isCryptoCurrency(currencyCode))
factor = dataModel.getDirection() == Offer.Direction.SELL ? 1 - percentage : 1 + percentage;
else
factor = dataModel.getDirection() == Offer.Direction.BUY ? 1 - percentage : 1 + percentage;
double targetPrice = marketPriceAsDouble * factor;
int precision = CurrencyUtil.isCryptoCurrency(currencyCode) ? Altcoin.SMALLEST_UNIT_EXPONENT : 2;
ignorePriceStringListener = true;
price.set(formatter.formatRoundedDoubleWithPrecision(targetPrice, precision));
ignorePriceStringListener = false;
setPriceToModel();
dataModel.calculateVolume();
dataModel.calculateTotalToPay();
updateButtonDisableState();
} else {
new Popup().warning("There is no price feed available for that currency. You cannot use a percent based price.\n" + "Please select the fixed price.").show();
marketPriceMargin.set("");
}
}
}
} catch (Throwable t) {
new Popup().warning("Your input is not a valid number. Please enter a percentage number like \"5.4\" for 5.4%").show();
}
}
};
useMarketBasedPriceListener = (observable, oldValue, newValue) -> {
if (newValue)
priceValidationResult.set(new InputValidator.ValidationResult(true));
};
volumeStringListener = (ov, oldValue, newValue) -> {
if (!ignoreVolumeStringListener) {
if (isVolumeInputValid(newValue).isValid) {
setVolumeToModel();
setPriceToModel();
dataModel.calculateAmount();
dataModel.calculateTotalToPay();
}
updateButtonDisableState();
}
};
amountAsCoinListener = (ov, oldValue, newValue) -> {
if (newValue != null)
amount.set(formatter.formatCoin(newValue));
else
amount.set("");
};
minAmountAsCoinListener = (ov, oldValue, newValue) -> {
if (newValue != null)
minAmount.set(formatter.formatCoin(newValue));
else
minAmount.set("");
};
priceListener = (ov, oldValue, newValue) -> {
ignorePriceStringListener = true;
if (newValue != null)
price.set(newValue.toString());
else
price.set("");
ignorePriceStringListener = false;
};
volumeListener = (ov, oldValue, newValue) -> {
ignoreVolumeStringListener = true;
if (newValue != null)
volume.set(newValue.toString());
else
volume.set("");
ignoreVolumeStringListener = false;
};
isWalletFundedListener = (ov, oldValue, newValue) -> updateButtonDisableState();
/* feeFromFundingTxListener = (ov, oldValue, newValue) -> {
updateButtonDisableState();
};*/
}
Aggregations