Created
May 18, 2018 06:10
-
-
Save yuyueugene84/c1397fb1e85979ef19a26ae20cf19216 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 打開你的 stock_price_data.xlsx 檔案 | |
wb = xw.Book(r'在這裏輸入你放置 Excel 檔案的位置') | |
# 從 檔案裡找出名為 2330 的試算表,存入 tsmc_sheet 這個變數裏 | |
tsmc_sheet = wb.sheets['2330'] | |
# 算出所有的報酬率 | |
for i in range(3, 100): | |
# 到從左數起第二欄,也就是 B 欄截取今日收盤價 | |
price_today = tsmc_sheet.cells(i, 2).value | |
# 到 B 欄截取迭代到的上一個 row, 也就是昨日收盤價 | |
price_yesterday = tsmc_sheet.cells(i-1, 2).value | |
# 算出該筆資料的日報酬率了 | |
daily_return = (price_today — price_yesterday) / price_yesterday | |
# 最後再將算出的日報酬率寫入同一列,從左數起第三欄,也就是 C 欄的儲存格 | |
tsmc_sheet.cells(i, 3).value = daily_return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment