Skip to content

Commit

Permalink
Level 3 Simulation for smart order routing v.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Silviu VLASCEANU committed Feb 8, 2014
1 parent 583ffd5 commit 7688539
Show file tree
Hide file tree
Showing 24 changed files with 538 additions and 5 deletions.
40 changes: 40 additions & 0 deletions agent/SORAgent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'''
Created on Feb 8, 2014
@author: Silver
'''

from objects.Order import *

class SORAgent():
def __init__(self, service_locator, agent_id):
self.services = service_locator
self.agent_id = agent_id

def process(self, symbol):
data = self.services.bus.get_market_data()
self.services.logger.info("Market data received %s" % (data))

self.services.logger.info("Sending Order Now:")
o = Order()
o.side = 1
o.price = data["Euronext"]["ask-1"]["price"]
o.size = 1000000
o.leaves = o.size
o.symbol = "Euronext"
o.id = "MyOrder"
o.timeinforce = "ioc"
o.parent = self.agent_id

self.services.order_dispatcher.new_order(o)

def process_report(self, id):
execs = self.services.bus.get_executions()[id]
self.services.logger.info("execution!!!!!!!!!!!!" + str(id) + " number: " + str(len(execs)))
for e in execs:
self.services.logger.info( e)


def process_reject(self, order_id):
reject = self.services.bus.get_rejects()[order_id]
self.services.logger.info("reject: %s. RemainingQty: %d" % (order_id, reject.leaves))
Binary file added agent/SORAgent.pyc
Binary file not shown.
32 changes: 32 additions & 0 deletions agent/ZeroIntelligenceAgent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'''
Created on Jan 29, 2014
@author: Silver
'''
from objects.Order import *
from utils.generate_snapshots_SOR import generate_snapshot

class ZeroIntelligenceAgent():
def __init__(self, service_locator, agent_id):
self.services = service_locator
self.orderbooks = generate_snapshot(1, 0)
self.agent_id = agent_id

self.sent_orders = False

def process(self, symbol):
if not self.sent_orders:
self.sent_orders = True
for venue in self.orderbooks:
for price in self.orderbooks[venue]:
for o in self.orderbooks[venue][price]:
o.parent = self.agent_id
self.services.order_dispatcher.new_order(o)

def process_report(self, order_id):
executions = self.services.bus.get_executions()[order_id]
for e in executions:
self.services.publisher.publish("Execution", {"venue": e.symbol, "price": e.price, "size": e.size})

def process_reject(self, order_id):
pass
Binary file added agent/ZeroIntelligenceAgent.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions bus/Bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def __init__(self):
self._private_data_update = {}

self._last_execution = {}
self._last_reject = {}

self._indicators = {}

Expand Down
97 changes: 97 additions & 0 deletions bus/ZIBus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
'''
Created on Feb 8, 2014
@author: Silver
'''
from datetime import datetime

class ZIBus(object):
'''
classdocs
'''
def __init__(self):
'''
Constructor
'''
self._public_data = {}
self._private_data_buy = {}
self._private_data_sell = {}

self._last_execution = {}
self._last_reject = {}

self._new_order = None

self._indicators = {}

def __setitem__(self, key, value):
self.data[key] = value

def get_market_data(self):

snapshot = {"time": datetime.now(),
"event": "lob"
}

for symbol, ob in self._private_data_buy.iteritems():
if not snapshot.has_key(symbol):
snapshot[symbol] = {}
bid_prices = sorted(ob.keys(), reverse = True)
for i in range(len(bid_prices)):
price = bid_prices[i]
snapshot[symbol]["bid-%d" % i] = {"price": price, "size": sum([o.shown for o in ob[price]])}
i += 1

for symbol, ob in self._private_data_sell.iteritems():
if not snapshot.has_key(symbol):
snapshot[symbol] = {}

ask_prices = sorted(ob.keys())
for i in range(len(ask_prices)):
price = ask_prices[i]
snapshot[symbol]["ask-%d" % i] = {"price": price, "size": sum([o.shown for o in ob[price]])}

return snapshot


def get_executions(self):
return self._last_execution

def get_rejects(self):
return self._last_reject

def get_indicators(self):
return self._indicators

def inject_snapshot(self, symbol, snapshot):
self._public_data_update[symbol] = snapshot

def insert_new_order(self, order):
self._new_order = order

def push_private_update(self, id):
order = self._new_order
if order == None:
return

side = order.side
symbol = order.symbol
price = order.price
if side == 1:
if not self._private_data_buy.has_key(symbol):
self._private_data_buy[symbol] = {}

