File size: 6,304 Bytes
182c4ed
441f684
3776d99
 
 
 
 
8448555
73670cb
8448555
73670cb
8448555
3776d99
8448555
3776d99
 
441f684
 
3776d99
8448555
7e2ed99
3776d99
8448555
73670cb
441f684
73670cb
 
8448555
 
 
 
441f684
 
8448555
441f684
 
8448555
441f684
7e2ed99
3776d99
441f684
8448555
3776d99
 
8448555
9c65345
8448555
 
 
9c65345
 
 
8448555
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c65345
 
 
 
 
 
 
8448555
 
 
 
 
 
 
 
 
 
 
 
 
 
9c65345
8448555
 
 
 
 
 
 
 
 
 
 
 
 
9c65345
8448555
 
9c65345
 
 
 
8448555
 
9c65345
 
8448555
 
 
 
 
 
 
9c65345
 
 
 
ad04e27
9c65345
 
8448555
73670cb
8448555
 
 
 
 
 
 
 
 
 
 
 
9c65345
 
 
 
 
 
 
 
8448555
 
9c65345
8448555
 
73670cb
8448555
 
9c65345
8448555
 
 
 
9c65345
8448555
9c65345
8448555
9c65345
8448555
9c65345
8448555
9c65345
8448555
9c65345
8448555
 
9c65345
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import pandas as pd
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import gradio as gr
import os

# Define the Dataset class
class BankNiftyDataset(Dataset):
    def __init__(self, data, seq_len, target_cols=['close']):
        self.data = data
        self.seq_len = seq_len
        self.target_cols = target_cols

    def __len__(self):
        return max(0, len(self.data) - self.seq_len + 1)

    def __getitem__(self, idx):
        seq_data = self.data.iloc[idx:idx+self.seq_len]
        features = torch.tensor(seq_data[['open', 'high', 'low', 'close', 'volume', 'oi']].values, dtype=torch.float32)
        label = torch.tensor(seq_data[self.target_cols].iloc[-1].values, dtype=torch.float32)
        return features, label

# Define the LSTM model
class LSTMModel(nn.Module):
    def __init__(self, input_dim, hidden_dim, output_dim, num_layers=2, dropout=0.1):
        super(LSTMModel, self).__init__()
        self.lstm = nn.LSTM(input_dim, hidden_dim, num_layers=num_layers, batch_first=True, dropout=dropout)
        self.fc = nn.Sequential(
            nn.Linear(hidden_dim, hidden_dim // 2),
            nn.ReLU(),
            nn.Dropout(dropout),
            nn.Linear(hidden_dim // 2, output_dim)
        )

    def forward(self, x):
        lstm_out, _ = self.lstm(x)
        out = self.fc(lstm_out[:, -1, :])
        return out

# Function to train the model
def train_model(model, train_loader, val_loader, num_epochs=10):
    criterion = nn.MSELoss()
    optimizer = optim.Adam(model.parameters(), lr=0.001)
    
    best_val_loss = float('inf')
    best_model = None
    
    for epoch in range(num_epochs):
        model.train()
        for features, labels in train_loader:
            optimizer.zero_grad()
            outputs = model(features)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()
        
        model.eval()
        val_loss = 0
        with torch.no_grad():
            for features, labels in val_loader:
                outputs = model(features)
                val_loss += criterion(outputs, labels).item()
        val_loss /= len(val_loader)
        
        print(f"Epoch {epoch+1}/{num_epochs}, Validation Loss: {val_loss:.4f}")
        
        if val_loss < best_val_loss:
            best_val_loss = val_loss
            best_model = model.state_dict().copy()
    
    model.load_state_dict(best_model)
    return model, best_val_loss

# Function to generate trading signals
def generate_signals(predictions, actual_values, stop_loss_threshold=0.05):
    signals = []
    for pred, actual in zip(predictions, actual_values):
        if pred > actual * (1 + stop_loss_threshold):
            signals.append("Buy CE")
        elif pred < actual * (1 - stop_loss_threshold):
            signals.append("Buy PE")
        else:
            signals.append("Hold")
    return signals

# Function to generate a report
def generate_report(predictions, actual_values, signals, val_loss):
    report = []
    cumulative_profit = 0
    for i in range(len(signals)):
        signal = signals[i]
        profit = actual_values[i] - predictions[i]
        if signal == "Buy CE":
            cumulative_profit += profit
        elif signal == "Buy PE":
            cumulative_profit -= profit
        report.append(f"Signal: {signal}, Actual: {actual_values[i]:.2f}, Predicted: {predictions[i]:.2f}, Profit: {profit:.2f}")
    
    total_profit = cumulative_profit
    report.append(f"Total Profit: {total_profit:.2f}")
    report.append(f"Model Validation Loss: {val_loss:.4f}")
    return "\n".join(report)

# Global variables to store the model and scaler
global_model = None
global_scaler = None

# Function to process data and make predictions
def predict():
    global global_model, global_scaler
    
    # Load the pre-existing CSV file
    csv_path = 'BANKNIFTY_OPTION_CHAIN_data.csv'
    if not os.path.exists(csv_path):
        return "Error: CSV file not found in the expected location."

    # Load and preprocess data
    data = pd.read_csv(csv_path)
    
    if global_scaler is None:
        global_scaler = StandardScaler()
        scaled_data = global_scaler.fit_transform(data[['open', 'high', 'low', 'close', 'volume', 'oi']])
    else:
        scaled_data = global_scaler.transform(data[['open', 'high', 'low', 'close', 'volume', 'oi']])
    
    data[['open', 'high', 'low', 'close', 'volume', 'oi']] = scaled_data

    # Split data
    train_data, val_data = train_test_split(data, test_size=0.2, random_state=42)

    # Create datasets and dataloaders
    seq_len = 20
    target_cols = ['close']
    train_dataset = BankNiftyDataset(train_data, seq_len, target_cols)
    val_dataset = BankNiftyDataset(val_data, seq_len, target_cols)
    train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
    val_loader = DataLoader(val_dataset, batch_size=32, shuffle=False)

    # Initialize and train the model
    input_dim = 6
    hidden_dim = 64
    output_dim = len(target_cols)
    
    if global_model is None:
        global_model = LSTMModel(input_dim, hidden_dim, output_dim)
    
    global_model, val_loss = train_model(global_model, train_loader, val_loader)

    # Make predictions
    global_model.eval()
    predictions = []
    actual_values = val_data['close'].values[seq_len-1:]
    with torch.no_grad():
        for i in range(len(val_dataset)):
            features, _ = val_dataset[i]
            pred = global_model(features.unsqueeze(0)).item()
            predictions.append(pred)

    # Generate signals and report
    signals = generate_signals(predictions, actual_values)
    report = generate_report(predictions, actual_values, signals, val_loss)

    return report

# Set up the Gradio interface
iface = gr.Interface(
    fn=predict,
    inputs=None,
    outputs=gr.Textbox(label="Prediction Report"),
    title="BankNifty Option Chain Predictor",
    description="Click 'Submit' to generate predictions and trading signals based on the latest BankNifty option chain data. The model is automatically trained and improved with each run."
)

# Launch the app
iface.launch()