-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.py
202 lines (163 loc) · 5.49 KB
/
event.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
"""
The "language" used between components
"""
from abc import ABC
class EventBase(ABC):
"""
Event base class, has type
A object to carry information between different engines
Attributes:
type : str
market, signal, order, filled
date : str
when this event is created (str)
"""
def __init__(self):
"""
Initialize a Event class with type and date field
"""
super().__init__()
self.type = 'None'
self.date = ''
def get_type(self):
"""
returns the type of this event
Returns
-------
type: str, the type of this event
"""
return self.type
class EventMarket(EventBase):
"""
EventMarket, an event containing market information
All daily data are stored as dictionary of dictionary, first dict by contract second by o,h,l,c,etc
Attributes:
type : str
market, signal, order, filled
date : str
when this event is created (str)
data : dict
key is product id, value is an inner dict with key o,h,l,c,etc
"""
def __init__(self):
super().__init__()
self.type = 'Market'
self.date = ''
self.data = {}
def write_date(self, date: str) -> None:
"""write_date writes date to object
Parameters
----------
date : str
date (string)
"""
self.date = date
def write_data(self, contract: str, dict_of_data: dict) -> None:
"""
write_data writes the price volume data of a contract into this event
Parameters
----------
contract : str
the contract name
dict_of_data : dict
the actual data
"""
self.data[contract] = dict_of_data
class EventSignal(EventBase):
"""
EventSignal, a event for strategy engine to send out buy/sell, open/close position signal for ALL contracts
Attributes:
type : str
market, signal, order, filled
date : str
when this event is created (str)
data : dict
key is product id, value is a list of integer representing corresonding orders
"""
def __init__(self):
"""
__init__ intialize a signal event
"""
super().__init__()
self.type = 'Signal'
self.date = ''
self.data = {}
def write_data(self, contract: str, signal: list) -> None:
"""
write_data writes the signal and corresponding contract to data
Parameters
----------
contract : str
contract name
signal : list
name of the signal, 0: close, 1: long, -1: short
Note: current implementation of strategy class returns at most ONE signal per event, but in theory there can be multiple signals.
Dealing with multiple/conflicting signals can be done either here or at strategy or at portfolio.
"""
self.data[contract] = signal
class EventOrder(EventBase):
"""
EventOrder contains order specification from portfolio to executer
Attributes:
type : str
market, signal, order, filled
data : dict
key is product id, value is a list of tuples (action, amount).
"""
def __init__(self):
super().__init__()
self.type = 'Order'
self.data = {}
def write_data(self, contract: str, action: str, amount: int) -> None:
"""
write_data writes the action and corresponding amount of action to the event object
Parameters
----------
contract : str
name of contract
action : str
what action to be performed
amount : int
how much this action need to be performed
Note : for calculation efficiency, amount field carries cash value when it is a open position order, it carries position value when it is a closing order
"""
self.data[contract] = (action, amount)
class EventOrderInterDay(EventOrder):
def __init__(self):
super().__init__()
self.price = {}
def write_price(self, contract, price: int) -> None:
self.price[contract] = price
class EventFilled(EventOrder):
"""
EventFilled contains the confirmation a certain order has been executed, it is used to update
the portfolio class's position
It can also be captured by a seperate logging engine to be logged
Attributes:
type : str
market, signal, order, filled
date : str
when this event is created (str)
data : dict
key is product id, value is a list of tuples (action, amount, cash).
"""
def __init__(self):
super().__init__()
self.type = 'Fill'
self.data = {}
self.date = ''
def write_data(self, contract: str, action: str, amount: int, cash: int) -> None:
"""
write_data writes the action and corresponding amount of action to the event object
Parameters
----------
contract : str
name of contract
action : str
what action to be performed
amount : int
how much this action need to be performed. For calculation efficiency, all amount are values multiplied by the "multipliers".
cash : int
the cost/ revenue of buy/sell given amount asset
"""
self.data[contract] = (action, amount, cash)