Skip to content

Instantly share code, notes, and snippets.

@yuyueugene84
Created May 18, 2018 06:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuyueugene84/c1397fb1e85979ef19a26ae20cf19216 to your computer and use it in GitHub Desktop.
Save yuyueugene84/c1397fb1e85979ef19a26ae20cf19216 to your computer and use it in GitHub Desktop.
# 打開你的 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