Spaces:
Runtime error
Runtime error
import streamlit as st | |
import yfinance as yf | |
import pandas as pd | |
import plotly.graph_objects as go | |
def fetch_data(ticker, start_date, end_date): | |
data = yf.download(ticker, start=start_date, end=end_date) | |
return data | |
def calculate_indicators(data, window_short, window_long): | |
data['High Short'] = data['High'].rolling(window=window_short).max() | |
data['Low Short'] = data['Low'].rolling(window=window_short).min() | |
return data | |
def identify_signals(data): | |
data['Buy Signal'] = (data['Close'] > data['High Short'].shift(1)) | |
data['Sell Signal'] = (data['Close'] < data['Low Short'].shift(1)) | |
return data | |
def collect_signals(data): | |
signals = pd.DataFrame() | |
signals['Date'] = data.index | |
signals['Price'] = data['Close'] | |
signals['Signal'] = None # Initialize all to None | |
buy_indices = data[data['Buy Signal']].index | |
sell_indices = data[data['Sell Signal']].index | |
signals.loc[signals['Date'].isin(buy_indices), 'Signal'] = 'Buy' | |
signals.loc[signals['Date'].isin(sell_indices), 'Signal'] = 'Sell' | |
signals = signals.dropna(subset=['Signal']) | |
return signals | |
def plot_data(data): | |
fig = go.Figure() | |
fig.add_trace(go.Scatter(x=data.index, y=data['Close'], name='Close Price', line=dict(color='blue'))) | |
buys = data[data['Buy Signal']] | |
sells = data[data['Sell Signal']] | |
fig.add_trace(go.Scatter(x=buys.index, y=buys['Close'], mode='markers', name='Buy Signal', marker_symbol='triangle-up', marker_color='green', marker_size=10)) | |
fig.add_trace(go.Scatter(x=sells.index, y=sells['Close'], mode='markers', name='Sell Signal', marker_symbol='triangle-down', marker_color='red', marker_size=10)) | |
fig.update_layout(title='Stock Price and Trading Signals', xaxis_title='Date', yaxis_title='Price', template='plotly_dark') | |
return fig | |
def main(): | |
st.title("Turtle Trading Strategy Visualization") | |
st.markdown(""" | |
## Description | |
This app visualizes the Turtle Trading Strategy, a well-known systematic trading method that follows trends to decide on buying and selling positions based on historical price breakouts. Users can test different short and long window sizes to see how the strategy performs with different settings. | |
## How to Use | |
1. **Enter the Ticker Symbol:** Input the stock symbol you want to analyze (e.g., 'AAPL', 'GOOGL'). | |
2. **Select Date Range:** Choose the start and end dates for the data you wish to analyze. | |
3. **Set Window Sizes:** Adjust the window sizes for the short and long term indicators. | |
4. **Analyze:** Press the analyze button to see the trading signals and performance charts. | |
5. **Review the Outputs:** The chart and the signals table provide visual and data-driven insights respectively. | |
""") | |
# Sidebar for user inputs | |
with st.sidebar: | |
ticker = st.text_input("Enter the ticker symbol, e.g., 'AAPL'") | |
start_date = st.date_input("Select the start date") | |
end_date = st.date_input("Select the end date") | |
window_short = st.number_input("Short term window", min_value=5, max_value=60, value=20) | |
window_long = st.number_input("Long term window", min_value=5, max_value=120, value=55) | |
if st.button("Analyze"): | |
data = fetch_data(ticker, start_date, end_date) | |
if not data.empty: | |
data = calculate_indicators(data, window_short, window_long) | |
data = identify_signals(data) | |
signals = collect_signals(data) | |
fig = plot_data(data) | |
st.plotly_chart(fig, use_container_width=True) | |
st.write("Trading Signals:") | |
st.dataframe(signals) | |
else: | |
st.error("No data found for the selected ticker and date range.") | |
if __name__ == "__main__": | |
main() | |