1998Abu commited on
Commit
929e441
·
verified ·
1 Parent(s): 4814a81

from kaggle 2nd i guess

Browse files
Files changed (1) hide show
  1. app.py +116 -3
app.py CHANGED
@@ -1,3 +1,116 @@
1
- a = 10
2
- b = 5
3
- print(a+b)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ !pip install yfinance pandas numpy scikit-learn matplotlib
2
+ import yfinance as yf
3
+ import pandas as pd
4
+ import numpy as np
5
+ from sklearn.model_selection import train_test_split
6
+ from sklearn.ensemble import RandomForestClassifier
7
+ from sklearn.metrics import accuracy_score
8
+ import matplotlib.pyplot as plt
9
+ import time
10
+
11
+ # Function to fetch historical data
12
+ def get_historical_data(ticker, interval, period):
13
+ stock_data = yf.download(ticker, interval=interval, period=period)
14
+ return stock_data
15
+
16
+ # Function to fetch real-time data
17
+ def get_realtime_data(ticker):
18
+ stock = yf.Ticker(ticker)
19
+ data = stock.history(period='7d', interval='1m')
20
+ return data
21
+
22
+ # Function to preprocess data
23
+ def preprocess_data(data):
24
+ data['returns'] = data['Close'].pct_change()
25
+ data['target'] = np.where(data['returns'] > 0, 1, 0)
26
+ data.dropna(inplace=True)
27
+ return data
28
+
29
+ # Function to train a simple model
30
+ def train_model(data):
31
+ X = data[['Open', 'High', 'Low', 'Close', 'Volume']]
32
+ y = data['target']
33
+
34
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
35
+
36
+ model = RandomForestClassifier(n_estimators=100, random_state=42)
37
+ model.fit(X_train, y_train)
38
+
39
+ predictions = model.predict(X_test)
40
+
41
+ accuracy = accuracy_score(y_test, predictions)
42
+ print(f'Model Accuracy: {accuracy}')
43
+
44
+ return model
45
+
46
+ # Function to simulate trading
47
+ def simulate_trading(model, data, initial_balance=1):
48
+ data['predicted'] = model.predict(data[['Open', 'High', 'Low', 'Close', 'Volume']])
49
+
50
+ balance = initial_balance
51
+ positions = 0
52
+
53
+ for i in range(len(data)-1):
54
+ if data['predicted'].iloc[i] == 1:
55
+ # Buy
56
+ positions += balance / data['Close'].iloc[i]
57
+ balance = 0
58
+ else:
59
+ # Sell
60
+ balance += positions * data['Close'].iloc[i]
61
+ positions = 0
62
+
63
+ # Sell remaining positions at the last data point
64
+ balance += positions * data['Close'].iloc[-1]
65
+
66
+ return balance
67
+
68
+ # Function to plot real and predicted data
69
+ def plot_data(data):
70
+ plt.figure(figsize=(10, 6))
71
+
72
+ plt.plot(data['Close'], label='Real Data', color='blue')
73
+ plt.scatter(data.index, data['Close'], c=data['predicted'], cmap='coolwarm', marker='o', label='Predicted Data')
74
+
75
+ plt.title('Real vs Predicted Data')
76
+ plt.xlabel('Date')
77
+ plt.ylabel('Closing Price')
78
+ plt.legend()
79
+ plt.show()
80
+
81
+ # Main function
82
+ def main():
83
+ ticker = 'AAPL' # You can change this to the desired stock symbol
84
+
85
+ # Fetch real-time data every 1 minute for the last 1 day
86
+ data = get_realtime_data(ticker)
87
+
88
+ # Preprocess data
89
+ data = preprocess_data(data)
90
+
91
+ model = train_model(data)
92
+
93
+ while True:
94
+ # Fetch real-time data every 1 minute
95
+ new_data = get_realtime_data(ticker)
96
+
97
+ # Update the existing data with new data
98
+ data = pd.concat([data, new_data])
99
+
100
+ # Preprocess data
101
+ data = preprocess_data(data)
102
+
103
+ # Simulate trading
104
+ final_balance = simulate_trading(model, data)
105
+
106
+ # Print current balance
107
+ print(f'Current Balance: {final_balance:.2f} Rupees')
108
+
109
+ # Plot real and predicted data
110
+ plot_data(data)
111
+
112
+ # Wait for 1 minute before fetching new data
113
+ time.sleep(60)
114
+
115
+ if __name__ == "__main__":
116
+ main()