def test_calc_ratio_table_index_and_columns(self): dict = wv.calc_ratio_table_index_and_columns(max_ratio=0.02, min_ratio=-0.018) index, columns = dict["index"], dict["columns"] self.assertEqual(len(index), 180) self.assertEqual(len(columns), 200) self.assertEqual(index[1], "0.0001") self.assertEqual(columns[151], "0.0151") df = wv.calc_ratio_table(self.wave_ratio_df, index, columns) self.assertEqual(df["0.0000"]["0.0000"], 4) self.assertEqual(df["0.0001"]["0.0001"], 4) self.assertEqual(df["0.0002"]["0.0002"], 3) self.assertEqual(df["0.0002"]["0.0003"], 2) self.assertEqual(df["0.0140"]["0.0140"], 1) self.assertEqual(df["0.0190"]["0.0160"], 0) # print(df) df.to_csv("/home/chengang/Data/df.csv") table = wv.calc_length_ratio(df, 4)
def export_ratio_table(code, start, end, thread_id): # queue_lock.acquire() ts.set_token(cfg.get_datayes_key()) mkt = ts.Market() # print("exporting " + code + " from " + start + " to " + end) st = time.time() df = mkt.MktEqud(ticker=code, beginDate=start, endDate=end, field='ticker,tradeDate,preClosePrice,openPrice,highestPrice,lowestPrice,closePrice') print(" Thread {0} fetch online: {1}".format(thread_id, time.time()-st)) # queue_lock.release() # df = ts.get_h_data(code, start, end) # print(df) wave_ratio_df = pd.DataFrame(columns=["max_ratio", "min_ratio"]) for i, row in df.iterrows(): dict = wv.calc_wave_ratio(row["preClosePrice"], row["openPrice"], row["highestPrice"], row["lowestPrice"]) wave_ratio_df.loc[row["tradeDate"]] = dict st = time.time() idx_col = wv.calc_ratio_table_index_and_columns(max_ratio=0.03, min_ratio=-0.03) index, columns = idx_col["index"], idx_col["columns"] ratio_table = wv.calc_ratio_table(wave_ratio_df, index, columns) print(" Thread {0} calc ratio table: {1}".format(thread_id, time.time()-st)) st = time.time() length_ratio_df = wv.calc_length_ratio(ratio_table, len(wave_ratio_df.index)) print(" Thread {0} calc length ratio: {1}".format(thread_id, time.time()-st)) # write csv st = time.time() ratio_table.to_csv(cfg.get_ratio_table_path(code, start, end)) length_ratio_df.to_csv(cfg.get_length_ratio_path(code, start, end)) # print(" save csv: {0}".format(time.time()-st)) return length_ratio_df