netflypsb commited on
Commit
649b4ce
·
verified ·
1 Parent(s): 4414ced

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -35
app.py CHANGED
@@ -3,43 +3,53 @@ import pandas as pd
3
  import plotly.graph_objects as go
4
  import streamlit as st
5
 
6
- # Sidebar inputs for stock symbol and period
7
  sidebar = st.sidebar
8
  symbol = sidebar.text_input("Enter stock symbol:", "AAPL")
9
  data = yf.download(symbol, start="2020-01-01", end="2021-01-01")
10
 
11
- # New sidebar inputs for defining the Fibonacci period
12
- fib_start_date = sidebar.date_input("Select Fibonacci start date", value=pd.to_datetime("2020-01-01"), min_value=pd.to_datetime(data.index.min()), max_value=pd.to_datetime(data.index.max()))
13
- fib_end_date = sidebar.date_input("Select Fibonacci end date", value=pd.to_datetime("2021-01-01"), min_value=pd.to_datetime(data.index.min()), max_value=pd.to_datetime(data.index.max()))
14
-
15
- # Ensure start date is before end date
16
- if fib_start_date >= fib_end_date:
17
- st.error('Error: End date must be after start date.')
18
  else:
19
- # Filter data for the selected Fibonacci period
20
- fib_data = data.loc[fib_start_date:fib_end_date]
21
-
22
- # Find high and low prices within the Fibonacci period
23
- high_price = fib_data['High'].max()
24
- low_price = fib_data['Low'].min()
25
-
26
- # Calculate Fibonacci Levels
27
- price_diff = high_price - low_price
28
- fib_levels = [0, 0.236, 0.382, 0.5, 0.618, 0.786, 1]
29
- fib_values = [high_price - price_diff * level for level in fib_levels]
30
-
31
- # Plotting
32
- fig = go.Figure()
33
- fig.add_trace(go.Scatter(x=data.index, y=data['Close'], name='Close Price', line=dict(color='black')))
34
-
35
- # Plot Fibonacci levels
36
- for i, val in enumerate(fib_values):
37
- fig.add_hline(y=val, line_dash="dot", annotation_text=f'Fib {fib_levels[i]*100}%', annotation_position="right")
38
-
39
- # Calculate and plot moving averages
40
- for ma in [20, 50, 200]:
41
- data[f'MA{ma}'] = data['Close'].rolling(window=ma).mean()
42
- fig.add_trace(go.Scatter(x=data.index, y=data[f'MA{ma}'], name=f'{ma}-Period MA'))
43
-
44
- # Display the chart
45
- st.plotly_chart(fig)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import plotly.graph_objects as go
4
  import streamlit as st
5
 
6
+ # Sidebar inputs for stock symbol
7
  sidebar = st.sidebar
8
  symbol = sidebar.text_input("Enter stock symbol:", "AAPL")
9
  data = yf.download(symbol, start="2020-01-01", end="2021-01-01")
10
 
11
+ # Check if data is empty
12
+ if data.empty:
13
+ st.error("No data available for the selected symbol. Please try another symbol.")
 
 
 
 
14
  else:
15
+ # Determine the valid date range for Fibonacci analysis
16
+ min_date, max_date = data.index.min().date(), data.index.max().date()
17
+
18
+ # Dynamic default dates based on data range
19
+ default_start, default_end = min_date, max_date
20
+
21
+ # Sidebar inputs for Fibonacci time frame
22
+ fib_start_date = sidebar.date_input("Fibonacci Start Date", value=default_start, min_value=min_date, max_value=max_date)
23
+ fib_end_date = sidebar.date_input("Fibonacci End Date", value=default_end, min_value=min_date, max_value=max_date)
24
+
25
+ if fib_start_date >= fib_end_date:
26
+ st.error("Error: The start date must be before the end date.")
27
+ else:
28
+ # Filter data for Fibonacci analysis
29
+ fib_data = data.loc[fib_start_date:fib_end_date]
30
+
31
+ # Calculate high and low prices for Fibonacci levels
32
+ high_price = fib_data['High'].max()
33
+ low_price = fib_data['Low'].min()
34
+
35
+ # Fibonacci retracement levels
36
+ fib_levels = [0, 0.236, 0.382, 0.5, 0.618, 0.786, 1]
37
+ price_diff = high_price - low_price
38
+
39
+ # Initialize figure for plotting
40
+ fig = go.Figure()
41
+
42
+ # Plot close prices and moving averages
43
+ fig.add_trace(go.Scatter(x=data.index, y=data['Close'], name='Close Price', line=dict(color='black')))
44
+ for ma in [20, 50, 200]:
45
+ ma_label = f"MA{ma}"
46
+ data[ma_label] = data['Close'].rolling(window=ma).mean()
47
+ fig.add_trace(go.Scatter(x=data.index, y=data[ma_label], name=ma_label, line=dict(width=2)))
48
+
49
+ # Plot Fibonacci levels
50
+ for level in fib_levels:
51
+ value = high_price - (price_diff * level)
52
+ fig.add_hline(y=value, line_dash="dot", annotation_text=f"{level*100}%", annotation_position="right")
53
+
54
+ # Display chart
55
+ st.plotly_chart(fig)