-
Notifications
You must be signed in to change notification settings - Fork 1
/
backtester.py
441 lines (413 loc) · 21.9 KB
/
backtester.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
from trade import Trader
from datamodel import *
from typing import Any #, Callable
import numpy as np
import pandas as pd
import statistics
import copy
import uuid
import random
from datetime import datetime
# Timesteps used in training files
TIME_DELTA = 100
# Please put all! the price and log files into
# the same directory or adjust the code accordingly
TRAINING_DATA_PREFIX = "island-data-by-round"
ALL_SYMBOLS = [
'PEARLS',
'BANANAS',
'COCONUTS',
'PINA_COLADAS',
'DIVING_GEAR',
'BERRIES',
'DOLPHIN_SIGHTINGS',
'BAGUETTE',
'DIP',
'UKULELE',
'PICNIC_BASKET'
]
POSITIONABLE_SYMBOLS = [
'PEARLS',
'BANANAS',
'COCONUTS',
'PINA_COLADAS',
'DIVING_GEAR',
'BERRIES',
'BAGUETTE',
'DIP',
'UKULELE',
'PICNIC_BASKET'
]
first_round = ['PEARLS', 'BANANAS']
snd_round = first_round + ['COCONUTS', 'PINA_COLADAS']
third_round = snd_round + ['DIVING_GEAR', 'DOLPHIN_SIGHTINGS', 'BERRIES']
fourth_round = third_round + ['BAGUETTE', 'DIP', 'UKULELE', 'PICNIC_BASKET']
fifth_round = fourth_round # + secret, maybe pirate gold?
SYMBOLS_BY_ROUND = {
1: first_round,
2: snd_round,
3: third_round,
4: fourth_round,
5: fifth_round,
}
first_round_pst = ['PEARLS', 'BANANAS']
snd_round_pst = first_round_pst + ['COCONUTS', 'PINA_COLADAS']
third_round_pst = snd_round_pst + ['DIVING_GEAR', 'BERRIES']
fourth_round_pst = third_round_pst + ['BAGUETTE', 'DIP', 'UKULELE', 'PICNIC_BASKET']
fifth_round_pst = fourth_round_pst # + secret, maybe pirate gold?
SYMBOLS_BY_ROUND_POSITIONABLE = {
1: first_round_pst,
2: snd_round_pst,
3: third_round_pst,
4: fourth_round_pst,
5: fifth_round_pst,
}
def process_prices(df_prices, round, time_limit) -> dict[int, TradingState]:
states = {}
for _, row in df_prices.iterrows():
time: int = int(row["timestamp"])
if time > time_limit:
break
product: str = row["product"]
if states.get(time) == None:
position: Dict[Product, Position] = {}
own_trades: Dict[Symbol, List[Trade]] = {}
market_trades: Dict[Symbol, List[Trade]] = {}
observations: Dict[Product, Observation] = {}
listings = {}
depths = {}
states[time] = TradingState(time, listings, depths, own_trades, market_trades, position, observations)
if product not in states[time].position and product in SYMBOLS_BY_ROUND_POSITIONABLE[round]:
states[time].position[product] = 0
states[time].own_trades[product] = []
states[time].market_trades[product] = []
states[time].listings[product] = Listing(product, product, "1")
if product == "DOLPHIN_SIGHTINGS":
states[time].observations["DOLPHIN_SIGHTINGS"] = row['mid_price']
depth = OrderDepth()
if row["bid_price_1"]> 0:
depth.buy_orders[row["bid_price_1"]] = int(row["bid_volume_1"])
if row["bid_price_2"]> 0:
depth.buy_orders[row["bid_price_2"]] = int(row["bid_volume_2"])
if row["bid_price_3"]> 0:
depth.buy_orders[row["bid_price_3"]] = int(row["bid_volume_3"])
if row["ask_price_1"]> 0:
depth.sell_orders[row["ask_price_1"]] = -int(row["ask_volume_1"])
if row["ask_price_2"]> 0:
depth.sell_orders[row["ask_price_2"]] = -int(row["ask_volume_2"])
if row["ask_price_3"]> 0:
depth.sell_orders[row["ask_price_3"]] = -int(row["ask_volume_3"])
states[time].order_depths[product] = depth
return states
def process_trades(df_trades, states: dict[int, TradingState], time_limit):
for _, trade in df_trades.iterrows():
time: int = trade['timestamp']
if time > time_limit:
break
symbol = trade['symbol']
if symbol not in states[time].market_trades:
states[time].market_trades[symbol] = []
t = Trade(
symbol,
trade['price'],
trade['quantity'],
'', #trade['buyer'],
'', #trade['seller'],
time)
states[time].market_trades[symbol].append(t)
return states
current_limits = {
'PEARLS': 20,
'BANANAS': 20,
'COCONUTS': 600,
'PINA_COLADAS': 300,
'DIVING_GEAR': 50,
'BERRIES': 250,
'BAGUETTE': 150,
'DIP': 300,
'UKULELE': 70,
'PICNIC_BASKET': 70,
}
def calc_mid(states: dict[int, TradingState], round: int, time: int, max_time: int) -> dict[str, float]:
medians_by_symbol = {}
non_empty_time = time
for psymbol in SYMBOLS_BY_ROUND_POSITIONABLE[round]:
hitted_zero = False
while len(states[non_empty_time].order_depths[psymbol].sell_orders.keys()) == 0 or len(states[non_empty_time].order_depths[psymbol].buy_orders.keys()) == 0:
# little hack
if time == 0 or hitted_zero and time != max_time:
print(psymbol)
hitted_zero = True
non_empty_time += TIME_DELTA
else:
non_empty_time -= TIME_DELTA
min_ask = min(states[non_empty_time].order_depths[psymbol].sell_orders.keys())
max_bid = max(states[non_empty_time].order_depths[psymbol].buy_orders.keys())
median_price = statistics.median([min_ask, max_bid])
medians_by_symbol[psymbol] = median_price
return medians_by_symbol
# Setting a high time_limit can be harder to visualize
# print_position prints the position before! every Trader.run
def simulate_alternative(round: int, day: int, trader, time_limit=999900, halfway=False, print_position=False):
prices_path = f"{TRAINING_DATA_PREFIX}/round-{round}-data/prices_round_{round}_day_{day}.csv"
trades_path = f"{TRAINING_DATA_PREFIX}/round-{round}-data/trades_round_{round}_day_{day}_nn.csv"
df_prices = pd.read_csv(prices_path, sep=';')
df_trades = pd.read_csv(trades_path, sep=';')
states = process_prices(df_prices, round, time_limit)
states = process_trades(df_trades, states, time_limit)
position = copy.copy(states[0].position)
ref_symbols = list(states[0].position.keys())
max_time = max(list(states.keys()))
# handling these four is rather tricky
profits_by_symbol: dict[int, dict[str, float]] = { 0: dict(zip(ref_symbols, [0.0]*len(ref_symbols))) }
balance_by_symbol: dict[int, dict[str, float]] = { 0: copy.deepcopy(profits_by_symbol[0]) }
credit_by_symbol: dict[int, dict[str, float]] = { 0: copy.deepcopy(profits_by_symbol[0]) }
unrealized_by_symbol: dict[int, dict[str, float]] = { 0: copy.deepcopy(profits_by_symbol[0]) }
for time, state in states.items():
position = copy.deepcopy(state.position)
orders = trader.run(state)
trades = clear_order_book(orders, state.order_depths, time, halfway)
mids = calc_mid(states, round, time, max_time)
if print_position:
print(position)
if profits_by_symbol.get(time + TIME_DELTA) == None and time != max_time:
profits_by_symbol[time + TIME_DELTA] = copy.deepcopy(profits_by_symbol[time])
if credit_by_symbol.get(time + TIME_DELTA) == None and time != max_time:
credit_by_symbol[time + TIME_DELTA] = copy.deepcopy(credit_by_symbol[time])
if balance_by_symbol.get(time + TIME_DELTA) == None and time != max_time:
balance_by_symbol[time + TIME_DELTA] = copy.deepcopy(balance_by_symbol[time])
if unrealized_by_symbol.get(time + TIME_DELTA) == None and time != max_time:
unrealized_by_symbol[time + TIME_DELTA] = copy.deepcopy(unrealized_by_symbol[time])
for psymbol in SYMBOLS_BY_ROUND_POSITIONABLE[round]:
unrealized_by_symbol[time + TIME_DELTA][psymbol] = mids[psymbol]*position[psymbol]
valid_trades = []
failed_symbol = []
grouped_by_symbol = {}
if len(trades) > 0:
for trade in trades:
if trade.symbol in failed_symbol:
continue
n_position = position[trade.symbol] + trade.quantity
if abs(n_position) > current_limits[trade.symbol]:
print('ILLEGAL TRADE, WOULD EXCEED POSITION LIMIT, KILLING ALL REMAINING ORDERS')
trade_vars = vars(trade)
trade_str = ', '.join("%s: %s" % item for item in trade_vars.items())
print(f'Stopped at the following trade: {trade_str}')
print(f"All trades that were sent:")
for trade in trades:
trade_vars = vars(trade)
trades_str = ', '.join("%s: %s" % item for item in trade_vars.items())
print(trades_str)
failed_symbol.append(trade.symbol)
valid_trades.append(trade)
FLEX_TIME_DELTA = TIME_DELTA
if time == max_time:
FLEX_TIME_DELTA = 0
for valid_trade in valid_trades:
position[valid_trade.symbol] += valid_trade.quantity
if grouped_by_symbol.get(valid_trade.symbol) == None:
grouped_by_symbol[valid_trade.symbol] = []
grouped_by_symbol[valid_trade.symbol].append(valid_trade)
credit_by_symbol[time + FLEX_TIME_DELTA][valid_trade.symbol] += -valid_trade.price * valid_trade.quantity
if states.get(time + FLEX_TIME_DELTA) != None:
states[time + FLEX_TIME_DELTA].own_trades = grouped_by_symbol
for psymbol in SYMBOLS_BY_ROUND_POSITIONABLE[round]:
unrealized_by_symbol[time + FLEX_TIME_DELTA][psymbol] = mids[psymbol]*position[psymbol]
if position[psymbol] == 0 and states[time].position[psymbol] != 0:
profits_by_symbol[time + FLEX_TIME_DELTA][psymbol] += credit_by_symbol[time + FLEX_TIME_DELTA][psymbol] #+unrealized_by_symbol[time + FLEX_TIME_DELTA][psymbol]
credit_by_symbol[time + FLEX_TIME_DELTA][psymbol] = 0
balance_by_symbol[time + FLEX_TIME_DELTA][psymbol] = 0
else:
balance_by_symbol[time + FLEX_TIME_DELTA][psymbol] = credit_by_symbol[time + FLEX_TIME_DELTA][psymbol] + unrealized_by_symbol[time + FLEX_TIME_DELTA][psymbol]
if time == max_time:
print("End of simulation reached. All positions left are liquidated")
# i have the feeling this already has been done, and only repeats the same values as before
for osymbol in position.keys():
profits_by_symbol[time + FLEX_TIME_DELTA][osymbol] += credit_by_symbol[time + FLEX_TIME_DELTA][osymbol] + unrealized_by_symbol[time + FLEX_TIME_DELTA][osymbol]
balance_by_symbol[time + FLEX_TIME_DELTA][osymbol] = 0
#liquidate_leftovers(position, profits_by_symbol, credit_by_symbol, state, time)
if states.get(time + FLEX_TIME_DELTA) != None:
states[time + FLEX_TIME_DELTA].position = copy.deepcopy(position)
create_log_file(round, day, states, profits_by_symbol, balance_by_symbol, trader)
if hasattr(trader, 'after_last_round'):
if callable(trader.after_last_round):
trader.after_last_round()
def liquidate_leftovers(position: dict[Product, Position], profits_by_symbol: dict[int, dict[str, float]], credit_by_symbol: dict[int, dict[str, float]], state: TradingState, time: int):
liquidated_position = copy.deepcopy(position)
for symbol in position.keys():
if liquidated_position[symbol] != 0:
if liquidated_position[symbol] > 0:
sorted_sell_prices = list(state.order_depths[symbol].sell_orders.keys())
sorted_sell_prices.sort(reverse=True)
for ask_order_price in sorted_sell_prices:
if abs(liquidated_position[symbol]) <= abs(state.order_depths[symbol].sell_orders[ask_order_price]):
profits_by_symbol[time][symbol] += ask_order_price*liquidated_position[symbol]
liquidated_position[symbol] = 0
break
else:
profits_by_symbol[time][symbol] += ask_order_price*state.order_depths[symbol].sell_orders[ask_order_price]
liquidated_position[symbol] -= state.order_depths[symbol].sell_orders[ask_order_price]
if liquidated_position[symbol] > 0:
print(f'Unable to liquidate all LONG positions for {symbol}, left with {liquidated_position[symbol]}')
else:
sorted_buy_prices = list(state.order_depths[symbol].buy_orders.keys())
sorted_buy_prices.sort(reverse=True)
for buy_order_price in sorted_buy_prices:
if abs(liquidated_position[symbol]) <= abs(state.order_depths[symbol].buy_orders[buy_order_price]):
profits_by_symbol[time][symbol] -= buy_order_price*liquidated_position[symbol]
liquidated_position[symbol] = 0
break
else:
profits_by_symbol[time][symbol] -= buy_order_price*state.order_depths[symbol].buy_orders[buy_order_price]
liquidated_position[symbol] += state.order_depths[symbol].buy_orders[buy_order_price]
if liquidated_position[symbol] < 0:
print(f'Unable to liquidate all SHORT positions for {symbol}, left with {liquidated_position[symbol]}')
position = liquidated_position
print(f'\n')
def cleanup_order_volumes(org_orders: List[Order]) -> List[Order]:
orders = [] #copy.deepcopy(org_orders)
for order_1 in org_orders:
final_order = copy.copy(order_1)
for order_2 in org_orders:
if order_1.price == order_2.price and order_1.quantity == order_2.quantity:
continue
if order_1.price == order_2.price:
final_order.quantity += order_2.quantity
orders.append(final_order)
return orders
def clear_order_book(trader_orders: dict[str, List[Order]], order_depth: dict[str, OrderDepth], time: int, halfway: bool) -> list[Trade]:
trades = []
for symbol in trader_orders.keys():
if order_depth.get(symbol) != None:
symbol_order_depth = copy.deepcopy(order_depth[symbol])
t_orders = cleanup_order_volumes(trader_orders[symbol])
for order in t_orders:
if order.quantity < 0:
if halfway:
bids = symbol_order_depth.buy_orders.keys()
asks = symbol_order_depth.sell_orders.keys()
max_bid = max(bids)
min_ask = min(asks)
if order.price <= statistics.median([max_bid, min_ask]):
trades.append(Trade(symbol, order.price, order.quantity, "BOT", "YOU", time))
else:
potential_matches = list(filter(lambda o: o[0] == order.price, symbol_order_depth.buy_orders.items()))
if len(potential_matches) > 0:
match = potential_matches[0]
final_volume = 0
if abs(match[1]) > abs(order.quantity):
final_volume = order.quantity
else:
#this should be negative
final_volume = -match[1]
trades.append(Trade(symbol, order.price, final_volume, "BOT", "YOU", time))
if order.quantity > 0:
if halfway:
bids = symbol_order_depth.buy_orders.keys()
asks = symbol_order_depth.sell_orders.keys()
max_bid = max(bids)
min_ask = min(asks)
if order.price >= statistics.median([max_bid, min_ask]):
trades.append(Trade(symbol, order.price, order.quantity, "YOU", "BOT", time))
else:
potential_matches = list(filter(lambda o: o[0] == order.price, symbol_order_depth.sell_orders.items()))
if len(potential_matches) > 0:
match = potential_matches[0]
final_volume = 0
#Match[1] will be negative so needs to be changed to work here
if abs(match[1]) > abs(order.quantity):
final_volume = order.quantity
else:
final_volume = abs(match[1])
trades.append(Trade(symbol, order.price, final_volume, "YOU", "BOT", time))
return trades
csv_header = "day;timestamp;product;bid_price_1;bid_volume_1;bid_price_2;bid_volume_2;bid_price_3;bid_volume_3;ask_price_1;ask_volume_1;ask_price_2;ask_volume_2;ask_price_3;ask_volume_3;mid_price;profit_and_loss\n"
log_header = [
'Sandbox logs:\n',
'0 OpenBLAS WARNING - could not determine the L2 cache size on this system, assuming 256k\n',
'START RequestId: 8ab36ff8-b4e6-42d4-b012-e6ad69c42085 Version: $LATEST\n',
'END RequestId: 8ab36ff8-b4e6-42d4-b012-e6ad69c42085\n',
'REPORT RequestId: 8ab36ff8-b4e6-42d4-b012-e6ad69c42085 Duration: 18.73 ms Billed Duration: 19 ms Memory Size: 128 MB Max Memory Used: 94 MB Init Duration: 1574.09 ms\n',
]
def create_log_file(round: int, day: int, states: dict[int, TradingState], profits_by_symbol: dict[int, dict[str, float]], balance_by_symbol: dict[int, dict[str, float]], trader: Trader):
file_name = uuid.uuid4()
timest = datetime.timestamp(datetime.now())
max_time = max(list(states.keys()))
with open(f'./separate_scripts/logs/{timest}_{file_name}.log', 'w', encoding="utf-8", newline='\n') as f:
f.writelines(log_header)
f.write('\n')
for time, state in states.items():
if hasattr(trader, 'logger'):
if hasattr(trader.logger, 'local_logs') != None:
if trader.logger.local_logs.get(time) != None:
f.write(f'{time} {trader.logger.local_logs[time]}\n')
continue
if time != 0:
f.write(f'{time}\n')
f.write(f'\n\n')
f.write('Submission logs:\n\n\n')
f.write('Activities log:\n')
f.write(csv_header)
for time, state in states.items():
for symbol in SYMBOLS_BY_ROUND[round]:
f.write(f'{day};{time};{symbol};')
bids_length = len(state.order_depths[symbol].buy_orders)
bids = list(state.order_depths[symbol].buy_orders.items())
bids_prices = list(state.order_depths[symbol].buy_orders.keys())
bids_prices.sort()
asks_length = len(state.order_depths[symbol].sell_orders)
asks_prices = list(state.order_depths[symbol].sell_orders.keys())
asks_prices.sort()
asks = list(state.order_depths[symbol].sell_orders.items())
if bids_length >= 3:
f.write(f'{bids[0][0]};{bids[0][1]};{bids[1][0]};{bids[1][1]};{bids[2][0]};{bids[2][1]};')
elif bids_length == 2:
f.write(f'{bids[0][0]};{bids[0][1]};{bids[1][0]};{bids[1][1]};;;')
elif bids_length == 1:
f.write(f'{bids[0][0]};{bids[0][1]};;;;;')
else:
f.write(f';;;;;;')
if asks_length >= 3:
f.write(f'{asks[0][0]};{asks[0][1]};{asks[1][0]};{asks[1][1]};{asks[2][0]};{asks[2][1]};')
elif asks_length == 2:
f.write(f'{asks[0][0]};{asks[0][1]};{asks[1][0]};{asks[1][1]};;;')
elif asks_length == 1:
f.write(f'{asks[0][0]};{asks[0][1]};;;;;')
else:
f.write(f';;;;;;')
if len(asks_prices) == 0 or max(bids_prices) == 0:
if symbol == 'DOLPHIN_SIGHTINGS':
dolphin_sightings = state.observations['DOLPHIN_SIGHTINGS']
f.write(f'{dolphin_sightings};{0.0}\n')
else:
f.write(f'{0};{0.0}\n')
else:
actual_profit = 0.0
if symbol in SYMBOLS_BY_ROUND_POSITIONABLE[round]:
actual_profit = profits_by_symbol[time][symbol] + balance_by_symbol[time][symbol]
min_ask = min(asks_prices)
max_bid = max(bids_prices)
median_price = statistics.median([min_ask, max_bid])
f.write(f'{median_price};{actual_profit}\n')
if time == max_time:
if profits_by_symbol[time].get(symbol) != None:
print(f'Final profit for {symbol} = {actual_profit}')
print(f"\nSimulation on round {round} day {day} for time {max_time} complete")
# Adjust accordingly the round and day to your needs
if __name__ == "__main__":
trader = Trader()
max_time = int(input("Input a timestamp to end (blank for 999000): ") or 999000)
round = int(input("Input a round (blank for 4): ") or 4)
day = int(input("Input a day (blank for random): ") or random.randint(1, 3))
halfway = bool(input("Matching orders halfway (sth. not blank for True): ")) or False
#liqudation = bool(input("Should all positions be liquidated in the final run (sth. not blank for True): ")) or False
"""
max_time = 60000
round = 4
day = 1
halfway = False
liqudation = True
"""
print(f"Running simulation on round {round} day {day} for time {max_time}")
print("Remember to change the trader import")
simulate_alternative(round, day, trader, max_time, halfway, False)