上一次我們測試了autogen + gpt4的抓股價並畫圖的功能

這次我們再來試試,請他判別該股價是否trend up or trend down

我們整段程式碼都不變的形況下

只改動 task,請他辦別該股價是趨勢向上、向下、還是持平

task =check trend up, tend down or stable of stock price of 'TSM','META','TSLA' and 'MSFT', stock price date form this month begin to current YTD."

而autogen給我們回答則是如下,希望大家看完不要噴血

user_proxy (to coder): check trend up, tend down or stable of stock price of 'TSM','META','TSLA' and 'MSFT', stock price date form this month bengin to current YTD.
--------------------------------------------------------------------------------
coder (to user_proxy): To solve this task, we can use the `yfinance` library in Python,
which allows us to download stock price data from Yahoo Finance.
We will download the data for the specified stocks from the beginning of this month to the current date.
Then, we will calculate the trend of each stock by comparing the latest price with the price at the beginning of the month.
If the latest price is higher, the trend is up. If it's lower, the trend is down.
If it's the same, the trend is stable. Here is the Python code to perform this task:
```python
import yfinance as yf
from datetime import datetime
import pandas as pd
# Define the stocks and the date range stocks = ['TSM', 'META', 'TSLA', 'MSFT']
start_date = datetime(datetime.now().year, datetime.now().month, 1)
end_date = datetime.now()
# Download the stock data data = yf.download(stocks, start=start_date, end=end_date)
# Calculate the trend
for each stock for stock in stocks:
first_price = data['Close'][stock].iloc[0]
last_price = data['Close'][stock].iloc[-1]
if last_price > first_price:
print(f"The trend for {stock} is up.")
elif last_price < first_price:
print(f"The trend for {stock} is down.")
else:
print(f"The trend for {stock} is stable.")

除了給了code,並且還將結論summary成下面的文字,但是我們可以從程式碼上發現他的邏輯只是將第一天與最後一天的股價做大小比較判斷,當最後一天比前一次的高,就判別試trend up

由此可見,下面的判斷不是很聰明,很容易出錯

coder (to user_proxy):

The code has been executed successfully and the trends for the stocks from the beginning of this month to the current date are as follows:

- The trend for TSM (Taiwan Semiconductor Manufacturing Company Limited) is down.
- The trend for META (Meta Platforms Inc., formerly Facebook) is down.
- The trend for TSLA (Tesla, Inc.) is down.
- The trend for MSFT (Microsoft Corporation) is up.

但是無論如何有了這樣的協助,其實我們可以自行將他組合起來,但仔細分析內部的程式碼,

這樣其實有一點弱,大家應該不滿足吧

但是我們可以再問問看他有沒有比較聰明的方法,於是我們新增了一句話

task =check trend up, tend down or stable of stock price of 'TSM','META','TSLA' and 'MSFT',
by statistical method to judge,
stock price date form this month begin to current YTD."

而autogen給我們的程式碼如下,一字不修改地給大家看看

# python code
import yfinance as yf
import pandas as pd
import numpy as np
from scipy import stats
from datetime import datetime

# Define the stocks
stocks = ['TSM', 'META', 'TSLA', 'MSFT']

# Get the current date
now = datetime.now()

# Get the first day of this month
start_date = datetime(now.year, now.month, 1)

# Download the stock data
data = yf.download(stocks, start=start_date, end=now) 

# For each stock, perform a linear regression on the closing prices
for stock in stocks:
    closing_prices = data['Close'][stock].dropna()
    slope, intercept, r_value, p_value, std_err = stats.linregress(np.arange(len(closing_prices)), closing_prices)
    
    # Based on the slope of the regression line, determine if the stock price is trending up, down, or stable
    if slope > 0:
        trend = 'up'
    elif slope < 0:
        trend = 'down'
    else:
        trend = 'stable'
    
    print(f'The trend of {stock} is {trend}.')

他的summary結果如下,他這次是抓2023/10/1至今的股價來分析與判斷,使用了斜率來分析股價趨勢,合理一點了吧

coder (to user_proxy):

The code has been executed successfully and the trends for the stocks from the beginning of this month to the current date are as follows:

- The trend for TSM (Taiwan Semiconductor Manufacturing Company Limited) is up.
- The trend for META (Meta Platforms Inc., formerly Facebook) is up.
- The trend for TSLA (Tesla, Inc.) is up.
- The trend for MSFT (Microsoft Corporation) is down.

[ChatBot] AutoGen - is stock t

從code與股價圖來看,這次比較聰明好一點,是利用斜率來判斷股價是否上升或下降

當然我們也可以自行修改一下"閥值",定義一下穩定的區間

例如 +/1 0.1

相信到這裡,各位可以自行試試看

也可以衍伸一點,將所有的股票的資料,都請電腦幫我們診斷一下

 

 

end

 

arrow
arrow

    Cheng yichung 發表在 痞客邦 留言(0) 人氣()