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.append('MA20') #第7欄,為header串列list新曾元素
header.append('部位') #第8欄
header.append('最大') #第9欄
header.append('獲利') #第10欄
header.append('最小') #第11欄
header.append('損失') #第12欄
for i in range(20):
rows[i].append(rows[i][5])
for i in range(20,len(rows)):
sum20 = 0.0 #實數=浮點數
for j in range(i-20, i):
sum20 += float(rows[j][5])
rows[i].append(sum20/20) #第7欄20日平均
rows[i].append(0) #第8欄預設部位0
listx = [y/1000 for y in range (31)] #測試31個突破點0, 0.1%...3%
print('測試用的參數\n',listx)
'''for i in range(80,len(rows)):
listgain = [0.0 for y in range (31)] #儲存31個突破點的損益,預設0
for x in range(len(listx)): #測試特定參數
for j in range (i-60,i):
if row[j-1][8]小於1 and row[j][5]大於row[j-1][7]*(1+listx[x]):
row[j][8]=1 #做多
elif row[j-1][8]大於-1 and row[j][5]小於row[j-1][7]*(1-listx[x]):
row[j][8]=-1 #做空
else:
row[j][8]=row[j-1][8] #維持原來的部位
for j in range (i-60,i):
if row[j-1][8] == 1:
listgain[x] += row[j][6] - row[j-1][6]
elif row[j-1][8] == -1:
listgain[x] += row[j-1][6] - row[j][6]
else:
pass #沒有損益'''
file = open('SPYW.CSV','w',newline='',encoding='utf-8')
w = csv.writer(file) #寫入檔案 VScode是IDE(程式)整合開發環境
w.writerow(header) #第5列將檔案欄位名稱列header
w.writerows(rows) #第7列將檔案內容名稱rows
file.close()
D11017108李嘉芊
回覆刪除https://jia-0909.blogspot.com/2024/05/vs-codecsvappendencodingutf8.html
金三乙洪均諺D11017243
回覆刪除https://yan911222.blogspot.com/2024/05/vs-codecsvappendencoding.html
D11017153王芷榆
回覆刪除https://wangjhihyu0207.blogspot.com/2024/05/vs-codecsvappendencodingutf8.html
D11017202吳睿捷 https://draft.blogger.com/blog/post/edit/preview/6174802439427569065/8739172317689106664
回覆刪除D11017157 陳名君
回覆刪除https://takming12345.blogspot.com/2024/05/vs-codecsvappendencodingutf8.html
D11017257蔡欣辰
回覆刪除https://takming123.blogspot.com/2024/05/vs-codecsvappendencodingutf8.html
D11017312 陳泓錡
回覆刪除https://stephen-ky.blogspot.com/2024/05/vs-codecsvappendencodingutf8.html
D11017125江宥蓉
回覆刪除https://takingfinance.blogspot.com/2024/05/vs-codecsvappendencodingutf8.html
D11017311陳佑愷
回覆刪除https://danley1116.blogspot.com/2024/05/vs-codecsvappendencodingutf8.html