-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathmarket_maker.algo
331 lines (269 loc) · 9.93 KB
/
market_maker.algo
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
-----BEGIN ALGO DEFINITION-----
{
"id": "blinktrade",
"description": "Make the market following bitstamp order book",
"params": [
{"name":"dollar", "label":"Dollar ex rate", "type":"number", "value":"", "filter":"positive_number", "validator":"required; validateMin 0; validateNumber;" },
{"name":"spread", "label":"Spread", "type":"number", "value":"1", "filter":"positive_number", "validator":"required; validateMin 0; validateNumber;" },
{"name":"limit", "label":"Limit", "type":"number", "value":"" , "filter":"positive_number", "validator":"required; validateMin 0; validateNumber;" }
],
"creator": "blinktrade.SimpleMarketMarketAlgo.create",
"destructor": "blinktrade.SimpleMarketMarketAlgo.destroy",
"permissions": ["notification", "balance", "execution_report", "new_order_limited", "cancel_order"]
}
-----END ALGO DEFINITION-----
-----BEGIN ALGO-----
/**/
/**
* Namespace.
*/
var blinktrade = {};
/**
* @param {Object} application
* @param {string} symbol
* @constructor
*/
blinktrade.SimpleMarketMarketAlgo = function(application, symbol){
this.application_ = application;
this.symbol_ = symbol;
this.bitstamp_order_book_channel_subscription_ = false;
};
/**
* @type {boolean}
*/
blinktrade.SimpleMarketMarketAlgo.prototype.bitstamp_order_book_channel_subscription_;
/**
* @type {number}
*/
blinktrade.SimpleMarketMarketAlgo.prototype.last_best_bid_;
/**
* @type {number}
*/
blinktrade.SimpleMarketMarketAlgo.prototype.last_best_ask_;
/**
* @param {Application} application
* @param {string} symbol
* @return {blinktrade.SimpleMarketMarketAlgo}
*/
blinktrade.SimpleMarketMarketAlgo.create = function(application,symbol) {
return new blinktrade.SimpleMarketMarketAlgo(application,symbol);
};
/**
* @param {Object} params
*/
blinktrade.SimpleMarketMarketAlgo.prototype.start = function(params) {
this.startPusher_();
};
blinktrade.SimpleMarketMarketAlgo.prototype.stop = function() {
this.ws_pusher_.close();
this.ws_pusher_ = null;
};
blinktrade.SimpleMarketMarketAlgo.prototype.startPusher_ = function() {
this.ws_pusher_ = new WebSocket('wss://ws.pusherapp.com/app/de504dc5763aeef9ff52?protocol=7&client=js&version=2.1.6&flash=false');
this.ws_pusher_.onopen = goog.bind(this.onPusherOpen_, this);
this.ws_pusher_.onmessage = goog.bind(this.onPusherMessage_, this);
this.ws_pusher_.onclose = goog.bind(this.onPusherClose_, this);
};
blinktrade.SimpleMarketMarketAlgo.prototype.onPusherOpen_ = function() {
this.ws_pusher_.send( JSON.stringify({"event":"pusher:subscribe","data":{"channel":"order_book"}}));
this.ws_pusher_.send( JSON.stringify({"event":"pusher:subscribe","data":{"channel":"live_trades"}}));
};
blinktrade.SimpleMarketMarketAlgo.prototype.onPusherClose_ = function() {
this.startPusher_();
};
blinktrade.SimpleMarketMarketAlgo.prototype.onPusherMessage_ = function (e) {
var msg = JSON.parse(e.data);
switch(msg["event"]) {
case 'pusher:error':
this.stop( msg["data"]["message"] );
break;
case 'pusher_internal:subscription_succeeded':
if (msg["channel"] == "order_book") {
this.bitstamp_order_book_channel_subscription_ = true;
}
break;
case 'data':
switch(msg["channel"]){
case "order_book":
this.onBitStampOrderBookData(JSON.parse(msg["data"]));
return;
case "live_trades":
this.onBitStampTrade(JSON.parse(msg["data"]));
return;
}
}
};
/**
* @param {Object} trade
*/
blinktrade.SimpleMarketMarketAlgo.prototype.onBitStampTrade = function(trade) {
var exchange_rate = this.application_.getParameters()['dollar'];
};
/**
* @param {Object.<string, Array.<Array.<number>>> } order_book
*/
blinktrade.SimpleMarketMarketAlgo.prototype.onBitStampOrderBookData = function(order_book) {
var exchange_rate = this.application_.getParameters()['dollar'];
var best_bid = parseInt(parseFloat(order_book['bids'][0][0]) * exchange_rate * 1e8,10);
var best_ask = parseInt(parseFloat(order_book['asks'][0][0]) * exchange_rate * 1e8,10);
var market_has_changed = false;
if (this.last_best_bid_ != best_bid) {
this.last_best_bid_ = best_bid;
market_has_changed = true;
}
if ( this.last_best_ask_ != best_ask ) {
this.last_best_ask_ = best_ask;
market_has_changed = true;
}
if (market_has_changed) {
this.cancelOrdersOutsideOfLimits();
this.makeTheMarket();
}
};
blinktrade.SimpleMarketMarketAlgo.prototype.calculateLimits = function() {
var params = this.application_.getParameters();
var best_bid = this.last_best_bid_;
var best_ask = this.last_best_ask_;
var spread = params["spread"];
if (best_bid == null && best_ask == null) {
this.application_.stop("No best bid/ask found.");
return;
}
// calculate the operation limits
this.min_limit_bid_price_ = null;
this.max_limit_bid_price_ = null;
this.min_limit_ask_price_ = null;
this.max_limit_ask_price_ = null;
if (best_bid != null) {
this.min_limit_bid_price_ = best_bid * ( 100 - spread/2 - spread/10 )/100 ;
this.max_limit_bid_price_ = best_bid * ( 100 - spread/2 )/100 ;
}
if (best_ask != null) {
this.min_limit_ask_price_ = best_ask * ( 100 + spread/2 )/100;
this.max_limit_ask_price_ = best_ask * ( 100 + spread/2 + spread/10 )/100;
}
return {
"min_limit_bid_price": this.min_limit_bid_price_,
"max_limit_bid_price": this.max_limit_bid_price_,
"min_limit_ask_price": this.min_limit_ask_price_,
"max_limit_ask_price": this.max_limit_ask_price_
};
};
blinktrade.SimpleMarketMarketAlgo.prototype.getMyVolume = function() {
var sum_buy_volume = 0;
var sum_sell_volume = 0;
var my_orders = this.application_.getOpenOrders();
for (var order_id in my_orders) {
var order = my_orders[order_id];
var is_an_order_sent_from_this_algo = (order['ClOrdID'].substr(0,4) == 'algo');
var volume_not_yet_executed = (order['LeavesQty'] * order['Price'] / 1e8 );
var is_buy_order = (order['Side'] == '1' );
if (is_an_order_sent_from_this_algo && is_buy_order){
sum_buy_volume += volume_not_yet_executed;
}
var is_sell_order = (order['Side'] == '2' );
if (is_an_order_sent_from_this_algo && is_sell_order){
sum_sell_volume += volume_not_yet_executed;
}
}
return [ sum_buy_volume, sum_sell_volume];
};
blinktrade.SimpleMarketMarketAlgo.prototype.makeTheMarket = function() {
if (!this.calculateLimits()) {
return;
}
var buy_sell_volume = this.getMyVolume();
var sum_buy_volume = buy_sell_volume[0];
var sum_sell_volume = buy_sell_volume[1];
// get customer balance
var balance_crypto = this.application_.getBalance(this.symbol_.substr(0,3), 'available' );
var balance_fiat = this.application_.getBalance(this.symbol_.substr(3,3), 'available' );
var bid_price = (this.min_limit_bid_price_ + this.max_limit_bid_price_) / 2 ;
var ask_price = (this.min_limit_ask_price_ + this.max_limit_ask_price_) / 2 ;
var params = this.application_.getParameters();
// find the quantity to buy
var limit_buy_volume = parseInt( params["limit"] * 1e8);
if (limit_buy_volume > balance_fiat) {
limit_buy_volume = balance_fiat;
}
var volume_to_buy = limit_buy_volume - sum_buy_volume;
var qty_to_buy = 0;
if ( volume_to_buy > 0 ) {
qty_to_buy = parseInt(volume_to_buy / bid_price * 1e8, 10);
}
// find the quantity to sell
var limit_sell_volume = parseInt( params["limit"] * 1e8 );
if (limit_sell_volume > (balance_crypto * ask_price / 1e8)) {
limit_sell_volume = (balance_crypto * ask_price / 1e8);
}
var volume_to_sell = limit_sell_volume - sum_sell_volume;
var qty_to_sell = parseInt(volume_to_sell / ask_price * 1e8 , 10) ;
if (qty_to_sell > balance_crypto) {
qty_to_sell = balance_crypto;
}
if (qty_to_buy > 100000){
this.application_.sendBuyLimitedOrder(qty_to_buy, bid_price);
}
if (qty_to_sell > 100000) {
this.application_.sendSellLimitedOrder(qty_to_sell, ask_price);
}
};
blinktrade.SimpleMarketMarketAlgo.prototype.cancelOrdersOutsideOfLimits = function(){
if (!this.calculateLimits()) {
return;
}
var my_orders = this.application_.getOpenOrders();
for (var order_id in my_orders) {
var order = my_orders[order_id];
var is_an_order_sent_from_this_algo = (order['ClOrdID'].substr(0,4) == 'algo');
if (!is_an_order_sent_from_this_algo){
// order was not sent from this algo .... just continue to the next order
continue;
}
var is_buy_order = (order['Side'] == '1' );
var is_sell_order = (order['Side'] == '2' );
//
// let's cancel the order in case we don't know the limits
//
if (is_buy_order && (this.min_limit_bid_price_ == null) ) {
this.application_.cancelOrder(order['ClOrdID']);
continue;
}
if (is_sell_order && (this.min_limit_ask_price_ == null) ) {
this.application_.cancelOrder(order['ClOrdID']);
continue;
}
//
// Let's now check if the order is within the limits
//
var is_order_outside_of_limits;
if (is_buy_order) {
is_order_outside_of_limits = !(order['Price'] >= this.min_limit_bid_price_ && order['Price'] <= this.max_limit_bid_price_);
} else if (is_sell_order) {
is_order_outside_of_limits = !(order['Price'] >= this.min_limit_ask_price_ && order['Price'] <= this.max_limit_ask_price_);
}
if (is_order_outside_of_limits) {
// this order not within the limits .... cancel it and go to the next order
this.application_.cancelOrder(order['ClOrdID']);
continue;
}
}
};
/**
* @param {Object.<string,*>} params
*/
blinktrade.SimpleMarketMarketAlgo.prototype.onUpdateParams = function(params) {
this.cancelOrdersOutsideOfLimits();
this.makeTheMarket();
};
/**
* Invoked whenever your balance change
* @param {string} currency
* @param {number} balance
*/
blinktrade.SimpleMarketMarketAlgo.prototype.onBalanceUpdate = function(currency, balance) {
this.cancelOrdersOutsideOfLimits();
this.makeTheMarket();
};
//-----END ALGO-----