话不多说先上代码:
#请使用前复权的日K线数据import os#上涨天数upCount = 0#下跌天数downCount = 0#打开日志文件fs = open(r'C:\Py\result.txt','a+')#循环遍历所有的日K线文件for root, dirs, files in os.walk(r'C:\Py\day'): for file in files: #写日志 print('正在计算' + file) #打开日K线文件 fs2 = open(os.path.join(root,file), 'r', True) #昨日收盘价 lastClose = 0 #索引 pos = 0 #循环遍历每一行 while True: #读取该行 line = fs2.readline() #没有行的时候退出 if not line: break #去除前2行和尾行 if pos > 1 and len(line) > 20: #分割字符串 strs = line.split(',') #收盘价 closePrice = float(strs[4]) #开盘价 openPrice = float(strs[1]) if pos == 2: lastClose = openPrice if closePrice > lastClose: upCount = upCount