CodeFEATUREDFinance

Cashflow tracker with AI

In this post, we aim to guide you through a step-by-step approach to gaining insights from your financial situation with the assistance of AI, particularly ChatGPT. In this brief post, we will demonstrate how to utilize the free GPT-4 to categorize your monthly statement into income, savings, and expenses. We will provide the source code for free, allowing you to customize it according to your specific use case.

Step 0: Get your monthly bank statement

In this step, you simply need to access your online banking and request a monthly or period statement. The best option is a CSV file, as it facilitates more accurate parsing. However, if your bank does not provide that option, you can opt for a PDF format. Then, try copying the contents, including dates, prices, and descriptions, to a text file. To maximize the effectiveness of this tracker, it is recommended to use your checking account, which encompasses both income and expenses related to your credit card or other payments. However, you can choose either account.

Step 1: access free GPT-4

Wait! Is it possible? Yes, you can access the free version of GPT-4 on the Bing website of Microsoft Edge. To do so, simply download Microsoft Edge, then type bing.com. There is a chat option at the top that you can select to engage with GPT-4. For our case, the best option is to choose the more precise option.

Step 2: Prompt engineering to Jsonify our statement

For this step, you can use this specific prompt and add the statement to the end of it delimited by ###.

Restricted content! log in or register for free.

Step 3: Use python to plot

Finally in the last step, you save the json file generate by the GPT and put inside your python working directory. More detail in our Github page.

Click to show the whole code
    cashflow = []
    incomes_plot = {}
    expenses_plot = {}
    savings_plot = {}
    for month in data:
        # Income
        incomes = data[month]["Income"]
        income_values = list(incomes.values())
        # compute the sum for the dictionary values in the list
        income_values = [sum(i.values()) for i in income_values]
        income_category = list(incomes.keys())
        total_incomes = sum(income_values)
        print(income_category)
        # put all the income values in  income_plot dictioanry for each category, on each month.
        # if specific category is not in the dictionary, add it to the dictionary. put zero for previous months
        # if some categories does not have values, put zero for that month
        for i in income_category:
            if i not in incomes_plot:
                incomes_plot[i] = [0] * len(data)
            incomes_plot[i][list(data).index(month)] = income_values[income_category.index(i)]


        # Expenses
        expenses = data[month]["Expenses"]
        expenses_values = list(expenses.values())
        expenses_values = [sum(i.values()) for i in expenses_values]
        expense_category = list(expenses.keys())
        total_expenses = sum(expenses_values)
        # put all the expenses values in  expenses_plot dictioanry for each category, on each month.
        for i in expense_category:
            if i not in expenses_plot:
                expenses_plot[i] = [0] * len(data)
            expenses_plot[i][list(data).index(month)] = expenses_values[expense_category.index(i)]

        # Savings
        savings = data[month]["Savings"]
        savings_values = list(savings.values())
        savings_values = [sum(i.values()) for i in savings_values]
        savings_category = list(savings.keys())
        total_savings = sum(savings_values)
        # put all the savings values in  savings_plot dictioanry for each category, on each month.
        for i in savings_category:
            if i not in savings_plot:
                savings_plot[i] = [0] * len(data)
            savings_plot[i][list(data).index(month)] = savings_values[savings_category.index(i)]


        # Calculate the cashflow for each month
        

        cashflow.append(total_incomes + total_expenses + total_savings)

        # Print the cashflow for each month
    print(cashflow)
    # plot the cashflow over time
    plt.figure(figsize=(10, 5))
    plt.plot(list(data), cashflow, label='Cashflow')
    # plot the summation of the cashflow over time
    plt.plot(list(data), [sum(cashflow[:i]) for i in range(1, len(cashflow)+1)], label='Cumulative Cashflow')
    plt.xlabel('Month')
    plt.legend(loc='best')

    # let's move the Bill Payement, MB-QUESTRADE from expenses to savings
    # remove the Bill Payement, MB-QUESTRADE from expenses_plot dictionary
    invest = expenses_plot.pop('Bill Payment')
    # add the Bill Payement, MB-QUESTRADE to savings_plot dictionary and make the values negative
    # savings_plot['Bill Payment'] = invest
    # make all the values in the saving_plot dictionary negative
    # savings_plot = {k: [-i for i in v] for k, v in savings_plot.items()}
    

    # plot the  incomes_plot, expenses_plot, savings_plot
    plt.figure(figsize=(10, 5))
    plt.stackplot(list(data), incomes_plot.values(), labels=incomes_plot.keys())
    plt.legend(loc='upper left')
    plt.title('Income')

    plt.figure(figsize=(10, 5))
    plt.stackplot(list(data), expenses_plot.values(), labels=expenses_plot.keys())
    plt.legend(loc='best')
    plt.title('Expenses')

    plt.figure(figsize=(10, 5))
    plt.stackplot(list(data), savings_plot.values(), labels=savings_plot.keys())
    # plot the negative of invest on the same graph as savings
    plt.plot(list(data), [-i for i in invest], label='Invested')
    plt.title('Savings')
    plt.legend(loc='best')


    plt.show()

Restricted content! log in or register for free.

Join Upaspro to get email for news in AI and Finance

3 thoughts on “Cashflow tracker with AI

  • Melina

    So useful I really appreciated this!!

    Reply
  • Robert Mclery

    That is actually really handy tool. As a newbie in python, what do you suggest to be able to change the code to personalize it.

    Reply
    • Hi Robert,
      Thanks for your question. We offer a 10-session pack to get you ready to work on your own python project which requires no prior coding knowledge. We recommend you to take a look at this link.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses User Verification plugin to reduce spam. See how your comment data is processed.