from numba import jit
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt


@jit(nopython=True)
def process_data(close, labels, metalabels, stop, take, markup, forward, backward):
    last_deal = 2
    last_price = 0.0
    report = [0.0]
    chart = [0.0]
    line_f = 0
    line_b = 0

    for i in range(len(close)):
        line_f = len(report) if i <= forward else line_f
        line_b = len(report) if i <= backward else line_b

        pred = labels[i]
        pr = close[i]
        pred_meta = metalabels[i]  # 1 = allow trades

        if last_deal == 2 and pred_meta == 1:
            last_price = pr
            last_deal = 0 if pred < 0.5 else 1
            continue

        if last_deal == 0:
            if (-markup + (pr - last_price) >= take) or (
                -markup + (last_price - pr) >= stop
            ):
                last_deal = 2
                profit = -markup + (pr - last_price)
                report.append(report[-1] + profit)
                chart.append(chart[-1] + profit)
                continue

        if last_deal == 1:
            if (-markup + (pr - last_price) >= stop) or (
                -markup + (last_price - pr) >= take
            ):
                last_deal = 2
                profit = -markup + (last_price - pr)
                report.append(report[-1] + profit)
                chart.append(chart[-1] + (pr - last_price))
                continue

        # close deals by signals
        if last_deal == 0 and pred > 0.5:
            last_deal = 2
            profit = -markup + (pr - last_price)
            report.append(report[-1] + profit)
            chart.append(chart[-1] + profit)
            continue

        if last_deal == 1 and pred < 0.5:
            last_deal = 2
            profit = -markup + (last_price - pr)
            report.append(report[-1] + profit)
            chart.append(chart[-1] + (pr - last_price))
            continue

    return np.array(report), np.array(chart), line_f, line_b


@jit(nopython=True)
def process_single_data(close, labels, stop, take, markup, forward, backward):
    last_deal = 2
    last_price = 0.0
    report = [0.0]
    chart = [0.0]
    line_f = 0
    line_b = 0

    for i in range(len(close)):
        line_f = len(report) if i <= forward else line_f
        line_b = len(report) if i <= backward else line_b

        pred = labels[i]
        pr = close[i]

        if last_deal == 2:
            last_price = pr
            last_deal = 0 if pred < 0.5 else 1
            continue

        if last_deal == 0:
            if (-markup + (pr - last_price) >= take) or (
                -markup + (last_price - pr) >= stop
            ):
                last_deal = 2
                profit = -markup + (pr - last_price)
                report.append(report[-1] + profit)
                chart.append(chart[-1] + profit)
                continue

        if last_deal == 1:
            if (-markup + (pr - last_price) >= stop) or (
                -markup + (last_price - pr) >= take
            ):
                last_deal = 2
                profit = -markup + (last_price - pr)
                report.append(report[-1] + profit)
                chart.append(chart[-1] + (pr - last_price))
                continue

        # close deals by signals
        if last_deal == 0 and pred > 0.5:
            last_deal = 2
            profit = -markup + (pr - last_price)
            report.append(report[-1] + profit)
            chart.append(chart[-1] + profit)
            continue

        if last_deal == 1 and pred < 0.5:
            last_deal = 2
            profit = -markup + (last_price - pr)
            report.append(report[-1] + profit)
            chart.append(chart[-1] + (pr - last_price))
            continue

    return np.array(report), np.array(chart), line_f, line_b


