Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import yfinance as yf
|
| 6 |
+
|
| 7 |
+
# Streamlit App Title
|
| 8 |
+
st.title("Enhanced Turtle Strategy with Streamlit")
|
| 9 |
+
|
| 10 |
+
# File Uploader for CSV
|
| 11 |
+
uploaded_file = st.file_uploader("Upload a CSV file", type=["csv"])
|
| 12 |
+
|
| 13 |
+
if uploaded_file:
|
| 14 |
+
# Read the uploaded CSV file
|
| 15 |
+
data = pd.read_csv(uploaded_file)
|
| 16 |
+
|
| 17 |
+
# Display the raw data for reference
|
| 18 |
+
st.write("### Raw Data Preview:")
|
| 19 |
+
st.dataframe(data.head(10))
|
| 20 |
+
|
| 21 |
+
# Convert Date column to datetime format for analysis
|
| 22 |
+
data['Date'] = pd.to_datetime(data['Date'])
|
| 23 |
+
|
| 24 |
+
# Turtle Strategy Parameters
|
| 25 |
+
entry_days = st.sidebar.slider("Entry Lookback Period (days)", min_value=10, max_value=60, value=20, step=1)
|
| 26 |
+
exit_days = st.sidebar.slider("Exit Lookback Period (days)", min_value=5, max_value=30, value=10, step=1)
|
| 27 |
+
|
| 28 |
+
# Calculate the 20-day high and 10-day low based on user inputs
|
| 29 |
+
data['20D_High'] = data['High'].rolling(window=entry_days).max()
|
| 30 |
+
data['10D_Low'] = data['Low'].rolling(window=exit_days).min()
|
| 31 |
+
|
| 32 |
+
# Entry Signal: When Close price exceeds the 20-day high
|
| 33 |
+
data['Long'] = np.where(data['Close'] > data['20D_High'].shift(1), 1, 0)
|
| 34 |
+
|
| 35 |
+
# Exit Signal: When Close price goes below the 10-day low
|
| 36 |
+
data['Exit'] = np.where(data['Close'] < data['10D_Low'].shift(1), -1, 0)
|
| 37 |
+
|
| 38 |
+
# Position Management
|
| 39 |
+
data['Position'] = 0
|
| 40 |
+
data.loc[data['Long'] == 1, 'Position'] = 1
|
| 41 |
+
data.loc[data['Exit'] == -1, 'Position'] = 0
|
| 42 |
+
|
| 43 |
+
# Correct the Position shift and forward fill
|
| 44 |
+
data['Position'] = data['Position'].ffill().shift(1).fillna(0)
|
| 45 |
+
|
| 46 |
+
# Strategy Returns Calculation
|
| 47 |
+
data['Strategy Returns'] = data['Position'] * data['Close'].pct_change()
|
| 48 |
+
data['Cumulative Returns'] = (1 + data['Strategy Returns']).cumprod()
|
| 49 |
+
|
| 50 |
+
# Buy and Hold Strategy Returns
|
| 51 |
+
data['Buy and Hold Returns'] = (1 + data['Close'].pct_change()).cumprod()
|
| 52 |
+
|
| 53 |
+
# Show calculated signals and strategy
|
| 54 |
+
st.write("### Strategy Calculation Preview:")
|
| 55 |
+
st.dataframe(data[['Date', 'Close', '20D_High', '10D_Low', 'Long', 'Exit', 'Position', 'Strategy Returns', 'Cumulative Returns']].tail(20))
|
| 56 |
+
|
| 57 |
+
# Plotting the Turtle Strategy vs Buy and Hold
|
| 58 |
+
st.subheader("Turtle Strategy vs Buy and Hold Returns")
|
| 59 |
+
plt.figure(figsize=(14, 7))
|
| 60 |
+
plt.plot(data['Date'], data['Cumulative Returns'], label='Turtle Strategy Returns')
|
| 61 |
+
plt.plot(data['Date'], data['Buy and Hold Returns'], label='Buy and Hold Returns')
|
| 62 |
+
plt.title('Turtle Strategy Performance')
|
| 63 |
+
plt.xlabel('Date')
|
| 64 |
+
plt.ylabel('Cumulative Returns')
|
| 65 |
+
plt.legend()
|
| 66 |
+
st.pyplot(plt)
|
| 67 |
+
|
| 68 |
+
# Show the Final Cumulative Returns
|
| 69 |
+
st.write(f"### Final Turtle Strategy Cumulative Returns: {data['Cumulative Returns'].iloc[-1]:.2f}")
|
| 70 |
+
st.write(f"### Final Buy and Hold Cumulative Returns: {data['Buy and Hold Returns'].iloc[-1]:.2f}")
|
| 71 |
+
|
| 72 |
+
else:
|
| 73 |
+
st.write("Please upload a CSV file to analyze the Turtle Strategy.")
|