Get the best levels (bid and ask) prices and the corresponding discretized labels. Args: dataset (str): Name of the dataset to be used (e.g. nasdaq, lse, ...). history_length (int): Length of the history (each model's sample is a 2D array of shape ( , <feature
(
dataset: str,
target_stocks: str,
history_length: int,
all_horizons: list[int],
prediction_horizon: int,
threshold: float,
)
| 279 | |
| 280 | |
| 281 | def get_best_levels_prices_and_labels( |
| 282 | dataset: str, |
| 283 | target_stocks: str, |
| 284 | history_length: int, |
| 285 | all_horizons: list[int], |
| 286 | prediction_horizon: int, |
| 287 | threshold: float, |
| 288 | ) -> tuple[Any, ...]: |
| 289 | """ |
| 290 | Get the best levels (bid and ask) prices and the corresponding discretized labels. |
| 291 | Args: |
| 292 | dataset (str): Name of the dataset to be used (e.g. nasdaq, lse, ...). |
| 293 | history_length (int): Length of the history (each model's sample is a 2D array of shape (<history_length>, <features>)). |
| 294 | all_horizons (list): List all horizons computed in the preprocessing stage. |
| 295 | prediction_horizon (int): Horizon to be considered. |
| 296 | threshold (float): Threshold to be used to discretize the labels. |
| 297 | |
| 298 | Returns: |
| 299 | A tuple containing the best levels (bid and ask) prices and the corresponding discretized labels. |
| 300 | """ |
| 301 | |
| 302 | # List the test files. |
| 303 | test_files = sorted(glob.glob(f"./data/{dataset}/unscaled_data/test/*{target_stocks[0]}*.csv")) |
| 304 | |
| 305 | best_levels_prices = pd.DataFrame() |
| 306 | |
| 307 | # Get the position of the prediction horizon in the list of all horizons. |
| 308 | position = next( |
| 309 | ( |
| 310 | index |
| 311 | for index, value in enumerate(all_horizons) |
| 312 | if value == prediction_horizon |
| 313 | ), |
| 314 | None, |
| 315 | ) |
| 316 | all_labels_temp = [] |
| 317 | |
| 318 | for file in test_files: |
| 319 | # Load the file. |
| 320 | df = pd.read_csv(file).iloc[history_length:, :] |
| 321 | # Reset the index. |
| 322 | df.reset_index(drop=True, inplace=True) |
| 323 | # Get all the labels. |
| 324 | label_df = df.iloc[:, 41:] |
| 325 | # Get the label corresponding to the prediction horizon. |
| 326 | label = label_df.iloc[:, position] |
| 327 | # Get the best levels (ask and bid) prices and the datetime corresponding to each tick. |
| 328 | best_levels_prices = pd.concat( |
| 329 | [best_levels_prices, df[["seconds", "ASKp1", "BIDp1"]]] |
| 330 | ) |
| 331 | # Append the label to the list of labels. |
| 332 | all_labels_temp = all_labels_temp + label.tolist() |
| 333 | |
| 334 | # Discretize the labels (0: downtrend, 1: no trend, 2: uptrend). |
| 335 | all_labels = [ |
| 336 | 2 if label >= threshold else 0 if label <= -threshold else 1 |
| 337 | for label in all_labels_temp |
| 338 | ] |