@jit(nopython=True)
def process_data_r(close, labels, metalabels, stop, take, markup, forward, backward):
    last_deal = 2
    last_price = 0.0
    report = [0.0]
    chart = [0.0]
    line_f = 0
    line_b = 0

    for i in range(len(close)):
        line_f = len(report) if i <= forward else line_f
        line_b = len(report) if i <= backward else line_b

        pred = labels[i]
        pr = close[i]
        pred_meta = metalabels[i]  # 1 = allow trades

        if last_deal == 2 and pred_meta > 0:
            last_price = pr
            last_deal = 0 if pred > 0.0 else 1
            continue

        if last_deal == 0:
            if (-markup + (pr - last_price) >= take) or (
                -markup + (last_price - pr) >= stop
            ):
                last_deal = 2
                profit = -markup + (pr - last_price)
                report.append(report[-1] + profit)
                chart.append(chart[-1] + profit)
                continue

        if last_deal == 1:
            if (-markup + (pr - last_price) >= stop) or (
                -markup + (last_price - pr) >= take
            ):
                last_deal = 2
                profit = -markup + (last_price - pr)
                report.append(report[-1] + profit)
                chart.append(chart[-1] + (pr - last_price))
                continue

        # close deals by signals
        if last_deal == 0 and pred < 0:
            last_deal = 2
            profit = -markup + (pr - last_price)
            report.append(report[-1] + profit)
            chart.append(chart[-1] + profit)
            continue

        if last_deal == 1 and pred > 0:
            last_deal = 2
            profit = -markup + (last_price - pr)
            report.append(report[-1] + profit)
            chart.append(chart[-1] + (pr - last_price))
            continue

    return np.array(report), np.array(chart), line_f, line_b


@jit(nopython=True)
def process_data_single_r(
    close, labels, stop, take, markup, forward, backward, threshold
):
    last_deal = 2
    last_price = 0.0
    report = [0.0]
    chart = [0.0]
    line_f = 0
    line_b = 0

    for i in range(len(close)):
        line_f = len(report) if i <= forward else line_f
        line_b = len(report) if i <= backward else line_b

        pred = labels[i]
        pr = close[i]

        if last_deal == 2:
            last_price = pr
            if pred > threshold:
                last_deal = 0
            elif pred < -threshold:
                last_deal = 1
            continue

        if last_deal == 0:
            if (-markup + (pr - last_price) >= take) or (
                -markup + (last_price - pr) >= stop
            ):
                last_deal = 2
                profit = -markup + (pr - last_price)
                report.append(report[-1] + profit)
                chart.append(chart[-1] + profit)
                continue

        if last_deal == 1:
            if (-markup + (pr - last_price) >= stop) or (
                -markup + (last_price - pr) >= take
            ):
                last_deal = 2
                profit = -markup + (last_price - pr)
                report.append(report[-1] + profit)
                chart.append(chart[-1] + (pr - last_price))
                continue

        # close deals by signals
        if last_deal == 0 and pred < 0:
            last_deal = 2
            profit = -markup + (pr - last_price)
            report.append(report[-1] + profit)
            chart.append(chart[-1] + profit)
            continue

        if last_deal == 1 and pred > 0:
            last_deal = 2
            profit = -markup + (last_price - pr)
            report.append(report[-1] + profit)
            chart.append(chart[-1] + (pr - last_price))
            continue

    return np.array(report), np.array(chart), line_f, line_b


@jit(nopython=True)
def process_data_one_direction(
    close, labels, metalabels, stop, take, markup, forward, backward, direction
):
    last_deal = 2
    last_price = 0.0
    report = [0.0]
    chart = [0.0]
    line_f = 0
    line_b = 0

    for i in range(len(close)):
        line_f = len(report) if i <= forward else line_f
        line_b = len(report) if i <= backward else line_b

        pred = labels[i]
        pr = close[i]
        pred_meta = metalabels[i]  # 1 = allow trades

        if last_deal == 2 and pred_meta == 1:
            last_price = pr
            last_deal = 2 if pred < 0.5 else 1
            continue

        if last_deal == 1 and direction == "buy":
            if (-markup + (pr - last_price) >= take) or (
                -markup + (last_price - pr) >= stop
            ):
                last_deal = 2
                profit = -markup + (pr - last_price)
                report.append(report[-1] + profit)
                chart.append(chart[-1] + profit)
                continue

        if last_deal == 1 and direction == "sell":
            if (-markup + (pr - last_price) >= stop) or (
                -markup + (last_price - pr) >= take
            ):
                last_deal = 2
                profit = -markup + (last_price - pr)
                report.append(report[-1] + profit)
                chart.append(chart[-1] + (pr - last_price))
                continue

        # close deals by signals
        if last_deal == 1 and pred < 0.5 and direction == "buy":
            last_deal = 2
            profit = -markup + (pr - last_price)
            report.append(report[-1] + profit)
            chart.append(chart[-1] + profit)
            continue

        if last_deal == 1 and pred < 0.5 and direction == "sell":
            last_deal = 2
            profit = -markup + (last_price - pr)
            report.append(report[-1] + profit)
            chart.append(chart[-1] + (pr - last_price))
            continue

    return np.array(report), np.array(chart), line_f, line_b


