-
Notifications
You must be signed in to change notification settings - Fork 10
/
trading_tests.py
430 lines (361 loc) · 15 KB
/
trading_tests.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from datetime import datetime
from pytest import fixture
from pytz import utc
from trading import Trading
from trading import MARKET_TIMEZONE
from trading import TRADEKING_CONSUMER_KEY
from trading import TRADEKING_CONSUMER_SECRET
from trading import TRADEKING_ACCESS_TOKEN
from trading import TRADEKING_ACCESS_TOKEN_SECRET
from trading import TRADEKING_ACCOUNT_NUMBER
from trading import USE_REAL_MONEY
@fixture
def trading():
return Trading(logs_to_cloud=False)
def as_market_time(year, month, day, hour=0, minute=0, second=0):
"""Creates a timestamp in market time."""
market_time = datetime(year, month, day, hour, minute, second)
return MARKET_TIMEZONE.localize(market_time)
def test_environment_variables():
assert TRADEKING_CONSUMER_KEY
assert TRADEKING_CONSUMER_SECRET
assert TRADEKING_ACCESS_TOKEN
assert TRADEKING_ACCESS_TOKEN_SECRET
assert TRADEKING_ACCOUNT_NUMBER
assert not USE_REAL_MONEY
def test_get_strategy_blacklist(trading):
assert trading.get_strategy({
"exchange": "NASDAQ",
"name": "Google",
"sentiment": 0.4,
"ticker": "GOOG"}, "open") == {
"action": "hold",
"exchange": "NASDAQ",
"name": "Google",
"reason": "blacklist",
"sentiment": 0.4,
"ticker": "GOOG"}
assert trading.get_strategy({
"exchange": "New York Stock Exchange",
"name": "Ford",
"sentiment": 0.3,
"ticker": "F"}, "open") == {
"action": "bull",
"exchange": "New York Stock Exchange",
"name": "Ford",
"reason": "positive sentiment",
"sentiment": 0.3,
"ticker": "F"}
def test_get_strategy_market_status(trading):
assert trading.get_strategy({
"exchange": "New York Stock Exchange",
"name": "General Motors",
"sentiment": 0.5,
"ticker": "GM"}, "pre") == {
"action": "bull",
"exchange": "New York Stock Exchange",
"name": "General Motors",
"reason": "positive sentiment",
"sentiment": 0.5,
"ticker": "GM"}
assert trading.get_strategy({
"exchange": "New York Stock Exchange",
"name": "General Motors",
"sentiment": 0.5,
"ticker": "GM"}, "open") == {
"action": "bull",
"exchange": "New York Stock Exchange",
"name": "General Motors",
"reason": "positive sentiment",
"sentiment": 0.5,
"ticker": "GM"}
assert trading.get_strategy({
"exchange": "New York Stock Exchange",
"name": "General Motors",
"sentiment": 0.5,
"ticker": "GM"}, "after") == {
"action": "hold",
"exchange": "New York Stock Exchange",
"name": "General Motors",
"reason": "market closed",
"sentiment": 0.5,
"ticker": "GM"}
assert trading.get_strategy({
"exchange": "New York Stock Exchange",
"name": "General Motors",
"sentiment": 0.5,
"ticker": "GM"}, "close") == {
"action": "hold",
"exchange": "New York Stock Exchange",
"name": "General Motors",
"reason": "market closed",
"sentiment": 0.5,
"ticker": "GM"}
def test_get_strategy_sentiment(trading):
assert trading.get_strategy({
"exchange": "New York Stock Exchange",
"name": "General Motors",
"sentiment": 0,
"ticker": "GM"}, "open") == {
"action": "hold",
"exchange": "New York Stock Exchange",
"name": "General Motors",
"reason": "neutral sentiment",
"sentiment": 0,
"ticker": "GM"}
assert trading.get_strategy({
"exchange": "New York Stock Exchange",
"name": "Ford",
"sentiment": 0.5,
"ticker": "F"}, "open") == {
"action": "bull",
"exchange": "New York Stock Exchange",
"name": "Ford",
"reason": "positive sentiment",
"sentiment": 0.5,
"ticker": "F"}
assert trading.get_strategy({
"exchange": "New York Stock Exchange",
"name": "Fiat",
"root": "Fiat Chrysler Automobiles",
"sentiment": -0.5,
"ticker": "FCAU"}, "open") == {
"action": "bear",
"exchange": "New York Stock Exchange",
"name": "Fiat",
"reason": "negative sentiment",
"root": "Fiat Chrysler Automobiles",
"sentiment": -0.5,
"ticker": "FCAU"}
def test_get_budget(trading):
assert trading.get_budget(11000.0, 1) == 10000.0
assert trading.get_budget(11000.0, 2) == 5000.0
assert trading.get_budget(11000.0, 3) == 3333.33
assert trading.get_budget(11000.0, 0) == 0.0
def test_utc_to_market_time(trading):
assert trading.utc_to_market_time(datetime(
2017, 1, 3, 16, 44, 13)) == as_market_time(
2017, 1, 3, 11, 44, 13)
def test_market_time_to_utc(trading):
assert trading.market_time_to_utc(datetime(
2017, 1, 3, 11, 44, 13)) == datetime(
2017, 1, 3, 16, 44, 13, tzinfo=utc)
def test_make_request_success(trading):
url = "https://api.tradeking.com/v1/member/profile.json"
response = trading.make_request(url=url)
assert response is not None
account = response["response"]["userdata"]["account"]["account"]
assert account == TRADEKING_ACCOUNT_NUMBER
assert response["response"]["error"] == "Success"
def test_make_request_fail(trading):
url = "https://api.tradeking.com/v1/member/profile.xml"
response = trading.make_request(url=url)
assert response is None
def test_fixml_buy_now(trading):
assert trading.fixml_buy_now("GM", 23, 38.32) == (
'<FIXML xmlns="http://www.fixprotocol.org/FIXML-5-0-SP2">'
'<Order TmInForce="0" Typ="2" Side="1" Px="38.32" Acct="%s">'
'<Instrmt SecTyp="CS" Sym="GM"/>'
'<OrdQty Qty="23"/>'
'</Order>'
'</FIXML>' % TRADEKING_ACCOUNT_NUMBER)
def test_fixml_sell_eod(trading):
assert trading.fixml_sell_eod("GM", 23, 31.36) == (
'<FIXML xmlns="http://www.fixprotocol.org/FIXML-5-0-SP2">'
'<Order TmInForce="7" Typ="2" Side="2" Px="31.36" Acct="%s">'
'<Instrmt SecTyp="CS" Sym="GM"/>'
'<OrdQty Qty="23"/>'
'</Order>'
'</FIXML>' % TRADEKING_ACCOUNT_NUMBER)
def test_fixml_short_now(trading):
assert trading.fixml_short_now("GM", 23, 31.36) == (
'<FIXML xmlns="http://www.fixprotocol.org/FIXML-5-0-SP2">'
'<Order TmInForce="0" Typ="2" Side="5" Px="31.36" Acct="%s">'
'<Instrmt SecTyp="CS" Sym="GM"/>'
'<OrdQty Qty="23"/>'
'</Order>'
'</FIXML>' % TRADEKING_ACCOUNT_NUMBER)
def test_fixml_cover_eod(trading):
assert trading.fixml_cover_eod("GM", 23, 38.32) == (
'<FIXML xmlns="http://www.fixprotocol.org/FIXML-5-0-SP2">'
'<Order TmInForce="7" Typ="2" Side="1" Px="38.32" AcctTyp="5"'
' Acct="%s">'
'<Instrmt SecTyp="CS" Sym="GM"/>'
'<OrdQty Qty="23"/>'
'</Order>'
'</FIXML>' % TRADEKING_ACCOUNT_NUMBER)
def test_get_buy_limit(trading):
assert trading.get_buy_limit(34.84) == 38.32
def test_get_sell_limit(trading):
assert trading.get_sell_limit(34.84) == 31.36
def test_get_balance(trading):
assert trading.get_balance() > 0.0
def test_get_last_price(trading):
assert trading.get_last_price("GM") > 0.0
assert trading.get_last_price("GOOG") > 0.0
assert trading.get_last_price("$NAP") is None
assert trading.get_last_price("") is None
def test_get_market_status(trading):
assert trading.get_market_status() in ["pre", "open", "after", "close"]
def test_get_order_url(trading):
assert trading.get_order_url() == (
"https://api.tradeking.com/v1/accounts/%s/"
"orders/preview.json") % TRADEKING_ACCOUNT_NUMBER
def test_get_quantity(trading):
assert trading.get_quantity("F", 10000.0) > 0
def test_get_historical_prices(trading):
assert trading.get_historical_prices(
"F", as_market_time(2017, 1, 24, 19, 46, 57)) == {
"at": 12.6, "eod": 12.78}
assert trading.get_historical_prices(
"GM", as_market_time(2017, 1, 24, 19, 46, 57)) == {
"at": 37.09, "eod": 38.28}
assert trading.get_historical_prices(
"BLK", as_market_time(2017, 1, 18, 8, 0, 51)) == {
"at": 374.8, "eod": 378.0}
assert trading.get_historical_prices(
"F", as_market_time(2017, 1, 18, 7, 34, 9)) == {
"at": 12.6, "eod": 12.41}
assert trading.get_historical_prices(
"GM", as_market_time(2017, 1, 18, 7, 34, 9)) == {
"at": 37.31, "eod": 37.47}
assert trading.get_historical_prices(
"LMT", as_market_time(2017, 1, 18, 7, 34, 9)) == {
"at": 254.12, "eod": 254.07}
assert trading.get_historical_prices(
"GM", as_market_time(2017, 1, 17, 12, 55, 38)) == {
"at": 37.54, "eod": 37.31}
assert trading.get_historical_prices(
"STT", as_market_time(2017, 1, 17, 12, 55, 38)) == {
"at": 81.19, "eod": 80.2}
assert trading.get_historical_prices(
"WMT", as_market_time(2017, 1, 17, 12, 55, 38)) == {
"at": 68.53, "eod": 68.5}
assert trading.get_historical_prices(
"F", as_market_time(2017, 1, 9, 9, 16, 34)) == {
"at": 12.84, "eod": 12.64}
assert trading.get_historical_prices(
"FCAU", as_market_time(2017, 1, 9, 9, 16, 34)) == {
"at": 10.57, "eod": 10.57}
assert trading.get_historical_prices(
"FCAU", as_market_time(2017, 1, 9, 9, 14, 10)) == {
"at": 10.55, "eod": 10.57}
assert trading.get_historical_prices(
"TM", as_market_time(2017, 1, 5, 13, 14, 30)) == {
"at": 121.12, "eod": 120.44}
assert trading.get_historical_prices(
"F", as_market_time(2017, 1, 4, 8, 19, 9)) == {
"at": 12.59, "eod": 13.17}
assert trading.get_historical_prices(
"F", as_market_time(2017, 1, 3, 11, 44, 13)) == {
"at": 12.41, "eod": 12.59}
assert trading.get_historical_prices(
"GM", as_market_time(2017, 1, 3, 7, 30, 5)) == {
"at": 34.84, "eod": 35.15}
assert trading.get_historical_prices(
"BA", as_market_time(2016, 12, 22, 17, 26, 5)) == {
"at": 157.46, "eod": 157.92}
assert trading.get_historical_prices(
"BA", as_market_time(2016, 12, 6, 8, 52, 35)) == {
"at": 152.16, "eod": 152.3}
assert trading.get_historical_prices(
"M", as_market_time(2015, 11, 12, 16, 5, 28)) == {
"at": 40.73, "eod": 40.15}
assert trading.get_historical_prices(
"M", as_market_time(2015, 7, 16, 9, 14, 15)) == {
"at": 71.75, "eod": 72.8}
def test_get_day_quotes(trading):
quotes = trading.get_day_quotes(
"BA", as_market_time(2016, 12, 22, 17, 26, 0))
assert len(quotes) == 395
assert quotes[0] == {
"price": 158.73, "time": as_market_time(2016, 12, 22, 9, 3, 0)}
assert quotes[-1] == {
"price": 157.46, "time": as_market_time(2016, 12, 22, 16, 30, 0)}
def test_is_trading_day(trading):
assert not trading.is_trading_day(as_market_time(2017, 1, 22))
assert trading.is_trading_day(as_market_time(2017, 1, 23))
assert trading.is_trading_day(as_market_time(2017, 1, 24))
assert trading.is_trading_day(as_market_time(2017, 1, 25))
assert trading.is_trading_day(as_market_time(2017, 1, 26))
assert trading.is_trading_day(as_market_time(2017, 1, 27))
assert not trading.is_trading_day(as_market_time(2017, 1, 28))
assert not trading.is_trading_day(as_market_time(2017, 1, 2))
def test_get_previous_day(trading):
assert trading.get_previous_day(as_market_time(
2017, 1, 22)) == as_market_time(2017, 1, 20)
assert trading.get_previous_day(as_market_time(
2017, 1, 23)) == as_market_time(2017, 1, 20)
assert trading.get_previous_day(as_market_time(
2017, 1, 24)) == as_market_time(2017, 1, 23)
assert trading.get_previous_day(as_market_time(
2017, 1, 25)) == as_market_time(2017, 1, 24)
assert trading.get_previous_day(as_market_time(
2017, 1, 26)) == as_market_time(2017, 1, 25)
assert trading.get_previous_day(as_market_time(
2017, 1, 27)) == as_market_time(2017, 1, 26)
assert trading.get_previous_day(as_market_time(
2017, 1, 28)) == as_market_time(2017, 1, 27)
assert trading.get_previous_day(as_market_time(
2017, 1, 3)) == as_market_time(2016, 12, 30)
def test_get_next_day(trading):
assert trading.get_next_day(as_market_time(
2017, 1, 22)) == as_market_time(2017, 1, 23)
assert trading.get_next_day(as_market_time(
2017, 1, 23)) == as_market_time(2017, 1, 24)
assert trading.get_next_day(as_market_time(
2017, 1, 24)) == as_market_time(2017, 1, 25)
assert trading.get_next_day(as_market_time(
2017, 1, 25)) == as_market_time(2017, 1, 26)
assert trading.get_next_day(as_market_time(
2017, 1, 26)) == as_market_time(2017, 1, 27)
assert trading.get_next_day(as_market_time(
2017, 1, 27)) == as_market_time(2017, 1, 30)
assert trading.get_next_day(as_market_time(
2017, 1, 28)) == as_market_time(2017, 1, 30)
assert trading.get_next_day(as_market_time(
2016, 12, 30)) == as_market_time(2017, 1, 3)
def test_make_order_request_success(trading):
assert not USE_REAL_MONEY
assert trading.make_order_request((
'<FIXML xmlns="http://www.fixprotocol.org/FIXML-5-0-SP2">'
'<Order TmInForce="0" Typ="2" Side="1" Px="38.32" Acct="%s">'
'<Instrmt SecTyp="CS" Sym="GM"/>'
'<OrdQty Qty="23"/>'
'</Order>'
'</FIXML>' % TRADEKING_ACCOUNT_NUMBER))
def test_make_order_request_fail(trading):
assert not USE_REAL_MONEY
assert not trading.make_order_request("<FIXML\>")
def test_bull(trading):
assert not USE_REAL_MONEY
# TODO: Find a way to test while the markets are closed and how to test
# sell short orders without holding the stock.
# assert trading.bull("F", 10000.0)
def test_bear(trading):
assert not USE_REAL_MONEY
# TODO: Find a way to test while the markets are closed and how to test
# sell short orders without holding the stock.
# assert trading.bear("F", 10000.0)
def test_make_trades_success(trading):
assert not USE_REAL_MONEY
# TODO: Find a way to test while the markets are closed and how to test
# sell short orders without holding the stock.
# assert trading.make_trades([{
# "exchange": "New York Stock Exchange",
# "name": "Lockheed Martin",
# "sentiment": -0.1,
# "ticker": "LMT"}, {
# "exchange": "New York Stock Exchange",
# "name": "Boeing",
# "sentiment": 0.1,
# "ticker": "BA"}])
def test_make_trades_fail(trading):
assert not USE_REAL_MONEY
assert not trading.make_trades([{
"exchange": "New York Stock Exchange",
"name": "Boeing",
"sentiment": 0,
"ticker": "BA"}])