Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import yfinance as yf
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
|
7 |
+
# Function to fetch data
|
8 |
+
def fetch_data(ticker, start_date, end_date):
|
9 |
+
data = yf.download(ticker, start=start_date, end=end_date)
|
10 |
+
return data
|
11 |
+
|
12 |
+
# Function to calculate support and resistance
|
13 |
+
def calculate_support_resistance(data):
|
14 |
+
data['Support'] = data['Low'].rolling(window=20, min_periods=1).min()
|
15 |
+
data['Resistance'] = data['High'].rolling(window=20, min_periods=1).max()
|
16 |
+
return data
|
17 |
+
|
18 |
+
# Function to plot data
|
19 |
+
def plot_data(data):
|
20 |
+
plt.figure(figsize=(10, 5))
|
21 |
+
plt.plot(data['Close'], label='Close Price', color='blue')
|
22 |
+
plt.plot(data['Support'], label='Support', linestyle='--', color='green')
|
23 |
+
plt.plot(data['Resistance'], label='Resistance', linestyle='--', color='red')
|
24 |
+
plt.title('Range Trading Strategy Visualization')
|
25 |
+
plt.xlabel('Date')
|
26 |
+
plt.ylabel('Price')
|
27 |
+
plt.legend()
|
28 |
+
plt.grid(True)
|
29 |
+
st.pyplot(plt)
|
30 |
+
|
31 |
+
# Sidebar
|
32 |
+
st.sidebar.header('User Input Parameters')
|
33 |
+
ticker = st.sidebar.text_input('Ticker Symbol', value='AAPL')
|
34 |
+
start_date = st.sidebar.date_input('Start Date', pd.to_datetime('2020-01-01'))
|
35 |
+
end_date = st.sidebar.date_input('End Date', pd.to_datetime('today'))
|
36 |
+
|
37 |
+
if st.sidebar.button('Analyze'):
|
38 |
+
data = fetch_data(ticker, start_date, end_date)
|
39 |
+
if not data.empty:
|
40 |
+
data = calculate_support_resistance(data)
|
41 |
+
plot_data(data)
|
42 |
+
else:
|
43 |
+
st.error("No data found for the given ticker and date range. Please adjust your inputs and try again.")
|
44 |
+
|
45 |
+
# Main screen
|
46 |
+
st.title('Range Trading Strategy Visualizer')
|
47 |
+
st.markdown("""
|
48 |
+
This app retrieves stock data from Yahoo Finance and visualizes the range trading strategy by identifying and plotting support and resistance levels.
|
49 |
+
To use this app:
|
50 |
+
- Enter the stock ticker symbol in the sidebar.
|
51 |
+
- Choose the start and end date for your analysis.
|
52 |
+
- Click on the 'Analyze' button to display the stock chart with marked support and resistance levels.
|
53 |
+
""")
|