golang金融计算插件库go-finance实现时间价值、现金流、利率转换与债券计算
Golang金融计算插件库go-finance实现时间价值、现金流、利率转换与债券计算
go-finance是一个包含金融函数集合的Go库,用于计算货币时间价值(年金)、现金流、利率转换、债券和折旧计算。
主要功能
利率计算
- EffectiveRate - 计算有效利率
- NominalRate - 计算名义利率
现金流分析
- NetPresentValue - 计算净现值
- InternalRateOfReturn - 计算内部收益率
- ModifiedInternalRateOfReturn - 计算修正内部收益率
- ScheduledNetPresentValue - 计算计划净现值
- ScheduledInternalRateOfReturn - 计算计划内部收益率
货币时间价值(TVM)
- PresentValue - 计算现值
- FutureValue - 计算终值
- Payment - 计算付款额
- Periods - 计算期数
- Rate - 计算利率
- InterestPayment - 计算利息支付
- PrincipalPayment - 计算本金支付
债券计算
- DaysDifference - 计算天数差
- DaysPerYear - 计算年天数
- TBillEquivalentYield - 计算国库券等价收益率
- TBillPrice - 计算国库券价格
- TBillYield - 计算国库券收益率
- DiscountRate - 计算贴现率
- PriceDiscount - 计算价格折扣
折旧计算
- DepreciationFixedDeclining - 固定余额递减折旧法
- DepreciationSYD - 年数总和折旧法
- DepreciationStraightLine - 直线折旧法
示例代码
货币时间价值计算示例
package main
import (
"fmt"
"github.com/alpeb/go-finance/fin"
)
func main() {
// 计算现值
pv := fin.PresentValue(0.05, 5, 1000, 0, 0)
fmt.Printf("现值: %.2f\n", pv) // 输出: 现值: 783.53
// 计算终值
fv := fin.FutureValue(0.05, 5, 0, -1000, 0)
fmt.Printf("终值: %.2f\n", fv) // 输出: 终值: 1276.28
// 计算每期付款额
pmt := fin.Payment(0.05, 5, 10000, 0, 0)
fmt.Printf("每期付款额: %.2f\n", pmt) // 输出: 每期付款额: -2309.75
}
现金流分析示例
package main
import (
"fmt"
"github.com/alpeb/go-finance/fin"
)
func main() {
// 现金流数据
cashFlows := []float64{-5000, 1500, 1500, 1500, 1500}
// 计算净现值(NPV)
npv := fin.NetPresentValue(0.1, cashFlows)
fmt.Printf("净现值: %.2f\n", npv) // 输出: 净现值: 254.52
// 计算内部收益率(IRR)
irr, err := fin.InternalRateOfReturn(cashFlows)
if err != nil {
fmt.Println("计算IRR出错:", err)
return
}
fmt.Printf("内部收益率: %.2f%%\n", irr*100) // 输出: 内部收益率: 15.24%
}
利率转换示例
package main
import (
"fmt"
"github.com/alpeb/go-finance/fin"
)
func main() {
// 名义利率转有效利率
effectiveRate := fin.EffectiveRate(0.12, 12) // 12%名义利率,按月复利
fmt.Printf("有效年利率: %.2f%%\n", effectiveRate*100) // 输出: 有效年利率: 12.68%
// 有效利率转名义利率
nominalRate := fin.NominalRate(0.1268, 12) // 12.68%有效利率,按月复利
fmt.Printf("名义年利率: %.2f%%\n", nominalRate*100) // 输出: 名义年利率: 12.00%
}
债券计算示例
package main
import (
"fmt"
"time"
"github.com/alpeb/go-finance/fin"
)
func main() {
// 计算国库券价格
settlement := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
maturity := time.Date(2023, 6, 30, 0, 0, 0, 0, time.UTC)
discount := 0.05 // 5%贴现率
price := fin.TBillPrice(settlement, maturity, discount)
fmt.Printf("国库券价格: %.4f\n", price) // 输出: 国库券价格: 0.9757
// 计算国库券收益率
yield := fin.TBillYield(settlement, maturity, price)
fmt.Printf("国库券收益率: %.2f%%\n", yield*100) // 输出: 国库券收益率: 5.00%
}
折旧计算示例
package main
import (
"fmt"
"github.com/alpeb/go-finance/fin"
)
func main() {
// 直线折旧法
cost := 10000.0 // 资产原值
salvage := 1000.0 // 残值
life := 5 // 使用年限
dep := fin.DepreciationStraightLine(cost, salvage, life)
fmt.Printf("直线折旧每年折旧额: %.2f\n", dep) // 输出: 直线折旧每年折旧额: 1800.00
// 年数总和折旧法
period := 1 // 第一年
depSYD := fin.DepreciationSYD(cost, salvage, life, period)
fmt.Printf("第一年年数总和折旧额: %.2f\n", depSYD) // 输出: 第一年年数总和折旧额: 3000.00
}
许可证
Mozilla Public License 2.0
更多关于golang金融计算插件库go-finance实现时间价值、现金流、利率转换与债券计算的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复
更多关于golang金融计算插件库go-finance实现时间价值、现金流、利率转换与债券计算的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
Go-Finance: Golang金融计算插件库
Go-Finance是一个用于金融计算的Golang库,提供了时间价值、现金流、利率转换和债券计算等功能。下面我将介绍如何使用这个库进行常见的金融计算。
安装
go get github.com/FlashBoys/go-finance
基本功能实现
1. 时间价值计算 (TVM)
package main
import (
"fmt"
"github.com/FlashBoys/go-finance"
)
func main() {
// 计算现值
pv := finance.PV(0.05, 5, 100, 0, false)
fmt.Printf("现值: %.2f\n", pv)
// 计算终值
fv := finance.FV(0.05, 5, 100, 0, false)
fmt.Printf("终值: %.2f\n", fv)
// 计算年金支付
pmt := finance.PMT(0.05, 5, 1000, 0, false)
fmt.Printf("年金支付: %.2f\n", pmt)
// 计算期数
nper := finance.NPER(0.05, 100, 1000, 0, false)
fmt.Printf("期数: %.2f\n", nper)
// 计算利率
rate := finance.RATE(5, 100, 1000, 0, false, 0.1)
fmt.Printf("利率: %.4f\n", rate)
}
2. 现金流计算
package main
import (
"fmt"
"github.com/FlashBoys/go-finance"
)
func main() {
// 计算净现值
cashFlows := []float64{-1000, 200, 300, 400, 500}
npv := finance.NPV(0.1, cashFlows)
fmt.Printf("净现值: %.2f\n", npv)
// 计算内部收益率
irr := finance.IRR(cashFlows, 0.1)
fmt.Printf("内部收益率: %.4f\n", irr)
}
3. 利率转换
package main
import (
"fmt"
"github.com/FlashBoys/go-finance"
)
func main() {
// 年利率转换为月利率
monthlyRate := finance.EffectiveRate(0.12, 12)
fmt.Printf("月利率: %.4f\n", monthlyRate)
// 名义年利率转换为实际年利率
effectiveRate := finance.NominalToEffective(0.12, 12)
fmt.Printf("实际年利率: %.4f\n", effectiveRate)
// 实际年利率转换为名义年利率
nominalRate := finance.EffectiveToNominal(0.1268, 12)
fmt.Printf("名义年利率: %.4f\n", nominalRate)
}
4. 债券计算
package main
import (
"fmt"
"github.com/FlashBoys/go-finance"
"time"
)
func main() {
// 计算债券价格
settlement := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
maturity := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)
rate := 0.05 // 票面利率
yld := 0.06 // 收益率
redemption := 100.0 // 偿还价值
frequency := 2 // 每年付息次数
basis := finance.DayCountBasisActualActual
price := finance.PRICE(settlement, maturity, rate, yld, redemption, frequency, basis)
fmt.Printf("债券价格: %.4f\n", price)
// 计算债券收益率
yield := finance.YIELD(settlement, maturity, rate, price, redemption, frequency, basis)
fmt.Printf("债券收益率: %.4f\n", yield)
// 计算债券久期
duration := finance.DURATION(settlement, maturity, rate, yld, frequency, basis)
fmt.Printf("债券久期: %.4f\n", duration)
// 计算修正久期
modifiedDuration := finance.MDURATION(settlement, maturity, rate, yld, frequency, basis)
fmt.Printf("修正久期: %.4f\n", modifiedDuration)
}
高级功能
1. 不规则现金流分析
package main
import (
"fmt"
"github.com/FlashBoys/go-finance"
)
func main() {
// 不规则现金流分析
dates := []finance.Date{
finance.NewDate(2023, 1, 1),
finance.NewDate(2023, 7, 1),
finance.NewDate(2024, 1, 1),
finance.NewDate(2024, 7, 1),
finance.NewDate(2025, 1, 1),
}
cashFlows := []float64{-1000, 50, 50, 50, 1050}
xirr := finance.XIRR(dates, cashFlows, 0.1)
fmt.Printf("不规则现金流内部收益率: %.4f\n", xirr)
xnv := finance.XNPV(0.1, dates, cashFlows)
fmt.Printf("不规则现金流净现值: %.4f\n", xnv)
}
2. 债券收益率曲线分析
package main
import (
"fmt"
"github.com/FlashBoys/go-finance"
"time"
)
func main() {
// 构建收益率曲线
settlement := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
maturities := []time.Time{
time.Date(2023, 7, 1, 0, 0, 0, 0, time.UTC),
time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC),
time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC),
}
prices := []float64{99.5, 98.7, 97.2}
faceValue := 100.0
couponRate := 0.05
frequency := 2
// 计算即期收益率曲线
spotRates := make([]float64, len(maturities))
for i, maturity := range maturities {
spotRates[i] = finance.YIELD(settlement, maturity, couponRate, prices[i], faceValue, frequency, finance.DayCountBasisActualActual)
fmt.Printf("%v 的即期收益率: %.4f\n", maturity.Format("2006-01-02"), spotRates[i])
}
}
注意事项
- 利率参数应为小数形式,如5%应输入0.05
- 现金流数组的第一个元素通常是初始投资(负值)
- 债券计算中的日期应使用time.Time类型
- 对于不规则现金流分析,确保日期和现金流数组长度一致
Go-Finance库提供了丰富的金融计算功能,可以满足大多数金融分析需求。通过合理组合这些函数,您可以构建复杂的金融模型和分析工具。