netflypsb commited on
Commit
7215fad
·
verified ·
1 Parent(s): 8cec41e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import yfinance as yf
3
+ import pandas as pd
4
+ import matplotlib.pyplot as plt
5
+
6
+ def fetch_data(ticker, start_date, end_date):
7
+ data = yf.download(ticker, start=start_date, end=end_date)
8
+ return data
9
+
10
+ def calculate_indicators(data):
11
+ # High and low for the breakout signals
12
+ data['20 Day High'] = data['High'].rolling(window=20).max()
13
+ data['20 Day Low'] = data['Low'].rolling(window=20).min()
14
+ data['55 Day High'] = data['High'].rolling(window=55).max()
15
+ data['55 Day Low'] = data['Low'].rolling(window=55).min()
16
+
17
+ return data
18
+
19
+ def identify_signals(data):
20
+ # Buy signals are generated when the price exceeds the 20-day high
21
+ data['Buy Signal'] = (data['Close'] > data['20 Day High'].shift(1))
22
+ # Sell signals are generated when the price drops below the 20-day low
23
+ data['Sell Signal'] = (data['Close'] < data['20 Day Low'].shift(1))
24
+
25
+ signals = []
26
+ for index, row in data.iterrows():
27
+ if row['Buy Signal']:
28
+ signals.append({'Date': index, 'Signal Type': 'Buy', 'Price': row['Close']})
29
+ if row['Sell Signal']:
30
+ signals.append({'Date': index, 'Signal Type': 'Sell', 'Price': row['Close']})
31
+
32
+ return data, pd.DataFrame(signals)
33
+
34
+ def plot_data(data):
35
+ plt.figure(figsize=(12, 6))
36
+ plt.plot(data['Close'], label='Close Price')
37
+
38
+ buy_signals = data[data['Buy Signal']]
39
+ sell_signals = data[data['Sell Signal']]
40
+ plt.scatter(buy_signals.index, buy_signals['Close'], marker='^', color='green', s=100, label='Buy Signal')
41
+ plt.scatter(sell_signals.index, sell_signals['Close'], marker='v', color='red', s=100, label='Sell Signal')
42
+
43
+ plt.title('Stock Price and Turtle Trading Signals')
44
+ plt.xlabel('Date')
45
+ plt.ylabel('Price')
46
+ plt.legend()
47
+ plt.grid(True)
48
+ plt.show()
49
+
50
+ def main():
51
+ st.title("Turtle Trading Strategy Visualization")
52
+ ticker = st.text_input("Enter the ticker symbol, e.g., 'AAPL'")
53
+ start_date = st.date_input("Select the start date")
54
+ end_date = st.date_input("Select the end date")
55
+
56
+ if st.button("Analyze"):
57
+ data = fetch_data(ticker, start_date, end_date)
58
+ data = calculate_indicators(data)
59
+ data, signals = identify_signals(data)
60
+ plot_data(data)
61
+ st.pyplot(plt)
62
+ if not signals.empty:
63
+ st.write("Trading Signals:")
64
+ st.dataframe(signals)
65
+ else:
66
+ st.write("No trading signals found for the selected period.")
67
+
68
+ if __name__ == "__main__":
69
+ main()