-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgo_template.py
122 lines (96 loc) · 4.19 KB
/
Algo_template.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
122
# Following template is for imc algo visualizer
import json
from datamodel import Listing, Observation, Order, OrderDepth, ProsperityEncoder, Symbol, Trade, TradingState
from typing import Any
class Logger:
def __init__(self) -> None:
self.logs = ""
self.max_log_length = 3750
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]], conversions: int, trader_data: str) -> None:
base_length = len(self.to_json([
self.compress_state(state, ""),
self.compress_orders(orders),
conversions,
"",
"",
]))
# We truncate state.traderData, trader_data, and self.logs to the same max. length to fit the log limit
max_item_length = (self.max_log_length - base_length) // 3
print(self.to_json([
self.compress_state(state, self.truncate(state.traderData, max_item_length)),
self.compress_orders(orders),
conversions,
self.truncate(trader_data, max_item_length),
self.truncate(self.logs, max_item_length),
]))
self.logs = ""
def compress_state(self, state: TradingState, trader_data: str) -> list[Any]:
return [
state.timestamp,
trader_data,
self.compress_listings(state.listings),
self.compress_order_depths(state.order_depths),
self.compress_trades(state.own_trades),
self.compress_trades(state.market_trades),
state.position,
self.compress_observations(state.observations),
]
def compress_listings(self, listings: dict[Symbol, Listing]) -> list[list[Any]]:
compressed = []
for listing in listings.values():
compressed.append([listing["symbol"], listing["product"], listing["denomination"]])
return compressed
def compress_order_depths(self, order_depths: dict[Symbol, OrderDepth]) -> dict[Symbol, list[Any]]:
compressed = {}
for symbol, order_depth in order_depths.items():
compressed[symbol] = [order_depth.buy_orders, order_depth.sell_orders]
return compressed
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.price,
trade.quantity,
trade.buyer,
trade.seller,
trade.timestamp,
])
return compressed
def compress_observations(self, observations: Observation) -> list[Any]:
conversion_observations = {}
for product, observation in observations.conversionObservations.items():
conversion_observations[product] = [
observation.bidPrice,
observation.askPrice,
observation.transportFees,
observation.exportTariff,
observation.importTariff,
observation.sunlight,
observation.humidity,
]
return [observations.plainValueObservations, conversion_observations]
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
def to_json(self, value: Any) -> str:
return json.dumps(value, cls=ProsperityEncoder, separators=(",", ":"))
def truncate(self, value: str, max_length: int) -> str:
if len(value) <= max_length:
return value
return value[:max_length - 3] + "..."
logger = Logger()
class Trader:
def run(self, state: TradingState) -> tuple[dict[Symbol, list[Order]], int, str]:
result = {}
conversions = 0
trader_data = ""
# TODO: Add logic
logger.flush(state, result, conversions, trader_data)
return result, conversions, trader_data