-
Notifications
You must be signed in to change notification settings - Fork 109
/
follow_mercadobitcoin.algo
109 lines (90 loc) · 3.01 KB
/
follow_mercadobitcoin.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
/*
-----BEGIN ALGO DEFINITION-----
{
"id": "bitstamp",
"description": "Notifies the user when there is a new best bid/ask at MercadoBitcoin",
"params": [],
"creator": "blinktrade.FollowMercadoBitcoin.create",
"destructor": "blinktrade.FollowMercadoBitcoin.destroy",
"permissions": ["notification", "balance", "execution_report", "new_order_limited", "cancel_order"]
}
-----END ALGO DEFINITION-----
-----BEGIN ALGO-----
/**/
/**
* Namespace.
*/
var blinktrade = {};
/**
* Workaround using yahoo yql api to make cross domain requests
* @param {string} url
* @param {function(*)} fn
* @param {Object} selfObj
*/
function crossDomainRequest( url, fn, selfObj) {
var callback_function_name = 'yqlCallback' + parseInt(Math.random() * 1000000, 10);
self[callback_function_name] = function( yqlResult ) {
goog.bind(fn,selfObj, yqlResult.query.results)();
};
var yql = 'select * from json where url="' + url + '"';
var uri = 'https://query.yahooapis.com/v1/public/yql?q=' +
encodeURIComponent(yql) + '&format=json&callback=' + callback_function_name ;
importScripts(uri);
}
/**
* @param {Object} application
* @param {string} symbol
* @constructor
*/
blinktrade.FollowMercadoBitcoin = function(application, symbol){
this.application_ = application;
this.symbol_ = symbol;
global_instance = this;
};
/**
* @type {number}
*/
blinktrade.FollowMercadoBitcoin.prototype.last_best_bid_;
/**
* @type {number}
*/
blinktrade.FollowMercadoBitcoin.prototype.last_best_ask_;
/**
* @param {Application} application
* @param {string} symbol
* @return {blinktrade.FollowMercadoBitcoin}
*/
blinktrade.FollowMercadoBitcoin.create = function(application,symbol) {
return new blinktrade.FollowMercadoBitcoin(application,symbol);
};
/**
* @param {Object} params
*/
blinktrade.FollowMercadoBitcoin.prototype.start = function(params) {
console.log('blinktrade.FollowMercadoBitcoin.prototype.start');
this.timer_ = setInterval(goog.bind(crossDomainRequest,
this,
'https://www.mercadobitcoin.net/api/v2/ticker/',
this.onMBCallBack_,
this ), 6000 ); // every 60 seconds
};
blinktrade.FollowMercadoBitcoin.prototype.stop = function() {
clearInterval(this.timer_);
};
/**
* @param {Object.<string,*>} params
*/
blinktrade.FollowMercadoBitcoin.prototype.onUpdateParams = function(params) {};
blinktrade.FollowMercadoBitcoin.prototype.onMBCallBack_ = function(ticker){
var best_bid = parseFloat(ticker['ticker']['buy']);
var best_ask = parseFloat(ticker['ticker']['sell']);
if (this.last_best_bid_ != best_bid) {
this.last_best_bid_ = best_bid;
this.application_.showNotification('MercadoBitcoin', 'The new best bid is ' + best_bid, 'success' );
}
if ( this.last_best_ask_ != best_ask ) {
this.last_best_ask_ = best_ask;
this.application_.showNotification('MercadoBitcoin', 'The new best ask is ' + best_ask, 'info' );
}
};
//-----END ALGO-----