def tester(*args):
    """
    This is a fast strategy tester based on numba
    List of parameters:

    dataset: must contain first column as 'close' and last columns with "labels" and "meta_labels"

    stop: stop loss value

    take: take profit value

    forward: forward time interval

    backward: backward time interval

    markup: markup value

    plot: false/true
    """
    dataset, stop, take, forward, backward, markup, plot = args

    forw = dataset.index.get_indexer([forward], method="nearest")[0]
    backw = dataset.index.get_indexer([backward], method="nearest")[0]

    close = dataset["close"].to_numpy()
    labels = dataset["labels"].to_numpy()
    metalabels = dataset["meta_labels"].to_numpy()

    report, chart, line_f, line_b = process_data(
        close, labels, metalabels, stop, take, markup, forw, backw
    )

    y = report.reshape(-1, 1)
    X = np.arange(len(report)).reshape(-1, 1)
    lr = LinearRegression()
    lr.fit(X, y)

    l = 1 if lr.coef_[0][0] >= 0 else -1

    if plot:
        plt.plot(report)
        plt.plot(chart)
        plt.axvline(x=line_f, color="purple", ls=":", lw=1, label="OOS")
        plt.axvline(x=line_b, color="red", ls=":", lw=1, label="OOS2")
        plt.plot(lr.predict(X))
        plt.title("Strategy performance R^2 " + str(format(lr.score(X, y) * l, ".2f")))
        plt.xlabel("the number of trades")
        plt.ylabel("cumulative profit in pips")
        plt.show()

    return lr.score(X, y) * l


def tester_single_model(*args):
    """
    This is a fast strategy tester based on numba
    List of parameters:

    dataset: must contain first column as 'close' and last columns with "labels" and "meta_labels"

    stop: stop loss value

    take: take profit value

    forward: forward time interval

    backward: backward time interval

    markup: markup value

    plot: false/true
    """
    dataset, stop, take, forward, backward, markup, plot = args

    forw = dataset.index.get_indexer([forward], method="nearest")[0]
    backw = dataset.index.get_indexer([backward], method="nearest")[0]

    close = dataset["close"].to_numpy()
    labels = dataset["labels"].to_numpy()

    report, chart, line_f, line_b = process_single_data(
        close, labels, stop, take, markup, forw, backw
    )

    y = report.reshape(-1, 1)
    X = np.arange(len(report)).reshape(-1, 1)
    lr = LinearRegression()
    lr.fit(X, y)

    l = 1 if lr.coef_[0][0] >= 0 else -1

    if plot:
        plt.plot(report)
        plt.plot(chart)
        plt.axvline(x=line_f, color="purple", ls=":", lw=1, label="OOS")
        plt.axvline(x=line_b, color="red", ls=":", lw=1, label="OOS2")
        plt.plot(lr.predict(X))
        plt.title("Strategy performance R^2 " + str(format(lr.score(X, y) * l, ".2f")))
        plt.xlabel("the number of trades")
        plt.ylabel("cumulative profit in pips")
        plt.show()

    return lr.score(X, y) * l


def tester_r(*args):
    """
    This is a fast strategy tester based on numba
    List of parameters:

    dataset: must contain first column as 'close' and last columns with "labels" and "meta_labels"

    stop: stop loss value

    take: take profit value

    forward: forward time interval

    backward: backward time interval

    markup: markup value

    plot: false/true
    """
    dataset, stop, take, forward, backward, markup, plot = args

    forw = dataset.index.get_indexer([forward], method="nearest")[0]
    backw = dataset.index.get_indexer([backward], method="nearest")[0]

    close = dataset["close"].to_numpy()
    labels = dataset["labels"].to_numpy()
    metalabels = dataset["meta_labels"].to_numpy()

    report, chart, line_f, line_b = process_data_r(
        close, labels, metalabels, stop, take, markup, forw, backw
    )

    y = report.reshape(-1, 1)
    X = np.arange(len(report)).reshape(-1, 1)
    lr = LinearRegression()
    lr.fit(X, y)

    l = 1 if lr.coef_[0][0] >= 0 else -1

    if plot:
        plt.plot(report)
        plt.plot(chart)
        plt.axvline(x=line_f, color="purple", ls=":", lw=1, label="OOS")
        plt.axvline(x=line_b, color="red", ls=":", lw=1, label="OOS2")
        plt.plot(lr.predict(X))
        plt.title("Strategy performance R^2 " + str(format(lr.score(X, y) * l, ".2f")))
        plt.xlabel("the number of trades")
        plt.ylabel("cumulative profit in pips")
        plt.show()

    return lr.score(X, y) * l


