Ejemplo n.º 1
0
    def test_model(self):
        global row_data
        cwd = os.getcwd()
        cwd = os.path.basename(cwd)
        if cwd == 'lstm':
            resultsdir = "results"
            logsdir = "logs"
        else:
            resultsdir = "lstm/results"

        self.textbox.setText('Testing...')
        # load the data
        data = load_data(ticker,
                         parameters.N_STEPS,
                         lookup_step=parameters.LOOKUP_STEP,
                         test_size=parameters.TEST_SIZE,
                         feature_columns=parameters.FEATURE_COLUMNS,
                         shuffle=False,
                         row_data=row_data)

        #   construct the model
        model = create_model(N_STEPS,
                             loss=parameters.LOSS,
                             units=parameters.UNITS,
                             cell=parameters.CELL,
                             n_layers=parameters.N_LAYERS,
                             dropout=parameters.DROPOUT,
                             optimizer=parameters.OPTIMIZER)

        model_path = os.path.join(resultsdir, parameters.model_name) + ".h5"
        model.load_weights(model_path)

        # evaluate the model
        results = model.evaluate(data["X_test"], data["y_test"])

        self.textbox.setPlainText('test loss, test acc:' + str(results) + '\n')

        if not cwd == 'lstm':
            print("Results: " + str(results))
            print('test loss, test acc:', results)
            self.plot_graph(model, data)
Ejemplo n.º 2
0
import pandas as pd
from parameters import *


# create these folders if they does not exist
if not os.path.isdir("results"):
    os.mkdir("results")

if not os.path.isdir("logs"):
    os.mkdir("logs")

if not os.path.isdir("data"):
    os.mkdir("data")

# load the data
data = load_data(ticker_code, N_STEPS, lookup_step=LOOKUP_STEP, test_size=TEST_SIZE, feature_columns=FEATURE_COLUMNS)

# save the dataframe
data["df"].to_csv(ticker_data_filename)

# construct the model
model = create_model(N_STEPS, loss=LOSS, units=UNITS, cell=CELL, n_layers=N_LAYERS,
                    dropout=DROPOUT, optimizer=OPTIMIZER, bidirectional=BIDIRECTIONAL)

# some tensorflow callbacks
checkpointer = ModelCheckpoint(os.path.join("results", model_name + ".h5"), save_weights_only=True, save_best_only=True, verbose=1)
tensorboard = TensorBoard(log_dir=os.path.join("logs", model_name))

