-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.py
239 lines (227 loc) · 9.32 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import streamlit
import discord_interaction
import metatrader_interface
import dotenv
import os
import helper_functions
import strategy_router
# Function to get the data for button push
def get_data():
"""
Function to get the data for button push
"""
# Get the platform from the session state
platform = streamlit.session_state['platform']
# If the platform state is None, write an error message to the copilot container
if platform is None:
copilot_container.error("Please select a platform")
return False
else:
# If the platform is MetaTrader 5, make sure the platform is MetaTrader5
if platform == 'MetaTrader 5':
platform = 'MetaTrader5'
# Get the symbol and timeframe from the session state
symbol = streamlit.session_state['symbol']
# If the symbol state is None, write an error message to the copilot container
if symbol is None:
copilot_container.error("Please select a symbol")
return False
# Get the timeframe from the session state
timeframe = streamlit.session_state['timeframe']
# If the timeframe state is None, write an error message to the copilot container
if timeframe is None:
copilot_container.error("Please select a timeframe")
return False
# Get the strategy from the session state
strategy = streamlit.session_state['strategy']
# If the strategy state is None, write an error message to the copilot container
if strategy is None:
copilot_container.error("Please select a strategy")
return False
# Run the get_data function from the helper_functions.py file
try:
data = strategy_router.strategy_router(
platform=platform,
symbol=symbol,
timeframe=timeframe,
strategy_name=strategy
)
# Write the dataframe to the copilot container
copilot_container.write(data)
except Exception as exception:
copilot_container.error(f"An exception occurred when getting data: {exception}")
return False
return True
# Function to get information for a given platform
def get_platform_info():
"""
Function to get information for a given platform
:param platform: The platform to get information for
:param container: The container to add the widgets to
"""
# Set up branching variables
platform_connected = False
# Get the platform from the session state
platform = streamlit.session_state['platform']
# Get the settings file selection from the session state
settings_file = streamlit.session_state['settings_file']
# Get any required information based on the platform
if platform == "MetaTrader 5":
if settings_file == "Yes":
# Load environment variables from the .env file
dotenv.load_dotenv()
# Get the MetaTrader 5 username, password, server, and filepath from the .env file
mt5_username = os.getenv('metatrader_username')
mt5_password = os.getenv('metatrader_password')
mt5_server = os.getenv('metatrader_server')
mt5_filepath = os.getenv('metatrader_filepath')
else:
# Add four text inputs to the third column
mt5_username = trading_platform.text_input('Username', value='', max_chars=None, key=None, type='default')
mt5_password = trading_platform.text_input('Password', value='', max_chars=None, key=None, type='password')
mt5_server = trading_platform.text_input('Server', value='', max_chars=None, key=None, type='default')
mt5_filepath = trading_platform.text_input('Filepath', value='', max_chars=None, key=None, type='default')
# When all four text inputs are filled, try to open the terminal
if mt5_username and mt5_password and mt5_server and mt5_filepath:
try:
# Start MetaTrader 5
mt5_start = metatrader_interface.start_metatrader(
mt5_username=mt5_username,
mt5_password=mt5_password,
mt5_server=mt5_server,
mt5_filepath=mt5_filepath
)
# If successful, log in to MetaTrader 5
if mt5_start:
copilot_container.success("MetaTrader5 started successfully")
platform_connected = True
else:
copilot_container.error(f"MetaTrader 5 failed to start. Reason: {mt5_start}")
except Exception as exception:
copilot_container.error(f"An exception occurred when starting MetaTrader 5: {exception}")
else:
print(f"{platform} is not supported yet.")
# Check if the platform is connected
if platform_connected is False:
raise Exception(f"Failed to connect to {platform}")
# Get the platform information
symbols, timeframes, strategies = helper_functions.get_platform_info('MetaTrader5', symbol_st, timeframe_st)
# Update the symbol, timeframe and strategies selectboxes
streamlit.session_state['symbols'] = symbols
streamlit.session_state['timeframes'] = timeframes
streamlit.session_state['strategies'] = strategies
return True
if __name__ == '__main__':
#### Streamlit Setup ####
# Start the streamlit app
streamlit.set_page_config(
page_title='TradeOxy Terminal',
page_icon='📈',
layout='wide'
)
# Create the session states
if "settings_file" not in streamlit.session_state:
streamlit.session_state.settings_file = None
if "platform" not in streamlit.session_state:
streamlit.session_state.platform = None
if "symbol" not in streamlit.session_state:
streamlit.session_state.symbol = None
if "timeframe" not in streamlit.session_state:
streamlit.session_state.timeframe = None
if "symbols" not in streamlit.session_state:
streamlit.session_state.symbols = []
if "timeframes" not in streamlit.session_state:
streamlit.session_state.timeframes = []
if "strategies" not in streamlit.session_state:
streamlit.session_state.strategies = []
if "strategy" not in streamlit.session_state:
streamlit.session_state.strategy = None
# Create the header
streamlit.header('TradeOxy Terminal')
# Create the header container
header_container = streamlit.container()
# Create four columns in the header container
settings_choice, alert_listener, trading_platform, settings = header_container.columns(4)
# Create a start_stop container
start_stop = streamlit.container()
start_button = False
# Create a start button
if start_button == False:
# Create a start button that stretches across the page and is green
start = start_stop.button(
'Start',
use_container_width=True
)
start_button = True
# Create a copilot container
copilot_container = streamlit.container()
# Add a title to the copilot container
copilot_container.header(
'TradeOxy Intelligence Copilot'
)
# Create three columns in the copilot container
symbol_st, timeframe_st, strategy_st = copilot_container.columns(3)
# Add an empty dropdown to symbol_st and timeframe_st
symbol_st.selectbox(
'Symbol',
streamlit.session_state['symbols'],
index=None,
key='symbol'
)
timeframe_st.selectbox(
'Timeframe',
streamlit.session_state['timeframes'],
index=None,
key='timeframe'
)
strategy_st.selectbox(
'Strategy',
streamlit.session_state['strategies'],
index=None,
placeholder='Select a strategy',
key='strategy'
)
# Add a button to the Copilot container
copilot_container.button('Get Data', on_click=get_data, use_container_width=True)
# Add an option to use a settings file
settings_file = settings_choice.selectbox(
'Use Settings File',
['Yes', 'No'],
index=None,
placeholder='Select an option',
key='settings_file'
)
# Configure the alert listener column #
# Add a dropdown to the alert listener column
alerting_option = alert_listener.selectbox(
'Alert Listener',
['Discord', 'Direct'],
index=None,
placeholder='Select an alerting option'
)
if alerting_option == 'Discord':
copilot_container.write('Connecting to Discord...')
if settings_file:
# Load environment variables from the .env file
dotenv.load_dotenv()
# Get the Discord key from the .env file
discord_key = os.getenv('discord_key')
else:
# Add a text input to the alert listener column
discord_key = alert_listener.text_input('Discord Key', value='', max_chars=None, key=None, type='default')
# Add a dropdown to the trading platform column
trading_platform_selection = trading_platform.selectbox(
'Trading Platform',
['Alpaca', 'MetaTrader 5', 'Binance', 'Coinbase'],
index=None,
placeholder='Select a trading platform',
on_change=get_platform_info,
key='platform'
)
# Add a selectbox to the fourth column
make_trades = settings.selectbox(
'Make Trades',
['Yes', 'No'],
index=None,
placeholder='Select an option'
)