-
Notifications
You must be signed in to change notification settings - Fork 2
/
trading_sandbox.py
121 lines (105 loc) · 3.86 KB
/
trading_sandbox.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import sys
import time
import socket
from multiprocessing.pool import ThreadPool
from loguru import logger
from pymongo_inmemory import MongoClient
from pytrading import random_string
from pytrading import MongoStorage
from pytrading import Buy, Sell
from pytrading import TradingServer
from pytrading import TradingClient
from pytrading import ProtobufSerialization
class AbstractTrader(TradingClient):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.order_to_send = 1
self.way = None
def main_loop_hook(self):
if self.order_to_send:
self.ordersender.push_order(
way=self.way,
price=42.0,
quantity=1.0,
instrument_identifier=1
)
self.order_to_send -= 1
class BuyTrader(AbstractTrader):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.way = Buy()
class SellTrader(AbstractTrader):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.way = Sell()
class TradingSandbox:
"""
Start a server and two clients (one selling, the other buying)
"""
def __init__(self):
self.serializer = ProtobufSerialization
def start_all_components(self):
thread_pool = ThreadPool(processes=3)
async_server_result = thread_pool.apply_async(self.start_server)
liquidity_provider_result = thread_pool.apply_async(self.start_selling)
liquidity_taker_result = thread_pool.apply_async(self.start_buying)
async_server_result.get()
liquidity_taker_result.get()
liquidity_provider_result.get()
def start_server(self):
"""
Using in-memory MongoDB client
"""
try:
db = MongoStorage(client=MongoClient())
server = TradingServer(
storage=db,
client_authentication=False,
marshaller=self.serializer(),
feeder_port=60000,
matching_engine_port=60001,
uptime_in_seconds=3
)
server.start()
except Exception as exception:
logger.exception(exception)
def start_selling(self):
try:
# Let the server starts properly...
time.sleep(1)
client = BuyTrader(login="HedgeFund",
password=random_string(length=5),
marshaller=self.serializer(),
host=socket.gethostbyname(socket.gethostname()),
feeder_port=60000,
matching_engine_port=60001,
uptime_in_seconds=3.0)
client.start([client.feedhandler, client.ordersender])
except Exception as exception:
logger.exception(exception)
return exception
return None
def start_buying(self):
try:
# Let the server starts properly...
time.sleep(1)
client = SellTrader(login="Bank",
password=random_string(length=5),
marshaller=self.serializer(),
host=socket.gethostbyname(socket.gethostname()),
feeder_port=60000,
matching_engine_port=60001,
uptime_in_seconds=3.0)
client.start([client.feedhandler, client.ordersender])
except Exception as exception:
logger.exception(exception)
if __name__ == '__main__':
logger.remove()
logger.add(
sink=sys.stdout,
format="{time:HH:mm:ss} | {thread} | {level} | {message}",
level="DEBUG",
colorize=True,
)
sandbox = TradingSandbox()
sandbox.start_all_components()