-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathExecution.py
55 lines (42 loc) · 1.59 KB
/
Execution.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
from abc import ABCMeta, abstractmethod
from Events import FillEvent, OrderEvent
from datetime import datetime
class ExecutionHandler(object):
"""
Execution handler class to handle Order and Fill events
for different types of APIs (brokers, exchanges), or protocols such as FIX
"""
__metaclass__ = ABCMeta
@abstractmethod
def execute_order(self, event):
"""
Takes an Order event and executes it, producing
a Fill event that gets placed onto the Events queue.
Parameters:
event - Contains an Event object with order information.
"""
raise NotImplementedError("Should implement execute_order()")
class SimpleSimulatedExecutionHandler(ExecutionHandler):
"""
Simple handler with no latency or slippage modelling
"""
def __init__(self, events):
"""
Initialises the handler, setting the event queues
up internally.
Parameters:
events - The Queue of Event objects.
"""
self.events = events
def execute_order(self, event):
"""
Order event converted to Fill event to
execute the order on "live" broker. The event is
then added to the queue
Parameters:
event - Contains an Event object with order information.
"""
if isinstance(event, OrderEvent):
fill_event = FillEvent(datetime.utcnow(), event.symbol, "FAKE_EXCHANGE", event.quantity, event.direction,
None)
self.events.put(fill_event)