Update app.py
Browse files
app.py
CHANGED
@@ -1,59 +1,44 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import yfinance as yf
|
3 |
import pandas as pd
|
4 |
-
import
|
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 |
-
name='Candlesticks'))
|
48 |
-
|
49 |
-
# Plot Double Top pattern signals
|
50 |
-
fig.add_trace(go.Scatter(x=signals.index[signals['double_top'] == 1],
|
51 |
-
y=stock_prices['High'][signals['double_top'] == 1],
|
52 |
-
mode='markers',
|
53 |
-
marker=dict(color='red', size=10),
|
54 |
-
name='Double Top Signal'))
|
55 |
-
|
56 |
-
st.plotly_chart(fig)
|
57 |
-
|
58 |
-
if __name__ == "__main__":
|
59 |
-
main()
|
|
|
|
|
|
|
1 |
import pandas as pd
|
2 |
+
import yfinance as yf
|
3 |
+
from ta import add_all_ta_features
|
4 |
+
from ta.utils import dropna
|
5 |
+
from sklearn.model_selection import train_test_split
|
6 |
+
from sklearn.ensemble import RandomForestClassifier
|
7 |
+
from sklearn.metrics import accuracy_score
|
8 |
+
|
9 |
+
# Load historical stock data using yfinance
|
10 |
+
symbol = 'AAPL'
|
11 |
+
start_date = '2021-01-01'
|
12 |
+
end_date = '2022-01-01'
|
13 |
+
stock_data = yf.download(symbol, start=start_date, end=end_date)
|
14 |
+
|
15 |
+
# Clean NaN values
|
16 |
+
stock_data = dropna(stock_data)
|
17 |
+
|
18 |
+
# Add all ta features
|
19 |
+
stock_data = add_all_ta_features(
|
20 |
+
stock_data, open="Open", high="High", low="Low", close="Close", volume="Volume")
|
21 |
+
|
22 |
+
# Define target variable (1 for pattern occurrence, 0 otherwise)
|
23 |
+
stock_data['DoubleTop'] = stock_data['close'].shift(-1) > stock_data['close']
|
24 |
+
|
25 |
+
# Drop NaN values introduced by the shift
|
26 |
+
stock_data = dropna(stock_data)
|
27 |
+
|
28 |
+
# Features and target
|
29 |
+
X = stock_data.drop(['DoubleTop'], axis=1)
|
30 |
+
y = stock_data['DoubleTop']
|
31 |
+
|
32 |
+
# Split into train and test sets
|
33 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
34 |
+
|
35 |
+
# Train a simple RandomForestClassifier (you may need a more sophisticated model)
|
36 |
+
clf = RandomForestClassifier()
|
37 |
+
clf.fit(X_train, y_train)
|
38 |
+
|
39 |
+
# Predictions
|
40 |
+
y_pred = clf.predict(X_test)
|
41 |
+
|
42 |
+
# Evaluate the model
|
43 |
+
accuracy = accuracy_score(y_test, y_pred)
|
44 |
+
print(f"Model Accuracy: {accuracy}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|