<jupyter_start><jupyter_text>Load model<jupyter_code>from tensorflow.keras import models
from lstm_creator import createLstmModelFromDatasets, scaleData, preprocessData
import numpy as np

# Uncomment this line to load the model from a file for testing
# model = models.load_model('../../../models/dump_model_1')<jupyter_output><empty_output><jupyter_text>Testing<jupyter_code>def evaluate_model(real_data, predicted_data, slope_to_buy_at, inherent_loss=2):
    real_data = real_data.reshape(len(real_data))
    predicted_data = predicted_data.reshape(len(predicted_data))

    real_diff = np.diff(real_data)
    predicted_diff = np.diff(predicted_data)

    profit = 0
    profits = 0
    losses = 0
    bought = False
    errors = []
    for i in range(len(real_data)-1):
            #  Error calculation
            # if real_diff[i] != 0:
            errors.append( 1 if np.sign(real_diff[i]) == np.sign(predicted_diff[i]) else 0)
                # errors.append( abs(real_diff[i] - predicted_diff[i]) / abs(real_diff[i]) )

            if predicted_diff[i] > slope_to_buy_at:
                if bought == False:
                    bought = real_data[i]
                else:
                    # Already bought, continue
                    continue
            if bought != False and predicted_diff[i] < slope_to_buy_at:
                # Now we sell
                profit += (real_data[i+1] - real_data[i]) - inherent_loss
                if ((real_data[i+1] - real_data[i]) - inherent_loss) > 0:
                    profits += 1
                else:
                    losses += 1
                bought = False

    return (errors, profit, profits, losses)

def eval_model_on_dataset(actual, prediction, slope_to_buy_at, subsampling, inherent_loss):
    # Determine the number of cases in which we predicted a correct increase
    errors, profit, profits, losses = evaluate_model(actual, prediction, slope_to_buy_at, inherent_loss)

    # print(f"Found {correct_slopes} out of {len(actual)-1}")
    percent_error = (sum(errors)/len(errors))*100
    # print(f"{precent_success}%")
    # print("Profit:", profit)
    return percent_error, profit, profits, losses
def testModel(model, length, slope_to_buy_at, path_to_testing_dataset, quiet=False):

    datasets, scaler = scaleData([path_to_testing_dataset], 3)

    hist, actual = preprocessData(datasets[0], length)

    pred = model.predict(hist)

    pred_transformed = scaler.inverse_transform(pred)
    actual_transformed = scaler.inverse_transform(actual)

    if not quiet:
        plt.figure(figsize=(12,8))
        plt.plot(actual_transformed, color='blue', label='Real')
        plt.plot(pred_transformed, color='red', label='Prediction')
        plt.title('ETH Price Prediction')
        plt.legend()
        plt.show()

    return eval_model_on_dataset(actual=actual_transformed, prediction=pred_transformed, slope_to_buy_at=slope_to_buy_at, subsampling=1, inherent_loss=1.5)
overall = (0, 0, 0, 0)
lookback_length = model.input_shape[1]
slope_to_buy_at = 2


result1 = testModel(model, lookback_length, slope_to_buy_at, "../../../data/test_data/test6.csv", quiet=True)
accuracy, profit, profits, losses = result1
print(f"Error: {accuracy:.2f}% Profit: ${profit:.2f} (Profits/Losses: {profits}/{losses})")

result2 = testModel(model, lookback_length, slope_to_buy_at, "../../../data/test_data/test8.csv", quiet=True)
accuracy, profit, profits, losses = result2
print(f"Error: {accuracy:.2f}% Profit: ${profit:.2f} (Profits/Losses: {profits}/{losses})")

result3 = testModel(model, lookback_length, slope_to_buy_at, "../../../data/test_data/test10.csv", quiet=True)
accuracy, profit, profits, losses = result3
print(f"Error: {accuracy:.2f}% Profit: ${profit:.2f} (Profits/Losses: {profits}/{losses})")

overall = [sum(x) for x in zip(result1,overall)]
overall = [sum(x) for x in zip(result2,overall)]
overall = [sum(x) for x in zip(result3,overall)]
print("-----------------------------------")
print(f"Error: {overall[0]/3:.2f}% Profit: ${overall[1]:.2f} (Profits/Losses: {overall[2]}/{overall[3]})")<jupyter_output>Error: 48.66% Profit: $-8.18 (Profits/Losses: 1/5)
Error: 48.55% Profit: $-14.35 (Profits/Losses: 4/6)
Error: 52.61% Profit: $-13.64 (Profits/Losses: 6/9)
-----------------------------------
Error: 49.94% Profit: $-36.17 (Profits/Losses: 11/20)
{'fraction_non_alphanumeric': 0.15155709342560553, 'fraction_numerical': 0.022837370242214532, 'mean_word_length': 3.3273453093812377, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '198666', 'license': 'MIT', 'n_tokens_mistral': 1559, 'n_tokens_neox': 1448, 'n_words': 303, 'path': 'night_trader/predictors/lstm/create_model.ipynb', 'repo_name': 'jsngalloway/night-trader', 'size': '4335'}