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
#[derive(Debug)]
pub struct TradeAction {
string: String,
convert: f32,
fee: f32,
remaining: f32,
}
#[derive(Debug)]
pub struct ArbitrageResult {
pub action_i: TradeAction,
pub action_ii: TradeAction,
pub action_iii: TradeAction,
}
#[derive(Debug)]
pub struct BiDirectionalArbitrageResult {
pub is_profitable: bool,
pub clockwise: ArbitrageResult,
pub counter_clockwise: ArbitrageResult,
}
pub fn check_triangle_trades_clockwise(
a: f32,
b: f32,
c: f32,
start_amt: f32,
default_fee_percentage: f32,
) -> ArbitrageResult {
let convert_b = start_amt * a;
let b_fee = convert_b * default_fee_percentage;
let remaining_b = convert_b - b_fee;
let convert_c = remaining_b / b;
let c_fee = convert_c * default_fee_percentage;
let remaining_c = convert_c - c_fee;
let convert_a = remaining_c * c;
let a_fee = convert_a * default_fee_percentage;
let remaining_a = convert_a - a_fee;
ArbitrageResult {
action_i: TradeAction {
string: "A->B".to_string(),
convert: convert_b,
fee: b_fee,
remaining: remaining_b,
},
action_ii: TradeAction {
string: "B->C".to_string(),
convert: convert_c,
fee: c_fee,
remaining: remaining_c,
},
action_iii: TradeAction {
string: "C->A".to_string(),
convert: convert_a,
fee: a_fee,
remaining: remaining_a,
},
}
}
pub fn check_triangle_trades_counter_clockwise(
a: f32,
b: f32,
c: f32,
start_amt: f32,
default_fee_percentage: f32,
) -> ArbitrageResult {
let convert_c = start_amt / a;
let c_fee = convert_c * default_fee_percentage;
let remaining_c = convert_c - c_fee;
let convert_b = remaining_c * b;
let b_fee = convert_b * default_fee_percentage;
let remaining_b = convert_b - b_fee;
let convert_a = remaining_b / c;
let a_fee = convert_a * default_fee_percentage;
let remaining_a = convert_a - a_fee;
ArbitrageResult {
action_i: TradeAction {
string: "A->C".to_string(),
convert: convert_c,
fee: c_fee,
remaining: remaining_c,
},
action_ii: TradeAction {
string: "C->B".to_string(),
convert: convert_b,
fee: b_fee,
remaining: remaining_b,
},
action_iii: TradeAction {
string: "B->A".to_string(),
convert: convert_a,
fee: a_fee,
remaining: remaining_a,
},
}
}
pub fn check(
a: f32,
b: f32,
c: f32,
start_amt: f32,
default_fee_percentage: f32,
) -> BiDirectionalArbitrageResult {
let clockwise = check_triangle_trades_clockwise(a, b, c, start_amt, default_fee_percentage);
let counter_clockwise =
check_triangle_trades_counter_clockwise(a, b, c, start_amt, default_fee_percentage);
let is_profitable =
clockwise.action_iii.remaining > 1.0 || counter_clockwise.action_iii.remaining > 1.0;
BiDirectionalArbitrageResult {
is_profitable,
clockwise,
counter_clockwise,
}
}