def tester_single_model_r(*args):
    """
    This is a fast strategy tester based on numba
    List of parameters:
    dataset: must contain first column as 'close' and last columns with "labels" and "meta_labels"
    stop: stop loss value
    take: take profit value
    forward: forward time interval
    backward: backward time interval
    markup: markup value
    plot: false/true
    """
    dataset, stop, take, forward, backward, markup, threshold, plot = args

    forw = dataset.index.get_indexer([forward], method="nearest")[0]
    backw = dataset.index.get_indexer([backward], method="nearest")[0]

    close = dataset["close"].to_numpy()
    labels = dataset["labels"].to_numpy()

    report, chart, line_f, line_b = process_data_single_r(
        close, labels, stop, take, markup, forw, backw, threshold
    )

    y = report.reshape(-1, 1)
    X = np.arange(len(report)).reshape(-1, 1)
    lr = LinearRegression()
    lr.fit(X, y)

    l = 1 if lr.coef_[0][0] >= 0 else -1

    if plot:
        plt.plot(report)
        plt.plot(chart)
        plt.axvline(x=line_f, color="purple", ls=":", lw=1, label="OOS")
        plt.axvline(x=line_b, color="red", ls=":", lw=1, label="OOS2")
        plt.plot(lr.predict(X))
        plt.title("Strategy performance R^2 " + str(format(lr.score(X, y) * l, ".2f")))
        plt.xlabel("the number of trades")
        plt.ylabel("cumulative profit in pips")
        plt.show()

    return lr.score(X, y) * l


def tester_one_direction(*args):
    """
    This is a fast strategy tester based on numba
    List of parameters:

    dataset: must contain first column as 'close' and last columns with "labels" and "meta_labels"

    stop: stop loss value

    take: take profit value

    forward: forward time interval

    backward: backward time interval

    markup: markup value

    direction: buy/sell

    plot: false/true
    """
    dataset, stop, take, forward, backward, markup, direction, plot = args

    forw = dataset.index.get_indexer([forward], method="nearest")[0]
    backw = dataset.index.get_indexer([backward], method="nearest")[0]

    close = dataset["close"].to_numpy()
    labels = dataset["labels"].to_numpy()
    metalabels = dataset["meta_labels"].to_numpy()

    report, chart, line_f, line_b = process_data_one_direction(
        close, labels, metalabels, stop, take, markup, forw, backw, direction
    )

    y = report.reshape(-1, 1)
    X = np.arange(len(report)).reshape(-1, 1)
    lr = LinearRegression()
    lr.fit(X, y)

    l = 1 if lr.coef_[0][0] >= 0 else -1

    if plot:
        plt.plot(report)
        plt.axvline(x=line_f, color="purple", ls=":", lw=1, label="OOS")
        plt.axvline(x=line_b, color="red", ls=":", lw=1, label="OOS2")
        plt.plot(lr.predict(X))
        plt.title("Strategy performance R^2 " + str(format(lr.score(X, y) * l, ".2f")))
        plt.xlabel("the number of trades")
        plt.ylabel("cumulative profit in pips")
        plt.show()

    return lr.score(X, y) * l