history = model.fit(data["X_train"], data["y_train"],
                    batch_size=BATCH_SIZE,
                    epochs=EPOCHS,
Ejemplo n.º 3
0
    last_sequence = data["last_sequence"][-N_STEPS:]
    # expand dimension
    last_sequence = np.expand_dims(last_sequence, axis=0)
    # get the prediction (scaled from 0 to 1)
    prediction = model.predict(last_sequence)
    # get the price (by inverting the scaling)
    if SCALE:
        predicted_price = data["column_scaler"]["adjclose"].inverse_transform(prediction)[0][0]
    else:
        predicted_price = prediction[0][0]
    return predicted_price


# load the data
data = load_data(ticker, N_STEPS, scale=SCALE, split_by_date=SPLIT_BY_DATE,
                shuffle=SHUFFLE, lookup_step=LOOKUP_STEP, test_size=TEST_SIZE,
                feature_columns=FEATURE_COLUMNS)

# construct the model
model = create_model(N_STEPS, len(FEATURE_COLUMNS), loss=LOSS, units=UNITS, cell=CELL, n_layers=N_LAYERS,
                    dropout=DROPOUT, optimizer=OPTIMIZER, bidirectional=BIDIRECTIONAL)

# load optimal model weights from results folder
model_path = os.path.join("results", model_name) + ".h5"
model.load_weights(model_path)

# evaluate the model
loss, mae = model.evaluate(data["X_test"], data["y_test"], verbose=0)
# calculate the mean absolute error (inverse scaling)
if SCALE:
    mean_absolute_error = data["column_scaler"]["adjclose"].inverse_transform([[mae]])[0][0]
Ejemplo n.º 4
0
def neural():
    for symbol in symbols: #Loop trough the stock summary
        try:
          symbol=(symbol[0])
          name=symbol_full_name(symbol, 3)
          previous_predicted_price = symbol_full_name(symbol, 11)
          ticker= symbol
          print ("Now lets test the model")
          print (ticker, futuredate)
          ticker_data_filename = os.path.join("/root/PycharmProjects/stock-advisor/data", f"{ticker}_{date_now}.csv")
          print ("model name to save")
          model_name = f"{date_now}_{ticker}-{LOSS}-{CELL.__name__}-seq-{N_STEPS}-step-{LOOKUP_STEP}-layers-{N_LAYERS}-units-{UNITS}"

          print ("load the data")
          data = load_data(ticker, N_STEPS, lookup_step=LOOKUP_STEP, test_size=TEST_SIZE, feature_columns=FEATURE_COLUMNS, shuffle=False)

          # construct the model
          model = create_model(N_STEPS, loss=LOSS, units=UNITS, cell=CELL, n_layers=N_LAYERS, dropout=DROPOUT, optimizer=OPTIMIZER)

          model_path = os.path.join("/root/PycharmProjects/stock-advisor/results", model_name) + ".h5"
          model.load_weights(model_path)

          # evaluate the model
          mse, mae = model.evaluate(data["X_test"], data["y_test"])
          # calculate the mean absolute error (inverse scaling)
          mean_absolute_error = data["column_scaler"]["adjclose"].inverse_transform(mae.reshape(1, -1))[0][0]
          print("Mean Absolute Error:", mean_absolute_error)
          # predict the future price
          future_price = predict(model, data)
          print (future_price, futuredate)
          if future_price<previous_predicted_price:
             ai_direction="DOWN"
          else:
             ai_direction="UP"
          printed = (symbol, f"Future price after {LOOKUP_STEP} days is {future_price:.2f}$")
          try:
              db = pymysql.connect("localhost", "stockuser", "123456", "stock_advisor")
              cursor = db.cursor()
              cursor.execute("update symbols set predicted_price='%s', ai_direction='%s'  where symbol='%s'" % (future_price, ai_direction, symbol))
              cursor.execute('insert into logs(date, entry) values("%s", "%s")', (currenttime, printed))			  
              db.commit()
          except pymysql.Error as e:
              print ("Error %d: %s" % (e.args[0], e.args[1]))
              sys.exit(1)
          finally:
              db.close()
			  
          print (date_exist(symbol, futuredate))
		  
          if date_exist(symbol, futuredate) != 1:
             try:
                 db = pymysql.connect("localhost", "stockuser", "123456", "stock_advisor")
                 cursor = db.cursor()
                 cursor.execute('insert into history(predicted_price, date, symbol) values("%s", "%s", "%s")' % (future_price, futuredate, symbol))
                 db.commit()
             except pymysql.Error as e:
                 print ("Error %d: %s" % (e.args[0], e.args[1]))
                 sys.exit(1)
             finally:
                 db.close()
		  


          print(f"Future price after {LOOKUP_STEP} days is {future_price:.2f}$")
          print("Accuracy Score:", get_accuracy(model, data))
          plot_graph(model, data, name)
          newfilename=("{}_result.png".format(symbol))
          my_path = "/root/PycharmProjects/stock-advisor/images/results.png"
          new_name = os.path.join(os.path.dirname(my_path), newfilename)
          os.rename(my_path, new_name)

          print (new_name)

          src_dir = "/root/PycharmProjects/stock-advisor/images/"
          dst_dir = "/var/www/html/images/"
          for pngfile in glob.iglob(os.path.join(src_dir, "*.png")):
            shutil.copy(pngfile, dst_dir)



        except:
            continue
Ejemplo n.º 5
0
def neural():
    for symbol in symbols:  #Loop trough the stock summary
        try:
            symbol = (symbol[0])
            name = symbol_full_name(symbol, 3)
            # create these folders if they does not exist
            if not os.path.isdir(
                    "/root/PycharmProjects/stock-advisor/results"):
                os.mkdir("/root/PycharmProjects/stock-advisor/results")
            if not os.path.isdir("/root/PycharmProjects/stock-advisor/logs"):
                os.mkdir("/root/PycharmProjects/stock-advisor/logs")
            if not os.path.isdir("/root/PycharmProjects/stock-advisor/data"):
                os.mkdir("/root/PycharmProjects/stock-advisor/data")

            print(symbol)

            ticker = symbol
            ticker_data_filename = os.path.join(
                "/root/PycharmProjects/stock-advisor/data",
                f"{ticker}_{date_now}.csv")
            # model name to save
            model_name = f"{date_now}_{ticker}-{LOSS}-{CELL.__name__}-seq-{N_STEPS}-step-{LOOKUP_STEP}-layers-{N_LAYERS}-units-{UNITS}"
            fileexist = ("/root/PycharmProjects/stock-advisor/results/" +
                         model_name + ".h5")
            if path.exists(fileexist):
                print("Model already trained")
            else:
                print("Starting to train model")

                #          # load the CSV file from disk (dataset) if it already exists (without downloading)
                if os.path.isfile(ticker_data_filename):
                    ticker = pd.read_csv(ticker_data_filename)

                print("load the data")
                data = load_data(ticker,
                                 N_STEPS,
                                 lookup_step=LOOKUP_STEP,
                                 test_size=TEST_SIZE,
                                 feature_columns=FEATURE_COLUMNS)

                if not os.path.isfile(ticker_data_filename):
                    # save the CSV file (dataset)
                    data["df"].to_csv(ticker_data_filename)

            # construct the model
                model = create_model(N_STEPS,
                                     loss=LOSS,
                                     units=UNITS,
                                     cell=CELL,
                                     n_layers=N_LAYERS,
                                     dropout=DROPOUT,
                                     optimizer=OPTIMIZER)

                # some tensorflow callbacks
                checkpointer = ModelCheckpoint(os.path.join(
                    "/root/PycharmProjects/stock-advisor/results", model_name),
                                               save_weights_only=True,
                                               save_best_only=True,
                                               verbose=1)
                tensorboard = TensorBoard(log_dir=os.path.join(
                    "/root/PycharmProjects/stock-advisor/logs", model_name))

                history = model.fit(data["X_train"],
                                    data["y_train"],
                                    batch_size=BATCH_SIZE,
                                    epochs=EPOCHS,
                                    validation_data=(data["X_test"],
                                                     data["y_test"]),
                                    callbacks=[checkpointer, tensorboard],
                                    verbose=1)

                model.save(
                    os.path.join("/root/PycharmProjects/stock-advisor/results",
                                 model_name) + ".h5")

                print("Model trained")

        except:
            continue
Ejemplo n.º 6
0
    def train_model(self):
        self.textbox.setPlainText('Training...')
        global row_data
        # create these folders if they does not exist
        cwd = os.getcwd()
        cwd = os.path.basename(cwd)
        if cwd == 'lstm':
            if not os.path.isdir("results"):
                os.mkdir("results")

            if not os.path.isdir("logs"):
                os.mkdir("logs")

            datadir = "./data"
            resultsdir = "results"
            logsdir = "logs"
        else:
            if not os.path.isdir("lstm/results"):
                os.mkdir("lstm/results")

            if not os.path.isdir("lstm/logs"):
                os.mkdir("lstm/logs")

            if not os.path.isdir("data"):
                os.mkdir("data")
            datadir = "data"
            resultsdir = "lstm/results"
            logsdir = "lstm/logs"

        # load the data
        data = load_data(parameters.ticker,
                         parameters.N_STEPS,
                         lookup_step=parameters.LOOKUP_STEP,
                         test_size=parameters.TEST_SIZE,
                         feature_columns=parameters.FEATURE_COLUMNS,
                         row_data=row_data)

        # construct the model
        model = create_model(parameters.N_STEPS,
                             loss=parameters.LOSS,
                             units=parameters.UNITS,
                             cell=parameters.CELL,
                             n_layers=parameters.N_LAYERS,
                             dropout=parameters.DROPOUT,
                             optimizer=parameters.OPTIMIZER)

        # some tensorflow callbacks
        checkpointer = ModelCheckpoint(os.path.join(resultsdir,
                                                    parameters.model_name),
                                       save_weights_only=True,
                                       save_best_only=True,
                                       verbose=1)

        tensorboard = TensorBoard(
            log_dir=os.path.join(logsdir, parameters.model_name))

        print('# Fit model on training data')
        history = model.fit(data["X_train"],
                            data["y_train"],
                            batch_size=parameters.BATCH_SIZE,
                            epochs=parameters.EPOCHS,
                            validation_data=(data["X_test"], data["y_test"]),
                            callbacks=[checkpointer, tensorboard],
                            verbose=1)

        model.save(os.path.join(resultsdir, parameters.model_name) + ".h5")
        if not cwd == 'lstm':
            self.plot_train_graph(model, data)

        self.textbox.setPlainText(
            'The model finished training. Proceed with testing.')
        self.button2.setEnabled(True)
Ejemplo n.º 7
0
    plt.title(TICKER+" Stock Price Forecast "+ f"{LOOKUP_STEP}" +" days out", fontsize=16)
    plt.plot(test_df[f'true_adjclose_{LOOKUP_STEP}'].tail(N_STEPS), c='b')
    plt.plot(test_df[f'adjclose_{LOOKUP_STEP}'].tail(N_STEPS), c='r')
    plt.plot(df2['forecast'].tail(LOOKUP_STEP-1), c='r')
    plt.xlabel("Days")
    plt.ylabel("Price")
    plt.legend(["Actual Price", "Predicted Price"])
    forecast_folder = "forecasts"
    if not os.path.isdir(forecast_folder):
        os.mkdir(forecast_folder)
    filename = os.path.join(forecast_folder, TICKER.lower() + "_" + f"{LOOKUP_STEP}" + "_forecast.png")
    plt.savefig(filename)
    plt.show()    
    
# load the data
data = load_data(TICKER, N_STEPS, scale=SCALE, split_by_date=SPLIT_BY_DATE, shuffle=SHUFFLE, lookup_step=LOOKUP_STEP, test_size=TEST_SIZE, feature_columns=FEATURE_COLUMNS, ma_periods=MA_PERIODS)

# construct the model
model = create_model(N_STEPS, len(FEATURE_COLUMNS), loss=LOSS, units=UNITS, cell=CELL, n_layers=N_LAYERS, dropout=DROPOUT, optimizer=OPTIMIZER, bidirectional=BIDIRECTIONAL)

# load optimal model weights from results folder
model_path = os.path.join("results", model_name) + ".h5"
model.load_weights(model_path)

# evaluate the model
loss, mae = model.evaluate(data["X_test"], data["y_test"], verbose=0)
# calculate the mean absolute error (inverse scaling)
if SCALE:
    mean_absolute_error = data["column_scaler"]["adjclose"].inverse_transform([[mae]])[0][0]
else:
    mean_absolute_error = mae