-
Notifications
You must be signed in to change notification settings - Fork 72
/
main.py
290 lines (172 loc) · 9.3 KB
/
main.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import tpqoa
from datetime import datetime
from livetrading.BollingerBandsLive import BollingerBandsLive
from livetrading.ContrarianLive import ContrarianLive
from livetrading.MLClassificationLive import MLClassificationLive
from livetrading.MomentumLive import MomentumLive
from livetrading.SMALive import SMALive
from backtesting.ContrarianBacktest import ContrarianBacktest
from backtesting.BollingerBandsBacktest import BollingerBandsBacktest
from backtesting.MomentumBacktest import MomentumBacktest
from backtesting.SMABacktest import SMABacktest
from backtesting.MLClassificationBacktest import MLClassificationBacktest
if __name__ == "__main__":
while True:
# step 1 ensure they have this
cfg = "oanda.cfg"
# step 1.5 open oanda connection
oanda = tpqoa.tpqoa("oanda.cfg")
# step 2 decide instrument
print("Enter an instrument to trade (index or pair name): \n")
choices = []
for index, instrument in enumerate(oanda.get_instruments()):
temp = instrument[1]
choices.append(temp)
print(f"({index}: {temp})", end=", ")
print("")
choice = input("\n")
while True:
if choice not in choices:
try:
val = int(choice)
if val < len(choices) and val >= 0:
choice = choices[val]
break
except:
pass
choice = input("Please choose an instrument from the list above: ")
else:
break
instrument = choice
print(f"Instrument: {instrument}")
# step 3 decide if live or backtesting
choice = input("Live Trading (1) or Backtesting (2)? \n")
while choice not in ["1", "2"]:
choice = input("Please choose between \"1\" or \"2\": \n")
# step 4, depending on decision, showcase available strategies
if choice == "1":
live_strategies = ["sma", "bollinger_bands", "contrarian", "momentum", "ml_classification"]
print("Please choose the strategy you would like to utilize: \n")
for strategy in live_strategies:
print(strategy, end=", ")
choice = input("\n").lower()
while choice not in live_strategies:
choice = input("Please choose a strategy listed above. \n").lower()
strategy = choice
print("Please enter the granularity for your session (NOT from list in README, try \"1hr\", \"1m\", \"30s\" (less strict): \n")
granularity = input("")
print("Please enter the number of units you'd like to trade with (integer, IE 200000 units): \n")
units = int(input(""))
print("Enter stop profit dollars to halt trading at (float, IE 25, 15.34, etc) (enter \"n\" if not applicable): \n")
# TODO: Enable stop datetime
stop_profit = input("")
if stop_profit == "n":
stop_profit = None
else:
stop_profit = float(stop_profit)
print("Enter a negative stop loss to halt trading at (float, IE -25, -1.32, etc) (enter \"n\" if not applicable): \n")
stop_loss = input("")
if stop_loss == "n":
stop_loss = None
else:
stop_loss = float(stop_loss)
###################################################################################
# Strategies
###################################################################################
if strategy == "sma":
print("Enter SMAS value (integer, IE 9): \n")
smas = int(input(""))
print("Enter SMAL value (integer, IE 20): \n")
smal = int(input(""))
while smal < smas:
smal = int(input("SMAL must be larger or equal to SMAS: \n"))
trader = SMALive(cfg, instrument, granularity, smas, smal, units, stop_loss=stop_loss, stop_profit=stop_profit)
# TODO: Post trading analysis, maybe some graphs etc
elif strategy == "bollinger_bands":
print("Enter SMA value (integer, IE 9): \n")
sma = int(input(""))
print("Enter deviation value (integer, IE 2): \n")
deviation = int(input(""))
trader = BollingerBandsLive(cfg, instrument, granularity, sma, deviation, units, stop_loss=stop_loss,
stop_profit=stop_profit)
# TODO: Post trading analysis, maybe some graphs etc
elif strategy == "momentum":
print("Enter window value (integer, IE 3): \n")
window = int(input(""))
trader = MomentumLive(cfg, instrument, granularity, window, units, stop_loss=stop_loss,
stop_profit=stop_profit)
# TODO: Post trading analysis, maybe some graphs etc
elif strategy == "contrarian":
print("Enter window value (integer, IE 3): \n")
window = int(input(""))
trader = ContrarianLive(cfg, instrument, granularity, window, units, stop_loss=stop_loss,
stop_profit=stop_profit)
# TODO: Post trading analysis, maybe some graphs etc
elif strategy == "ml_classification":
print("Enter number of lags (integer, IE 6): \n")
lags = int(input(""))
trader = MLClassificationLive(cfg, instrument, granularity, lags, units, stop_loss=stop_loss,
stop_profit=stop_profit)
# TODO: Post trading analysis, maybe some graphs etc
else:
backtesting_strategies = ["sma", "bollinger_bands", "contrarian", "momentum", "ml_classification", "ml_regression"]
print("Please choose the strategy you would like to backtest: \n")
for strategy in backtesting_strategies:
print(strategy, end=", ")
choice = input("\n").lower()
while choice not in backtesting_strategies:
choice = input("Please choose a strategy listed above. \n").lower()
strategy = choice
print("Enter the start date for the backtest (string, in form of \"YYYY-MM-DD\"): \n")
start = input("")
print("Enter the end date for the backtest (string, in form of \"YYYY-MM-DD\"): \n")
end = input("")
while datetime.strptime(start, '%Y-%m-%d') > datetime.strptime(end, '%Y-%m-%d'):
end = input("End date must be after start date. \n")
print("Enter the trading cost to consider: (float, IE 0.0, 0.00007): \n")
trading_cost = float(input(""))
print("Please enter the granularity for your session (See list in README, IE \"S30\", \"M1\", \"H1\"): \n")
granularity = input("")
if strategy == "sma":
print("Enter SMAS value (integer, IE 9): \n")
smas = int(input(""))
print("Enter SMAL value (integer, IE 20): \n")
smal = int(input(""))
while smal < smas:
smal = int(input("SMAL must be larger or equal to SMAS: \n"))
trader = SMABacktest(instrument, start, end, smas, smal, granularity, trading_cost)
trader.test()
trader.optimize()
trader.plot_results()
# TODO: Post trading analysis, maybe some graphs etc
elif strategy == "bollinger_bands":
print("Enter SMA value (integer, IE 9): \n")
sma = int(input(""))
print("Enter deviation value (integer, IE 2): \n")
deviation = int(input(""))
trader = BollingerBandsBacktest(instrument, start, end, sma, deviation, granularity, trading_cost)
trader.test()
trader.optimize()
trader.plot_results()
# TODO: Post trading analysis, maybe some graphs etc
elif strategy == "momentum":
print("Enter window value (integer, IE 3): \n")
window = int(input(""))
trader = MomentumBacktest(instrument, start, end, window, granularity, trading_cost)
trader.test()
trader.optimize()
trader.plot_results()
# TODO: Post trading analysis, maybe some graphs etc
elif strategy == "contrarian":
print("Enter window value (integer, IE 3): \n")
window = int(input(""))
trader = ContrarianBacktest(instrument, start, end, window, granularity, trading_cost)
trader.test()
trader.optimize()
trader.plot_results()
# TODO: Post trading analysis, maybe some graphs etc
elif strategy == "ml_classification":
trader = MLClassificationBacktest(instrument, start, end, granularity, trading_cost)
trader.test()
trader.plot_results()
# TODO: Post trading analysis, maybe some graphs etc