빅데이터 김교수의 "AI노마드연구소" AI로 열어가는 노마드 세상!

빅데이터 김교수의 "AI노마드연구소" AI로 열어가는 노마드 세상입니다. AI 코딩작성, SNS 분석, AI업무자동화 컨설팅 0507-1419-0222

자세히보기

교육/인공지능교육

"5분만에 ChatGPT로 Tesla와 Apple 주가차트 분석하기"

빅데이터 김교수 2023. 1. 27. 00:52

ChatGPT는 OpenAI가 개발한 프로토타입 대화형 인공지능 챗봇이다. ChatGPT는 대형 언어 모델 GPT-3의 개선판인 GPT-3.5를 기반으로 만들어졌으며, 지도학습과 강화학습을 모두 사용해 파인 튜닝되었다

 chatGTP는 프롬프트라는 대화형 문장으로 사람과 소통하며, 사람의 질문과 요청에 반응한다. 특히, 코딩영역에선 탁월함을 보인다.

 오늘은 chatGTP와 대화를 통해 테슬라와 애플의 주가를 분석하는 프로그램을 작성해 보겠다.

 나는 chatGTP에 말한다. "make python code for tesla and apple stock anlysis"(파이썬 코드를 작성해줘, 애플과 테슬라 주식을 분석하는"

chatGTP는 답한다. I can provide some sample Python code for analyzing the historical stock prices of Tesla and Apple, but please note that this is just a starting point and would likely need to be modified and expanded upon for a more robust analysis. (Tesla 및 Apple의 과거 주가를 분석하기 위한 일부 샘플 Python 코드를 제공할 수 있지만 이는 시작점일 뿐이며 보다 강력한 분석을 위해 수정 및 확장이 필요할 수 있습니다)

) 그리고 코드를 쏫아낸다.

import pandas as pd
import yfinance as yf

# Get historical stock data for Tesla and Apple
tesla = yf.Ticker("TSLA")
apple = yf.Ticker("AAPL")
tesla_df = tesla.history(period="max")
apple_df = apple.history(period="max")

# Calculate the average closing price for each stock
tesla_avg_close = tesla_df["Close"].mean()
apple_avg_close = apple_df["Close"].mean()
print("Tesla average closing price: ", tesla_avg_close)
print("Apple average closing price: ", apple_avg_close)

# Plot the closing prices for each stock over time
import matplotlib.pyplot as plt
tesla_df["Close"].plot()
apple_df["Close"].plot()
plt.xlabel("Year")
plt.ylabel("Closing Price")
plt.title("Tesla and Apple Stock Prices")
plt.legend(["Tesla", "Apple"])
plt.show()

# Compare the stock performance of Tesla and Apple
tesla_return = (tesla_df["Close"][-1] - tesla_df["Close"][0]) / tesla_df["Close"][0]
apple_return = (apple_df["Close"][-1] - apple_df["Close"][0]) / apple_df["Close"][0]
print("Tesla return: ", tesla_return)
print("Apple return: ", apple_return)

 야후 파이낸스를 통해 테슬라와 애플의 주가를 보여주는 파이썬 코드를 작성해 낸다. 

 나는 다시 말한다. "테슬라와 애플의 10년간 주가 데이터를 그리는 파이썬 코드를 작성해줘"

 챗지피티는 빠르게 코드를 쏫아낸다. 

 

import yfinance as yf
import matplotlib.pyplot as plt

# Define the ticker symbols and the date range for the data
tesla_ticker = "TSLA"
apple_ticker = "AAPL"
start_date = "2011-01-01"
end_date = "2023-01-01"

# Download historical stock data for Tesla and Apple
tesla_stock_data = yf.download(tesla_ticker, start=start_date, end=end_date)
apple_stock_data = yf.download(apple_ticker, start=start_date, end=end_date)

# Plot the closing prices for each stock over time
tesla_stock_data["Close"].plot()
apple_stock_data["Close"].plot()
plt.xlabel("Year")
plt.ylabel("Closing Price")
plt.title("Tesla and Apple Stock Prices")
plt.legend(["Tesla", "Apple"])
plt.show()

나는 이코드를 코랩(Colab)에 카피하고 실행한다. 결과는 다음과 같이 이쁜 그래프가 완성된다.

 나는 "Tesla와 Apple의 return 값이 무엇인가?" 라고 물었다. 

 

 챗지피트는

 "주식의 수익률은 특정 기간 동안 주식이 얼마나 얻었는지 또는 잃었는지 측정한 것입니다. 앞서 제공한 예제 코드에서 Tesla와 Apple의 반환 값은 최종 종가와 초기 종가의 차이를 초기 종가로 나눈 값으로 계산됩니다. 이것은 종종 백분율로 표시되므로 0.2의 반환 값은 주식이 지정된 기간 동안 20% 상승했음을 의미합니다.

예를 들어 Tesla의 초기 종가가 $100이고 최종 종가가 $120인 경우 반환 값은 (120 - 100) / 100 = 0.2 또는 20%입니다. 즉, 투자자가 Tesla 주식을 초기 종가에 사서 최종 종가에 팔았다면 투자 수익의 20%를 얻었을 것입니다.

반환 값은 특정 기간 동안 주식 실적의 척도일 뿐이며 미래 실적을 예측하는 데 사용해서는 안 됩니다. 또한 종가를 기반으로 할 뿐만 아니라 Open, High, Low, Volume과 같은 다른 매개 변수도 반환 값을 결정하는 역할을 합니다." 라고 답변한다. 

 그래서 기간상의 비교기준이 틀려서 이런차이가 있나해서 기간을 줄여서 리턴값을 확인했는데 결과값은 동일했다. 

 코딩상으론 테슬라의 수익율은 98%, 애플의 수익율을 1,437% 인것로 보여진다. 다음시간에는 향후 전망을 주가에 영향을 주는 독립변수를 찾아  종속변수로 주가가 어떻게 예측되는지 확인해보겠다. 물론 이때, 사용하는 머신러닝알고리즘도 챗지피티에서 제안하도록 하겠다.