-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
146 lines (118 loc) · 5.92 KB
/
app.py
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
import streamlit as st
import backtrader as bt
from datetime import datetime
from strategy import CustomStrategy
from data_fetch import fetch_data
from plot_utils import plot_data
# Streamlit App
st.set_page_config(page_title="Backtesting with Backtrader", layout="wide")
# Dictionary of stocks and indices
stocks_dict = {
"Reliance Industries": "RELIANCE.NS",
"Tata Consultancy Services": "TCS.NS",
"HDFC Bank": "HDFCBANK.NS",
"Infosys": "INFY.NS",
"Hindustan Unilever": "HINDUNILVR.NS",
"ICICI Bank": "ICICIBANK.NS",
"Bharti Airtel": "BHARTIARTL.NS",
"State Bank of India": "SBIN.NS",
"Kotak Mahindra Bank": "KOTAKBANK.NS",
"Larsen & Toubro": "LT.NS",
"Nifty 50": "^NSEI",
"Bank Nifty": "^NSEBANK",
"Sensex": "^BSESN"
}
st.title('📈 Backtesting with Backtrader')
# User selection
selected_company = st.sidebar.selectbox("Select a company or index:", list(stocks_dict.keys()))
ticker = stocks_dict[selected_company]
# Input for date range
start_date = st.sidebar.date_input("Start Date", value=datetime(2020, 1, 1))
end_date = st.sidebar.date_input("End Date", value=datetime.today())
if start_date > end_date:
st.error("Start date cannot be after end date.")
# Strategy Parameters
st.sidebar.header("Select Strategy Parameters")
short_window = st.sidebar.slider("Short Moving Average Window", 1, 50, 10)
long_window = st.sidebar.slider("Long Moving Average Window", 1, 50, 30)
rsi_period = st.sidebar.slider("RSI Period", 1, 50, 14)
rsi_overbought = st.sidebar.slider("RSI Overbought Level", 50, 100, 70)
rsi_oversold = st.sidebar.slider("RSI Oversold Level", 0, 50, 30)
stop_loss_percent = st.sidebar.slider("Stop-Loss Percentage", 0.0, 10.0, 1.0)
# MACD parameters
st.sidebar.header("MACD Parameters")
macd_short = st.sidebar.slider("MACD Short Period", 1, 50, 12)
macd_long = st.sidebar.slider("MACD Long Period", 1, 50, 26)
macd_signal = st.sidebar.slider("MACD Signal Period", 1, 50, 9)
# Indicator selection
st.sidebar.header("Select Indicators")
use_sma = st.sidebar.checkbox("Use Short Moving Average", key='use_sma', value=True)
use_lma = st.sidebar.checkbox("Use Long Moving Average", key='use_lma', value=True)
use_rsi = st.sidebar.checkbox("Use RSI", key='use_rsi', value=True)
use_ema = st.sidebar.checkbox("Use Exponential Moving Average", key='use_ema', value=True)
use_macd = st.sidebar.checkbox("Use MACD", key='use_macd', value=True)
use_stop_loss = st.sidebar.checkbox("Enable Stop-Loss", key='use_stop_loss', value=True)
# Check if at least one indicator is selected
if not (use_sma or use_lma or use_rsi or use_ema or use_macd):
st.sidebar.error("Please select at least one indicator to proceed.")
else:
if st.button("Run Backtest"):
# Download data using yfinance
data = fetch_data(ticker, start=start_date, end=end_date)
# Check if data was fetched successfully
if data.empty:
st.error("No data fetched. Please check the ticker and date range.")
else:
# Plot stock data
plot_data(data, selected_company, 'Close')
# Prepare data for Backtrader
data_bt = bt.feeds.PandasData(dataname=data)
# Initialize Cerebro
cerebro = bt.Cerebro()
cerebro.adddata(data_bt)
# Add strategy with user-defined parameters
cerebro.addstrategy(CustomStrategy,
short_window=short_window,
long_window=long_window,
rsi_period=rsi_period,
rsi_overbought=rsi_overbought,
rsi_oversold=rsi_oversold,
use_stop_loss=st.session_state['use_stop_loss'],
stop_loss_percent=stop_loss_percent,
macd_short=macd_short,
macd_long=macd_long,
macd_signal=macd_signal)
initial_value = 1000000.0 # Initial balance
cerebro.broker.setcash(initial_value)
# Run the backtest
results = cerebro.run()
# Access the logs from the last strategy instance
strategy_instance = results[0]
log_messages = strategy_instance.log_messages
# Get final balance and calculate net profit/loss
final_value = cerebro.broker.getvalue()
net_profit_loss = final_value - initial_value
# Display results in a more structured way
st.markdown(f"### Initial Balance: **${initial_value:.2f}**")
st.markdown(f"### Final Balance: **${final_value:.2f}**")
st.markdown(f"### Net Profit/Loss: **${net_profit_loss:.2f}**")
st.markdown(f"### Total Buy Trades: **{strategy_instance.total_buy_trades}**")
st.markdown(f"### Total Sell Trades: **{strategy_instance.total_sell_trades}**")
# Calculate total trades
total_trades = strategy_instance.total_buy_trades + strategy_instance.total_sell_trades
# Safely calculate the win rate
if total_trades > 0:
win_rate = (strategy_instance.profitable_trades / total_trades) * 100
else:
win_rate = 0 # Avoid division by zero
# Display the win rate in the Streamlit app
st.markdown(f"### Win Rate: **{win_rate:.2f}%**")
st.markdown(f'### Sharpe Ratio: {strategy_instance.sharpe_ratio:.2f}') # Ensure sharpe_ratio is calculated correctly
st.markdown(f'### Maximum Drawdown: {strategy_instance.max_drawdown:.2f}') # Ensure max_drawdown is calculated correctly
# Show logs in a scrollable div
with st.expander("View Log Messages", expanded=False):
for msg in log_messages:
st.text(msg)
# Plot the results
fig = cerebro.plot()[0][0]
st.pyplot(fig)