forked from gferrin/triangular-arbitrage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriangle.js
More file actions
284 lines (220 loc) · 7.83 KB
/
triangle.js
File metadata and controls
284 lines (220 loc) · 7.83 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*-------------------------------------------------------
:: Tage (Taj) - Triangle Arbitrage Trader
-> Gabriel Ferrin & Connor Black
Operates on triangular arbitrage opportunities
within the BTC-e exchange.
--------------------------------------------------------*/
/*-- Dependencies --*/
// NPM
require('coffee-script');
var colors = require('colors');
// BTC-e
var btce = require('./clients/btc_e');
var book = require('./book/Books');
var pairs = require('./config/btce_pairs');
var arbitrage = require('./lib/arbitrage_functions');
var general = require('./lib/general_functions');
// Graph C++
var graph_constructor = require('./build/Release/graph');
/*- Variables -*/
var BTCE_E_FEE = 0.002; // 0.2%
/*-- Initializaion --*/
var Book = new book();
var Graph = new graph_constructor.Graph();
// initializes all the books
Book.update_all();
// btce.on('account_info', function(info){
// console.log("in account info");
// console.log(info);
// });
// initialize the graph
setTimeout( function(){
// Get the needed account info
//btce.getInfo();
for(var pair in pairs){
var nodes = arbitrage.separate_pair( pairs[pair] );
Graph.add_node( nodes[0] );
Graph.add_node( nodes[1] );
}
// Need two seperate loops because if the nodes dont exist then add edge will error
for(var pair in pairs){
var nodes = arbitrage.separate_pair( pairs[pair] );
arbitrage.add_edge( Graph, nodes[0], nodes[1], Book.books[pair].bid,
function( graph, from, to, weight ){
graph.add_edge( from, to, "bid", weight, BTCE_E_FEE );
});
arbitrage.add_edge( Graph, nodes[1], nodes[0], Book.books[pair].ask,
function( graph, from, to, weight ){
graph.add_edge( from, to, "ask", weight, BTCE_E_FEE );
});
}
Graph.print();
console.log(Graph.check_arbitrage( "usd"));
setInterval(function() {
for( var pair in pairs){
// update the inside values for all of the pairs
btce.ticker( pair, function(err, res){
if( err === null ){
Book.books[pair].update_inside(res.ticker.sell, res.ticker.buy);
} else {
console.log(err);
}
});
var nodes = arbitrage.separate_pair( pairs[pair] );
arbitrage.update_edge( Graph, nodes[0], nodes[1], Book.books[pair].bid,
function( graph, from, to, weight ){
graph.update_edge( from, to, "bid", weight, BTCE_E_FEE );
});
arbitrage.update_edge( Graph, nodes[1], nodes[0], Book.books[pair].ask,
function( graph, from, to, weight ){
graph.update_edge( from, to, "ask", weight, BTCE_E_FEE );
});
}
var negative_path = Graph.check_arbitrage("btc");
console.log("Negative path " + negative_path );
console.log(negative_path.length);
if( negative_path.length > 0 ){
// So In here we know there is a negative weight cycle
// Need to figure out the bids and asks for for each step in
// the cycle and exactly which pairs will be used
// While this is happening make sure the needed order books are updated
// Next create a subgraph for the three cureencies. Get the vols for the trade
// place the orders adjuct the current copys of the books, update the graph
var pair1, pair2, pair3;
var type1 = type2 = type3 = "bid"; // The type of trade I'm doing
// create a subgraph
var sub_graph = new graph_constructor.Graph();
// add nodes
sub_graph.add_node( negative_path[0] );
sub_graph.add_node( negative_path[1] );
sub_graph.add_node( negative_path[2] );
pair1 = arbitrage.join_pair( negative_path[0], negative_path[1] );
pair2 = arbitrage.join_pair( negative_path[1], negative_path[2] );
pair3 = arbitrage.join_pair( negative_path[2], negative_path[0] );
if( !arbitrage.search_pairs(pair1, pairs) ){
pair1 = arbitrage.join_pair( negative_path[1], negative_path[0] );
arbitrage.add_edge( sub_graph, negative_path[0], negative_path[1], Book.books[pair1].ask,
function( graph, from, to, weight ){
graph.add_edge( from, to, "ask", weight, BTCE_E_FEE );
});
} else {
type1 = "ask";
arbitrage.add_edge( sub_graph, negative_path[0], negative_path[1], Book.books[pair1].bid,
function( graph, from, to, weight ){
graph.add_edge( from, to, "bid", weight, BTCE_E_FEE );
});
}
if( !arbitrage.search_pairs(pair2, pairs) ){
pair2 = arbitrage.join_pair( negative_path[2], negative_path[1] );
arbitrage.add_edge( sub_graph, negative_path[1], negative_path[2], Book.books[pair2].ask,
function( graph, from, to, weight ){
graph.add_edge( from, to, "ask", weight, BTCE_E_FEE );
});
} else {
type2 = "ask";
arbitrage.add_edge( sub_graph, negative_path[1], negative_path[2], Book.books[pair2].bid,
function( graph, from, to, weight ){
graph.add_edge( from, to, "bid", weight, BTCE_E_FEE );
});
}
if( !arbitrage.search_pairs(pair3, pairs) ){
pair3 = arbitrage.join_pair( negative_path[0], negative_path[2] );
arbitrage.add_edge( sub_graph, negative_path[2], negative_path[0], Book.books[pair3].ask,
function( graph, from, to, weight ){
graph.add_edge( from, to, "ask", weight, BTCE_E_FEE );
});
} else {
type3 = "ask";
arbitrage.add_edge( sub_graph, negative_path[2], negative_path[0], Book.books[pair3].bid,
function( graph, from, to, weight ){
graph.add_edge( from, to, "bid", weight, BTCE_E_FEE );
});
}
Book.update( pair1 );
Book.update( pair2 );
Book.update( pair3 );
sub_graph.print();
arb_obj = {
first: {
type: type1,
pair: pair1,
vol: 0
},
second: {
type: type2,
pair: pair2,
vol: 0
},
third: {
type: type3,
pair: pair3,
vol: 0
}
}
min_volume( Book, arb_obj );
do_arbitrage( negative_path, arb_obj, function( np, obj ){
//console.log(sub_graph.check_arbitrage(negative_path[0]));
});
}
negative_path = [];
}, 2000);
}, 5000 );
var do_arbitrage = function(np, obj, cb){
cb( np, obj);
// if( np.length > 0 ){
// do_arbitrage( obj, cb );
// }
};
var min_volume = function( book, obj){
var vols = [];
// If the pair exists (x_y) it is in terms of x, otherwise in terms of y
// which will be the bids in this case
// get each volume
// and if the type is bid, put the currency in terms of itself
if( obj.first.type == "bid"){
vols.push( book.books[obj.first.pair].asks[0][1] * book.books[obj.first.pair].asks[0][0] );
} else {
vols.push( book.books[obj.first.pair].bids[0][1] );
}
if( obj.second.type == "bid"){
vols.push( book.books[obj.second.pair].asks[0][1] );
} else{
vols.push( book.books[obj.second.pair].bids[0][1] * book.books[obj.second.pair].bids[0][0] );
}
if( obj.third.type == "bid"){
vols.push( book.books[obj.third.pair].asks[0][1] );
vols[1] /= book.books[obj.third.pair].asks[0][0];
console.log("in case that might make this wrong");
} else {
vols.push( book.books[obj.third.pair].bids[0][1] * book.books[obj.third.pair].bids[0][0] );
vols[1] *= book.books[obj.third.pair].bids[0][0];
}
// then get the min of the three
var min = Math.min(vols[0], vols[1], vols[2]);
for( var i = 0; i < vols.length; ++i ){
vols[i] = min;
}
// now covert them back to their respective currencys
/****** THIS STILL NEED THE FEE's REMOVED *******/
if( obj.first.type == "bid"){
vols[0] /= book.books[obj.first.pair].asks[0][0];
vols[1] /= book.books[obj.first.pair].asks[0][0];
vols[2] /= book.books[obj.first.pair].asks[0][0];
} else {
vols[1] *= book.books[obj.first.pair].bids[0][0];
vols[2] *= book.books[obj.first.pair].bids[0][0];
}
if( obj.second.type == "bid"){
vols[1] /= book.books[obj.second.pair].asks[0][0];
vols[2] /= book.books[obj.second.pair].asks[0][0];
} else {
vols[2] *= book.books[obj.second.pair].bids[0][0];
}
if( obj.third.type == "bid"){
vols[2] /= book.books[obj.third.pair].asks[0][0];
}
obj.first.vol = vols[0];
obj.second.vol = vols[1];
obj.third.vol = vols[2];
return vols;
};