Compound Interest with Streamlit & Python


We can often hear investors and traders say the key for financial success is compound interest. I personally interpret it as reinvesting the profits. Here is what Ivestopedia says about compound interest:

Compound interest (or compounding interest) is the interest on a loan or deposit calculated based on both the initial principal and the accumulated interest from previous periods. Thought to have originated in 17th-century Italy, compound interest can be thought of as "interest on interest," and will make a sum grow at a faster rate than simple interest, which is calculated only on the principal amount.

The image above demonstrates the formula for calculating compound interest. Recently, a friend of mine jokingly asked if I could write a python code to display compound interest for staked assets. Even it is for fun, I thought why not? As a quick app making library I chose to use Streemlit and wrote up some code to display growth of the principal amount based on the interest rate and compounding interest every second. I believe normally compounding would work every month. I am not really sure how compounding works in defi. In any case, I just used every second. Below is the code, try it yourself.

import streamlit as st 
import time

amount = st.sidebar.text_input('Amount', '1000.00')
interest_rate = st.sidebar.text_input('Interest Rate', '10.00')
start = st.sidebar.button('Start')

if __name__ == '__main__':
    # Compound Interest!
    if start:

                    .big-font {
                    font-size:80px !important;
                    """, unsafe_allow_html=True)

        A = 0
        P = float(amount)
        r = float(interest_rate)
        n = 12 * 365 * 24 * 60 * 60
        t = 1  
        A = P * (1 + (r / 100) / n) ** (n*t)    
        b = st.empty()
        for i in range(n):
            I = P * (r / 100) / n
            P = P + I
            P = round(P, 8)
            txt = str(P)
            b.markdown('<p class="big-font">{}</p>'.format(txt), unsafe_allow_html=True)