Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,86 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: unknown
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: unknown
|
| 3 |
+
datasets:
|
| 4 |
+
- GeoTrix/Datathon
|
| 5 |
+
---
|
| 6 |
+
# Model For Datathon
|
| 7 |
+
|
| 8 |
+
This repo holds 2 final models that was used and made for Ristek Datathon. The first model is a Linear Regression Model,
|
| 9 |
+
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
|
| 10 |
+
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.
|
| 11 |
+
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
|
| 12 |
+
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
|
| 13 |
+
when just using the close feature.
|
| 14 |
+
|
| 15 |
+
## Using Linear Regression Model
|
| 16 |
+
|
| 17 |
+
Sample python code to see the linear regression model
|
| 18 |
+
```
|
| 19 |
+
from joblib import load
|
| 20 |
+
import yfinance as yf
|
| 21 |
+
|
| 22 |
+
# Grab the most recent stock data
|
| 23 |
+
data = yf.download('AAPL', period='40d')
|
| 24 |
+
|
| 25 |
+
# Adding SMA (Simple Moving Average) into the data
|
| 26 |
+
data['SMA_5'] = data['Close'].rolling(window=5).mean()
|
| 27 |
+
data['SMA_10'] = data['Close'].rolling(window=10).mean()
|
| 28 |
+
|
| 29 |
+
# Adding EMA (Exponential Moving Average) into the data
|
| 30 |
+
data['EMA_5'] = data['Close'].ewm(span=5, adjust=False).mean()
|
| 31 |
+
data['EMA_10'] = data['Close'].ewm(span=10, adjust=False).mean()
|
| 32 |
+
|
| 33 |
+
# Adding RSI (Relative Strength Index) into the data
|
| 34 |
+
delta = data['Close'].diff()
|
| 35 |
+
avg_gain = (delta.where(delta > 0, 0)).rolling(window=14).mean()
|
| 36 |
+
avg_loss = (-delta.where(delta < 0, 0)).rolling(window=14).mean()
|
| 37 |
+
rs = avg_gain / avg_loss
|
| 38 |
+
data['RSI'] = 100 - (100 / (1 + rs))
|
| 39 |
+
|
| 40 |
+
# Grab the features of the final stock price to predict the next day stock
|
| 41 |
+
feature = data[['Open', 'High', 'Low', 'Close', 'Volume', 'SMA_5', 'SMA_10', 'EMA_5', 'EMA_10', 'RSI']].tail().values
|
| 42 |
+
|
| 43 |
+
# Load the models in your directory
|
| 44 |
+
model = load('Path/Of/The/Model')
|
| 45 |
+
predictions = model.predict(feature)
|
| 46 |
+
|
| 47 |
+
# Predicts gives the next day stock
|
| 48 |
+
print(f"Predicted: ${predictions[0]:.2f}")
|
| 49 |
+
```
|
| 50 |
+
|
| 51 |
+
## Using LSTM Model
|
| 52 |
+
|
| 53 |
+
Sample python code to see the LSTM model
|
| 54 |
+
```
|
| 55 |
+
import yfinance as yf
|
| 56 |
+
from tensorflow.keras.models import load_model
|
| 57 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 58 |
+
import numpy as np
|
| 59 |
+
|
| 60 |
+
# Grab the most recent stock data
|
| 61 |
+
data = yf.download('AAPL', period='40d')
|
| 62 |
+
|
| 63 |
+
# Normalize the data
|
| 64 |
+
scaler = MinMaxScaler(feature_range=(0, 1))
|
| 65 |
+
scaled_features = scaler.fit_transform(data[['Close']])
|
| 66 |
+
|
| 67 |
+
# Grabbing the last 30 days of data for prediction
|
| 68 |
+
windows_size = 30
|
| 69 |
+
last_30_days = scaled_features[-windows_size:]
|
| 70 |
+
|
| 71 |
+
# Shaping the data to match the model input
|
| 72 |
+
x = np.reshape(last_30_days, (1, windows_size, 1))
|
| 73 |
+
|
| 74 |
+
# Loading model
|
| 75 |
+
model = load_model('Models/Stockly-lstm.keras')
|
| 76 |
+
|
| 77 |
+
# Predicting the next day's stock price
|
| 78 |
+
predictions = model.predict(x)
|
| 79 |
+
predictions = scaler.inverse_transform(predictions)
|
| 80 |
+
|
| 81 |
+
print(f"Predicted: ${predictions[0][0]:.2f}")
|
| 82 |
+
```
|
| 83 |
+
|
| 84 |
+
**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,
|
| 85 |
+
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
|
| 86 |
+
this NOTE should be gone.
|