劉任昌PYTHON回溯測試過去60天

#資料檔  https://drive.google.com/file/d/1eB8BT2dLb-I8u7EQmoQ4O1TdRG7oLUKq/view?usp=drive_link
import csv                     #輸入csv套件comma separated value
file = open('SPY.csv','r')     #打開下載的檔案SPY.CSV,模式是r讀取,
csvreader = csv.reader(file)   #將檔案逐列讀入串列變數csvreader
header, rows = [], []           #宣告空白串列(陣列,清單)
header = next(csvreader)       #串列header儲存檔案第一列
for row in csvreader:          #檔案接續逐列附加append於rows串列
   rows.append(row)
file.close()             #關閉檔案
header.extend(['MA20','部位','最大','獲利','最小','損失']) #第7至12欄
for i in range(19):
   rows[i].extend([rows[i][5],0]) #第7欄用收盤當20日平均,第8欄部位0
for i in range(19,len(rows)):
   sum20 = 0.0      #實數=浮點數
   for j in range(i-19, i+1):
      sum20 += float(rows[j][5])
   rows[i].extend([sum20/20, 0])    #第7欄20日平均,第8欄部位0
listx = [y/1000 for y in range (31)]#測試31個突破點0, 0.1%...3%
for i in range(80,len(rows)):   #目標是len(rows)
   listgain = [0.0 for y in range (31)]#儲存31個突破點的損益,預設0
   listextreme = [0, 0.0, 0, 0.0]      #儲存最大值、最小值
   for k in range(len(listx)):         #測試特定參數
      for j in range (i-60,i):
         dayClose = float(rows[j][5])
         if rows[j-1][8]< 1 and dayClose > rows[j-1][7]*(1+listx[k]):
            rows[j][8]=1 #做多
         elif rows[j-1][8]> -1 and dayClose < rows[j-1][7]*(1-listx[k]):
            rows[j][8]=-1 #做空
         else:
            rows[j][8]=rows[j-1][8] #維持原來的部位
      for j in range (i-60,i):     #計算損益
         dayGain = float(rows[j][6])-float(rows[j-1][6])
         if rows[j-1][8] == 1:
            listgain[k] += dayGain
         elif rows[j-1][8] == -1:
            listgain[k] += - dayGain
         else: pass #沒有損益
      if listextreme[1] < listgain[k]: #測試是否更大ctrl shift L
         listextreme[0] = k
         listextreme[1] = listgain[k]
      if listextreme[3] > listgain[k]: #測試是否更小
         listextreme[2] = k
         listextreme[3] = listgain[k]
   rows[i].extend(listextreme)   #形成9至12欄
file = open('SPYW.CSV','w',newline='',encoding='utf-8')
w = csv.writer(file) #寫入檔案
w.writerow(header)   #寫入標題
w.writerows(rows)    #第7列將檔案內容名稱rows
file.close()

留言

  1. https://wangjhihyu0207.blogspot.com/2024/05/python60.html

    回覆刪除
  2. D11017153 王芷榆
    https://wangjhihyu0207.blogspot.com/2024/05/python60.html

    回覆刪除
  3. https://takingfinance.blogspot.com/2024/05/python60.html
    D11017125江宥蓉

    回覆刪除
  4. https://shiwanchun69.blogspot.com/2024/05/python60.html

    回覆刪除
  5. D11017312陳泓錡
    https://stephen-ky.blogspot.com/2024/05/python60.html

    回覆刪除
  6. D11050131鄭智元
    https://ryu2022.blogspot.com/2024/05/python60.html

    回覆刪除
  7. D11017157 陳名君
    https://takming12345.blogspot.com/2024/05/python60.html

    回覆刪除
  8. D11017257蔡欣辰
    https://takming123.blogspot.com/2024/05/python60.html

    回覆刪除
  9. D11017202吳睿捷
    https://draft.blogger.com/blog/post/edit/preview/6174802439427569065/8288113528387254380

    回覆刪除
  10. https://yu-han18.blogspot.com/2024/05/python60.html

    回覆刪除
  11. D11017121 江舜廷 https://ting1008finan.blogspot.com/2024/05/python60.html

    回覆刪除
  12. https://jasmine4132231.blogspot.com/2024/05/0520.html

    回覆刪除
  13. D11017108李嘉芊
    https://jia-0909.blogspot.com/2024/05/python60.html?lr=1716177986441&m=1

    回覆刪除
  14. D11017311陳佑愷
    https://danley1116.blogspot.com/2024/05/python60.html

    回覆刪除
  15. D11017352 張弘陽
    https://35607161.blogspot.com/2024/06/python60.html

    回覆刪除

張貼留言

這個網誌中的熱門文章

2025年3月7日福斯汽車與遊戲驛站軋工事件

劉任昌利用Python讀取SPY日成交資料(繳交作業情形)

劉任昌python讀取CSV檔案SPDR標普500ETF在1993/1/29至2025/3/20