Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -22,27 +22,37 @@ def identify_signals(data):
|
|
22 |
data['Sell Signal'] = (data['Close'] < data['Low Short'].shift(1))
|
23 |
return data
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
# Plotting function using Plotly for interactive charts
|
26 |
def plot_data(data):
|
27 |
fig = go.Figure()
|
28 |
-
# Add traces for Close price, High, and Low bands
|
29 |
fig.add_trace(go.Scatter(x=data.index, y=data['Close'], name='Close Price', line=dict(color='blue')))
|
30 |
fig.add_trace(go.Scatter(x=data.index, y=data['High Short'], name='High Short', line=dict(dash='dot')))
|
31 |
fig.add_trace(go.Scatter(x=data.index, y=data['Low Short'], name='Low Short', line=dict(dash='dot')))
|
32 |
-
|
33 |
-
# Add buy and sell signal markers
|
34 |
buys = data[data['Buy Signal']]
|
35 |
sells = data[data['Sell Signal']]
|
36 |
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))
|
37 |
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))
|
38 |
-
|
39 |
-
# Update layout for better visualization
|
40 |
fig.update_layout(title='Stock Price and Trading Signals', xaxis_title='Date', yaxis_title='Price', template='plotly_dark')
|
41 |
return fig
|
42 |
|
43 |
# Main application function
|
44 |
def main():
|
45 |
-
st.title("Enhanced Turtle Trading Strategy
|
46 |
|
47 |
# Sidebar for user inputs
|
48 |
with st.sidebar:
|
@@ -57,8 +67,12 @@ def main():
|
|
57 |
if not data.empty:
|
58 |
data = calculate_indicators(data, window_short, window_long)
|
59 |
data = identify_signals(data)
|
|
|
60 |
fig = plot_data(data)
|
61 |
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
|
|
|
62 |
else:
|
63 |
st.error("No data found for the selected ticker and date range.")
|
64 |
|
|
|
22 |
data['Sell Signal'] = (data['Close'] < data['Low Short'].shift(1))
|
23 |
return data
|
24 |
|
25 |
+
# Calculate returns and metrics for backtesting
|
26 |
+
def backtest_signals(data):
|
27 |
+
data['Position'] = 0
|
28 |
+
data['Position'] = data['Buy Signal'].replace(True, 1).cumsum()
|
29 |
+
data['Position'] = data['Position'] - data['Sell Signal'].replace(True, 1).cumsum()
|
30 |
+
data['Position'] = data['Position'].clip(lower=0, upper=1)
|
31 |
+
|
32 |
+
data['Market Returns'] = data['Close'].pct_change()
|
33 |
+
data['Strategy Returns'] = data['Market Returns'] * data['Position'].shift(1)
|
34 |
+
|
35 |
+
data['Cumulative Market Returns'] = (1 + data['Market Returns']).cumprod() - 1
|
36 |
+
data['Cumulative Strategy Returns'] = (1 + data['Strategy Returns']).cumprod() - 1
|
37 |
+
|
38 |
+
return data, data['Cumulative Market Returns'].iloc[-1], data['Cumulative Strategy Returns'].iloc[-1]
|
39 |
+
|
40 |
# Plotting function using Plotly for interactive charts
|
41 |
def plot_data(data):
|
42 |
fig = go.Figure()
|
|
|
43 |
fig.add_trace(go.Scatter(x=data.index, y=data['Close'], name='Close Price', line=dict(color='blue')))
|
44 |
fig.add_trace(go.Scatter(x=data.index, y=data['High Short'], name='High Short', line=dict(dash='dot')))
|
45 |
fig.add_trace(go.Scatter(x=data.index, y=data['Low Short'], name='Low Short', line=dict(dash='dot')))
|
|
|
|
|
46 |
buys = data[data['Buy Signal']]
|
47 |
sells = data[data['Sell Signal']]
|
48 |
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))
|
49 |
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))
|
|
|
|
|
50 |
fig.update_layout(title='Stock Price and Trading Signals', xaxis_title='Date', yaxis_title='Price', template='plotly_dark')
|
51 |
return fig
|
52 |
|
53 |
# Main application function
|
54 |
def main():
|
55 |
+
st.title("Enhanced Turtle Trading Strategy with Backtesting")
|
56 |
|
57 |
# Sidebar for user inputs
|
58 |
with st.sidebar:
|
|
|
67 |
if not data.empty:
|
68 |
data = calculate_indicators(data, window_short, window_long)
|
69 |
data = identify_signals(data)
|
70 |
+
data, market_return, strategy_return = backtest_signals(data)
|
71 |
fig = plot_data(data)
|
72 |
st.plotly_chart(fig, use_container_width=True)
|
73 |
+
st.subheader("Backtesting Results")
|
74 |
+
st.write(f"Market Return: {market_return * 100:.2f}%")
|
75 |
+
st.write(f"Strategy Return: {strategy_return * 100:.2f}%")
|
76 |
else:
|
77 |
st.error("No data found for the selected ticker and date range.")
|
78 |
|