netflypsb commited on
Commit
6fd0501
·
verified ·
1 Parent(s): 090096f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -12
app.py CHANGED
@@ -21,10 +21,16 @@ def collect_signals(data):
21
  signals = pd.DataFrame()
22
  signals['Date'] = data.index
23
  signals['Price'] = data['Close']
24
- signals['Signal'] = None # Initialize the column with None
25
- signals.loc[data['Buy Signal'], 'Signal'] = 'Buy'
26
- signals.loc[data['Sell Signal'], 'Signal'] = 'Sell'
 
 
 
 
 
27
  signals = signals.dropna(subset=['Signal'])
 
28
  return signals
29
 
30
  def plot_data(data):
@@ -38,14 +44,29 @@ def plot_data(data):
38
  return fig
39
 
40
  def main():
41
- st.title("Enhanced Turtle Trading Strategy with Backtesting and Signal Table")
42
- ticker = st.sidebar.text_input("Enter the ticker symbol, e.g., 'AAPL'")
43
- start_date = st.sidebar.date_input("Select the start date")
44
- end_date = st.sidebar.date_input("Select the end date")
45
- window_short = st.sidebar.number_input("Short term window", min_value=5, max_value=60, value=20)
46
- window_long = st.sidebar.number_input("Long term window", min_value=5, max_value=120, value=55)
47
-
48
- if st.sidebar.button("Analyze"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  data = fetch_data(ticker, start_date, end_date)
50
  if not data.empty:
51
  data = calculate_indicators(data, window_short, window_long)
@@ -54,7 +75,7 @@ def main():
54
  fig = plot_data(data)
55
  st.plotly_chart(fig, use_container_width=True)
56
  st.write("Trading Signals:")
57
- st.dataframe(signals.style.hide_index())
58
  else:
59
  st.error("No data found for the selected ticker and date range.")
60
 
 
21
  signals = pd.DataFrame()
22
  signals['Date'] = data.index
23
  signals['Price'] = data['Close']
24
+ signals['Signal'] = None # Initialize all to None
25
+
26
+ buy_indices = data[data['Buy Signal']].index
27
+ sell_indices = data[data['Sell Signal']].index
28
+
29
+ signals.loc[signals['Date'].isin(buy_indices), 'Signal'] = 'Buy'
30
+ signals.loc[signals['Date'].isin(sell_indices), 'Signal'] = 'Sell'
31
+
32
  signals = signals.dropna(subset=['Signal'])
33
+
34
  return signals
35
 
36
  def plot_data(data):
 
44
  return fig
45
 
46
  def main():
47
+ st.title("Turtle Trading Strategy Visualization")
48
+
49
+ st.markdown("""
50
+ ## Description
51
+ 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.
52
+
53
+ ## How to Use
54
+ 1. **Enter the Ticker Symbol:** Input the stock symbol you want to analyze (e.g., 'AAPL', 'GOOGL').
55
+ 2. **Select Date Range:** Choose the start and end dates for the data you wish to analyze.
56
+ 3. **Set Window Sizes:** Adjust the window sizes for the short and long term indicators.
57
+ 4. **Analyze:** Press the analyze button to see the trading signals and performance charts.
58
+ 5. **Review the Outputs:** The chart and the signals table provide visual and data-driven insights respectively.
59
+ """)
60
+
61
+ # Sidebar for user inputs
62
+ with st.sidebar:
63
+ ticker = st.text_input("Enter the ticker symbol, e.g., 'AAPL'")
64
+ start_date = st.date_input("Select the start date")
65
+ end_date = st.date_input("Select the end date")
66
+ window_short = st.number_input("Short term window", min_value=5, max_value=60, value=20)
67
+ window_long = st.number_input("Long term window", min_value=5, max_value=120, value=55)
68
+
69
+ if st.button("Analyze"):
70
  data = fetch_data(ticker, start_date, end_date)
71
  if not data.empty:
72
  data = calculate_indicators(data, window_short, window_long)
 
75
  fig = plot_data(data)
76
  st.plotly_chart(fig, use_container_width=True)
77
  st.write("Trading Signals:")
78
+ st.dataframe(signals)
79
  else:
80
  st.error("No data found for the selected ticker and date range.")
81