## Optimizing the Savings Calculation on Direct Mutual Funds7 min read

There are two categories of Mutual Funds namely Direct Mutual Fund and Regular Mutual Fund.

**What are Direct Mutual Funds? **

Direct Mutual Funds is the type of mutual fund that is directly offered by the AMC or fund house. In other words, there is no involvement of third party agents – brokers or distributors. Since there are no third party agents involved, there are no commissions and brokerage.

**What are Regular Mutual Funds?**

Regular plans are those mutual fund plans that are bought through an intermediary. These intermediaries can be brokers, advisors, or distributors. The intermediaries charge the fund house a certain fee for selling their mutual funds.

**What is Expense Ratio? **

The expense ratio is the percentage that denotes the amount of money you are paying to the AMC as a fee to manage your investments. In other words, it is the per-unit cost for running and managing the mutual fund. The expense ratio differs from one mutual fund to another. You do not pay for this expense ratio separately; it is calculated as a percentage of the daily investment value.

For example, if you invest Rs 5000 in a mutual fund with an expense ratio of 2%, then (2%/365=0.0054%) will be deducted from the investment value each day. The per-day levying of the expense ratio ensures that you only pay for the period you stay invested. But this deduction of the expense ratio is lowering your returns by a tiny amount every day. Hence, a mutual fund scheme with a lower expense ratio is more beneficial to you because it takes away a lesser portion of money from your returns.

**Mutual Funds in Paytm Money**

All Mutual Funds in Paytm Money are Direct Mutual Funds. Hence Paytm Money ensures investors to save their money since expense ratio is lower compared to regular funds.

Hence a new amazing feature has been proposed to show the savings card to the users in the portfolio. This card contains the information of total savings till date by investing in Paytm Money Platform.

**Formula to Calculate Savings**

For every Transaction, savings is calculated on a particular day. This formula has been taken from Compound Interest calculation.

**Savings** = (**Transaction_Sign**)(txn_amount) [(1+expense_ratio)^{days_till_today / 365} – 1]

Txn_type |
Transaction_Sign |

BUY | +1 |

SELL | -1 |

The expense ratio varies from one mutual fund to another. After the analysis by the product team, expense ratio has been kept the same across all funds which is **one percent**.

**Straight Approach:**

Below are the steps to find total savings using straight approach :

- Let’s assume the user is having 500 transactions till today.
- Fetch all the valid transactions . Each transaction will contain txn_amount, txn_date and txn_type.
- The savings for each transaction will be calculated using the above mentioned formula. In order to use the above formula, the number of days from a transaction date till today has to be deduced.
- Then total savings is derived after adding each savings.

**Total Savings**= Savings_{1}+ Savings_{2}+ Savings_{3}+ ........Savings_{N}

where N is the number of transactions done by the user.

This will be stored in database table like

userId |
total_savings |
as_on_date |

1234 | 2000 | 2022-03-16 |

**Problem with the above Approach:**

- Calculating a total savings for each user involves many steps.
- The cron is required to calculate the total savings and store it in a table. The table will be used for redirecting the total savings amount to the front-end.
- If it is a daily cron, the CPU and memory consumption of servers will be significant along with other heavy jobs since enormous calculations and bulky database calls are involved here.
- If it is a weekly cron, the user will not be knowing the updated savings everyday. The next savings would update after every week. The user could do many transactions in the interval of week and these transactions would not contribute in total savings until the next cron triggers.

**Optimized Approach:**

The aim of the optimized approach is to store the previously calculated savings and use the same for the subsequent days instead of fetching all the transactions from the first day.

Optimised Approach involves simplification of the formula.

**Simplification:**

1- Assume there are three transactions present for a user.

txn_type |
txn_amount |
days_till_now |

BUY | x | a |

BUY | y | b |

SELL | z | c |

2- Expense ratio is “**r**“.

**Total savings as on till date **

= [x(1+r)^{a/365}-x] + [y(1+r)^{b/365}-y] - [z(1+r)^{c/365}- z] = (x(1+r)^{a/365}+ y(1+r)^{b/365}- z(1+r)^{c/365}) - (x + y - z)

`Considering “`**x + y - z**” is equal to **Net_Txn_Amount
**

