common/math: Add math.Abs to PercentageDifference calculation (#1617)

* fix bug and add decimal calc

* pew pew

* Update common/math/math.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: nits

* nits: plus change name convention

* gk: nits and splits

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
Co-authored-by: Scott <gloriousCode@users.noreply.github.com>
This commit is contained in:
Ryan O'Hara-Reid
2024-11-27 10:27:15 +11:00
committed by GitHub
parent 0fb27542fe
commit a0d82f2a7d
5 changed files with 78 additions and 44 deletions

View File

@@ -46,7 +46,7 @@ func (b *Base) WhaleBomb(priceTarget float64, buy bool) (*WhaleBombResult, error
minPrice = action.ReferencePrice
maxPrice = action.TranchePositionPrice
amount = action.QuoteAmount
percent = math.CalculatePercentageGainOrLoss(action.TranchePositionPrice, action.ReferencePrice)
percent = math.PercentageChange(action.ReferencePrice, action.TranchePositionPrice)
status = fmt.Sprintf("Buying using %.2f %s worth of %s will send the price from %v to %v [%.2f%%] and impact %d price tranche(s). %s",
amount, b.Pair.Quote, b.Pair.Base, minPrice, maxPrice,
percent, len(action.Tranches), warning)
@@ -54,7 +54,7 @@ func (b *Base) WhaleBomb(priceTarget float64, buy bool) (*WhaleBombResult, error
minPrice = action.TranchePositionPrice
maxPrice = action.ReferencePrice
amount = action.BaseAmount
percent = math.CalculatePercentageGainOrLoss(action.TranchePositionPrice, action.ReferencePrice)
percent = math.PercentageChange(action.ReferencePrice, action.TranchePositionPrice)
status = fmt.Sprintf("Selling using %.2f %s worth of %s will send the price from %v to %v [%.2f%%] and impact %d price tranche(s). %s",
amount, b.Pair.Base, b.Pair.Quote, maxPrice, minPrice,
percent, len(action.Tranches), warning)
@@ -108,7 +108,7 @@ func (b *Base) SimulateOrder(amount float64, buy bool) (*WhaleBombResult, error)
warning = fullLiquidityUsageWarning
}
pct := math.CalculatePercentageGainOrLoss(action.TranchePositionPrice, action.ReferencePrice)
pct := math.PercentageChange(action.ReferencePrice, action.TranchePositionPrice)
status := fmt.Sprintf("%s using %f %v worth of %v will send the price from %v to %v [%.2f%%] and impact %v price tranche(s). %s",
direction, soldAmount, sold, bought, action.ReferencePrice,
action.TranchePositionPrice, pct, len(action.Tranches), warning)

View File

@@ -400,7 +400,7 @@ func (bids *bidTranches) hitBidsByNominalSlippage(slippage, refPrice float64) (*
currentTotalAmounts := cumulativeAmounts + bids.Tranches[x].Amount
nominal.AverageOrderCost = currentFullValue / currentTotalAmounts
percent := math.CalculatePercentageGainOrLoss(nominal.AverageOrderCost, refPrice)
percent := math.PercentageChange(refPrice, nominal.AverageOrderCost)
if percent != 0 {
percent *= -1
}
@@ -461,7 +461,7 @@ func (bids *bidTranches) hitBidsByImpactSlippage(slippage, refPrice float64) (*M
impact := &Movement{StartPrice: refPrice, EndPrice: refPrice}
for x := range bids.Tranches {
percent := math.CalculatePercentageGainOrLoss(bids.Tranches[x].Price, refPrice)
percent := math.PercentageChange(refPrice, bids.Tranches[x].Price)
if percent != 0 {
percent *= -1
}
@@ -529,7 +529,7 @@ func (ask *askTranches) liftAsksByNominalSlippage(slippage, refPrice float64) (*
currentAmounts := cumulativeAmounts + ask.Tranches[x].Amount
nominal.AverageOrderCost = currentValue / currentAmounts
percent := math.CalculatePercentageGainOrLoss(nominal.AverageOrderCost, refPrice)
percent := math.PercentageChange(refPrice, nominal.AverageOrderCost)
if slippage < percent {
targetCost := (1 + slippage/100) * refPrice
@@ -582,7 +582,7 @@ func (ask *askTranches) liftAsksByImpactSlippage(slippage, refPrice float64) (*M
impact := &Movement{StartPrice: refPrice, EndPrice: refPrice}
for x := range ask.Tranches {
percent := math.CalculatePercentageGainOrLoss(ask.Tranches[x].Price, refPrice)
percent := math.PercentageChange(refPrice, ask.Tranches[x].Price)
impact.ImpactPercentage = percent
impact.EndPrice = ask.Tranches[x].Price
if slippage <= percent {
@@ -625,7 +625,7 @@ func (m *Movement) finalizeFields(cost, amount, headPrice, leftover float64, swa
// Nominal percentage is the difference from the reference price to average
// order cost.
m.NominalPercentage = math.CalculatePercentageGainOrLoss(m.AverageOrderCost, m.StartPrice)
m.NominalPercentage = math.PercentageChange(m.StartPrice, m.AverageOrderCost)
if m.NominalPercentage < 0 {
m.NominalPercentage *= -1
}
@@ -634,7 +634,7 @@ func (m *Movement) finalizeFields(cost, amount, headPrice, leftover float64, swa
// Impact percentage is how much the orderbook slips from the reference
// price to the remaining tranche price.
m.ImpactPercentage = math.CalculatePercentageGainOrLoss(m.EndPrice, m.StartPrice)
m.ImpactPercentage = math.PercentageChange(m.StartPrice, m.EndPrice)
if m.ImpactPercentage < 0 {
m.ImpactPercentage *= -1
}