-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5001 from chimp1984/deactivate-open-offer-if-trig…
…ger-price-is-reached Deactivate open offer if trigger price is reached
- Loading branch information
Showing
34 changed files
with
970 additions
and
337 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
core/src/main/java/bisq/core/offer/TriggerPriceService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* | ||
* This file is part of Bisq. | ||
* | ||
* Bisq is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or (at | ||
* your option) any later version. | ||
* | ||
* Bisq is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public | ||
* License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Bisq. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package bisq.core.offer; | ||
|
||
import bisq.core.locale.CurrencyUtil; | ||
import bisq.core.monetary.Altcoin; | ||
import bisq.core.monetary.Price; | ||
import bisq.core.provider.price.MarketPrice; | ||
import bisq.core.provider.price.PriceFeedService; | ||
|
||
import bisq.common.util.MathUtils; | ||
|
||
import org.bitcoinj.utils.Fiat; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
import javafx.collections.ListChangeListener; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import static bisq.common.util.MathUtils.roundDoubleToLong; | ||
import static bisq.common.util.MathUtils.scaleUpByPowerOf10; | ||
|
||
@Slf4j | ||
@Singleton | ||
public class TriggerPriceService { | ||
private final OpenOfferManager openOfferManager; | ||
private final PriceFeedService priceFeedService; | ||
private final Map<String, Set<OpenOffer>> openOffersByCurrency = new HashMap<>(); | ||
|
||
@Inject | ||
public TriggerPriceService(OpenOfferManager openOfferManager, PriceFeedService priceFeedService) { | ||
this.openOfferManager = openOfferManager; | ||
this.priceFeedService = priceFeedService; | ||
} | ||
|
||
public void onAllServicesInitialized() { | ||
openOfferManager.getObservableList().addListener((ListChangeListener<OpenOffer>) c -> { | ||
c.next(); | ||
if (c.wasAdded()) { | ||
onAddedOpenOffers(c.getAddedSubList()); | ||
} | ||
if (c.wasRemoved()) { | ||
onRemovedOpenOffers(c.getRemoved()); | ||
} | ||
}); | ||
onAddedOpenOffers(openOfferManager.getObservableList()); | ||
|
||
priceFeedService.updateCounterProperty().addListener((observable, oldValue, newValue) -> onPriceFeedChanged()); | ||
onPriceFeedChanged(); | ||
} | ||
|
||
private void onPriceFeedChanged() { | ||
openOffersByCurrency.keySet().stream() | ||
.map(priceFeedService::getMarketPrice) | ||
.filter(Objects::nonNull) | ||
.filter(marketPrice -> openOffersByCurrency.containsKey(marketPrice.getCurrencyCode())) | ||
.forEach(marketPrice -> { | ||
openOffersByCurrency.get(marketPrice.getCurrencyCode()).stream() | ||
.filter(openOffer -> !openOffer.isDeactivated()) | ||
.forEach(openOffer -> checkPriceThreshold(marketPrice, openOffer)); | ||
}); | ||
} | ||
|
||
public static boolean wasTriggered(MarketPrice marketPrice, OpenOffer openOffer) { | ||
Price price = openOffer.getOffer().getPrice(); | ||
if (price == null) { | ||
return false; | ||
} | ||
|
||
String currencyCode = openOffer.getOffer().getCurrencyCode(); | ||
boolean cryptoCurrency = CurrencyUtil.isCryptoCurrency(currencyCode); | ||
int smallestUnitExponent = cryptoCurrency ? | ||
Altcoin.SMALLEST_UNIT_EXPONENT : | ||
Fiat.SMALLEST_UNIT_EXPONENT; | ||
long marketPriceAsLong = roundDoubleToLong( | ||
scaleUpByPowerOf10(marketPrice.getPrice(), smallestUnitExponent)); | ||
long triggerPrice = openOffer.getTriggerPrice(); | ||
if (triggerPrice <= 0) { | ||
return false; | ||
} | ||
|
||
OfferPayload.Direction direction = openOffer.getOffer().getDirection(); | ||
boolean isSellOffer = direction == OfferPayload.Direction.SELL; | ||
boolean condition = isSellOffer && !cryptoCurrency || !isSellOffer && cryptoCurrency; | ||
return condition ? | ||
marketPriceAsLong < triggerPrice : | ||
marketPriceAsLong > triggerPrice; | ||
} | ||
|
||
private void checkPriceThreshold(MarketPrice marketPrice, OpenOffer openOffer) { | ||
if (wasTriggered(marketPrice, openOffer)) { | ||
String currencyCode = openOffer.getOffer().getCurrencyCode(); | ||
int smallestUnitExponent = CurrencyUtil.isCryptoCurrency(currencyCode) ? | ||
Altcoin.SMALLEST_UNIT_EXPONENT : | ||
Fiat.SMALLEST_UNIT_EXPONENT; | ||
long triggerPrice = openOffer.getTriggerPrice(); | ||
|
||
log.info("Market price exceeded the trigger price of the open offer.\n" + | ||
"We deactivate the open offer with ID {}.\nCurrency: {};\nOffer direction: {};\n" + | ||
"Market price: {};\nTrigger price: {}", | ||
openOffer.getOffer().getShortId(), | ||
currencyCode, | ||
openOffer.getOffer().getDirection(), | ||
marketPrice.getPrice(), | ||
MathUtils.scaleDownByPowerOf10(triggerPrice, smallestUnitExponent) | ||
); | ||
|
||
openOfferManager.deactivateOpenOffer(openOffer, () -> { | ||
}, errorMessage -> { | ||
}); | ||
} | ||
} | ||
|
||
private void onAddedOpenOffers(List<? extends OpenOffer> openOffers) { | ||
openOffers.forEach(openOffer -> { | ||
String currencyCode = openOffer.getOffer().getCurrencyCode(); | ||
openOffersByCurrency.putIfAbsent(currencyCode, new HashSet<>()); | ||
openOffersByCurrency.get(currencyCode).add(openOffer); | ||
|
||
MarketPrice marketPrice = priceFeedService.getMarketPrice(openOffer.getOffer().getCurrencyCode()); | ||
if (marketPrice != null) { | ||
checkPriceThreshold(marketPrice, openOffer); | ||
} | ||
}); | ||
} | ||
|
||
private void onRemovedOpenOffers(List<? extends OpenOffer> openOffers) { | ||
openOffers.forEach(openOffer -> { | ||
String currencyCode = openOffer.getOffer().getCurrencyCode(); | ||
if (openOffersByCurrency.containsKey(currencyCode)) { | ||
Set<OpenOffer> set = openOffersByCurrency.get(currencyCode); | ||
set.remove(openOffer); | ||
if (set.isEmpty()) { | ||
openOffersByCurrency.remove(currencyCode); | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.