-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdontlooseshells_algo.py
88 lines (73 loc) · 2.83 KB
/
dontlooseshells_algo.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
import json
from datamodel import Order, ProsperityEncoder, Symbol, TradingState, Trade
from typing import Any
class Logger:
# Set this to true, if u want to create
# local logs
local: bool
# this is used as a buffer for logs
# instead of stdout
local_logs: dict[int, str] = {}
def __init__(self, local=False) -> None:
self.logs = ""
self.local = local
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:
output = json.dumps({
"state": state,
"orders": orders,
"logs": self.logs,
}, cls=ProsperityEncoder, separators=(",", ":"), sort_keys=True)
if self.local:
self.local_logs[state.timestamp] = output
print(output)
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
# This is provisionary, if no other algorithm works.
# Better to loose nothing, then dreaming of a gain.
class Trader:
logger = Logger(local=True)
def run(self, state: TradingState):
"""
Only method required. It takes all buy and sell orders for all symbols as an input,
and outputs a list of orders to be sent
"""
# Initialize the method output dict as an empty dict
result = {}
self.logger.flush(state, result)
return result