Spaces:
Sleeping
Sleeping
Rahul-Crudcook
commited on
Upload 6 files
Browse files- .gitattributes +1 -0
- BANKNIFTY_Future.csv +0 -0
- BANKNIFTY_Option.csv +3 -0
- app.py +124 -0
- banknifty_model.h5 +3 -0
- requirements.txt +6 -0
- scaler.npy +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
BANKNIFTY_Option.csv filter=lfs diff=lfs merge=lfs -text
|
BANKNIFTY_Future.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
BANKNIFTY_Option.csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8bad0f8d39a100144c444ac8640545c2bc1a51a19322482e26c6095dac7b5288
|
3 |
+
size 35508486
|
app.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
from sklearn.preprocessing import MinMaxScaler
|
5 |
+
from sklearn.metrics import mean_squared_error, r2_score
|
6 |
+
from tensorflow.keras.models import load_model
|
7 |
+
import streamlit as st
|
8 |
+
|
9 |
+
# Step 1: Load the datasets
|
10 |
+
def load_data():
|
11 |
+
options_path = 'BANKNIFTY_Option.csv'
|
12 |
+
futures_path = 'BANKNIFTY_Future.csv'
|
13 |
+
options_data = pd.read_csv(options_path)
|
14 |
+
futures_data = pd.read_csv(futures_path)
|
15 |
+
return options_data, futures_data
|
16 |
+
|
17 |
+
# Step 2: Preprocessing and Merging
|
18 |
+
def preprocess_data(options_data, futures_data):
|
19 |
+
options_data['lasttradetime'] = pd.to_datetime(options_data['lasttradetime'], unit='s')
|
20 |
+
futures_data['lasttradetime'] = pd.to_datetime(futures_data['lasttradetime'], unit='s')
|
21 |
+
|
22 |
+
# Merge datasets on lasttradetime
|
23 |
+
merged_data = pd.merge(options_data, futures_data, on='lasttradetime', how='inner', suffixes=('_options', '_futures'))
|
24 |
+
|
25 |
+
# Create new features
|
26 |
+
merged_data['price_diff'] = merged_data['close_options'] - merged_data['close_futures']
|
27 |
+
merged_data['volume_ratio'] = merged_data['tradedqty'] / merged_data['volume']
|
28 |
+
merged_data['openinterest_diff'] = merged_data['openinterest_options'] - merged_data['openinterest_futures']
|
29 |
+
|
30 |
+
# Drop unnecessary columns
|
31 |
+
merged_data = merged_data[['lasttradetime', 'close_options', 'price_diff', 'volume_ratio', 'openinterest_diff']]
|
32 |
+
merged_data = merged_data.set_index('lasttradetime')
|
33 |
+
|
34 |
+
return merged_data
|
35 |
+
|
36 |
+
# Step 3: Load Pre-Trained Model and Scaler
|
37 |
+
def load_trained_model_and_scaler():
|
38 |
+
model = load_model('banknifty_model.h5')
|
39 |
+
scaler = np.load('scaler.npy', allow_pickle=True).item()
|
40 |
+
return model, scaler
|
41 |
+
|
42 |
+
# Step 4: Predict Future Prices
|
43 |
+
def predict_future_prices(model, scaler, X, last_date, steps=5):
|
44 |
+
last_sequence = X[-1:, :, :]
|
45 |
+
future_forecast = []
|
46 |
+
future_dates = pd.date_range(start=last_date, periods=steps + 1, freq='5min')[1:]
|
47 |
+
|
48 |
+
for _ in range(steps):
|
49 |
+
next_pred = model.predict(last_sequence)[0, 0]
|
50 |
+
future_forecast.append(next_pred)
|
51 |
+
next_input = np.concatenate([last_sequence[:, 1:, :], np.array([[[next_pred, 0, 0, 0]]])], axis=1)
|
52 |
+
last_sequence = next_input
|
53 |
+
|
54 |
+
future_forecast_rescaled = scaler.inverse_transform(
|
55 |
+
np.hstack((np.array(future_forecast).reshape(-1, 1), np.zeros((steps, X.shape[2] - 1))))
|
56 |
+
)[:, 0]
|
57 |
+
return future_forecast_rescaled, future_dates
|
58 |
+
|
59 |
+
# Step 5: Evaluate Model
|
60 |
+
def evaluate_model(model, X_test, y_test, scaler):
|
61 |
+
y_pred = model.predict(X_test)
|
62 |
+
y_pred_rescaled = scaler.inverse_transform(
|
63 |
+
np.hstack((y_pred, np.zeros((y_pred.shape[0], X_test.shape[2] - 1))))
|
64 |
+
)[:, 0]
|
65 |
+
y_test_rescaled = scaler.inverse_transform(
|
66 |
+
np.hstack((y_test.reshape(-1, 1), np.zeros((y_test.shape[0], X_test.shape[2] - 1))))
|
67 |
+
)[:, 0]
|
68 |
+
|
69 |
+
mse = mean_squared_error(y_test_rescaled, y_pred_rescaled)
|
70 |
+
r2 = r2_score(y_test_rescaled, y_pred_rescaled)
|
71 |
+
|
72 |
+
# Calculate accuracy
|
73 |
+
accuracy = 1 - (np.abs(y_test_rescaled - y_pred_rescaled).mean() / y_test_rescaled.mean())
|
74 |
+
return mse, r2, accuracy
|
75 |
+
|
76 |
+
# Streamlit App
|
77 |
+
def main():
|
78 |
+
st.title("Bank Nifty Options & Futures Forecasting")
|
79 |
+
|
80 |
+
# Load and preprocess data
|
81 |
+
options_data, futures_data = load_data()
|
82 |
+
merged_data = preprocess_data(options_data, futures_data)
|
83 |
+
|
84 |
+
st.write("### Merged Dataset")
|
85 |
+
st.dataframe(merged_data.head(10))
|
86 |
+
|
87 |
+
# Feature Scaling
|
88 |
+
model, scaler = load_trained_model_and_scaler()
|
89 |
+
scaled_data = scaler.transform(merged_data)
|
90 |
+
|
91 |
+
# Create Sequences
|
92 |
+
time_steps = 72
|
93 |
+
X = np.array([scaled_data[i:i + time_steps] for i in range(len(scaled_data) - time_steps)])
|
94 |
+
y = scaled_data[time_steps:, 0]
|
95 |
+
|
96 |
+
# Evaluate Model
|
97 |
+
mse, r2, accuracy = evaluate_model(model, X, y, scaler)
|
98 |
+
st.write(f"### Model Evaluation")
|
99 |
+
st.write(f"Mean Squared Error (MSE): {mse:.2f}")
|
100 |
+
st.write(f"R² Score: {r2:.2f}")
|
101 |
+
st.write(f"Accuracy: {accuracy * 100:.2f}%")
|
102 |
+
|
103 |
+
# Predict Future Prices
|
104 |
+
st.write("### Predicting Future Prices...")
|
105 |
+
last_date = merged_data.index[-1]
|
106 |
+
steps = st.slider("Select Number of Future Steps to Predict", min_value=1, max_value=20, value=5)
|
107 |
+
future_prices, future_dates = predict_future_prices(model, scaler, X, last_date, steps)
|
108 |
+
|
109 |
+
st.write("### Predicted Future Prices")
|
110 |
+
for date, price in zip(future_dates, future_prices):
|
111 |
+
st.write(f"{date}: {price:.2f}")
|
112 |
+
|
113 |
+
# Plot Results
|
114 |
+
st.write("### Predicted Prices Plot")
|
115 |
+
fig, ax = plt.subplots(figsize=(12, 6))
|
116 |
+
ax.plot(future_dates, future_prices, marker='o', label="Predicted Prices")
|
117 |
+
ax.set_title("Future Predicted Prices")
|
118 |
+
ax.set_xlabel("Date")
|
119 |
+
ax.set_ylabel("Price")
|
120 |
+
ax.legend()
|
121 |
+
st.pyplot(fig)
|
122 |
+
|
123 |
+
if __name__ == "__main__":
|
124 |
+
main()
|
banknifty_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e3baa682d1b1fd7c8911331cd30a5b7ad0cd0c9d475a197206cd634fc69fbf69
|
3 |
+
size 4277752
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pandas
|
2 |
+
numpy
|
3 |
+
matplotlib
|
4 |
+
scikit-learn
|
5 |
+
tensorflow
|
6 |
+
streamlit
|
scaler.npy
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1d3b0aa02fd64ca0a63a88eb47366e80bd00444a2835531d4449aa95e318d94f
|
3 |
+
size 1069
|