Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
196 lines (167 loc) ยท 7.12 KB
/
Copy pathapp.py
File metadata and controls
196 lines (167 loc) ยท 7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import streamlit as st
import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
# ----- Page Config -----
st.set_page_config(page_title="TradeSight", page_icon="๐", layout="wide")
# ----- Sidebar Inputs -----
st.sidebar.title("๐ Strategy Parameters")
st.sidebar.markdown("---")
# Preset stocks grouped by market
PRESET_STOCKS = {
"๐ฎ๐ณ NSE โ Large Cap": {
"Reliance Industries": "RELIANCE.NS",
"TCS": "TCS.NS",
"Infosys": "INFY.NS",
"HDFC Bank": "HDFCBANK.NS",
"ICICI Bank": "ICICIBANK.NS",
"Wipro": "WIPRO.NS",
"Tata Motors": "TATAMOTORS.NS",
"Bajaj Finance": "BAJFINANCE.NS",
"Maruti Suzuki": "MARUTI.NS",
"Sun Pharma": "SUNPHARMA.NS",
},
"๐ฎ๐ณ NSE โ Mid Cap": {
"Tata Power": "TATAPOWER.NS",
"Zomato": "ZOMATO.NS",
"Paytm": "PAYTM.NS",
"Nykaa": "NYKAA.NS",
"Delhivery": "DELHIVERY.NS",
},
"๐บ๐ธ US โ Tech": {
"Apple": "AAPL",
"Microsoft": "MSFT",
"NVIDIA": "NVDA",
"Alphabet (Google)": "GOOGL",
"Meta": "META",
"Amazon": "AMZN",
"Tesla": "TSLA",
},
"๐บ๐ธ US โ Others": {
"Berkshire Hathaway": "BRK-B",
"JPMorgan Chase": "JPM",
"Coca-Cola": "KO",
},
"โ๏ธ Custom Symbol": {},
}
selected_group = st.sidebar.selectbox("Select Market / Group", list(PRESET_STOCKS.keys()))
if selected_group == "โ๏ธ Custom Symbol":
symbol = st.sidebar.text_input(
"Enter Stock Symbol",
placeholder="e.g. INFY.NS or AAPL",
help="NSE India: add .NS suffix ยท BSE India: add .BO ยท US: plain ticker"
)
if not symbol:
st.sidebar.info("Enter a ticker symbol above to get started.")
else:
stock_options = PRESET_STOCKS[selected_group]
selected_name = st.sidebar.selectbox("Select Stock", list(stock_options.keys()))
symbol = stock_options[selected_name]
st.sidebar.code(symbol, language=None) # show the ticker being used
st.sidebar.markdown("---")
start_date = st.sidebar.date_input("Start Date", datetime(2020, 1, 1))
end_date = st.sidebar.date_input("End Date", datetime(2024, 1, 1))
sma_short = st.sidebar.slider("Short-term SMA (days)", 3, 20, 5)
sma_long = st.sidebar.slider("Long-term SMA (days)", 10, 50, 15)
# ----- Title -----
st.title("๐ TradeSight โ SMA Crossover Backtest Dashboard")
st.caption("Backtest a Simple Moving Average (SMA) crossover strategy on any stock.")
# ----- Data Download -----
@st.cache_data(show_spinner=False)
def load_data(symbol, start, end):
try:
ticker = yf.Ticker(symbol)
df = ticker.history(start=start, end=end, auto_adjust=True)
if df.empty:
return None, f"No data returned for **{symbol}**. The symbol may be delisted, incorrect, or unavailable for the selected date range."
# Flatten MultiIndex columns if present
if isinstance(df.columns, pd.MultiIndex):
df.columns = df.columns.get_level_values(0)
if 'Close' not in df.columns:
return None, f"Could not find 'Close' price data for **{symbol}**."
return df[['Close']], None
except Exception as e:
return None, f"Failed to download data for **{symbol}**: {str(e)}"
# ----- Load Data -----
if not symbol or not symbol.strip():
st.info("๐ Select a stock from the sidebar to get started.")
st.stop()
with st.spinner(f"Fetching data for {symbol}..."):
df, error = load_data(symbol, start_date, end_date)
if error:
st.error(f"โ ๏ธ {error}")
st.info(
"**Troubleshooting tips:**\n"
"- For **NSE (India)** stocks: use `SYMBOL.NS` format (e.g. `RELIANCE.NS`, `TCS.NS`, `HDFCBANK.NS`)\n"
"- For **BSE (India)** stocks: use `SYMBOL.BO` format (e.g. `RELIANCE.BO`)\n"
"- For **US** stocks: use plain ticker (e.g. `AAPL`, `TSLA`)\n"
"- Try widening the date range โ some symbols have limited historical data\n"
"- Check [Yahoo Finance](https://finance.yahoo.com) to verify the symbol exists"
)
st.stop()
# ----- SMA Strategy -----
df['SMA_Short'] = df['Close'].rolling(window=sma_short).mean()
df['SMA_Long'] = df['Close'].rolling(window=sma_long).mean()
df['Signal'] = np.where(df['SMA_Short'] > df['SMA_Long'], 1, 0)
df['Position'] = df['Signal'].diff()
# Drop NaNs introduced by rolling windows
df.dropna(inplace=True)
df.reset_index(inplace=True)
# Rename index column to 'Date' if it came through as 'index' or 'Datetime'
if 'Date' not in df.columns:
for col in ['Datetime', 'index']:
if col in df.columns:
df.rename(columns={col: 'Date'}, inplace=True)
break
df['Date'] = pd.to_datetime(df['Date']).dt.tz_localize(None)
if df.empty:
st.error("Not enough data after applying SMA windows. Try a wider date range or smaller SMA values.")
st.stop()
# ----- Metrics -----
buy_signals = df[df['Position'] == 1]
sell_signals = df[df['Position'] == -1]
returns = df['Close'].pct_change().fillna(0)
strategy_returns = df['Signal'].shift(1).fillna(0) * returns
cumulative_return = (strategy_returns + 1).cumprod().iloc[-1] - 1
buy_hold_return = (df['Close'].iloc[-1] / df['Close'].iloc[0]) - 1
st.markdown("---")
col1, col2, col3, col4 = st.columns(4)
col1.metric("๐ Buy Signals", f"{len(buy_signals)}")
col2.metric("๐ Sell Signals", f"{len(sell_signals)}")
col3.metric("๐ฐ Strategy Return", f"{cumulative_return * 100:.2f}%")
col4.metric("๐ Buy & Hold Return", f"{buy_hold_return * 100:.2f}%")
st.markdown("---")
# ----- Plot -----
fig, ax = plt.subplots(figsize=(16, 7))
ax.plot(df['Date'], df['Close'], label='Close Price', alpha=0.6, linewidth=1.5)
ax.plot(df['Date'], df['SMA_Short'], label=f'{sma_short}-day SMA (Short)', linestyle='--', linewidth=1.2)
ax.plot(df['Date'], df['SMA_Long'], label=f'{sma_long}-day SMA (Long)', linestyle='--', linewidth=1.2)
# Buy/Sell Markers
if not buy_signals.empty:
ax.scatter(buy_signals['Date'], buy_signals['Close'], label='Buy Signal', marker='^', color='green', s=120, zorder=5)
if not sell_signals.empty:
ax.scatter(sell_signals['Date'], sell_signals['Close'], label='Sell Signal', marker='v', color='red', s=120, zorder=5)
ax.set_title(f"SMA Crossover Strategy โ {symbol}", fontsize=16, fontweight='bold')
ax.set_xlabel("Date", fontsize=12)
ax.set_ylabel("Price", fontsize=12)
ax.legend(fontsize=10)
ax.grid(True, alpha=0.3)
fig.tight_layout()
st.pyplot(fig)
# ----- Trade Log -----
st.subheader("๐ Trade Log")
if buy_signals.empty and sell_signals.empty:
st.info("No trades were generated. Try adjusting the SMA windows or selecting a more volatile period.")
else:
buy_df = buy_signals[['Date', 'Close']].copy()
buy_df['Action'] = 'Buy'
sell_df = sell_signals[['Date', 'Close']].copy()
sell_df['Action'] = 'Sell'
trade_log = pd.concat([buy_df, sell_df]).sort_values('Date').reset_index(drop=True)
trade_log['Close'] = trade_log['Close'].round(2)
st.dataframe(trade_log, use_container_width=True)
# ----- Raw Data -----
with st.expander("๐ View Raw Data"):
st.dataframe(df[['Date', 'Close', 'SMA_Short', 'SMA_Long', 'Signal']].round(2), use_container_width=True)