def CalcuatePeriodDayData(self, period, aryDate, aryYClose, aryOpen, aryHigh, aryLow, aryClose, aryVol, aryAmount): # 新的周期数据 aryDate2 = [] aryYClose2 = [] aryOpen2 = [] aryHigh2 = [] aryLow2 = [] aryClose2 = [] aryVol2 = [] aryAmount2 = [] startDate = 0 newData = None count = len(aryDate) dayCount = 0 for i in range(count): isNewData = False date = aryDate[i] if period == 1: # 周线 fridayDate = IHQData.GetFirday(date) if fridayDate != startDate: isNewData = True startDate = fridayDate elif period == 2: # 月线 if int(date / 100) != int(startDate / 100): isNewData = True startDate = date elif period == 3: # 年线 if int(date / 10000) != int(startDate / 10000): isNewData = True startDate = date elif (period >= 40001 and period < 50000): dayPeriod = period - 40000 if (dayCount >= dayPeriod): dayCount = 0 if (dayCount == 0): isNewData = True startDate = date dayCount += 1 yClose = aryYClose[i] open = aryOpen[i] high = aryHigh[i] low = aryLow[i] close = aryClose[i] vol = aryVol[i] amount = aryAmount[i] if isNewData: if (newData != None): aryYClose2.append(newData.YClose) aryOpen2.append(newData.Open) aryHigh2.append(newData.High) aryLow2.append(newData.Low) aryClose2.append(newData.Close) aryVol2.append(newData.Vol) aryAmount2.append(newData.Amount) aryDate2.append(newData.Date) newData = HistoryData() newData.Date = date if open == None or close == None: continue newData.Open = open newData.High = high newData.Low = low newData.YClose = yClose newData.Close = close newData.Vol = vol newData.Amount = amount else: if newData == None: continue if open == None or close == None: continue if newData.Open == None or newData.Close == None: newData.Open = open newData.High = high newData.Low = low newData.YClose = yClose newData.Close = close newData.Vol = vol newData.Amount = amount else: if newData.High < high: newData.High = high if newData.Low > low: newData.Low = low newData.Close = close newData.Vol += vol newData.Amount += amount newData.Date = date if (newData != None): aryYClose2.append(newData.YClose) aryOpen2.append(newData.Open) aryHigh2.append(newData.High) aryLow2.append(newData.Low) aryClose2.append(newData.Close) aryVol2.append(newData.Vol) aryAmount2.append(newData.Amount) aryDate2.append(newData.Date) return { "date": aryDate2, "yclose": aryYClose2, "open": aryOpen2, "high": aryHigh2, "low": aryLow2, "close": aryClose2, "vol": aryVol2, "amount": aryAmount2 }
def GetKLineAPIData(self, symbol, period, right, startDate, endDate): # 复权 0=不复权 1=前复权 2=后复权 fq = None # 复权 if (right == 1): # 前复权 fq = "qfq" elif (right == 2): # 后复权 fq = "hfq" # 周期 0=日线 1=周线 2=月线 3=年线 4=1分钟 5=5分钟 6=15分钟 7=30分钟 8=60分钟 9=季线 10=分笔 freq = 'D' if (period == 1): freq = "W" elif (period == 2): freq = "M" elif (period == 4): freq = "1MIN" elif (period == 5): freq = "5MIN" elif (period == 6): freq = "15MIN" elif (period == 7): freq = "30MIN" elif (period == 8): freq = "60MIN" # 指数 if (IHQData.IsSHSZIndex(symbol)): fq = None try: print( "[TushareHQChartData::GetKLineAPIData] 指数 ts.pro_bar(ts_code={0}, adj={1}, start_date={2}, end_date={3}, freq={4}, asset='I')" .format(symbol, fq, startDate, endDate, freq)) df = ts.pro_bar(ts_code=symbol, adj=fq, start_date=str(startDate), end_date=str(endDate), freq=freq, asset='I') except Exception as e: print( '[TushareHQChartData::GetKLineAPIData] Error. throw exception {0},' .format(e)) return {"error": str(e)} else: try: print( "[TushareHQChartData::GetKLineAPIData] 股票 ts.pro_bar(ts_code={0}, adj={1}, start_date={2}, end_date={3}, freq={4})" .format(symbol, fq, startDate, endDate, freq)) df = ts.pro_bar(ts_code=symbol, adj=fq, start_date=str(startDate), end_date=str(endDate), freq=freq) # df = self.TusharePro.daily(ts_code=symbol, start_date='20200101', end_date='20201231') except Exception as e: print( '[TushareHQChartData::GetKLineAPIData] Error. throw exception {0},' .format(e)) return {"error": str(e)} df = df.sort_index(ascending=False) # 数据要降序排 print(df) cacheData = {} if (period in (0, 1, 2, 3, 9)): # 日期转int aryDate = df["trade_date"] aryDate[aryDate == ''] = 0 aryDate = aryDate.astype(np.int) dataCount = len(aryDate) cacheData['count'] = dataCount # 数据个数 cacheData["date"] = aryDate.tolist() else: aryDateTime = df["trade_time"] dataCount = len(aryDateTime) cacheData['count'] = dataCount # 数据个数 aryDateTime = pd.to_datetime(aryDateTime, format="%Y-%m-%d %H:%M:%S") print(aryDateTime) aryDate = [] aryTime = [] for item in aryDateTime: aryDate.append(item.year * 10000 + item.month * 100 + item.day) aryTime.append(item.hour * 100 + item.minute) cacheData["time"] = aryTime cacheData["date"] = aryDate cacheData['name'] = symbol # 股票名称 cacheData['period'] = period # 周期 cacheData['right'] = right # 不复权 cacheData["yclose"] = np.array(df["pre_close"]).tolist() cacheData["open"] = np.array(df["open"]).tolist() cacheData["high"] = np.array(df["high"]).tolist() cacheData["low"] = np.array(df["low"]).tolist() cacheData["close"] = np.array(df["close"]).tolist() cacheData["vol"] = np.array(df["vol"]).tolist() cacheData["amount"] = np.array(df["amount"]).tolist() log = "K线:{0} - period={1} right={2} count={3} date=[{4}, {5}]".format( symbol, period, right, dataCount, startDate, endDate) print(log) return cacheData
def GetKLineAPIData(self, symbol, period, right, startDate, endDate): filePath = '{0}/day/{1}.csv'.format(self.CachePath, symbol) klineData = pd.read_csv(filePath) isSHSZIndex = IHQData.IsSHSZIndex(symbol) # 是否是指数 aryDate = [] aryClose = [] aryYClose = [] aryOpen = [] aryHigh = [] aryLow = [] aryVol = [] aryAmount = [] aryDate = list(klineData["date"]) aryClose = list(klineData["close"]) aryYClose = list(klineData["yclose"]) aryOpen = list(klineData["open"]) aryHigh = list(klineData["high"]) aryLow = list(klineData["low"]) aryVol = list(klineData["vol"]) aryAmount = list(klineData["amount"]) # 复权 0=不复权 1=前复权 2=后复权 if (isSHSZIndex == False and right in (1, 2)): self.CalculateRightDayData(right, aryDate, aryYClose, aryOpen, aryHigh, aryLow, aryClose) # 周期 if (period in (1, 2, 3, 9) or (period >= 40001 and period < 50000)): periodData = self.CalcuatePeriodDayData(period, aryDate, aryYClose, aryOpen, aryHigh, aryLow, aryClose, aryVol, aryVol) aryDate = periodData["date"] aryYClose = periodData["yclose"] aryOpen = periodData["open"] aryHigh = periodData["high"] aryLow = periodData["low"] aryClose = periodData["close"] aryVol = periodData["vol"] aryAmount = periodData["amount"] cacheData = {} cacheData['count'] = len(aryDate) # 数据个数 cacheData['name'] = symbol # 股票名称 cacheData['period'] = period # 周期 cacheData['right'] = right # 复权 cacheData["date"] = aryDate cacheData["yclose"] = aryYClose cacheData["open"] = aryOpen cacheData["high"] = aryHigh cacheData["low"] = aryLow cacheData["close"] = aryClose cacheData["vol"] = aryVol cacheData["amount"] = aryAmount start = startDate end = endDate if (cacheData['count'] > 0): start = aryDate[0] end = aryDate[-1] log = "[TushareKLocalHQChartData::GetKLineAPIData] K线:{0} - period={1} right={2} count={3} date=[{4}, {5}]".format( symbol, period, right, cacheData['count'], start, end) print(log) return cacheData