-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessaging-example.py
46 lines (41 loc) · 1.86 KB
/
messaging-example.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
from orderbook import OrderBook
from ordertree import OrderTree
from orderlist import OrderList
from order import Order
import random
import sys
def main():
# TODO: Create three agents:
order_book = OrderBook()
agents = [101, 102, 103]
# TODO: Agents send in buy/sell orders randomly with some probablility
for i in range(30):
print '\nTimestep# {}: '.format(i)
for a in agents:
if (random.random() > 0.5):
# make an order
# buy or sell?
# TO BE REPLACED WITH TRADING STRATEGIES
tradedir = random.choice(['bid','ask'])
# how many?
ordqty = random.randint(1,20)
# market or limit?
tradetype = random.choice(['market', 'limit'])
# if limit, what is your bid/ask price
if tradetype == 'limit':
# TO BE REPLACED WITH TRADING STRATEGIES
limitprice = random.uniform(90,100)
# send in the order
if tradetype == 'market':
agent_order = {'type':'market', 'side':tradedir, 'quantity':ordqty, 'trade_id':a}
print 'agent {} made a market order to {} {} shares'.format(a, tradedir, ordqty)
else:
agent_order = {'type':'limit', 'side':tradedir, 'quantity':ordqty, 'price':limitprice, 'trade_id':a}
print 'agent {} made a limit order at {} to {} {} shares'.format(a, limitprice, tradedir, ordqty)
trades, order_in_book = order_book.process_order(agent_order, False, False)
else:
print 'agent {} did not trade'.format(a)
# plot how the stock changes
print order_book
if __name__ == "__main__":
sys.exit(int(main() or 0))