-
Notifications
You must be signed in to change notification settings - Fork 13
/
arbitrage.py
58 lines (52 loc) · 1.93 KB
/
arbitrage.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
from decimal import Decimal
from workers.arbitrage import execute_arbitrage, getOptimalAmount, should_execute, simulate_execute_arbitrage
from workers.getChainData import ChainData
from workers.graph import Graph
def main():
# init phase
cd = ChainData()
cd.readDataFromFile()
graph = Graph()
while True:
graph.buildGraphFromChainData(cd.pairs)
graph.findMostProfitableCircuit()
# execute arbitrage trades
operations = []
maxOps = 129
if len(graph.options) > maxOps:
operations = [
graph.options[i] for i in range(
len(graph.options) - 1,
len(graph.options) - (maxOps + 1),
-1
)
]
else:
operations = graph.options
for op in operations:
best_input_amount = getOptimalAmount(graph, op)
if best_input_amount is not None:
print(f"the best trade size is {best_input_amount}, graph.tradeSize = {graph.tradeSize.value}")
if best_input_amount is None:
best_input_amount = graph.tradeSize.rawValue
if best_input_amount < graph.tradeSize.rawValue * Decimal(0.05):
best_input_amount = int(graph.tradeSize.rawValue * Decimal(0.05))
min_output = simulate_execute_arbitrage(graph, op, best_input_amount)
if should_execute(op, min_output):
print("executing a trade")
execute_arbitrage(op, min_output, best_input_amount)
graph.update_trade_size()
print("=" * 10)
try:
print("writing update data to file")
cd.write_refresh_data(graph.activeLPAddrs)
cd.readDataFromFile()
except Exception as e:
print(e)
pass
if __name__ == '__main__':
while True:
try:
main()
except:
pass