netflypsb commited on
Commit
6d97ada
·
verified ·
1 Parent(s): 86ad805

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import yfinance as yf
2
+ import pandas as pd
3
+ import plotly.graph_objects as go
4
+ import streamlit as st
5
+ import numpy as np
6
+
7
+ # Place input fields in the sidebar
8
+ sidebar = st.sidebar
9
+ symbol = sidebar.text_input("Enter stock symbol:", "AAPL")
10
+ period = sidebar.selectbox("Select period:", ["1d", "1wk", "1mo", "3mo", "6mo", "1y", "2y", "5y", "10y", "ytd", "max"])
11
+
12
+ # Download stock data
13
+ data = yf.download(symbol, period=period, interval='1d' if period in ['1d', '1wk'] else '1mo')
14
+
15
+ # Calculate Moving Averages if there are enough data points
16
+ if len(data) > 50:
17
+ data['MA50'] = data['Close'].rolling(window=50).mean()
18
+ if len(data) > 200:
19
+ data['MA200'] = data['Close'].rolling(window=200).mean()
20
+ if len(data) > 20:
21
+ data['MA20'] = data['Close'].rolling(window=20).mean()
22
+
23
+ # Finding highest and lowest price for the Fibonacci Retracement Levels
24
+ high_price = data['High'].max()
25
+ low_price = data['Low'].min()
26
+
27
+ # Calculate Fibonacci Levels
28
+ fib_levels = [0, 0.236, 0.382, 0.5, 0.618, 0.786, 1]
29
+ price_diff = high_price - low_price
30
+ data['Fib_Level_0'] = high_price
31
+ data['Fib_Level_1'] = high_price - price_diff * fib_levels[1]
32
+ data['Fib_Level_2'] = high_price - price_diff * fib_levels[2]
33
+ data['Fib_Level_3'] = high_price - price_diff * fib_levels[3]
34
+ data['Fib_Level_4'] = high_price - price_diff * fib_levels[4]
35
+ data['Fib_Level_5'] = high_price - price_diff * fib_levels[5]
36
+ data['Fib_Level_6'] = low_price
37
+
38
+ # Plotting
39
+ fig = go.Figure()
40
+
41
+ # Add trace for Close price
42
+ fig.add_trace(go.Scatter(x=data.index, y=data['Close'], name='Close Price', line=dict(color='black')))
43
+
44
+ # Add traces for Moving Averages if they have been calculated
45
+ if 'MA50' in data:
46
+ fig.add_trace(go.Scatter(x=data.index, y=data['MA50'], name='50-Period MA', line=dict(color='blue')))
47
+ if 'MA200' in data:
48
+ fig.add_trace(go.Scatter(x=data.index, y=data['MA200'], name='200-Period MA', line=dict(color='red')))
49
+ if 'MA20' in data:
50
+ fig.add_trace(go.Scatter(x=data.index, y=data['MA20'], name='20-Period MA', line=dict(color='green')))
51
+
52
+ # Add traces for Fibonacci Levels
53
+ for i in range(7):
54
+ fig.add_trace(go.Scatter(x=data.index, y=[data[f'Fib_Level_{i}'][0]]*len(data), name=f'Fib Level {fib_levels[i]*100}%', line=dict(dash='dot')))
55
+
56
+ # Display the chart
57
+ st.plotly_chart(fig)
58
+
59
+