def load_train_data(stock_id): stock_df = stock_kline_day(stock_id, qfq) values, labels = process_features(stock_df) inputs_x, inputs_y = to_time_series(values, labels) X_train, X_test, y_train, y_test = train_test_split(inputs_x, inputs_y, test_size=test_size, random_state=seed) return data_to_tensor(X_train, X_test, y_train, y_test)
def predict(model, stock_id): stock_df = stock_kline_day(stock_id, qfq) values, _ = process_features(stock_df) index = len(values) values = np.array([values[index - seq_length:index]]) values = torch.tensor(values).float().to(device=device) with torch.no_grad(): outputs = model(values) return outputs
def predict(model, stock_id): stock_df = stock_kline_day(stock_id, qfq) values, _ = process_features(stock_df) index = len(values) values = np.array([values[index - seq_length:index]]) values = torch.tensor(values).float().to(device=device) with torch.no_grad(): outputs = model(values) return pd.DataFrame(outputs.reshape(-1, 2).cpu().numpy(), columns=['high', 'low'])
def generate_dataset(stock_ids): x_list = [] y_list = [] for stock_id in stock_ids: stock_df = stock_kline_day(stock_id, qfq) values, labels, _ = process_features(stock_df) inputs_x, inputs_y = to_time_series(values, labels) x_list.append(inputs_x) y_list.append(inputs_y) c = list(zip(x_list, y_list)) random.shuffle(c) x_list, y_list = zip(*c) x = np.concatenate(x_list, axis=0) y = np.concatenate(y_list, axis=0) return x, y
def predict(model, stock_id): model = load_pre_trained_model(model, stock_id) data_context = load_data_context(stock_id) model.eval() stock_df = stock_kline_day(stock_id, qfq) values, _ = process_features(stock_df, data_context) index = len(values) values = np.array([values[index - seq_length:index]]) values = torch.tensor(values).float().to(device=device) with torch.no_grad(): outputs = model(values) outputs = pd.DataFrame(outputs.reshape(-1, 2).cpu().numpy(), columns=['high', 'low']) for column in outputs.columns: sc = data_context[column] values = outputs.loc[:, [column]].values values = sc.inverse_transform(values) outputs[column] = pd.Series(values.reshape(-1)) return outputs
def load_train_data(stock_ids): inputs_x_list = [] inputs_y_list = [] data_context = {} for stock_id in stock_ids: if not stock_id in data_context: data_context[stock_id] = {} stock_df = stock_kline_day(stock_id, qfq) values, labels = process_features(stock_df, data_context[stock_id]) inputs_x, inputs_y = to_time_series(values, labels) inputs_x_list.append(inputs_x) inputs_y_list.append(inputs_y) save_data_context(data_context) input_x = np.concatenate(inputs_x_list, axis=0) input_y = np.concatenate(inputs_y_list, axis=0) X_train, X_test, y_train, y_test = train_test_split(input_x, input_y, test_size=test_size, random_state=seed) return data_to_tensor(X_train, X_test, y_train, y_test)
def persist_stock_info(): pbar_stock_ids = tqdm(default_stock_ids) for stock_id in pbar_stock_ids: stock_df = stock_kline_day(stock_id, qfq) stock_df.to_csv(os.path.join(data_folder, "{0}.csv".format(stock_id)))