`= (x(1+r)`^{a/365} + y(1+r)^{b/365} - z(1+r)^{c/365}) - **Net_Txn_Amount**

`Considering (x(1+r)`^{a/365} + y(1+r)^{b/365} - z(1+r)^{c/365}) as **Net_Investment_Change**

`= `**Net_Investment_Change** - **Net_Txn_Amount**

As one day passes, the total savings has to be calculated for (t+1)th day

txn_type |
txn_amount |
days_till_now |

BUY | x | a+1 |

BUY | y | b+1 |

SELL | z | c+1 |

Total savings as on (t+1)th date =

= [x(1+r)^{a+1/365}-x] + [y(1+r)^{b+1/365}-y] - [z(1+r)^{c+1/365}- z] = (x(1+r)^{a+1/365}+ y(1+r)^{b+1/365}- z(1+r)^{c+1/365}) - (x + y - z) = (x(1+r)^{(a/365)(1/365)}+ y(1+r)^{(b/365)(1/365)}- z(1+r)^{(c/365)(1/365)}) -Net_Txn_AmountSince “x + y - z” is equal toNet_Txn_Amount= (1+r)^{1/365}[x(1+r)^{a/365}+ y(1+r)^{b/365}- z(1+r)^{c/365}] -Net_Txn_Amount// Taking out common multiplier(1+r)= (1+r)^{1/365}^{1/365}[Net_Investment_Change] -Net_Txn_Amount// becauseNet_Investment_Change= x(1+r)^{a/365}+ y(1+r)^{b/365}- z(1+r)^{c/365}

Hence total savings on (t+1)^{th}date =(1+r)^{1/365}[ Net_Investment_Change ] - Net_Txn_AmountTherefore total savings on (t+q)^{th}date =(1+r)^{q/365}[ Net_Investment_Change ] - Net_Txn_Amount

**How has the simplified formula been used ?**

**1-** A database table in portfolio-insights called **total_savings_info** is created.

userId |
net_investment_change |
net_transaction_amount |
total_savings |
as_on_date |

**2-** Let’s assume, today is March 16, 2022. The new transaction has been received for the userId : 1234 with the following details

userId |
txn_amount |
txn_type |
txn_date |

1234 | 1,00,000 | BUY | 2022-03-14 |

**3-** The **total_savings_info** table is populated once the new transaction has been received.

userId |
net_investment_change |
net_transaction_amount |
total_savings |
as_on_date |

1234 | 100005.45 | 100000 | 5.452 | 2022-03-16 |

**4-** Let’s suppose the user lands on a portfolio page on the next day which is March 17, 2022. The savings till 2022-03-17 will be calculated on the fly using the simplified formula.

Total savings on (t+q)^{th}date =(1+r)^{q/365}[ Net_Investment_Change ] - Net_Txn_AmountHere the “q” is 1 because the difference between 2022-03-16 and 2022-03-17 is 1.Total Savings= (1+r)^{1/365}[ Net_Investment_Change ] - Net_Txn_Amount = (1.000027)[100005.45] - 100000 = 8.178

Whenever there is a new transaction, , **total_savings_info **table is populated from the scratch for the given userId. Therefore net_investment_change, net_transaction_amount and total_savings will be changed in case of a new transaction.

Thus the pre-calculated values are used to calculate the savings for any subsequent days.

**Advantages of Optimized Approach: **

- This will save a lot of computations to calculate the savings for every user.
**With the above formula, the daily or weekly cron is not required.**Since as_on_date is saved in the total_savings_info table.

Thus the number of days from as_on_date till date is considered as “q” and below formula can be used.

Total savings on (**t+q**)^{th}date =**(1+r)**^{q/365}**[ Net_Investment_Change ] – Net_Txn_Amount.**

**Hence the total savings can be calculated on the fly whenever there is a request rather than updating in the database all the time.**- The CPU and memory utilization of servers will be reduced significantly if the cron is not configured for the bulky computation.
- Currently, there are around 6 lakh active users in the mutual fund. Calculating savings for all of them would have incurred cost on infrastructure because this would have taken around 1-2 hours to complete the process

Tech Design : Shreyas Hosur

Developers : Shreyas Hosur, Karan Tiwari

Product : Harpreet Bhatoa