if not self._private_data_buy[symbol].has_key(price):
self._private_data_buy[symbol][price] = []

self._private_data_buy[symbol][price].append(order)
elif side == -1:
if not self._private_data_sell.has_key(symbol):
self._private_data_sell[symbol] = {}

if not self._private_data_sell[symbol].has_key(price):
self._private_data_sell[symbol][price] = []

self._private_data_sell[symbol][price].append(order)


Binary file added bus/ZIBus.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions events/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"AckNew" : Event("AckNew"),
"NackNew" : Event("NackNew"),
"Execution" : Event("Execution"),
"Reject" : Event("Reject"),
"SnapshotInject" : Event("SimulatorInject"),
"ValidNewOrder" : Event("ValidNewOrder"),
"ValidModifyOrder" : Event("ValidModifyOrder"),
Expand Down
Binary file modified events/signals.pyc
Binary file not shown.
16 changes: 16 additions & 0 deletions injecter/ZIInjecter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@


import pandas as pd
from scheduler.ServiceLocator import ServiceLocator
import logging

class ZIInjecter():
def __init__(self, service_locator, filename):
self.services = service_locator
self.filename = filename
self.symbol = "a"

def main_loop(self, parameter):
self.services.events['SnapshotInject'].emit(self.symbol)


Binary file added injecter/ZIInjecter.pyc
Binary file not shown.
30 changes: 30 additions & 0 deletions matching_engine/ZeroIntelligenceMatchingEngine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'''
Created on Feb 8, 2014
@author: Silver
'''

from matching_engine.MatchingEngine import MatchingEngine

class ZeroIntelligenceMatchingEngine(MatchingEngine):
def __init__(self, service_locator):
MatchingEngine.__init__(self, service_locator)

def on_inject(self, symbol):
#self.services.bus.push_public_update(symbol)
#matching goes here
self.services.events['MarketData'].emit(symbol)

def on_new_order(self, id):
self.services.events['AckNew'].emit(id)

# push order on the market
self.services.bus.push_private_update(id)


def inject_modify_order(self, symbol, id, order):
pass

def inject_cancel_order(self, symbol, id):
pass

Binary file not shown.
22 changes: 22 additions & 0 deletions objects/AgentFactory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'''
Created on Feb 8, 2014
@author: Silver
'''

class AgentFactory():
def __init__(self, service_locator):
self.services = service_locator
self.agents = {}

def add_agent(self, agent_id, agent):
self.agents[agent_id] = agent

def process_report(self, order_id):
parent = self.services.bus.get_executions()[order_id][0].parent
self.agents[parent].process_report(order_id)

def process_reject(self, order_id):
parent = self.services.bus.get_rejects()[order_id].parent
self.agents[parent].process_reject(order_id)

Binary file added objects/AgentFactory.pyc
Binary file not shown.
16 changes: 11 additions & 5 deletions objects/Order.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ def __init__(self):
self.size = 0
self.price = 0
self.symbol = ""
self.leaves = "unset"
self.timeinforce = ""
self.leaves = 0
self.timeinforce = "day"
self.id = ""
self.executed = 0
self.side = 0
self.shown = 0
self.decay = 1
self.parent = ""

def __str__(self):
side = "U"
if self.side == 1: side = "B"
if self.side == -1: side = "S"
if self.side == 1: side = "Buy"
if self.side == -1: side = "Sell"
return "%s :: %s (%d / %d @ %f | %s) - %s" % (self.id, side, self.leaves, self.size, self.price, self.symbol, self.timeinforce)

class ExecutionReport():
Expand All @@ -29,7 +32,8 @@ def __init__(self):
self.price = 0
self.symbol = ""
self.leaves = self.size
self.id = ""
self.id = ""
self.parent = ""

def __str__(self):
return "Exec %s :: (%d @ %f | %s)" % (self.id, self.size, self.price, self.symbol)
Expand All @@ -42,7 +46,9 @@ def new_order(self, order):
if order.size == 0 : logging.warning("Order [%s]: size equals zero, unable to send order!" % order); return
if order.id == "": logging.warning("Order [%s]: has no id, unable to send order!" % order); return
if order.symbol == "": logging.warning("Order [%s]: has no symbol, unable to send order!" % order); return
order.leaves = order.size

logging.debug("Inserting Order [%s]" % order)
self.services.bus.insert_new_order(order)
self.services.events["ValidNewOrder"].emit(order.id)

Expand Down
Binary file modified objects/Order.pyc
Binary file not shown.
Loading

0 comments on commit 7688539

Please sign in to comment.