forked from evermarkets/market-maker-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
order_state.py
163 lines (134 loc) · 4.24 KB
/
order_state.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
import time
from enum import Enum, auto
class event(Enum):
on_creation = auto()
on_insert_ack = auto()
on_insert_nack = auto()
on_cancel = auto()
on_fill = auto()
on_full_fill = auto()
on_cancel_ack = auto()
on_cancel_nack = auto()
on_amend = auto()
on_amend_ack = auto()
on_amend_partial_ack = auto()
on_amend_nack = auto()
class state:
def on_event(self, event):
pass
def __repr__(self):
return self.__str__()
def __str__(self):
return self.__class__.__name__
class inactive(state):
def on_event(self, event):
if event == event.on_creation:
return insert_pending()
raise StateException(
"invalid event for the current state. event = {}, state = {}".format(event, self.__class__.__name__))
return self
class insert_pending(state):
def on_event(self, event):
if event == event.on_insert_nack:
return insert_failed()
elif event == event.on_cancel:
return cancel_pending()
elif event == event.on_insert_ack:
return active()
elif event == event.on_cancel_ack:
return cancelled()
elif event == event.on_fill:
return fill()
elif event == event.on_full_fill:
return full_fill()
return self
class active(state):
def on_event(self, event):
if event == event.on_fill:
return fill()
elif event == event.on_insert_nack:
return insert_failed()
elif event == event.on_cancel:
return cancel_pending()
elif event == event.on_amend:
return amend_pending()
elif event == event.on_fill:
return fill()
elif event == event.on_full_fill:
return full_fill()
elif event == event.on_amend_nack:
return inactive()
return self
class amend_pending(state):
def on_event(self, event):
if event == event.on_cancel:
return cancel_pending()
elif event == event.on_amend_ack:
return active()
elif event == event.on_amend_partial_ack:
return active()
elif event == event.on_amend_nack:
return inactive()
elif event == event.on_fill:
return fill()
elif event == event.on_full_fill:
return full_fill()
elif event == event.on_cancel_ack:
return cancelled()
return self
class cancelled(state):
def on_event(self, event):
if event == event.on_creation:
return insert_pending()
return self
class insert_failed(state):
def on_event(self, event):
return self
class fill(state):
def on_event(self, event):
if event == event.on_full_fill:
return full_fill()
elif event == event.on_cancel:
return cancel_pending()
elif event == event.on_amend:
return amend_pending()
elif event == event.on_cancel:
return cancel_pending()
elif event == event.on_cancel_ack:
return cancelled()
return self
class full_fill(state):
def on_event(self, event):
if event == event.on_cancel:
return cancel_pending()
elif event == event.on_fill:
return fill()
elif event == event.on_amend_partial_ack:
return fill()
elif event == event.on_creation:
return insert_pending()
return self
class cancel_pending(state):
def on_event(self, event):
if event == event.on_fill:
return fill()
elif event == event.on_cancel_ack:
return cancelled()
elif event == event.on_cancel_nack:
return cancel_failed()
return self
class cancel_failed(state):
def on_event(self, event):
if event == event.on_fill:
return fill()
elif event == event.on_full_fill:
return full_fill()
return self
class order_state:
def __init__(self):
self.order_id = None
self.state = inactive()
self.last_update_timestamp = None
def on_event(self, event):
self.state = self.state.on_event(event)
self.last_update_timestamp = time.time()