-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
132 lines (100 loc) · 3.59 KB
/
run.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
import sys
import time
import pickle
import argparse
from PySide6.QtWidgets import QApplication
from src.collect_events import collect_events
from src.utils import get_contract, check_data_exists
from src.gui import MainWindow
from src.strategy import Strategy
from src.provider import Provider
from src.protocol_state import ProtocolState
from src.position_manager import PositionManager
def main():
parser = argparse.ArgumentParser(description="Your program description here.")
parser.add_argument(
"pool_address",
type=str,
help="Specify Uniswap pool address to be used."
)
parser.add_argument(
"network",
type=str,
choices=["mainnet", "goerli", "optimism"],
help="Specify the network to be used."
)
parser.add_argument(
"--gui",
action='store_true',
default=False,
help="Activate the GUI.",
)
parser.add_argument(
"--simulate",
action='store_true',
default=False,
help="Real time execution but without making trades.",
)
parser.add_argument(
"--backtest",
action='store_true',
default=False,
help="Enable backtesting mode.",
)
parser.add_argument(
"--from_block",
type=str,
help="Specify the first block to be used for backtesting."
)
parser.add_argument(
"--to_block",
type=str,
help="Specify the last block to be used for backtesting."
)
parser.add_argument(
"--save_performance",
type=str,
help="Save the perforamance to a file."
)
args = parser.parse_args()
if args.backtest:
if args.from_block is None or args.to_block is None:
parser.error("--backtest requires --from_block and --to_block.")
print("Running in backtest mode")
data_exists = check_data_exists(int(args.from_block), int(args.to_block), args.pool_address)
if not data_exists:
print("Collecting data...")
collect_events(get_contract("POOL", args.pool_address), int(args.from_block), int(args.to_block))
else:
print("Running in normal mode")
provider = Provider(args.pool_address, args.network, sim=args.simulate, backtest=args.backtest, swap_data=f"data/{args.pool_address}/Swap.csv", mint_data=f"data/{args.pool_address}/Mint.csv", burn_data=f"data/{args.pool_address}/Burn.csv")
state = ProtocolState(provider)
position_manager = PositionManager(provider, state)
strategy = Strategy(provider, state, position_manager)
state.start()
strategy.start()
if args.gui:
app = QApplication(sys.argv)
window = MainWindow(provider, state, position_manager, backtest=args.backtest)
window.setWindowTitle(f"UniSwap v3 {provider.token0_symbol}-{provider.token1_symbol} Interface")
window.setGeometry(100, 100, 800, 600)
window.show()
exit_code = app.exec()
strategy.stop()
state.stop()
if args.save_performance:
with open(args.save_performance + ".pkl", "wb") as f:
pickle.dump(position_manager.performance, f)
sys.exit(exit_code)
else:
while True:
if args.backtest and state.current_block == -1:
state.stop()
strategy.stop()
if args.save_performance:
with open(args.save_performance + ".pkl", "wb") as f:
pickle.dump(position_manager.performance, f)
break
time.sleep(20)
if __name__ == "__main__":
main()