-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexample_backtest.py
96 lines (83 loc) · 2.65 KB
/
example_backtest.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
"""Run two agents.
One active one passive.
"""
from decimal import Decimal
from typing import Any, Optional
from examples.moving_averages.policy import MovingAveragePolicy
from policies.passiveLP import PassiveConcentratedLP
from dojo.agents.uniswapV3 import TotalWealthAgent
from dojo.common import time_to_block
from dojo.common.constants import Chain
from dojo.environments import UniswapV3Env
from dojo.market_agents.uniswapV3 import HistoricReplayAgent
from dojo.runners import backtest_run
def main(
*,
dashboard_server_port: Optional[int] = 8768,
simulation_status_bar: bool = False,
auto_close: bool,
num_sim_blocks: int = 1800,
**kwargs: dict[str, Any],
) -> None:
"""Running this strategy."""
# SNIPPET 1 START
pools = ["USDC/WETH-0.05"]
start_time = "2024-12-06 13:00:00"
chain = Chain.ETHEREUM
block_range = (
time_to_block(start_time, chain),
time_to_block(start_time, chain) + num_sim_blocks,
)
# block_range = 21303933, 21354333
market_agent = HistoricReplayAgent(
chain=chain, pools=pools, block_range=block_range
)
# Agents
trader_agent = TotalWealthAgent(
initial_portfolio={
"ETH": Decimal(100),
"USDC": Decimal(10_000),
"WETH": Decimal(100),
},
name="TraderAgent",
unit_token="USDC",
policy=MovingAveragePolicy(
pool="USDC/WETH-0.05", short_window=25, long_window=100
),
)
lp_agent = TotalWealthAgent(
initial_portfolio={"USDC": Decimal(10_000), "WETH": Decimal(100)},
name="LPAgent",
unit_token="USDC",
policy=PassiveConcentratedLP(lower_price_bound=0.95, upper_price_bound=1.05),
)
# Simulation environment (Uniswap V3)
env = UniswapV3Env(
chain=chain,
block_range=block_range,
agents=[market_agent, trader_agent, lp_agent],
# agents=[market_agent],
pools=pools,
backend_type="local",
)
# SNIPPET 2 START
backtest_run(
env,
dashboard_server_port=dashboard_server_port,
auto_close=auto_close,
simulation_status_bar=simulation_status_bar,
output_file="example_backtest.db",
simulation_title="Example backtest",
simulation_description="Example backtest. One LP agent, one trader agent.",
)
# SNIPPET 2 END
# SNIPPET 1 END
if __name__ == "__main__":
import dojo.config.logging_config
dojo.config.logging_config.set_normal_logging_config_and_print_explanation()
main(
dashboard_server_port=8768,
simulation_status_bar=True,
auto_close=False,
num_sim_blocks=600,
)