def loadHistory(Code, startYear, startMonth, FinalYear, FinalMonth): stock = Stock(str(Code)) monthList = list() for year in range(startYear, FinalYear): for month in range(1, 13): stock.fetch(year, month) for index in range(len(stock.data)): newItem = list(stock.data[index]) newDate = newItem.pop(0) newItem.insert(0, newDate.strftime("%Y-%m-%d")) monthList.append(newItem) time.sleep(3) print("========================================================") print("= Load ", year, " finish =") print("========================================================") i = 1 if FinalYear > startYear else startMonth for month in range(i, FinalMonth + 1): stock.fetch(FinalYear, month) for index in range(len(stock.data)): newItem = list(stock.data[index]) newDate = newItem.pop(0) newItem.insert(0, newDate.strftime("%Y-%m-%d")) monthList.append(newItem) time.sleep(3) print("========================================================") print("= Load ", str(Code), " finish =") print("========================================================") return monthList
def test_load_historical_data_months(code, year, begin_month, end_month): """ Goal ---------- Download historical data and basic unit is one month. Parameters ---------- code : int or str year : int begin_month : int end_month : int TODO ---------- 1. Thinl trade suspension case 2. """ data_list = [] if begin_month > end_month: return data_list stock = Stock(str(code)) print("\n[" + str(code) + "]") for month in range(begin_month, end_month+1): time.sleep(5) stock.fetch(year, month) print(str(year) + '/' + str(month) + ' \tfinish') for index in range(len(stock.data)): new_data = list(stock.data[index]) new_data[0] = stock.data[index][0].strftime("%Y-%m-%d %H:%M:%S") data_list.append(new_data) #if (month) --- check is empty? # 也有可能是停牌 # 回傳錯誤,並說明原因 return data_list