-
Notifications
You must be signed in to change notification settings - Fork 2
/
executor_test.py
131 lines (115 loc) · 4.55 KB
/
executor_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
import unittest
from executor import *
from trade import *
from position import *
from order_type import *
from position_manager import PositionManager, PositionDBManager
from model import db as db
class ExecutorTest(unittest.TestCase):
def getExecutorMock(self):
return ExecutorMock(crypto=10.0, fiat=1000.0, market="BTC/EUR", ctySize = 1)
def getExecutorMockWithPM(self):
#pm = PositionDBManager(db)
pm = PositionManager()
e = self.getExecutorMock()
e.setPositionManager(pm)
return e, pm
def test_buy_trade(self):
e = self.getExecutorMock()
t = e.buy(1000)
self.assertEqual(t.getType(), OrderType.BUY)
self.assertEqual(t.getCty(), 1)
self.assertEqual(t.getPrice(), 1000)
self.assertEqual(t.getFee(), e.getFee()*1000)
def test_sell_trade(self):
e = self.getExecutorMock()
t = e.sell(1000)
self.assertEqual(t.getType(), OrderType.SELL)
self.assertEqual(t.getCty(), 1)
self.assertEqual(t.getPrice(), 1000)
self.assertEqual(t.getFee(), e.getFee()*1000)
def test_buy(self):
e = self.getExecutorMock()
e.buy(1000)
self.assertEqual(e.getAvailableFiat(), -e.getFee()*1000)
self.assertEqual(e.getAvailableCrypto(), 11)
def test_sell(self):
e = self.getExecutorMock()
e.sell(1000)
self.assertEqual(e.getAvailableFiat(), 2000-e.getFee()*1000)
self.assertEqual(e.getAvailableCrypto(), 9)
def test_buy_sell(self):
e = self.getExecutorMock()
e.buy(1000)
e.sell(1000)
e.buy(1000)
self.assertEqual(e.getAvailableFiat(), -3*e.getFee()*1000)
self.assertEqual(e.getAvailableCrypto(), 11)
def test_position_buy(self):
e, pm = self.getExecutorMockWithPM()
e.buyCty(1, 1000)
ps = pm.getPositions(OrderType.BUY)
self.assertEqual(len(ps), 1)
p = ps[0]
self.assertEqual(p.getType(), OrderType.BUY)
self.assertEqual(p.getCty(), 1)
self.assertEqual(p.getPrice(), 1000)
def test_position_sell(self):
e, pm = self.getExecutorMockWithPM()
t = e.sellCty(1, 1000)
ps = pm.getPositions(t.getType())
self.assertEqual(len(ps), 1)
p = ps[0]
self.assertEqual(p.getType(), OrderType.SELL)
self.assertEqual(p.getCty(), 1)
self.assertEqual(p.getPrice(), 1000)
def test_position_buy_sell(self):
e, pm = self.getExecutorMockWithPM()
e.buyCty(1, 1000)
e.sellCty(1, 1000)
e.sellCty(1, 1000)
e.buyCty(1, 1000)
self.assertEqual(len(pm.getPositions(OrderType.BUY)), 0)
self.assertEqual(len(pm.getPositions(OrderType.SELL)), 0)
def test_position_buy_partially(self):
e, pm = self.getExecutorMockWithPM()
e.sellCty(2, 1000)
e.buyCty(1, 1000)
self.assertEqual(len(pm.getPositions(OrderType.SELL)), 1)
self.assertEqual(len(pm.getPositions(OrderType.BUY)), 0)
p = pm.getPositions(OrderType.SELL)[0]
self.assertEqual(p.getType(), OrderType.SELL)
self.assertEqual(p.getCty(), 1)
self.assertEqual(p.getPrice(), 1000)
def test_position_sell_partially(self):
e, pm = self.getExecutorMockWithPM()
e.buyCty(2, 1000)
e.sellCty(1, 1000)
self.assertEqual(len(pm.getPositions(OrderType.BUY)), 1)
self.assertEqual(len(pm.getPositions(OrderType.SELL)), 0)
p = pm.getPositions(OrderType.BUY)[0]
self.assertEqual(p.getType(), OrderType.BUY)
self.assertEqual(p.getCty(), 1)
self.assertEqual(p.getPrice(), 1000)
def test_position_close_and_buy(self):
e, pm = self.getExecutorMockWithPM()
e.sellCty(1, 1000)
e.buyCty(2, 1000)
self.assertEqual(len(pm.getPositions(OrderType.SELL)), 0)
self.assertEqual(len(pm.getPositions(OrderType.BUY)), 1)
p = pm.getPositions(OrderType.BUY)[0]
self.assertEqual(p.getType(), OrderType.BUY)
self.assertEqual(p.getCty(), 1)
self.assertEqual(p.getPrice(), 1000)
def test_position_close_and_sell(self):
e, pm = self.getExecutorMockWithPM()
e.buyCty(1, 1000)
e.sellCty(2, 1000)
self.assertEqual(len(pm.getPositions(OrderType.BUY)), 0)
self.assertEqual(len(pm.getPositions(OrderType.SELL)), 1)
p = pm.getPositions(OrderType.SELL)[0]
self.assertEqual(p.getType(), OrderType.SELL)
self.assertEqual(p.getCty(), 1)
self.assertEqual(p.getPrice(), 1000)
if __name__ == '__main__':
unittest.main()