def tester_slow(dataset, stop, take, markup, forward, plot=False):
    last_deal = 2
    last_price = 0.0
    report = [0.0]
    chart = [0.0]
    line_f = 0
    line_b = 0

    # Assuming variables are defined elsewhere
    forw = dataset.index.get_indexer([forward], method="nearest")
    backw = dataset.index.get_indexer([forward], method="nearest")

    close = dataset["close"].to_numpy()
    labels = dataset["labels"].to_numpy()
    metalabels = dataset["meta_labels"].to_numpy()

    for i in range(dataset.shape[0]):
        line_f = len(report) if i <= forw else line_f
        line_b = len(report) if i <= backw else line_b

        pred = labels[i]
        pr = close[i]
        pred_meta = metalabels[i]  # 1 = allow trades

        if last_deal == 2 and pred_meta == 1:
            last_price = pr
            last_deal = 0 if pred < 0.5 else 1
            continue

        if last_deal == 0:
            if (-markup + (pr - last_price) >= take) or (
                -markup + (last_price - pr) >= stop
            ):
                last_deal = 2
                profit = -markup + (pr - last_price)
                report.append(report[-1] + profit)
                chart.append(chart[-1] + profit)
                continue

        if last_deal == 1:
            if (-markup + (pr - last_price) >= stop) or (
                -markup + (last_price - pr) >= take
            ):
                last_deal = 2
                profit = -markup + (last_price - pr)
                report.append(report[-1] + profit)
                chart.append(chart[-1] + (pr - last_price))
                continue

        # close deals by signals
        if last_deal == 0 and pred > 0.5 and pred_meta == 1:
            last_deal = 2
            profit = -markup + (pr - last_price)
            report.append(report[-1] + profit)
            chart.append(chart[-1] + profit)
            continue

        if last_deal == 1 and pred < 0.5 and pred_meta == 1:
            last_deal = 2
            profit = -markup + (last_price - pr)
            report.append(report[-1] + profit)
            chart.append(chart[-1] + (pr - last_price))
            continue

    y = np.array(report).reshape(-1, 1)
    X = np.arange(len(report)).reshape(-1, 1)
    lr = LinearRegression()
    lr.fit(X, y)

    l = 1 if lr.coef_ >= 0 else -1

    if plot:
        plt.plot(report)
        plt.plot(chart)
        plt.axvline(x=line_f, color="purple", ls=":", lw=1, label="OOS")
        plt.axvline(x=line_b, color="red", ls=":", lw=1, label="OOS2")
        plt.plot(lr.predict(X))
        plt.title("Strategy performance R^2 " + str(format(lr.score(X, y) * l, ".2f")))
        plt.xlabel("the number of trades")
        plt.ylabel("cumulative profit in pips")
        plt.show()

    return lr.score(X, y) * l


def test_model(
    dataset: pd.DataFrame,
    result: list,
    stop: float,
    take: float,
    forward: float,
    backward: float,
    markup: float,
    plt=False,
):

    ext_dataset = dataset.copy()
    X = ext_dataset[ext_dataset.columns[1:]]

    ext_dataset["labels"] = result[0].predict_proba(X)[:, 1]
    ext_dataset["meta_labels"] = result[1].predict_proba(X)[:, 1]
    ext_dataset["labels"] = ext_dataset["labels"].apply(
        lambda x: 0.0 if x < 0.5 else 1.0
    )
    ext_dataset["meta_labels"] = ext_dataset["meta_labels"].apply(
        lambda x: 0.0 if x < 0.5 else 1.0
    )
    return tester(ext_dataset, stop, take, forward, backward, markup, plt)


def test_single_model(
    dataset: pd.DataFrame,
    result: list,
    stop: float,
    take: float,
    forward: float,
    backward: float,
    markup: float,
    plt=False,
):

    ext_dataset = dataset.copy()
    X = ext_dataset[ext_dataset.columns[1:]]

    ext_dataset["labels"] = result[0].predict_proba(X)[:, 1]
    ext_dataset["labels"] = ext_dataset["labels"].apply(
        lambda x: 0.0 if x < 0.5 else 1.0
    )
    return tester_single_model(ext_dataset, stop, take, forward, backward, markup, plt)


