-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deactivate open offer if trigger price is reached #5001
Merged
ripcurlx
merged 17 commits into
bisq-network:master
from
chimp1984:deactivate-open-offer-if-trigger-price-is-reached
Dec 28, 2020
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
b36802d
Add percentage price to open offer view
chimp1984 d1c8718
Merge branch 'add-percentage-price-to-open-offer-view' into deactivat…
chimp1984 8ac81d9
Refactor InfoInputTextField
chimp1984 99192e7
Apply formatting (no code change)
chimp1984 7529847
Add getRegularIconButton method
chimp1984 0e6b983
Add triggerPrice
chimp1984 229d013
Add PriceEventHandler
chimp1984 e5957cd
Set priceTypeToggleButton invisible once we move to funds section
chimp1984 bb9de0b
Add trigger price field to create offer view
chimp1984 f096351
Make rows smaller
chimp1984 39893a6
Add trigger price to OpenOffersView
chimp1984 64607de
Add trigger icon column
chimp1984 2a86d5b
Add triggerPrice to csv
chimp1984 57c1ad2
Add dontShowAgain to edit offer success popup
chimp1984 4f96ade
Rename wrong params
chimp1984 1336cbb
Fix test
chimp1984 681fed6
Fix log
chimp1984 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* 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.desktop.main; | ||
|
||
import bisq.core.locale.CurrencyUtil; | ||
import bisq.core.monetary.Altcoin; | ||
import bisq.core.monetary.Price; | ||
import bisq.core.offer.Offer; | ||
import bisq.core.offer.OfferPayload; | ||
import bisq.core.provider.price.MarketPrice; | ||
import bisq.core.provider.price.PriceFeedService; | ||
import bisq.core.trade.statistics.TradeStatisticsManager; | ||
import bisq.core.user.Preferences; | ||
import bisq.core.util.AveragePriceUtil; | ||
|
||
import bisq.common.util.MathUtils; | ||
|
||
import org.bitcoinj.utils.Fiat; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
import java.util.Optional; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import static bisq.desktop.main.shared.ChatView.log; | ||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
@Singleton | ||
public class PriceUtil { | ||
private final PriceFeedService priceFeedService; | ||
private final TradeStatisticsManager tradeStatisticsManager; | ||
private final Preferences preferences; | ||
@Nullable | ||
private Price bsq30DayAveragePrice; | ||
|
||
@Inject | ||
public PriceUtil(PriceFeedService priceFeedService, | ||
TradeStatisticsManager tradeStatisticsManager, | ||
Preferences preferences) { | ||
this.priceFeedService = priceFeedService; | ||
this.tradeStatisticsManager = tradeStatisticsManager; | ||
this.preferences = preferences; | ||
} | ||
|
||
public void recalculateBsq30DayAveragePrice() { | ||
bsq30DayAveragePrice = null; | ||
bsq30DayAveragePrice = getBsq30DayAveragePrice(); | ||
} | ||
|
||
public Price getBsq30DayAveragePrice() { | ||
if (bsq30DayAveragePrice == null) { | ||
bsq30DayAveragePrice = AveragePriceUtil.getAveragePriceTuple(preferences, | ||
tradeStatisticsManager, 30).second; | ||
} | ||
return bsq30DayAveragePrice; | ||
} | ||
|
||
public boolean hasMarketPrice(Offer offer) { | ||
String currencyCode = offer.getCurrencyCode(); | ||
checkNotNull(priceFeedService, "priceFeed must not be null"); | ||
MarketPrice marketPrice = priceFeedService.getMarketPrice(currencyCode); | ||
Price price = offer.getPrice(); | ||
return price != null && marketPrice != null && marketPrice.isRecentExternalPriceAvailable(); | ||
} | ||
|
||
public Optional<Double> getMarketBasedPrice(Offer offer, | ||
OfferPayload.Direction direction) { | ||
if (offer.isUseMarketBasedPrice()) { | ||
return Optional.of(offer.getMarketPriceMargin()); | ||
} | ||
|
||
if (!hasMarketPrice(offer)) { | ||
if (offer.getCurrencyCode().equals("BSQ")) { | ||
Price bsq30DayAveragePrice = getBsq30DayAveragePrice(); | ||
if (bsq30DayAveragePrice.isPositive()) { | ||
double scaled = MathUtils.scaleDownByPowerOf10(bsq30DayAveragePrice.getValue(), 8); | ||
return calculatePercentage(offer, scaled, direction); | ||
} else { | ||
return Optional.empty(); | ||
} | ||
} else { | ||
log.trace("We don't have a market price. " + | ||
"That case could only happen if you don't have a price feed."); | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
String currencyCode = offer.getCurrencyCode(); | ||
checkNotNull(priceFeedService, "priceFeed must not be null"); | ||
MarketPrice marketPrice = priceFeedService.getMarketPrice(currencyCode); | ||
double marketPriceAsDouble = checkNotNull(marketPrice).getPrice(); | ||
return calculatePercentage(offer, marketPriceAsDouble, direction); | ||
} | ||
|
||
public Optional<Double> calculatePercentage(Offer offer, | ||
double marketPrice, | ||
OfferPayload.Direction direction) { | ||
// If the offer did not use % price we calculate % from current market price | ||
String currencyCode = offer.getCurrencyCode(); | ||
Price price = offer.getPrice(); | ||
int precision = CurrencyUtil.isCryptoCurrency(currencyCode) ? | ||
Altcoin.SMALLEST_UNIT_EXPONENT : | ||
Fiat.SMALLEST_UNIT_EXPONENT; | ||
long priceAsLong = checkNotNull(price).getValue(); | ||
double scaled = MathUtils.scaleDownByPowerOf10(priceAsLong, precision); | ||
double value; | ||
if (direction == OfferPayload.Direction.SELL) { | ||
if (CurrencyUtil.isFiatCurrency(currencyCode)) { | ||
if (marketPrice == 0) { | ||
return Optional.empty(); | ||
} | ||
value = 1 - scaled / marketPrice; | ||
} else { | ||
if (marketPrice == 1) { | ||
return Optional.empty(); | ||
} | ||
value = scaled / marketPrice - 1; | ||
} | ||
} else { | ||
if (CurrencyUtil.isFiatCurrency(currencyCode)) { | ||
if (marketPrice == 1) { | ||
return Optional.empty(); | ||
} | ||
value = scaled / marketPrice - 1; | ||
} else { | ||
if (marketPrice == 0) { | ||
return Optional.empty(); | ||
} | ||
value = 1 - scaled / marketPrice; | ||
} | ||
} | ||
return Optional.of(value); | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I instinctively need to ask if
PriceUtil
could be moved to core. I see dependencies on ChatView.log and ui validators, but much of the util logic could be made available to the api.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I was thinking the same... was just to lazy to separate pure domain code with formatting stuff which is using desktop scope classes... But yes we should refactor that.