--- license: unknown datasets: - GeoTrix/Datathon --- # Model For Datathon This repo holds 2 final models that was used and made for Ristek Datathon. The first model is a Linear Regression Model, and the other is an LSTM Model. The datasets use to train the model rely with the yfinance python module, which is a module that was made by Yahoo! that stores the stock market data, from what price it opens, close, the highest price it reach on that day and the lowest and the volume. It was also slighly modified with some added features that was used for the Linear Regression Model, adding SMA, EMA and RSI. While the LSTM Model only relied only on the Closing price of the stock and without the added modified dataset due to during testing it generated the best result when just using the close feature. ## Using Linear Regression Model Sample python code to see the linear regression model ``` from joblib import load import yfinance as yf # Grab the most recent stock data data = yf.download('AAPL', period='40d') # Adding SMA (Simple Moving Average) into the data data['SMA_5'] = data['Close'].rolling(window=5).mean() data['SMA_10'] = data['Close'].rolling(window=10).mean() # Adding EMA (Exponential Moving Average) into the data data['EMA_5'] = data['Close'].ewm(span=5, adjust=False).mean() data['EMA_10'] = data['Close'].ewm(span=10, adjust=False).mean() # Adding RSI (Relative Strength Index) into the data delta = data['Close'].diff() avg_gain = (delta.where(delta > 0, 0)).rolling(window=14).mean() avg_loss = (-delta.where(delta < 0, 0)).rolling(window=14).mean() rs = avg_gain / avg_loss data['RSI'] = 100 - (100 / (1 + rs)) # Grab the features of the final stock price to predict the next day stock feature = data[['Open', 'High', 'Low', 'Close', 'Volume', 'SMA_5', 'SMA_10', 'EMA_5', 'EMA_10', 'RSI']].tail().values # Load the models in your directory model = load('Path/Of/The/Model') predictions = model.predict(feature) # Predicts gives the next day stock print(f"Predicted: ${predictions[0]:.2f}") ``` ## Using LSTM Model Sample python code to see the LSTM model ``` import yfinance as yf from tensorflow.keras.models import load_model from sklearn.preprocessing import MinMaxScaler import numpy as np # Grab the most recent stock data data = yf.download('AAPL', period='40d') # Normalize the data scaler = MinMaxScaler(feature_range=(0, 1)) scaled_features = scaler.fit_transform(data[['Close']]) # Grabbing the last 30 days of data for prediction windows_size = 30 last_30_days = scaled_features[-windows_size:] # Shaping the data to match the model input x = np.reshape(last_30_days, (1, windows_size, 1)) # Loading model model = load_model('Models/Stockly-lstm.keras') # Predicting the next day's stock price predictions = model.predict(x) predictions = scaler.inverse_transform(predictions) print(f"Predicted: ${predictions[0][0]:.2f}") ``` **NOTE:** These models are still very new and not that highly accurate. So don't really use them for actual usage to trade in the stock market, but rather have fun with it, since the model itself was made pretty rushed to complete the Datathon. If the model ever get updated to be more accurate this NOTE should be gone.