def test_MOE_model(
    dataset: pd.DataFrame,
    expert_models: list,
    router_model,
    stop: float,
    take: float,
    forward: float,
    backward: float,
    markup: float,
    plt: bool = False,
):

    ext_dataset = dataset.copy()
    X = ext_dataset[ext_dataset.columns[1:]]
    router_predictions_proba = router_model.predict_proba(X)
    # Get the index of the expert model with the highest probability
    expert_indices = np.argmax(router_predictions_proba, axis=1)
    # Initialize a column for the final predictions
    ext_dataset["labels"] = np.nan
    # Iterate through each sample and get a prediction from the selected expert model
    for i in range(len(ext_dataset)):
        selected_expert_index = expert_indices[i]
        # Ensure the selected_expert_index is valid
        if 0 <= selected_expert_index < len(expert_models):
            expert = expert_models[selected_expert_index]
            prediction_proba = expert.predict_proba(X.iloc[[i]])[:, 1]
            ext_dataset.loc[i, "labels"] = 0.0 if prediction_proba < 0.5 else 1.0
        else:
            print(
                f"Warning: Router model predicted an invalid expert index: {selected_expert_index} for sample {i}. Skipping prediction for this sample."
            )

    return tester_single_model(ext_dataset, stop, take, forward, backward, markup, plt)


def test_MOE_model_optimized(
    dataset: pd.DataFrame,
    expert_models: list,
    router_model,
    stop: float,
    take: float,
    forward: float,
    backward: float,
    markup: float,
    plt: bool = False,
):

    ext_dataset = dataset.copy()
    X = ext_dataset[ext_dataset.columns[1:]]

    router_predictions_proba = router_model.predict_proba(X)
    expert_indices = np.argmax(router_predictions_proba, axis=1)

    # 1. Forecasts for each expert model on the entire data set
    # This will be the list of numpy arrays, where each array represents forecasts of a single EA
    all_expert_predictions_proba = [
        expert.predict_proba(X)[:, 1] for expert in expert_models
    ]

    # Initialize the column for final forecasts
    ext_dataset["labels"] = np.nan

    # 2. Use advanced indexing to select correct forecast
    # Create an array for storing final forecasts
    final_predictions_proba = np.zeros(len(ext_dataset))

    for i, expert_pred_proba in enumerate(all_expert_predictions_proba):
        # For each EA, selec the rows where router_model selected this EA
        mask = expert_indices == i
        final_predictions_proba[mask] = expert_pred_proba[mask]

    # Apply the threshold of 0.5 to get labels
    ext_dataset["labels"] = (final_predictions_proba >= 0.5).astype(float)

    return tester_single_model(ext_dataset, stop, take, forward, backward, markup, plt)


def test_model_r(
    dataset: pd.DataFrame,
    result: list,
    stop: float,
    take: float,
    forward: float,
    backward: float,
    markup: float,
    plt=False,
):

    ext_dataset = dataset.copy()
    X = ext_dataset[ext_dataset.columns[1:]]
    ext_dataset["labels"] = result[0].predict(X)
    ext_dataset["meta_labels"] = result[1].predict(X)
    return tester_r(ext_dataset, stop, take, forward, backward, markup, plt)


def test_single_model_r(
    dataset: pd.DataFrame,
    result: list,
    stop: float,
    take: float,
    forward: float,
    backward: float,
    markup: float,
    threshold: float,
    plt=False,
):

    ext_dataset = dataset.copy()
    X = ext_dataset[ext_dataset.columns[1:]]
    ext_dataset["labels"] = result[0].predict(X)
    return tester_single_model_r(
        ext_dataset, stop, take, forward, backward, markup, threshold, plt
    )


def test_model_one_direction(
    dataset: pd.DataFrame,
    result: list,
    stop: float,
    take: float,
    forward: float,
    backward: float,
    markup: float,
    direction: str,
    plt=False,
):

    ext_dataset = dataset.copy()
    X = ext_dataset[ext_dataset.columns[1:]]

    ext_dataset["labels"] = result[0].predict_proba(X)[:, 1]
    ext_dataset["meta_labels"] = result[1].predict_proba(X)[:, 1]
    ext_dataset["labels"] = ext_dataset["labels"].apply(
        lambda x: 0.0 if x < 0.5 else 1.0
    )
    ext_dataset["meta_labels"] = ext_dataset["meta_labels"].apply(
        lambda x: 0.0 if x < 0.5 else 1.0
    )
    return tester_one_direction(
        ext_dataset, stop, take, forward, backward, markup, direction, plt
    )


