mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 15:09:42 +00:00
52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
package math
|
|
|
|
import "math"
|
|
|
|
// CalculateAmountWithFee returns a calculated fee included amount on fee
|
|
func CalculateAmountWithFee(amount, fee float64) float64 {
|
|
return amount + CalculateFee(amount, fee)
|
|
}
|
|
|
|
// CalculateFee returns a simple fee on amount
|
|
func CalculateFee(amount, fee float64) float64 {
|
|
return amount * (fee / 100)
|
|
}
|
|
|
|
// CalculatePercentageGainOrLoss returns the percentage rise over a certain
|
|
// period
|
|
func CalculatePercentageGainOrLoss(priceNow, priceThen float64) float64 {
|
|
return (priceNow - priceThen) / priceThen * 100
|
|
}
|
|
|
|
// CalculatePercentageDifference returns the percentage of difference between
|
|
// multiple time periods
|
|
func CalculatePercentageDifference(amount, secondAmount float64) float64 {
|
|
return (amount - secondAmount) / ((amount + secondAmount) / 2) * 100
|
|
}
|
|
|
|
// CalculateNetProfit returns net profit
|
|
func CalculateNetProfit(amount, priceThen, priceNow, costs float64) float64 {
|
|
return (priceNow * amount) - (priceThen * amount) - costs
|
|
}
|
|
|
|
// RoundFloat rounds your floating point number to the desired decimal place
|
|
func RoundFloat(x float64, prec int) float64 {
|
|
var rounder float64
|
|
pow := math.Pow(10, float64(prec))
|
|
intermed := x * pow
|
|
_, frac := math.Modf(intermed)
|
|
intermed += .5
|
|
x = .5
|
|
if frac < 0.0 {
|
|
x = -.5
|
|
intermed--
|
|
}
|
|
if frac >= x {
|
|
rounder = math.Ceil(intermed)
|
|
} else {
|
|
rounder = math.Floor(intermed)
|
|
}
|
|
|
|
return rounder / pow
|
|
}
|