Python Forum

Full Version: How to calculate compounded profits on a given day?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello,

I have a spreadsheet which im looking to convert into a python code to add onto my website.

Questions is how can I calculate the compounded results on a given day?

So for example on day one a person starts on a $228 capital he then earns approx 0.2% on that capital. That return gets put back into the capital, therefore the capital grows and now you are getting 0.2% on the new capital.

This whole process carries on and on until the user decides to leave the company.

So by default, your daily capital increases and because of this your daily returns also increase. And now the next daily capital has increased and also the next daily return and so on and so fourth.

Hope that made sense!!

thanks

Gourav
Would you agree that you earn 20% in approx; 4 years with this scheme?

Paul
DPaul, sorry?
It sounds like you just need a variable for the total capital, and create a loop for the compounding period and add in the interest for that period. Then print out the total at whatever event means the "end" has arrived.

Do you have a question about how to do any part of that in python?
(Aug-07-2020, 08:19 PM)guv114 Wrote: [ -> ]DPaul, sorry?

I understood that you have this worked out in excel,
so i wanted you to check if the compounded intrest formula does
what you expect, before anything else.

You can almost copy / paste the formula from wikipedia into python.

Paul
Paul...ok i will try this sounds...

bowlofred...i think I get what your saying but not sure how to code it up. I will give it a try. Sorry I'm new to this and in a middle of a career change to coding..so learning as I go!
Here is a little something I got going. I could finish the code for you, but I figure it isn't learning if it's all the way done ;)

def compound_investment():

    try:
        i = input("[Investment Amount]: $")

        if not i.isnumeric():
            raise TypeError

        else:
            print(int(i))

    except TypeError:
        print("[TypeError]: Please enter numerical values only!\n\n")
        compound_investment()

compound_investment()

print("Investment Float: {}{:.2f}".format( "$", int(2) ))

Here is a little something I got going. I could finish the code for you, but I figure it isn't learning if it's all the way done ;)

def compound_investment():

    try:
        i = input("[Investment Amount]: $")

        if not i.isnumeric():
            raise TypeError

        else:
            print(float(i))

    except TypeError:
        print("[TypeError]: Please enter numerical values only!\n\n")
        compound_investment()

compound_investment()

print("Investment Float: {}{:.2f}".format( "$", float(2) ))
You may want to consider adding some of your own variables to determine how many years they are employed. You also want to include a formula in the function that calculates your compounded interest. One way would be compounded_interest = float(investment*0.2)+investment
compounded_interest * years = sum

That will help you get to where you need to go. There are other ways you can futher this program by adding in features that could import your spreadsheets and take in the data for you. However, that is a little bit beyond me and I am still learning as well.
OK, I misinterpreted this post because there is some key information missing.
I read one would be compounding 0.2% per day. Is that per year ? That is (1 + 0.002) to the power of the years as shown in post #7.

But there seems to be a desire to get the value any given day during any period.
Then i think you need to provide a dayly intrest rate and raise it to the power of the number of days as appropriate.
Paul
Hi guys...thanks for your response. Agreed I need to learn and I am very much impressed with the response I get if I need help in coding. This was my first time posting something.

Yes. Im going to study the code and see if I can implement it to my script.
and Yes. On excel spreadsheet iv got two columns...left is capital & right is earnings for that day based on 0.2%. Then for the next day The capital results in previous capital+previous daily earnings.....so now the new capital has increased. This then the daily earnings has now also increased for this day.

and this whole cycle carries on and on....so in excel once I established the second days values...all I did was select and drag the results down for capital column and daily earnings column.

so in excel its simple. But obviously just need to get my head around it in python.

thanks
If I understand the problem then very simple loop will suffice, example for 10 time units (days?):

investment = 228
rate = 0.2 / 100


for i in range(1, 11):
    earning = investment * rate
    print(f'{i:<4} {investment:<12.4f} {earning:<10.4f}')
    investment += earning
This will output to screen:

Output:
1 228.0000 0.4560 2 228.4560 0.4569 3 228.9129 0.4578 4 229.3707 0.4587 5 229.8295 0.4597 6 230.2891 0.4606 7 230.7497 0.4615 8 231.2112 0.4624 9 231.6736 0.4633 10 232.1370 0.4643
Pages: 1 2