-
Notifications
You must be signed in to change notification settings - Fork 4
/
dao_test.py
503 lines (428 loc) · 18.5 KB
/
dao_test.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
import unittest
from mock import Mock
from mock import patch
from decimal import Decimal
from dao import *
class TestQuote(unittest.TestCase):
def test_get_latest_quote_for_cash(self):
quote = Quote.get_latest_quote('CASH')
self.assertEquals('CASH', quote.symbol)
self.assertEquals(Decimal('1'), quote.close)
self.assertEquals(Decimal('1'), quote.high)
self.assertEquals(Decimal('1'), quote.low)
self.assertEquals(Decimal('1'), quote.open)
def test_get_latest_quote_for_funds(self):
quote = Quote.get_latest_quote('FUNDS')
self.assertEquals('FUNDS', quote.symbol)
self.assertEquals(Decimal('1'), quote.close)
self.assertEquals(Decimal('1'), quote.high)
self.assertEquals(Decimal('1'), quote.low)
self.assertEquals(Decimal('1'), quote.open)
def test_get_latest_quote_not_found(self):
try:
Quote.get_latest_quote('abracadabra')
self.fail("Expected error")
except Quote.NotFound:
pass
def test_has_met_stop_short(self):
position = Position({})
position.get_stop = Mock(return_value = Decimal('10'))
position.is_long = False
q = Quote({})
q.close = 9
self.assertFalse(q.has_met_stop(position))
q.close = 10
self.assertTrue(q.has_met_stop(position))
q.close = 11
self.assertTrue(q.has_met_stop(position))
def test_has_met_stop_long(self):
position = Position({})
position.get_stop = Mock(return_value = Decimal('10'))
position.is_long = True
q = Quote({})
q.close = 9
self.assertTrue(q.has_met_stop(position))
q.close = 10
self.assertTrue(q.has_met_stop(position))
q.close = 11
self.assertFalse(q.has_met_stop(position))
def test_get_quote_not_found(self):
quote = Quote.get_quote('----', '2001-01-01')
self.assertEquals(None, quote)
def test_get_quote(self):
quote = Quote.get_quote('AAPL', '2001-01-01')
self.assertQuote(quote)
def test_get_quotes(self):
Quote.__skip_first__ = 0
quotes = Quote.get_quotes('AAPL')
found = False
for quote in quotes:
self.assertTrue(isinstance(quote, Quote))
self.assertEquals('AAPL', quote.symbol)
try:
self.assertQuote(quote)
found = True
except AssertionError:
pass
self.assertTrue(found)
def test_previous(self):
quote = Quote.get_quote('AAPL', '2001-01-02')
self.assertQuote(quote.previous())
def test_next(self):
quote = Quote.get_quote('AAPL', '2001-01-02')
next = quote.next()
self.assertEquals(quote.symbol, next.symbol)
self.assertEquals(datetime.date(2001, 1, 3), next.date)
def test_get_indicator(self):
quote = Quote({'symbol':'AAPL', 'date':'2001-01-02'})
indicator = Indicator({})
Indicator.get_indicator = Mock(return_value=indicator)
self.assertEquals(indicator, quote.get_indicator())
Indicator.get_indicator.assert_called_with('AAPL', '2001-01-02')
def test_get_indicator(self):
quote = Quote({'symbol':'CASH'})
self.assertEquals(None, quote.get_indicator())
quote = Quote({'symbol':'FUNDS'})
self.assertEquals(None, quote.get_indicator())
def test_get_trailing_indicators(self):
quote = Quote({'symbol':'AAPL', 'date':'2001-01-02'})
indicators = Indicator({})
Indicator.get_trailing_indicators = Mock(return_value=indicators)
self.assertEquals(indicators, quote.get_trailing_indicators(7))
Indicator.get_trailing_indicators.assert_called_with('AAPL', '2001-01-02', 7)
def test_set_tr(self):
Quote.set_tr('AAPL', '2001-01-02', '1234')
quote = Quote.get_quote('AAPL', '2001-01-02')
self.assertEquals(Decimal('1234'), quote.tr)
Quote.set_tr('AAPL', '2001-01-02', '5678')
quote = Quote.get_quote('AAPL', '2001-01-02')
self.assertEquals(Decimal('5678'), quote.tr)
def test_is_above_20_day_high_false(self):
quote = Quote({'close': Decimal('10')})
indicator = Indicator({'hh_20': Decimal('20')})
quote.get_indicator = Mock(return_value=indicator)
self.assertFalse(quote.is_above_20_day_high())
def test_is_above_20_day_high_true(self):
quote = Quote({'close': Decimal('30')})
indicator = Indicator({'hh_20': Decimal('20')})
quote.get_indicator = Mock(return_value=indicator)
self.assertTrue(quote.is_above_20_day_high())
def test_is_above_50_day_high_false(self):
quote = Quote({'close': Decimal('10')})
indicator = Indicator({'hh_50': Decimal('20')})
quote.get_indicator = Mock(return_value=indicator)
self.assertFalse(quote.is_above_50_day_high())
def test_is_above_50_day_high_true(self):
quote = Quote({'close': Decimal('30')})
indicator = Indicator({'hh_50': Decimal('20')})
quote.get_indicator = Mock(return_value=indicator)
self.assertTrue(quote.is_above_50_day_high())
def test_is_below_10_day_low_true(self):
quote = Quote({'close': Decimal('10')})
indicator = Indicator({'ll_10': Decimal('20')})
quote.get_indicator = Mock(return_value=indicator)
self.assertTrue(quote.is_below_10_day_low())
def test_is_below_10_day_low_false(self):
quote = Quote({'close': Decimal('30')})
indicator = Indicator({'ll_10': Decimal('20')})
quote.get_indicator = Mock(return_value=indicator)
self.assertFalse(quote.is_below_10_day_low())
def assertQuote(self, quote):
self.assertEquals('AAPL', quote.symbol)
self.assertEquals(('2001-01-01'), str(quote.date))
self.assertEquals(Decimal('100.1'), quote.close)
self.assertEquals(Decimal('110.1'), quote.high)
self.assertEquals(Decimal('120.1'), quote.low)
self.assertEquals(Decimal('130.1'), quote.open)
def test_is_above_sma20(self):
quote = Quote({'close': Decimal('30')})
quote.get_indicator = Mock(return_value =
Indicator({'sma_20': Decimal('20')}))
self.assertTrue(quote.is_above_sma20())
quote.get_indicator = Mock(return_value =
Indicator({'sma_20': Decimal('40')}))
self.assertFalse(quote.is_above_sma20())
def test_is_above_sma50(self):
quote = Quote({'close': Decimal('30')})
quote.get_indicator = Mock(return_value =
Indicator({'sma_50': Decimal('20')}))
self.assertTrue(quote.is_above_sma50())
quote.get_indicator = Mock(return_value =
Indicator({'sma_50': Decimal('40')}))
self.assertFalse(quote.is_above_sma50())
def test_get_latest_quotes(self):
quotes = Quote.get_latest_quotes()
self.assertEquals('LUPE', quotes[0].symbol)
self.assertEquals('2001-01-01', str(quotes[0].date))
self.assertEquals('AAPL', quotes[1].symbol)
self.assertEquals('2010-07-06', str(quotes[1].date))
def test_get_latest_quote(self):
quote = Quote.get_latest_quote('AAPL')
self.assertEquals('AAPL', quote.symbol)
self.assertEquals(('2010-07-06'), str(quote.date))
class TestIndicator(unittest.TestCase):
def test_get_indicator(self):
indicator = Indicator.get_indicator('AAPL', '2001-01-01')
self.assertIndicator(indicator)
def assertIndicator(self, indicator):
self.assertTrue(isinstance(indicator, Indicator))
self.assertEquals('AAPL', indicator.symbol)
self.assertEquals(('2001-01-01'), str(indicator.date))
self.assertEquals(Decimal('100.1'), indicator.sma_20)
self.assertEquals(Decimal('110.1'), indicator.sma_50)
self.assertEquals(Decimal('115.1'), indicator.atr_14)
self.assertEquals(Decimal('120.1'), indicator.atr_exp20)
def test_get_trailing_indicators(self):
indicators = Indicator.get_trailing_indicators('AAPL', '2001-01-02', 2)
self.assertEquals(2, len(indicators))
self.assertIndicator(indicators[1])
self.assertEquals('AAPL', indicators[0].symbol)
self.assertEquals(('2001-01-02'), str(indicators[0].date))
def test_calculate_stop(self):
indicator = Indicator({'atr_exp20': Decimal(1)})
self.assertEqual(8, indicator.calculate_stop(Decimal(10)))
indicator.atr_stop = 1
self.assertEqual(9, indicator.calculate_stop(Decimal(10)))
class TestQuery(unittest.TestCase):
class Dummy(Base):
pass
def test_keys(self):
query = Query("select a,b,c from bah", ())
self.assertEquals(['a', 'b', 'c'], query.keys_())
query = Query("select a,b,c from bah", ())
self.assertEquals(['a', 'b', 'c'], query.keys_())
def test_fillone(self):
query = Query("select a,b,c from bah", ())
query.execute = Mock()
query.keys_ = Mock(return_value=('a', 'b', 'c'))
query.fetchone_ = Mock(return_value=('1', '2', '3'))
dummy = query.fillone(TestQuery.Dummy)
self.assertTrue(isinstance(dummy, TestQuery.Dummy))
self.assertTrue(1, dummy.a)
self.assertTrue(2, dummy.b)
self.assertTrue(3, dummy.c)
def test_fillall(self):
query = Query("select a,b,c from bah", ());
query.execute = Mock()
query.keys_ = Mock(return_value=('a', 'b'))
values = [('1', '2'),('3', '4'), None]
values.reverse()
query.fetchone_ = values.pop
dummies = query.fillall(TestQuery.Dummy)
self.assertEquals(2, len(dummies))
self.assertTrue(isinstance(dummies[0], TestQuery.Dummy))
self.assertTrue(1, dummies[0].a)
self.assertTrue(2, dummies[0].b)
self.assertTrue(isinstance(dummies[1], TestQuery.Dummy))
self.assertTrue(3, dummies[1].a)
self.assertTrue(4, dummies[1].b)
class TestPosition(unittest.TestCase):
def test_get_max(self):
position = Position.get_position('AAPL', '2010-07-06')
self.assertEquals(Decimal('150.3'), position.get_max())
def test_get_position(self):
position = Position.get_position('MIC-SDB.ST', '2010-09-09')
self.assertTrue(isinstance(position, Position))
self.assertEquals('MIC-SDB.ST', position.symbol)
self.assertEquals('2010-09-09', str(position.enter_date))
self.assertEquals('2010-09-29', str(position.exit_date))
self.assertEquals('SEK', position.currency)
self.assertEquals(Decimal('1'), position.currency_rate)
self.assertEquals(Decimal('719.50'), position.enter_price)
self.assertEquals(Decimal('647'), position.exit_price)
self.assertEquals(Decimal('99'), position.enter_commission)
self.assertEquals(Decimal('99'), position.exit_commission)
self.assertEquals(Decimal('30'), position.shares)
self.assertEquals(1, position.portfolio_id)
def test_open(self):
Position.open('AAPL', 'SEK', 1, '2001-02-03', 200, 99, 2000).save()
position = Position.get_position('AAPL', '2001-02-03')
self.assertEquals('AAPL', position.symbol)
self.assertEquals('2001-02-03', str(position.enter_date))
self.assertEquals(None, position.exit_date)
self.assertEquals('SEK', position.currency)
self.assertEquals(Decimal('1.0'), position.currency_rate)
self.assertEquals(Decimal('200'), position.enter_price)
self.assertEquals(None, position.exit_price)
self.assertEquals(Decimal('99'), position.enter_commission)
self.assertEquals(None, position.exit_commission)
self.assertEquals(Decimal('2000'), position.shares)
def test_get_net_gain(self):
position = Position({
'enter_price': Decimal('100'),
'exit_price': Decimal('110'),
'shares': 50,
'enter_commission': Decimal('5')})
self.assertEquals(Decimal('490'), position.get_net_gain())
def test_get_net_gain_with_price(self):
position = Position({
'enter_price': Decimal('100'),
'shares': 50,
'enter_commission': Decimal('5')})
self.assertEquals(Decimal('485'), position.get_net_gain(Decimal('110'), Decimal('10')))
def test_get_net_gain_with_exit_commission(self):
position = Position({
'enter_price': Decimal('100'),
'shares': 50,
'enter_commission': Decimal('5')})
self.assertEquals(Decimal('485'), position.get_net_gain(Decimal('110'), Decimal('10')))
@patch("dao.Indicator.get_indicator")
def test_get_enter_indicator(self, get_indicator):
position = Position({
'symbol': 'AAPL',
'enter_date': '2010-01-01'})
indicator = Indicator({})
get_indicator.return_value = indicator
self.assertEquals(indicator, position.get_enter_indicator())
get_indicator.assert_called_with('AAPL', '2010-01-01')
def test_get_stop(self):
position = Position({
'symbol': 'AAPL',
'enter_date': '2010-01-01',
'enter_price': Decimal('10')})
indicator = Indicator({})
indicator.calculate_stop = Mock(return_value = Decimal(10))
position.get_enter_indicator = Mock(return_value = indicator)
position.current_quote = Quote({})
position.current_quote.is_cash = Mock(return_value = False)
self.assertEqual(Decimal(10), position.get_stop())
indicator.calculate_stop.assert_called_with(Decimal('10'))
@patch("dao.Indicator.get_indicator")
def test_get_stop_for_cash(self, get_indicator):
position = Position({})
position.current_quote = Quote({})
position.current_quote.is_cash = Mock(return_value = True)
self.assertEqual('', position.get_stop())
def test_get_open_positions(self):
positions = Position.get_open_positions(1234)
self.assertFalse(positions)
positions = Position.get_open_positions(1)
self.assertTrue(len(positions)>0)
for position in positions:
self.assertTrue(isinstance(position, Position))
self.assertEquals(None, position.exit_date)
def test_get_gain(self):
position = Position({
'shares': 1000, 'enter_price': Decimal('10'),
'enter_commission': Decimal('99') })
position.current_quote = Quote({'close': Decimal('10')})
self.assertEquals(Decimal('-99'), position.get_gain())
position.current_quote = Quote({'close': Decimal('11')})
self.assertEquals(Decimal('901'), position.get_gain())
def test_get_risk(self):
position = Position({
'shares': 1000, 'enter_price': Decimal('10'),
'enter_commission': Decimal('99') })
position.get_stop = Mock(return_value= Decimal('8'))
self.assertEquals(2099, position.get_risk())
def test_get_rtr(self):
position = Position({
'shares': 1, 'enter_price': Decimal('9'),
'enter_commission': Decimal('1') })
position.get_stop = Mock(return_value= Decimal('8'))
position.current_quote = Quote({'close': Decimal('10')})
self.assertEquals(0, position.get_rtr())
position.current_quote = Quote({'close': Decimal('12')})
self.assertEquals(1, position.get_rtr())
position.current_quote = Quote({'close': Decimal('14')})
self.assertEquals(2, position.get_rtr())
@patch("dao.Currency.get_rate")
def test_get_value(self, get_rate):
position = Position({'shares': 10, 'currency': 'USD'})
get_rate.return_value = Decimal('7')
position.current_quote = Quote({'close': Decimal('10')})
self.assertEquals(700, position.get_value('SEK'))
def test_should_sell_when_below_stop(self):
position = Position({})
position.current_quote = Quote({'close': Decimal('10')})
position.get_trailing_stop = Mock(return_value = Decimal('11'))
self.assertTrue(position.should_sell())
def test_should_sell_not_when_above_stop(self):
position = Position({})
position.current_quote = Quote({'close': Decimal('10')})
position.get_trailing_stop = Mock(return_value = Decimal('9'))
self.assertFalse(position.should_sell())
def test_should_sell_when_equal_stop(self):
position = Position({})
position.current_quote = Quote({'close': Decimal('10')})
position.get_trailing_stop = Mock(return_value = Decimal('10'))
self.assertTrue(position.should_sell())
def test_get_trailing_stop(self):
position = Position({})
position.get_stop = Mock(return_value = Decimal('10'))
position.current_quote = Quote({})
indicator = Indicator({'ll_10': Decimal('20')})
position.current_quote.get_indicator = Mock(return_value = indicator)
self.assertEquals(Decimal('20'), position.get_trailing_stop())
def test_get_trailing_stop_when_ll_10_lower_than_stop(self):
position = Position({})
position.get_stop = Mock(return_value = Decimal('30'))
position.current_quote = Quote({})
indicator = Indicator({'ll_10': Decimal('20')})
position.current_quote.get_indicator = Mock(return_value = indicator)
self.assertEquals(Decimal('30'), position.get_trailing_stop())
def test_get_trailing_stop_when_no_indicator(self):
position = Position({})
position.get_stop = Mock(return_value = Decimal('10'))
position.current_quote = Quote({})
indicator = Indicator({'ll_10': Decimal('20')})
position.current_quote.get_indicator = Mock(return_value = None)
self.assertEquals('', position.get_trailing_stop())
def test_get_atr_trailing_stop(self):
position = Position({})
position.get_max = Mock(return_value = Decimal('10'))
quote = Quote({'symbol':'AAPL'})
position.current_quote = quote
quote.get_indicator = Mock(return_value =
Indicator({'atr_exp20': Decimal('1')}))
self.assertEquals(8, position.get_atr_trailing_stop())
@patch("dao.Indicator.calculate_stop")
def test_get_shares(self, calculate_stop):
quote = Quote({'close': Decimal('100') })
quote.get_indicator = Mock(return_value =
Indicator({}))
calculate_stop.return_value = Decimal('80')
# risk per share is 20
shares = Position.get_shares(quote, 2000)
self.assertEquals(100, shares)
class TestPortfolio(unittest.TestCase):
@patch("dao.Quote.get_latest_quote")
@patch("dao.Position.get_open_positions")
def test_get_portfolio(self, get_open_positions, get_latest_quote):
positions = [
Position({'symbol': 'AAPL'}),
Position({'symbol': 'LUPE.ST'})
]
get_open_positions.return_value = positions
quotes = [
Quote({'symbol': 'LUPE.ST'}),
Quote({'symbol': 'AAPL'})
]
def side_effect(*args, **kwargs):
return quotes.pop()
get_latest_quote.side_effect = side_effect
portfolio = Portfolio.get_portfolio(1)
self.assertTrue(isinstance(portfolio, Portfolio))
self.assertEquals(positions, portfolio.positions)
get_open_positions.assert_called_with(1)
for position in positions:
quote = position.current_quote
self.assertTrue(isinstance(quote, Quote))
self.assertEquals(quote.symbol, position.symbol)
def test_get_value(self):
positions = [
Position({'symbol': 'AAPL'}),
Position({'symbol': 'LUPE.ST'})
]
positions[0].get_value = Mock(return_value = Decimal('60'))
positions[1].get_value = Mock(return_value = Decimal('40'))
portfolio = Portfolio({'positions': positions, 'currency': 'EUR'})
self.assertEquals(100, portfolio.get_value())
positions[0].get_value.assert_called_with('EUR')
positions[1].get_value.assert_called_with('EUR')
def test_load(self):
portfolio = Portfolio.load(1)
self.assertEquals(1, portfolio.id)
self.assertEquals("Avanza", portfolio.name)
if __name__ == '__main__':
unittest.main()