mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-14 07:26:47 +00:00
* refactored RoundFloat func in common/math package * cleanup Co-authored-by: Vazha Bezhanishvili <vazha.bezhanishvili@elegro.eu>
37 lines
1.1 KiB
Go
37 lines
1.1 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 {
|
|
pow := math.Pow(10, float64(prec))
|
|
return math.Round(x*pow) / pow
|
|
}
|