-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_trader.py
98 lines (82 loc) · 3.8 KB
/
example_trader.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
from datamodel import OrderDepth, UserId, TradingState, Order, ProsperityEncoder, Symbol, Trade
from typing import List
import json
from typing import Any
class Logger:
def __init__(self) -> None:
self.logs = ""
def print(self, *objects: Any, sep: str = " ", end: str = "\n") -> None:
self.logs += sep.join(map(str, objects)) + end
def flush(self, state: TradingState, orders: dict[Symbol, list[Order]]) -> None:
print(json.dumps({
"state": self.compress_state(state),
"orders": self.compress_orders(orders),
"logs": self.logs,
}, cls=ProsperityEncoder, separators=(",", ":"), sort_keys=True))
self.logs = ""
def compress_state(self, state: TradingState) -> dict[str, Any]:
listings = []
for listing in state.listings.values():
listings.append([listing["symbol"], listing["product"], listing["denomination"]])
order_depths = {}
for symbol, order_depth in state.order_depths.items():
order_depths[symbol] = [order_depth.buy_orders, order_depth.sell_orders]
return {
"t": state.timestamp,
"l": listings,
"od": order_depths,
"ot": self.compress_trades(state.own_trades),
"mt": self.compress_trades(state.market_trades),
"p": state.position,
"o": state.observations,
}
def compress_trades(self, trades: dict[Symbol, list[Trade]]) -> list[list[Any]]:
compressed = []
for arr in trades.values():
for trade in arr:
compressed.append([
trade.symbol,
trade.buyer,
trade.seller,
trade.price,
trade.quantity,
trade.timestamp,
])
return compressed
def compress_orders(self, orders: dict[Symbol, list[Order]]) -> list[list[Any]]:
compressed = []
for arr in orders.values():
for order in arr:
compressed.append([order.symbol, order.price, order.quantity])
return compressed
logger = Logger()
class Trader:
def run(self, state: TradingState):
print("traderData: " + state.traderData)
print("Observations: " + str(state.observations))
# Orders to be placed on exchange matching engine
result = {}
for product in state.order_depths:
order_depth: OrderDepth = state.order_depths[product]
orders: List[Order] = []
acceptable_price = 10 # Participant should calculate this value
# print("Acceptable price : " + str(acceptable_price))
# print("Buy Order depth : " + str(len(order_depth.buy_orders)) + ", Sell order depth : " + str(len(order_depth.sell_orders)))
if len(order_depth.sell_orders) != 0:
best_ask, best_ask_amount = list(order_depth.sell_orders.items())[0]
if int(best_ask) < acceptable_price:
# print("BUY", str(-best_ask_amount) + "x", best_ask)
orders.append(Order(product, best_ask, -best_ask_amount))
if len(order_depth.buy_orders) != 0:
best_bid, best_bid_amount = list(order_depth.buy_orders.items())[0]
if int(best_bid) > acceptable_price:
print("SELL", str(best_bid_amount) + "x", best_bid)
orders.append(Order(product, best_bid, -best_bid_amount))
result[product] = orders
# String value holding Trader state data required.
# It will be delivered as TradingState.traderData on next execution.
traderData = "SAMPLE"
# Sample conversion request. Check more details below.
conversions = 1
logger.flush(state, orders)
return result, conversions, traderData