MCPcopy Create free account
hub / github.com/FinancialComputingUCL/LOBFrame / backtest

Function backtest

simulator/market_sim.py:17–110  ·  view source on GitHub ↗
(experiment_id, trading_hyperparameters)

Source from the content-addressed store, hash-verified

15
16
17def backtest(experiment_id, trading_hyperparameters):
18 prices, sanity_check_labels, targets, predictions, probs = __get_data__(experiment_id)
19 TradingAgent = Trading(trading_hyperparameters)
20
21 prices["Mid"] = (prices["BIDp1"] + prices["ASKp1"]) / 2
22 prices["seconds"] = pd.to_datetime(prices["seconds"])
23
24 prices['Predictions'] = predictions
25 prices.reset_index(drop=True, inplace=True)
26 indices_to_delete = prices[prices['Predictions'] == 1].index
27 prices = prices.drop(indices_to_delete)
28 mask = (prices['Predictions'] != prices['Predictions'].shift()) | (prices.index == 0)
29 prices = prices[mask]
30 prices = prices.reset_index(drop=True)
31 predictions = prices['Predictions'].tolist()
32 prices = prices.drop(columns=['Predictions'])
33 prices.reset_index(drop=True, inplace=True)
34
35 dates = prices['seconds'].dt.date
36 day_changed_indices = dates.ne(dates.shift())
37 new_day_indices = day_changed_indices.index[day_changed_indices].tolist()
38 end_of_day_indices = [element - 1 for element in new_day_indices]
39 end_of_day_indices.append(len(prices) - 1)
40 end_of_day_indices = end_of_day_indices[1:]
41
42 for i in tqdm(range(len(predictions))):
43 mid_price = prices.at[i, "Mid"]
44 best_bid_price = prices.at[i, "BIDp1"]
45 best_ask_price = prices.at[i, "ASKp1"]
46 timestamp = prices.at[i, "seconds"]
47 prediction = predictions[i]
48 probability = np.max(probs[i])
49
50 if trading_hyperparameters['mid_side_trading'] == 'mid_to_mid':
51 if i in end_of_day_indices:
52 if TradingAgent.long_inventory > 0:
53 TradingAgent.exit_long(mid_price, timestamp)
54 if TradingAgent.short_inventory > 0:
55 TradingAgent.exit_short(mid_price, timestamp)
56 else:
57 if prediction == 2 and probability >= trading_hyperparameters['probability_threshold']:
58 if TradingAgent.long_inventory == 0 and TradingAgent.short_inventory == 0:
59 TradingAgent.long(mid_price, timestamp)
60 elif TradingAgent.long_inventory == 0 and TradingAgent.short_inventory > 0:
61 TradingAgent.exit_short(mid_price, timestamp)
62 TradingAgent.long(mid_price, timestamp)
63 elif prediction == 0 and probability >= trading_hyperparameters['probability_threshold']:
64 if TradingAgent.long_inventory == 0 and TradingAgent.short_inventory == 0:
65 TradingAgent.short(mid_price, timestamp)
66 elif TradingAgent.short_inventory == 0 and TradingAgent.long_inventory > 0:
67 TradingAgent.exit_long(mid_price, timestamp)
68 TradingAgent.short(mid_price, timestamp)
69 elif trading_hyperparameters['mid_side_trading'] == 'side_market_orders':
70 if i in end_of_day_indices:
71 if TradingAgent.long_inventory > 0:
72 TradingAgent.exit_long(best_bid_price, timestamp)
73 if TradingAgent.short_inventory > 0:
74 TradingAgent.exit_short(best_ask_price, timestamp)

Callers

nothing calls this directly

Calls 6

exit_longMethod · 0.95
exit_shortMethod · 0.95
longMethod · 0.95
shortMethod · 0.95
TradingClass · 0.90
__get_data__Function · 0.85

Tested by

no test coverage detected