Investing for the Future: A Python Code for Calculating Investment Returns with Dollar-Cost Averaging

A Step-by-Step Tutorial for Calculating Your Investment ROI with Python

Yanwei Liu
2 min readApr 13, 2023

Introduction:

Investing can be a daunting task, especially if you’re not sure where to start. However, one of the most effective ways to invest is through dollar-cost averaging (DCA). DCA is a strategy that involves investing a fixed amount of money in an asset at regular intervals, regardless of the asset’s price. This strategy can help investors take advantage of market fluctuations and reduce the risk of investing at a market high.

In this article, we will introduce a Python code that implements DCA for multiple assets, including cryptocurrencies and stocks. This code can help investors calculate their investment returns, profit, and ROI using the DCA strategy.

Code:

Code Strategy:

The Python code we will introduce in this article uses the yfinance and pycoingecko packages to fetch historical prices of stocks and cryptocurrencies, respectively. It then uses the dollar-cost averaging strategy to calculate the total investment, final value, profit, and ROI for each asset.

The code begins by defining a list of assets that the user wants to invest in, along with the monthly investment amount and the starting date. The code then loops through each asset, fetches its historical prices, and calculates the investment results using the dollar-cost averaging strategy.

The get_monthly_prices_stock() function fetches the historical prices of stocks using the yfinance package, while the get_monthly_prices() function fetches the historical prices of cryptocurrencies using the pycoingecko package. Both functions return the prices of the asset for the first day of each month starting from the starting date.

The dollar_cost_average() function implements the dollar-cost averaging strategy by calculating the total investment, total coins bought, and the average cost price of the asset. The calculate_roi() function then calculates the ROI of the investment.

The results for each asset are printed to the console, including the total investment, final value, profit, final ROI, and average cost price.

Conclusions

Overall, this Python code provides a simple yet effective way for investors to calculate their investment returns using the dollar-cost averaging strategy for multiple assets. By regularly investing a fixed amount of money, investors can take advantage of market fluctuations and achieve their investment goals.

Disclaimer:

The Python code presented in this article is for educational and informational purposes only. It is not intended as investment advice or as a recommendation to buy, sell or hold any particular asset. The author is not a financial advisor and is not responsible for any investment decisions made by the reader. The reader should conduct their own research and analysis before making any investment decisions. The author is not liable for any losses or damages that may arise from the use of this code.

--

--

No responses yet