台灣50各股市場風險比較

Описание к видео 台灣50各股市場風險比較

00:00:00 讀入與整理資料
00:29:36 變數迴圈、回歸與Beta係數
00:57:41 結合Python與RapidMiner設定


注意事項:使用Subprocess (Caching)運算式需先下載Operator Toolbox extension
參考資料:
1. 富時臺灣證券交易所臺灣50指數(FTSE TWSE Taiwan 50 Index)https://zh.wikipedia.org/wiki/%E8%87%...
2. 資產報酬計算 Asset Return Calculations https://www.mropengate.com/2015/07/ch...
3. 臺灣證券交易所發行量加權股價指數成分股暨市值比重 https://www.taifex.com.tw/cht/9/futur...

Python 程式:

import datetime
import pandas as pd
import io
import yfinance as yf

def rm_main():
tickers = ['^TWII', '1101.TW', '1216.TW', '1301.TW', '1303.TW', '1326.TW', '2002.TW', '2207.TW',
'2301.TW', '2303.TW', '2308.TW', '2317.TW', '2327.TW', '2330.TW', '2345.TW', '2357.TW', '2379.TW',
'2382.TW', '2395.TW', '2412.TW', '2454.TW', '2603.TW', '2609.TW', '3017.TW', '2880.TW', '2881.TW',
'2882.TW', '2883.TW', '2884.TW', '2885.TW', '2886.TW', '2887.TW', '2890.TW', '2891.TW', '2892.TW',
'2912.TW', '3008.TW', '3034.TW', '3037.TW', '3045.TW', '3231.TW', '3661.TW', '3711.TW', '4904.TW',
'4938.TW', '5871.TW', '5876.TW', '5880.TW', '6446.TW', '6505.TW', '6669.TW']

Create an in-memory Excel file
excel_buffer = io.BytesIO()

Use ExcelWriter to write multiple sheets to one Excel file in memory
with pd.ExcelWriter(excel_buffer, engine='openpyxl') as writer:
for ticker in tickers:

df = yf.download(ticker, start="2021-01-01", end="2024-12-05")
df.reset_index(inplace=True)

Change Date format to MM/dd/yyyy
df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%m/%d/%Y')

Add suffix for each column with the ticker symbol (except the Date column)
newdf = df.add_suffix(f'_{ticker}')
newdf.rename(columns={f'Date_{ticker}': 'Date'}, inplace=True) # Keep the Date column as is

Write to the in-memory Excel file with a separate sheet for each ticker
newdf.to_excel(writer, index=False, sheet_name=ticker)


Save the entire Excel file in memory
excel_buffer.seek(0) # Move to the beginning of the buffer
binary_data = excel_buffer.read() # Read the buffer data for output to RapidMiner

Return the complete Excel file containing all tickers as separate sheets
return binary_data

Комментарии

Информация по комментариям в разработке