-
Notifications
You must be signed in to change notification settings - Fork 19
/
Events.py
103 lines (84 loc) · 3.16 KB
/
Events.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
class Event(object):
"""
Event is base class providing an interface for all subsequent
(inherited) events, that will trigger further events in the
trading infrastructure.
"""
pass
class MarketEvent(Event):
"""
Handles the event of receiving a new market update with corresponding bars.
"""
def __init__(self):
"""
Initialises the MarketEvent.
"""
self.type = "MARKET"
class SignalEvent(Event):
"""
Signal event generated from a particular strategy, if signal met strategy conditions
Parameters:
symbol - The symbol for current asset.
datetime - A datetime at which the signal is generated.
signal_type - The signal type ('LONG', 'SHORT', 'EXIT')
strength - strength of the signal --> TODO: this should be given from a risk class when applying multiple strats
"""
def __init__(self, symbol, datetime, signal_type, strength):
self.type = "SIGNAL"
self.symbol = symbol
self.datetime = datetime
self.signal_type = signal_type
self.strength = strength
class OrderEvent(Event):
"""
Order event to be sent to a broker api. It takes into account the quantity,
type of ordering, and direction (long, short, exit...)
Parameters:
symbol - The symbol for current asset.
order_type - Whether is it a 'MARKET' or 'LIMIT' order
quantity --> TODO: this should be implemented in a risk class (Kelly Criterion, etc)
direction - 1 or -1 based on the type
"""
def __init__(self, symbol, order_type, quantity, direction):
self.type = "ORDER"
self.symbol = symbol
self.order_type = order_type
self.quantity = quantity
self.direction = direction
def print_order(self):
"""
Outputs the values within the Order.
"""
print("Order: Symbol=%s, Type=%s, Quantity=%s, Direction=%s") % \
(self.symbol, self.order_type, self.quantity, self.direction)
class FillEvent(Event):
"""
Fill event once an order based on the response from the broker
Parameters:
datetime - A datetime at which the signal is created.
symbol - The symbol for current asset.
exchange - The exchange, broker where the order is filled
quantity - quantity filled
direction
fill_cost - can contain commission already
commission - Defaulted to None if non specified
"""
def __init__(self, datetime, symbol, exchange, quantity, direction, fill_cost, commission=None):
self.type = "FILL"
self.datetime = datetime
self.symbol = symbol
self.exchange = exchange
self.quantity = quantity
self.direction = direction
self.fill_cost = fill_cost
# Calculate commission
if commission is None:
self.commission = self._calculate_commission()
else:
self.commission = commission
def _calculate_commission(self):
"""
TODO: Commission fees to be implemented
"""
# between 1 and 2%
return max(1.5, 0.015 * self.quantity)