
#資料檔 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()
https://wangjhihyu0207.blogspot.com/2024/05/python60.html
回覆刪除作者已經移除這則留言。
回覆刪除D11017153 王芷榆
回覆刪除https://wangjhihyu0207.blogspot.com/2024/05/python60.html
https://takingfinance.blogspot.com/2024/05/python60.html
回覆刪除D11017125江宥蓉
https://shiwanchun69.blogspot.com/2024/05/python60.html
回覆刪除D11017312陳泓錡
回覆刪除https://stephen-ky.blogspot.com/2024/05/python60.html
D11050131鄭智元
回覆刪除https://ryu2022.blogspot.com/2024/05/python60.html
D11017157 陳名君
回覆刪除https://takming12345.blogspot.com/2024/05/python60.html
D11017257蔡欣辰
回覆刪除https://takming123.blogspot.com/2024/05/python60.html
D11017202吳睿捷
回覆刪除https://draft.blogger.com/blog/post/edit/preview/6174802439427569065/8288113528387254380
https://yu-han18.blogspot.com/2024/05/python60.html
回覆刪除D11017121 江舜廷 https://ting1008finan.blogspot.com/2024/05/python60.html
回覆刪除https://jasmine4132231.blogspot.com/2024/05/0520.html
回覆刪除D11017108李嘉芊
回覆刪除https://jia-0909.blogspot.com/2024/05/python60.html?lr=1716177986441&m=1
D11017311陳佑愷
回覆刪除https://danley1116.blogspot.com/2024/05/python60.html
D11017352 張弘陽
回覆刪除https://35607161.blogspot.com/2024/06/python60.html