forked from market-microstructure/simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Level 3 Simulation for smart order routing v.1.0
- Loading branch information
Silviu VLASCEANU
committed
Feb 8, 2014
1 parent
583ffd5
commit 7688539
Showing
24 changed files
with
538 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.