Skip to content

Commit

Permalink
Merge pull request #21 from lazarospsa/HoldingPeriodReturn-#16
Browse files Browse the repository at this point in the history
HoldingPeriodReturn #16
  • Loading branch information
lazarospsa committed Dec 18, 2023
2 parents 9468681 + 76eb102 commit 8c80236
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
11 changes: 11 additions & 0 deletions gofin.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ func PresentValueAnnuity(interestRate float64, periods int, cashFlows []float64)
return presentValueAnnuity
}

// HoldingPeriodReturn calculates the holding period return (HPR)
func HoldingPeriodReturn(initialValue, finalValue float64) float64 {
return (finalValue - initialValue) / initialValue
}

// HoldingPeriodReturn calculates the holding period return (HPR)
// as a percentage
func HoldingPeriodReturnPercentage(initialValue, finalValue float64) float64 {
return ((finalValue - initialValue) / initialValue)*100
}

func PresentValueAnnuityDue(interestRate float64, periods int, cashFlows []float64) float64 {
presentValueAnnuityDue := 0.0
for i := 0; i < len(cashFlows); i++ {
Expand Down
26 changes: 26 additions & 0 deletions gofin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,32 @@ func TestPresentValuePerpetuity(t *testing.T) {
}
}

func TestHoldingPeriodReturn(t *testing.T) {
var initialValue float64 = 1000.0
var finalValue float64 = 1200.0
var expected float64 = 0.20

actual := HoldingPeriodReturn(initialValue, finalValue)

if compareFloat64(actual, expected) {
t.Errorf("Test failed, expected: '%f', got: '%f'", expected, actual)
}
}


func TestHoldingPeriodReturnPercentage(t *testing.T) {
var initialValue float64 = 1000.0
var finalValue float64 = 1200.0
var expected float64 = 20.0

actual := HoldingPeriodReturn(initialValue, finalValue)

if compareFloat64(actual, expected) {
t.Errorf("Test failed, expected: '%f', got: '%f'", expected, actual)
}
}


func TestFutureValueAnnuity(t *testing.T) {
var payment float64 = 100
var interestRate float64 = 0.1
Expand Down

0 comments on commit 8c80236

Please sign in to comment.