Skip to content

Commit

Permalink
level 2 market data, unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Silviu VLASCEANU committed Nov 3, 2013
1 parent 3dfeb37 commit 583ffd5
Show file tree
Hide file tree
Showing 42 changed files with 8,599 additions and 51 deletions.
27 changes: 20 additions & 7 deletions agent/DummyAgent.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,35 @@ def __init__(self, service_locator):
self.services = service_locator
self.x = 0

def process(self, symbol):
self.services.logger.info("Received Market Data Event on symbol %s" % symbol)
def process(self, symbol):
data = self.services.bus.get_market_data()
self.services.logger.info("BID: %d, ASK: %d, PRICE: %d, SIZE: %d" % (data[symbol]["bid"], data[symbol]["ask"], data[symbol]["price"], data[symbol]["size"]))
self.services.logger.info("Market data received %s" % (data))

if self.x == 0:
if self.x >= 0:
self.services.logger.info("Sending Order Now:")
o = Order()
o.side = 1
o.price = 16
o.size = 50
o.price = data[symbol]["ask-1"]["price"]
o.size = 5000000
o.leaves = o.size
o.symbol = symbol
o.id = "MyOrder"

self.services.order_dispatcher.new_order(o)

self.x += 1
print "!!!!!!!!!!!!!!!!!!! ", data["a"]["event"]
if data["a"]["event"] == "trade":
self.services.publisher.publish("DummyAgent", {"bid": data[symbol]["bid-0"]["price"],
"ask": data[symbol]["ask-0"]["price"],
"bid_size": data[symbol]["bid-0"]["size"],
"ask_size": data[symbol]["ask-0"]["size"],
"price": data[symbol]["last"]["price"],
"quantity": data[symbol]["last"]["quantity"],
"time": data[symbol]["time"]})

def process_report(self, id):
print "execution!!!!!!!!!!!!"
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)
Binary file modified agent/DummyAgent.pyc
Binary file not shown.
Binary file modified agent/__init__.pyc
Binary file not shown.
13 changes: 11 additions & 2 deletions bus/Bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def __init__(self):

self._last_execution = {}


self._indicators = {}

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

Expand All @@ -29,6 +30,12 @@ def get_market_data(self):

def get_orderbook(self):
return self._private_data

def get_executions(self):
return self._last_execution

def get_indicators(self):
return self._indicators

def inject_snapshot(self, symbol, snapshot):
self._public_data_update[symbol] = snapshot
Expand All @@ -38,7 +45,9 @@ def insert_new_order(self, order):
self._private_data_update[order.id] = order

def push_public_update(self, symbol):
self._public_data[symbol] = self._public_data_update[symbol]
if not self._public_data.has_key(symbol):
self._public_data[symbol] = {}
self._public_data[symbol].update(self._public_data_update[symbol])
del self._public_data_update[symbol]

def push_private_update(self, id):
Expand Down
Binary file modified bus/Bus.pyc
Binary file not shown.
Binary file modified bus/__init__.pyc
Binary file not shown.
Binary file modified events/__init__.pyc
Binary file not shown.
Binary file modified events/event.pyc
Binary file not shown.
Binary file modified events/idle_queue.pyc
Binary file not shown.
Binary file modified events/signals.pyc
Binary file not shown.
Binary file modified events/weak_ref.pyc
Binary file not shown.
Binary file modified injecter/Level1FastInjecter.pyc
Binary file not shown.
71 changes: 71 additions & 0 deletions injecter/Level2HDFInjecter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'''
Created on Nov 1, 2013
@author: Silver
'''
import pandas as pd
from datetime import datetime, timedelta

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

def main_loop(self, parameter):
data = pd.HDFStore(self.filename, "r")
orderbooks = data.get("/df_obs")
trades = data.get("/df_trades")

i_ob = 0
i_tr = 0

while i_ob < len(orderbooks) or i_tr < len(trades):
# decide which event to insert
if i_tr >= len(trades) or orderbooks.ix[i_ob]["ts_recv"] <= trades.ix[i_tr]["ts_market"]:
row = orderbooks.ix[i_ob]
i_ob += 1

