Python program for the Gold Digger
Gold prices are of significant interest to investors and enthusiasts, especially Indian women. In this article, I'll explain how to fetch real-time gold prices from Yahoo Finance, focusing on gold futures, and visualize the trends using Python.
For this you need to have some knowledge of Python and how to run these codes on any Python environment, I use Jupyter notebook for its easiness.
With these codes you can fetch and visualize Gold prices (24K) for any range of dates.
The following are the steps:
1) Install yfinance:
Begin by installing the yfinance library using the following command:
pip install yfinance
2) Fetching Real-Time Data:
Now that we have the yfinance library installed, let's proceed to fetch real-time gold prices using Python codes as given below.
import yfinance as yf
import matplotlib.pyplot as plt
# Use the correct ticker symbol for gold, for example, 'GC=F' (Gold Futures)
gold_ticker = 'GC=F'
# Fetch historical data
gold_data = yf.download(gold_ticker, start="2022-01-01", end="2023-01-01")
In this example, ‘GC=F’ represents Gold Futures on Yahoo Finance. Ensure that you check the available ticker symbols for gold on Yahoo Finance and replace ‘GC=F’ with the appropriate symbol for your analysis.
You can customize the dates by selecting start and end dates as required.
3) Plot
Now, we have fetched the data, let's use matplotlib to plot the data and visualize the trends.
# Plotting
plt.figure(figsize=(10, 6))
plt.plot(gold_data['Close'], label='Gold Price', color='gold')
plt.title('Gold Price Trend Over Time')
plt.xlabel('Date')
plt.ylabel('Gold Price (USD)')
plt.legend()
plt.grid(True)
plt.show()
Using the yfinance library in Python allows us to easily fetch real-time gold prices. This provides a practical solution for individuals and businesses looking to stay informed about the dynamics of the precious metal using the available data from Yahoo Finance.
Plot Twist :
17 likes