Surya152002 commited on
Commit
d2a1064
·
1 Parent(s): 507fffd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import backtrader as bt
3
+ import yfinance as yf
4
+ import matplotlib.pyplot as plt
5
+ from datetime import datetime
6
+
7
+ # Define the trading strategy
8
+ class SmaCross(bt.Strategy):
9
+ params = dict(
10
+ pfast=10, # period for the fast moving average
11
+ pslow=30 # period for the slow moving average
12
+ )
13
+
14
+ def __init__(self):
15
+ sma1 = bt.ind.SMA(period=self.p.pfast) # fast moving average
16
+ sma2 = bt.ind.SMA(period=self.p.pslow) # slow moving average
17
+ self.crossover = bt.ind.CrossOver(sma1, sma2) # crossover signal
18
+
19
+ def next(self):
20
+ if not self.position: # not in the market
21
+ if self.crossover > 0: # if fast crosses slow to the upside
22
+ self.buy() # enter long
23
+
24
+ elif self.crossover < 0: # in the market & cross to the downside
25
+ self.close() # close long position
26
+
27
+ # Function to run backtest and return cerebro
28
+ def run_backtest(data):
29
+ cerebro = bt.Cerebro()
30
+ cerebro.addstrategy(SmaCross)
31
+ feed = bt.feeds.PandasData(dataname=data)
32
+ cerebro.adddata(feed)
33
+ cerebro.broker.setcash(10000)
34
+ cerebro.addsizer(bt.sizers.AllInSizer, percents=95) # Use 95% of the portfolio for each trade
35
+ cerebro.run()
36
+ return cerebro
37
+
38
+ # Initialize session state for running status
39
+ if 'running' not in st.session_state:
40
+ st.session_state.running = False
41
+
42
+ # Streamlit app layout
43
+ st.title('Cryptocurrency Trading Bot Simulation')
44
+
45
+ # Sidebar for user input
46
+ st.sidebar.header('User Input Parameters')
47
+ selected_crypto = st.sidebar.selectbox('Select cryptocurrency', ('BTC-USD', 'ETH-USD', 'LTC-USD'))
48
+ start_date = st.sidebar.date_input('Start date', datetime(2020, 1, 1))
49
+ end_date = st.sidebar.date_input('End date', datetime(2021, 1, 1))
50
+
51
+ # Function to handle start button
52
+ def start_bot():
53
+ st.session_state.running = True
54
+
55
+ # Function to handle stop button
56
+ def stop_bot():
57
+ st.session_state.running = False
58
+
59
+ # Buttons to start/stop the bot
60
+ st.sidebar.button('Start Bot', on_click=start_bot)
61
+ st.sidebar.button('Stop Bot', on_click=stop_bot)
62
+
63
+ # Display bot status
64
+ if st.session_state.running:
65
+ st.success('Bot is running!')
66
+ # Fetch historical data from yfinance
67
+ data = yf.download(selected_crypto, start=start_date, end=end_date)
68
+
69
+ # Check if data was successfully fetched
70
+ if not data.empty:
71
+ # Run backtest
72
+ cerebro = run_backtest(data)
73
+
74
+ # Show results
75
+ st.header('Simulation Results')
76
+ st.write(f'Final Portfolio Value: ${cerebro.broker.getvalue():,.2f}')
77
+
78
+ # Plot the results manually using matplotlib
79
+ fig, ax = plt.subplots()
80
+ for data in cerebro.datas:
81
+ ax.plot(data.datetime.array, data.close.array, label='Close')
82
+ ax.legend()
83
+
84
+ # Show the plot
85
+ st.pyplot(fig)
86
+ else:
87
+ st.error('Bot is stopped!')