# Hledger | How to calculate interest with rate changing in the middle of the year

**URL:** <https://forum.plaintextaccounting.org/t/hledger-how-to-calculate-interest-with-rate-changing-in-the-middle-of-the-year/208>\
**Category:** General\
**Tags:** hledger\
**Created:** [March 21, 2024, 8:01am UTC](https://forum.plaintextaccounting.org/t/hledger-how-to-calculate-interest-with-rate-changing-in-the-middle-of-the-year/208 "2024-03-21T08:01:18Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![zalun](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.plaintextaccounting.org/zalun/32/59_2.png) [@zalun](https://forum.plaintextaccounting.org/u/zalun)\
**Post date:** [March 21, 2024, 8:01am UTC](https://forum.plaintextaccounting.org/t/hledger-how-to-calculate-interest-with-rate-changing-in-the-middle-of-the-year/208/1 "2024-03-21T08:01:18Z")

</div>

I’ve got a sample entry of an unpaid amount.

```auto
2021-09-10 Alimony Due 2021-09
    Receivable:Alimony 200.00 PLN
    Revenues:Alimony

# All other entries removed to be able to compare with an online calculator
# Last entry added as there was no output without it.
2024-03-10 Alimony Due 2024-03
    Receivable:Alimony 200.00 PLN
    Revenues:Alimony

```

I run the command with annual-schedule

```auto
hledger-interest -f ~/.hledger.alimony \
  --act \
  --annual-schedule='[(2020-10-07,0.056),(2021-10-07,0.06),(2021-11-04,0.0675),(2021-12-09,0.0725),(2022-01-05,0.0775),(2022-02-09,0.0825),(2022-03-09,0.09),(2022-04-07,0.1),(2022-05-06,0.1075),(2022-06-09,0.115),(2022-07-08,0.12),(2022-09-08,0.1225),(2023-09-07,0.115),(2023-10-05,0.1125)]' \
  -s Receivable:Interest \
  -t Receivable:Alimony:Interest \
  Receivable:Alimony                         

```

This is the result:

```auto
2021-09-10 Alimony Due 2021-09
    Receivable:Alimony 200.00 PLN
    Revenues:Alimony -200.00 PLN

2021-12-31 5.6% interest for 200.00 PLN over 112 days
    Receivable:Alimony:Interest 3.44 PLN
    Receivable:Interest -3.44 PLN

2022-12-31 7.25% interest for 200.00 PLN over 365 days
    Receivable:Alimony:Interest 14.50 PLN
    Receivable:Interest -14.50 PLN

2023-12-31 12.25% interest for 200.00 PLN over 365 days
    Receivable:Alimony:Interest 24.50 PLN
    Receivable:Interest -24.50 PLN

2024-03-10 11.25% interest for 200.00 PLN over 70 days
    Receivable:Alimony:Interest 4.30 PLN
    Receivable:Interest -4.30 PLN

2024-03-10 Alimony Due 2024-03
    Receivable:Alimony 200.00 PLN
    Revenues:Alimony -200.00 PLN

```

The interest are calculated for the full year in 2022 and 2023.

I expected it to be the same as in the [online calculator](https://kalkulatory.gofin.pl/kalkulatory/kalkulator-odsetek-ustawowych):

| from-to | days | rate | amount |
| --- | --- | --- | --- |
| 11.09.2021 - 06.10.2021 | 26 | 5,60% | 0,80 |
| 07.10.2021 - 03.11.2021 | 28 | 6,00% | 0,92 |
| 04.11.2021 - 08.12.2021 | 35 | 6,75% | 1,29 |
| 09.12.2021 - 04.01.2022 | 27 | 7,25% | 1,07 |
| 05.01.2022 - 08.02.2022 | 35 | 7,75% | 1,49 |
| 09.02.2022 - 08.03.2022 | 28 | 8,25% | 1,27 |
| 09.03.2022 - 06.04.2022 | 29 | 9,00% | 1,43 |
| 07.04.2022 - 05.05.2022 | 29 | 10,00% | 1,59 |
| 06.05.2022 - 08.06.2022 | 34 | 10,75% | 2,00 |
| 09.06.2022 - 07.07.2022 | 29 | 11,50% | 1,83 |
| 08.07.2022 - 07.09.2022 | 62 | 12,00% | 4,08 |
| 08.09.2022 - 06.09.2023 | 364 | 12,25% | 24,43 |
| 07.09.2023 - 04.10.2023 | 28 | 11,50% | 1,76 |
| 05.10.2023 - 21.03.2024 | 169 | 11,25% | 10,42 |

How should I change the data/command to achieve the result?

---

<div class="post-metadata">

**Author:** ![f-a](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.plaintextaccounting.org/f-a/32/32_2.png) [@f-a](https://forum.plaintextaccounting.org/u/f-a)\
**Post date:** [March 21, 2024, 5:02pm UTC](https://forum.plaintextaccounting.org/t/hledger-how-to-calculate-interest-with-rate-changing-in-the-middle-of-the-year/208/2 "2024-03-21T17:02:42Z")

</div>

I took the liberty of adding an `hledger` tag to the post.

Is this the most minimal reproducible example? I am sure the less there is, the easier it is to debug for @simonmic & friends!

---

<div class="post-metadata">

**Author:** ![shlewislee](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@shlewislee](https://forum.plaintextaccounting.org/u/shlewislee)\
**Post date:** [March 21, 2024, 5:23pm UTC](https://forum.plaintextaccounting.org/t/hledger-how-to-calculate-interest-with-rate-changing-in-the-middle-of-the-year/208/3 "2024-03-21T17:23:14Z")

</div>

It changes way too frequently without clear pattern. It seems like it’s going to be best to just do it manually. If you’re doing it annually, it’s not much of a hassle either. Just calculate how much it actually is going to be in a year. If it gets resolved earlier, you’d have to recalculate. But that’s going to be way easier than to write code for this.

What’s going to be a major hassle is writing code for this kind of very specific scenario… It’s not going to be impossible to write for _this_ but it’s going to be a lot of work writing something that can do _this kind of stuff_.

---

<div class="post-metadata">

**Author:** ![adept](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.plaintextaccounting.org/adept/32/36_2.png) [@adept](https://forum.plaintextaccounting.org/u/adept)\
**Post date:** [March 21, 2024, 6:14pm UTC](https://forum.plaintextaccounting.org/t/hledger-how-to-calculate-interest-with-rate-changing-in-the-middle-of-the-year/208/4 "2024-03-21T18:14:49Z")

</div>

@zalun so usually hledger-interest works off existing payment transactions and splits them into the principal and interest payments.

In your case, there’s just two payments, which is probably not right . You need to add more payments first (probably once per month, or however often they happen) and then try hledger-interest again?

If there are no transactions that pay back the liability, hledger-interest will just compute the annual interest, which is what seems to be happening in your example.

---

<div class="post-metadata">

**Author:** ![zalun](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.plaintextaccounting.org/zalun/32/59_2.png) [@zalun](https://forum.plaintextaccounting.org/u/zalun)\
**Post date:** [March 26, 2024, 9:52am UTC](https://forum.plaintextaccounting.org/t/hledger-how-to-calculate-interest-with-rate-changing-in-the-middle-of-the-year/208/5 "2024-03-26T09:52:09Z")

</div>

@f-a The payments are due monthly, and I restricted them to one to compare with the government calculator.

I tried to put in a fake payment, and immediately after adding receivable to reset the amount. Unfortunately the `hledger-interest` is not picking up:

Fake payment on the date the interest rate changes and interest calculated around it:

```auto
# Actual interest rate 09.12.2021 - 04.01.2022 27 days 7,25%

2022-01-04 7.25% interest for 200.00 PLN over 4 days
    Receivable:Alimony:Interest 0.16 PLN
    Receivable:Interest -0.16 PLN

# Fake payment to force the right interest rate
2022-01-04 Alimony Paid 2022-01
    Revenues:Alimony 200.00 PLN
    Receivable:Alimony
2022-01-04 Alimony Due 2022-01
    Receivable:Alimony 200.00 PLN
    Revenues:Alimony

# Actual interest rate 05.01.2022 - 08.02.2022 35 days 7,75%

2022-02-08 7.25% interest for 200.00 PLN over 35 days
    Receivable:Alimony:Interest 1.39 PLN
    Receivable:Interest -1.39 PLN

# Fake payment to force the right interest rate
2022-02-08 Alimony Paid 2022-02
    Revenues:Alimony 200.00 PLN
    Receivable:Alimony
2022-02-08 Alimony Due 2022-02
    Receivable:Alimony 200.00 PLN
    Revenues:Alimony

# Actual interest rate 09.02.2022 - 08.03.2022 28 days 8,25%

2022-03-08 7.75% interest for 200.00 PLN over 28 days
    Receivable:Alimony:Interest 1.19 PLN
    Receivable:Interest -1.19 PLN

```

---

<div class="post-metadata">

**Author:** ![simonmic](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.plaintextaccounting.org/simonmic/32/1_2.png) [@simonmic](https://forum.plaintextaccounting.org/u/simonmic)\
**Post date:** [March 26, 2024, 4:59pm UTC](https://forum.plaintextaccounting.org/t/hledger-how-to-calculate-interest-with-rate-changing-in-the-middle-of-the-year/208/6 "2024-03-26T16:59:25Z")

</div>

> [@zalun](#):
>
> I run the command with annual-schedule

I can’t replicate this for some reason, I get no output:

```cli
~/src/hledger/2024-03-20$ cat a.j
2021-09-10 Alimony Due 2021-09
    Receivable:Alimony 200.00 PLN
    Revenues:Alimony

# All other entries removed to be able to compare with an online calculator
# Last entry added as there was no output without it.
2024-03-10 Alimony Due 2024-03
    Receivable:Alimony 200.00 PLN
    Revenues:Alimony
~/src/hledger/2024-03-20$ hledger-interest --act --annual-schedule='[(2020-10-07,0.056),(2021-10-07,0.06),(2021-11-04,0.0675),(2021-12-09,0.0725),(2022-01-05,0.0775),(2022-02-09,0.0825),(2022-03-09,0.09),(2022-04-07,0.1),(2022-05-06,0.1075),(2022-06-09,0.115),(2022-07-08,0.12),(2022-09-08,0.1225),(2023-09-07,0.115),(2023-10-05,0.1125)]' -s Receivable:Interest -t Receivable:Alimony:Interest Receivable:Alimony
~/src/hledger/2024-03-20$ 

```

---

<div class="post-metadata">

**Author:** ![simonmic](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.plaintextaccounting.org/simonmic/32/1_2.png) [@simonmic](https://forum.plaintextaccounting.org/u/simonmic)\
**Post date:** [March 26, 2024, 5:11pm UTC](https://forum.plaintextaccounting.org/t/hledger-how-to-calculate-interest-with-rate-changing-in-the-middle-of-the-year/208/7 "2024-03-26T17:11:32Z")

</div>

hledger-interest is hard to use, but it’s only [about](https://github.com/peti/hledger-interest/blob/a1a31f2e0a852dec2a8c99f9d99c29a482f676b0/Hledger/Interest.hs) 200 [lines](https://github.com/peti/hledger-interest/blob/a1a31f2e0a852dec2a8c99f9d99c29a482f676b0/Hledger/Interest/Rate.hs) of [code](https://github.com/peti/hledger-interest/blob/a1a31f2e0a852dec2a8c99f9d99c29a482f676b0/Hledger/Interest/DayCountConvention.hs) - would anyone be interested in improving it ?

---

<div class="post-metadata">

**Author:** ![adept](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.plaintextaccounting.org/adept/32/36_2.png) [@adept](https://forum.plaintextaccounting.org/u/adept)\
**Post date:** [March 27, 2024, 12:31am UTC](https://forum.plaintextaccounting.org/t/hledger-how-to-calculate-interest-with-rate-changing-in-the-middle-of-the-year/208/8 "2024-03-27T00:31:36Z")

</div>

@zalun I think this PR should fix this issue: [Fix rate schedule to handle multiple rate changes during they year by adept · Pull Request #23 · peti/hledger-interest · GitHub](https://github.com/peti/hledger-interest/pull/23) . You can see the output on your test in the comment in that PR

If you are willing to try it from source, you could build from my fork at [GitHub - adept/hledger-interest: compute interest for hledger accounts](https://github.com/adept/hledger-interest)

---

<div class="post-metadata">

**Author:** ![zalun](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.plaintextaccounting.org/zalun/32/59_2.png) [@zalun](https://forum.plaintextaccounting.org/u/zalun)\
**Post date:** [March 27, 2024, 9:13am UTC](https://forum.plaintextaccounting.org/t/hledger-how-to-calculate-interest-with-rate-changing-in-the-middle-of-the-year/208/9 "2024-03-27T09:13:21Z")

</div>

Great!, thank you

---

<div class="post-metadata">

**Author:** ![simonmic](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.plaintextaccounting.org/simonmic/32/1_2.png) [@simonmic](https://forum.plaintextaccounting.org/u/simonmic)\
**Post date:** [May 10, 2024, 6:59am UTC](https://forum.plaintextaccounting.org/t/hledger-how-to-calculate-interest-with-rate-changing-in-the-middle-of-the-year/208/10 "2024-05-10T06:59:06Z")

</div>

> [@adept](#):
>
> If you are willing to try it from source, you could build from my fork

Did it solve your problem @zalun ? If so could be worth commenting on the PR and trying to get it released.

---

<div class="post-metadata">

**Author:** ![zalun](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.plaintextaccounting.org/zalun/32/59_2.png) [@zalun](https://forum.plaintextaccounting.org/u/zalun)\
**Post date:** [June 11, 2024, 5:52am UTC](https://forum.plaintextaccounting.org/t/hledger-how-to-calculate-interest-with-rate-changing-in-the-middle-of-the-year/208/11 "2024-06-11T05:52:41Z")

</div>

@adept  
I haven't been able to compile it. Today, I installed the released version, hoping it would include the change...

I guess I need to try again.

```bash
➜ Accounting cabal install --lib hledger-lib
Resolving dependencies...
➜ Accounting cd hledger-interest 
➜ hledger-interest git:(master) ✗ cabal install                  
Error: cabal: Could not resolve dependencies:
[__0] trying: hledger-interest-1.6.6 (user goal)
[__1] next goal: hledger-lib (dependency of hledger-interest)
[__1] rejecting: hledger-lib-1.34 (conflict: hledger-interest =>
hledger-lib>=1.26 && <1.33)
[__1] skipping: hledger-lib-1.33.1, hledger-lib-1.33 (has the same
characteristics that caused the previous version to fail: excluded by
constraint '>=1.26 && <1.33' from 'hledger-interest')
[__1] trying: hledger-lib-1.32.3
[__2] next goal: base (dependency of hledger-interest)
[__2] rejecting: base-4.19.1.0/installed-inplace (conflict: hledger-lib =>
base>=4.14 && <4.19)
[__2] skipping: base-4.20.0.1, base-4.20.0.0, base-4.19.1.0, base-4.19.0.0
(has the same characteristics that caused the previous version to fail:
excluded by constraint '>=4.14 && <4.19' from 'hledger-lib')
[__2] rejecting: base-4.18.2.1, base-4.18.2.0, base-4.18.1.0, base-4.18.0.0,
base-4.17.2.1, base-4.17.2.0, base-4.17.1.0, base-4.17.0.0, base-4.16.4.0,
base-4.16.3.0, base-4.16.2.0, base-4.16.1.0, base-4.16.0.0, base-4.15.1.0,
base-4.15.0.0, base-4.14.3.0, base-4.14.2.0, base-4.14.1.0, base-4.14.0.0,
base-4.13.0.0, base-4.12.0.0, base-4.11.1.0, base-4.11.0.0, base-4.10.1.0,
base-4.10.0.0, base-4.9.1.0, base-4.9.0.0, base-4.8.2.0, base-4.8.1.0,
base-4.8.0.0, base-4.7.0.2, base-4.7.0.1, base-4.7.0.0, base-4.6.0.1,
base-4.6.0.0, base-4.5.1.0, base-4.5.0.0, base-4.4.1.0, base-4.4.0.0,
base-4.3.1.0, base-4.3.0.0, base-4.2.0.2, base-4.2.0.1, base-4.2.0.0,
base-4.1.0.0, base-4.0.0.0, base-3.0.3.2, base-3.0.3.1 (constraint from
non-upgradeable package requires installed instance)
[__2] fail (backjumping, conflict set: base, hledger-interest, hledger-lib)
After searching the rest of the dependency tree exhaustively, these were the
goals I've had most trouble fulfilling: hledger-lib, base, hledger-interest

```

---

<div class="post-metadata">

**Author:** ![simonmic](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.plaintextaccounting.org/simonmic/32/1_2.png) [@simonmic](https://forum.plaintextaccounting.org/u/simonmic)\
**Post date:** [June 11, 2024, 11:12am UTC](https://forum.plaintextaccounting.org/t/hledger-how-to-calculate-interest-with-rate-changing-in-the-middle-of-the-year/208/12 "2024-06-11T11:12:29Z")

</div>

The official [hledger-interest](https://hackage.haskell.org/package/hledger-interest) has had some revisions to bounds so it builds with current hledger. You'll need to mimic those in your copy of hledger-interest.cabal (updating the [hledger-lib version](https://github.com/adept/hledger-interest/blob/1b1fc1c0cca7ab66bfd59a405d37343242108ba6/hledger-interest.cabal#L122) there, at least).

---

<div class="post-metadata">

**Author:** ![adept](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.plaintextaccounting.org/adept/32/36_2.png) [@adept](https://forum.plaintextaccounting.org/u/adept)\
**Post date:** [June 11, 2024, 12:10pm UTC](https://forum.plaintextaccounting.org/t/hledger-how-to-calculate-interest-with-rate-changing-in-the-middle-of-the-year/208/13 "2024-06-11T12:10:43Z")

</div>

Released version still does not have the fix, yet - my PR is still open [Fix rate schedule to handle multiple rate changes during the year by adept · Pull Request #23 · peti/hledger-interest · GitHub](https://github.com/peti/hledger-interest/pull/23)

---

<div class="post-metadata">

**Author:** ![simonmic](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.plaintextaccounting.org/simonmic/32/1_2.png) [@simonmic](https://forum.plaintextaccounting.org/u/simonmic)\
**Post date:** [June 11, 2024, 2:15pm UTC](https://forum.plaintextaccounting.org/t/hledger-how-to-calculate-interest-with-rate-changing-in-the-middle-of-the-year/208/14 "2024-06-11T14:15:59Z")

</div>

No disrespect to overworked maintainers, but if anyone wants to port this into hledger I'd accept it.
