How to Plot from CSV using Python with Financial Indicator (Converting from yfinance code)
Question:
I have the code that is scraping from yfinance the stocks data of "ATVI", now what I want is:
- Use the CSV instead of scraping from yfinance.
- Modify the code so I am able to use the financial indicator SMA / Moving Average with the data from the CSV.
For example you can get the csv here, for GJTL stocks:
https://www.investing.com/equities/gajah-tunggal-historical-data
the data for the csv:
- Monthly
- Ordered from earliest to the newest (top row should be 2003 and bottom row should be year 2023)
this is the code (once again open source code from website, internet, google, not from blank page that I just type without need to learn from others):
import datetime as dt
import yfinance as yf
import matplotlib.pyplot as plt
company = 'ATVI'
# Define a start date and End Date
start = dt.datetime(2000,1,1)
end = dt.datetime(2023,1,1)
# Read Stock Price Data
data = yf.download(company, start , end)
#data.tail(10)
#print(data)
# Creating and Plotting Moving Averages
data["SMA1"] = data['Close'].rolling(window=50).mean()
data["SMA2"] = data['Close'].rolling(window=200).mean()
data['ewma'] = data['Close'].ewm(halflife=0.5, min_periods=20).mean()
plt.figure(figsize=(10,10))
plt.plot(data['SMA1'], 'g--', label="SMA1")
plt.plot(data['SMA2'], 'r--', label="SMA2")
plt.plot(data['Close'], label="Close")
plt.title("Activision Blizzard Stock Price 1/1/00 - 1/1/23")
plt.legend()
plt.show()
What is the Python code to load the csv and make the same output plot as the code above?
Answers:
I am sure I don’t understand the question still, but if you want to get the data from a csv, just do
data = pd.read_csv("mydata.csv", index_col=0, parse_dates = True)
If your columns are named in the usual way, you are done. If not, you have to rename them.
I have the code that is scraping from yfinance the stocks data of "ATVI", now what I want is:
- Use the CSV instead of scraping from yfinance.
- Modify the code so I am able to use the financial indicator SMA / Moving Average with the data from the CSV.
For example you can get the csv here, for GJTL stocks:
https://www.investing.com/equities/gajah-tunggal-historical-data
the data for the csv:
- Monthly
- Ordered from earliest to the newest (top row should be 2003 and bottom row should be year 2023)
this is the code (once again open source code from website, internet, google, not from blank page that I just type without need to learn from others):
import datetime as dt
import yfinance as yf
import matplotlib.pyplot as plt
company = 'ATVI'
# Define a start date and End Date
start = dt.datetime(2000,1,1)
end = dt.datetime(2023,1,1)
# Read Stock Price Data
data = yf.download(company, start , end)
#data.tail(10)
#print(data)
# Creating and Plotting Moving Averages
data["SMA1"] = data['Close'].rolling(window=50).mean()
data["SMA2"] = data['Close'].rolling(window=200).mean()
data['ewma'] = data['Close'].ewm(halflife=0.5, min_periods=20).mean()
plt.figure(figsize=(10,10))
plt.plot(data['SMA1'], 'g--', label="SMA1")
plt.plot(data['SMA2'], 'r--', label="SMA2")
plt.plot(data['Close'], label="Close")
plt.title("Activision Blizzard Stock Price 1/1/00 - 1/1/23")
plt.legend()
plt.show()
What is the Python code to load the csv and make the same output plot as the code above?
I am sure I don’t understand the question still, but if you want to get the data from a csv, just do
data = pd.read_csv("mydata.csv", index_col=0, parse_dates = True)
If your columns are named in the usual way, you are done. If not, you have to rename them.