netflypsb commited on
Commit
4414ced
·
verified ·
1 Parent(s): 7e41dcf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import yfinance as yf
2
+ 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)