if row["Pbid 1"] == 0 or row["Pask 1"] == 0: continue

snapshot = {"time": row["ts_recv"],
"bid-0": {"price": row["Pbid 1"], "size": row["Qbid 1"]},
"bid-1": {"price": row["Pbid 2"], "size": row["Qbid 2"]},
"bid-2": {"price": row["Pbid 3"], "size": row["Qbid 3"]},
"bid-3": {"price": row["Pbid 4"], "size": row["Qbid 4"]},
"bid-4": {"price": row["Pbid 5"], "size": row["Qbid 5"]},

"ask-0": {"price": row["Pask 1"], "size": row["Qask 1"]},
"ask-1": {"price": row["Pask 2"], "size": row["Qask 2"]},
"ask-2": {"price": row["Pask 3"], "size": row["Qask 3"]},
"ask-3": {"price": row["Pask 4"], "size": row["Qask 4"]},
"ask-4": {"price": row["Pask 5"], "size": row["Qask 5"]},
"depth": 5,
"event": "lob"
}

self.services.logger.info("injecting orderbook row: %s" % snapshot)
self.services.bus.inject_snapshot(self.symbol, snapshot)
self.services.events['SnapshotInject'].emit(self.symbol)

else:
row = trades.ix[i_tr]
i_tr += 1
if row["qty"] == 0: continue;

snapshot = {"time": row["ts_market"],
"last": {"price": row["price"], "quantity": row["qty"], "exchange": row["source"], "side": row["side"], "type": row["type"]},
"stats": {"volume": row["volume"], "vwap": row["vwap"]},
"event": "trade"
}

self.services.logger.info("injecting trades row: %s" % snapshot)
self.services.bus.inject_snapshot(self.symbol, snapshot)
self.services.events['SnapshotInject'].emit(self.symbol)







Binary file added injecter/Level2HDFInjecter.pyc
Binary file not shown.
Binary file modified injecter/__init__.pyc
Binary file not shown.
Binary file added input_files/nestle_2013_10_21.h5
Binary file not shown.
Binary file added input_files/test_trades.h5
Binary file not shown.
19 changes: 0 additions & 19 deletions matching_engine/Level1MatchingEngine.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,6 @@ def on_new_order(self, id):

# add rejection logic here if needed
self.services.events['AckNew'].emit(id)

# immediate execution logic
executions = self.services.rulebook.match(id = id)
if executions:
# push execution in the bus
self.services.bus._last_execution[id] = executions

# update the order
for e in executions:
self.services.bus.get_orderbook()[id].leaves -= e.size
self.services.bus.get_orderbook()[id].executed += e.size
if self.services.bus.get_orderbook()[id].leaves <= 0:
del self.services.bus.get_orderbook()[id]
break

self.services.events['Execution'].emit(id)




def inject_modify_order(self, symbol, id, order):
pass
Expand Down
Binary file modified matching_engine/Level1MatchingEngine.pyc
Binary file not shown.
Binary file modified matching_engine/MatchingEngine.pyc
Binary file not shown.
Binary file modified matching_engine/__init__.pyc
Binary file not shown.
5 changes: 5 additions & 0 deletions objects/MarketManager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'''
Created on Nov 1, 2013
@author: Silver
'''
5 changes: 5 additions & 0 deletions objects/MoneyManager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'''
Created on Nov 1, 2013
@author: Silver
'''
2 changes: 1 addition & 1 deletion objects/Order.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self):
self.size = 0
self.price = 0
self.symbol = ""
self.leaves = self.size
self.leaves = "unset"
self.timeinforce = ""
self.id = ""
self.executed = 0
Expand Down
Binary file modified objects/Order.pyc
Binary file not shown.
Binary file modified objects/__init__.pyc
Binary file not shown.
Loading

0 comments on commit 583ffd5

Please sign in to comment.