Geek7 commited on
Commit
46c30ed
·
verified ·
1 Parent(s): 81fcf09

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -58
app.py CHANGED
@@ -1,59 +1,44 @@
1
- import streamlit as st
2
- import yfinance as yf
3
  import pandas as pd
4
- import ta
5
- import plotly.graph_objects as go
6
-
7
- # Function to load stock data using yfinance
8
- def get_stock_data(symbol, start_date, end_date, interval='1h'):
9
- stock_data = yf.download(symbol, start=start_date, end=end_date, interval=interval)
10
- return stock_data
11
-
12
- # Function to detect Double Top pattern
13
- def detect_double_top(data, lookback=20):
14
- signals = pd.DataFrame(index=data.index)
15
- signals['signal'] = 0.0
16
-
17
- # Identify potential double top pattern
18
- signals['double_top'] = ta.patterns.double_top(data['High'], data['Low'], data['Close'], lookback=lookback, threshold=0.02)
19
-
20
- # Generate trading orders
21
- signals['positions'] = signals['double_top'].diff()
22
-
23
- return signals
24
-
25
- # Streamlit App
26
- def main():
27
- st.title("Hourly Stock Price Analysis and Double Top Pattern Detection")
28
-
29
- # Load stock data using Streamlit sidebar
30
- symbol = st.sidebar.text_input("Enter Stock Symbol", value='AAPL')
31
- start_date = st.sidebar.text_input("Enter Start Date", value='2022-01-01')
32
- end_date = st.sidebar.text_input("Enter End Date", value='2022-02-01')
33
- stock_prices = get_stock_data(symbol, start_date, end_date, interval='1h')
34
-
35
- # Detect Double Top Pattern
36
- signals = detect_double_top(stock_prices)
37
-
38
- # Plotting Historical Stock Prices with Double Top Pattern Signals
39
- st.subheader("Hourly Historical Stock Prices with Double Top Pattern Signals")
40
- fig = go.Figure()
41
-
42
- fig.add_trace(go.Candlestick(x=stock_prices.index,
43
- open=stock_prices['Open'],
44
- high=stock_prices['High'],
45
- low=stock_prices['Low'],
46
- close=stock_prices['Close'],
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}")