def test_model_one_direction_meta(
    dataset: pd.DataFrame,
    result: list,
    result_meta: list,
    stop: float,
    take: float,
    forward: float,
    backward: float,
    markup: float,
    direction: str,
    plt=False,
):

    # Copy the source dataset
    ext_dataset = dataset.copy()

    # Check the set of features for the main model
    if hasattr(result[0], "feature_names_"):
        expected_features = result[0].feature_names_
        current_features = (
            ext_dataset.columns[1:-2].tolist()
            if len(ext_dataset.columns) > 3
            else ext_dataset.columns[1:].tolist()
        )

        # Add absent features
        for feature in expected_features:
            if feature not in current_features and feature.isdigit():
                ext_dataset[feature] = 0  # Fill in the absent features with zeros
                print(f"Added absent feature: {feature}")

    # Create X_main for the main model
    if hasattr(result[0], "feature_names_"):
        X_main = ext_dataset[result[0].feature_names_]
    else:
        X_main = (
            ext_dataset[ext_dataset.columns[1:-2]]
            if len(ext_dataset.columns) > 3
            else ext_dataset.columns[1:]
        )

    # Main model forecasts
    try:
        ext_dataset["labels"] = result[0].predict_proba(X_main)[:, 1]
        ext_dataset["labels"] = ext_dataset["labels"].apply(
            lambda x: 0.0 if x < 0.5 else 1.0
        )
    except Exception as e:
        print(f"Main model forecast error: {e}")
        if hasattr(result[0], "feature_names_"):
            print(f"Expected features: {result[0].feature_names_}")
        print(f"Provided features: {X_main.columns.tolist()}")
        raise

    # For meta model
    meta_data = ext_dataset.copy()

    # Retrieve submodels from result_meta
    # Make sure result_meta contains models, not lists
    submodels = []
    if result_meta:
        # If the first element has the predict_proba method, then result_meta contains the list of models
        if hasattr(result_meta[0], "predict_proba"):
            submodels = result_meta
        # Otherwise, this can be the list of lists, let's try to retrieve the first model from each element
        elif isinstance(result_meta[0], list) and len(result_meta[0]) > 0:
            for item in result_meta:
                if hasattr(item[0], "predict_proba"):
                    submodels.append(item[0])

    # Check if there are submodels for handling
    if submodels:
        try:
            count = 0
            for meta_model in submodels:
                # Create X for submodel
                if hasattr(meta_model, "feature_names_"):
                    X_for_metamodel = ext_dataset[meta_model.feature_names_]
                else:
                    X_for_metamodel = X_main

                meta_data[f"meta_feature{count}"] = meta_model.predict_proba(
                    X_for_metamodel
                )[:, 1]
                count += 1
        except Exception as e:
            print(f"Submodels forecast error: {e}")
            print(f"meta_model type: {type(meta_model)}")
            if hasattr(meta_model, "feature_names_"):
                print(f"Expected submodel features: {meta_model.feature_names_}")
            print(f"Provided features: {X_for_metamodel.columns.tolist()}")
            raise
    else:
        print("Warning: No submodels found for handling.")

    # Prepare data for the final meta model
    try:
        if hasattr(result[1], "feature_names_"):
            # Check and add absent features
            for feature in result[1].feature_names_:
                if feature not in meta_data.columns:
                    meta_data[feature] = 0
                    print(f"Added absent feature for the meta model: {feature}")

            X_meta_final = meta_data[result[1].feature_names_]
        else:
            # Remove columns with labels if any
            drop_cols = ["close", "labels", "meta_labels"]
            drop_cols = [col for col in drop_cols if col in meta_data.columns]
            X_meta_final = meta_data.drop(columns=drop_cols if drop_cols else [])
    except Exception as e:
        print(f"Error preparing data for meta model: {e}")
        raise

    # Meta model forecasts
    try:
        ext_dataset["meta_labels"] = result[1].predict_proba(X_meta_final)[:, 1]
        ext_dataset["meta_labels"] = ext_dataset["meta_labels"].apply(
            lambda x: 0.0 if x < 0.5 else 1.0
        )
    except Exception as e:
        print(f"Meta model forecast error: {e}")
        if hasattr(result[1], "feature_names_"):
            print(f"Expected meta model features: {result[1].feature_names_}")
        print(f"Provided features: {X_meta_final.columns.tolist()}")
        raise

    # Apply final test
    return tester_one_direction(
        ext_dataset, stop, take, forward, backward, markup, direction, plt
    )
