Skip to content

Commit 9ad18fc

Browse files
committed
better balance interface
1 parent f68a55a commit 9ad18fc

File tree

5 files changed

+231
-58
lines changed

5 files changed

+231
-58
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
.idea/

algorithm_application.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var Application = function( instance_id, websocket_url, symbol, open_orders, alg
3333
this.status_started_ = false;
3434
this.trade_history_ = [];
3535
this.order_book_ = {};
36+
this.balance_ = {};
3637

3738
this.ws_ = new WebSocket(this.websocket_url_);
3839

@@ -57,7 +58,7 @@ Application.prototype.instance_;
5758
* @return {number} Returns the clientOrderId for this order.
5859
*/
5960
Application.prototype.sendBuyLimitedOrder = function( qty, price, opt_clientOrderId ) {
60-
var clientOrderId = opt_clientOrderId || parseInt( 1e7 * Math.random() , 10 );
61+
var clientOrderId = opt_clientOrderId || 'algo_' + parseInt( 1e7 * Math.random() , 10 );
6162

6263
postMessage({ 'rep':'new_order_limited',
6364
'instance':this.instance_id_,
@@ -109,7 +110,7 @@ Application.prototype.cancelAllOrders = function() {
109110
* @return {number} Returns the clientOrderId for this order.
110111
*/
111112
Application.prototype.sendSellLimitedOrder = function( qty, price, opt_clientOrderId ) {
112-
var clientOrderId = opt_clientOrderId || parseInt( 1e7 * Math.random() , 10 );
113+
var clientOrderId = opt_clientOrderId || 'algo_' + parseInt( 1e7 * Math.random() , 10 );
113114

114115
postMessage({ 'rep':'new_order_limited',
115116
'instance':this.instance_id_,
@@ -136,6 +137,24 @@ Application.prototype.getOrderBook = function() {
136137
return this.order_book_[this.selected_symbol_];
137138
};
138139

140+
/**
141+
* Returns user balance for the given currency
142+
* @param {string} currency
143+
* @param {AlgorithmTradingInterface.BalanceType} type
144+
* return {number}
145+
*/
146+
Application.prototype.getBalance = function( currency, type ) {
147+
if (type == "deposit") {
148+
return this.balance_[currency];
149+
150+
} if (type == "available") {
151+
return this.balance_[currency] - this.balance_[currency + '_locked'];
152+
}
153+
return this.balance_[currency + '_' + type];
154+
};
155+
156+
157+
139158
/**
140159
* Return an array containing all trades that occurred in the past 24 hours
141160
* @return {Array.<Object>}
@@ -319,8 +338,15 @@ Application.prototype.terminate_ = function(error_message) {
319338
* @param {Object} msg
320339
*/
321340
Application.prototype.processBalanceMsg_ = function(msg) {
341+
goog.object.extend(this.balance_, msg);
322342
try {
323-
this.instance_.onBalanceUpdate(msg);
343+
goog.object.forEach( msg, function(balance, currency ) {
344+
if (currency.substring(4) == 'locked') {
345+
this.instance_.onBalanceUpdate(currency.substring(0,3), balance, 'locked');
346+
} else {
347+
this.instance_.onBalanceUpdate(currency, balance, 'deposit');
348+
}
349+
}, this );
324350
} catch(e) {}
325351
postMessage({'rep':'balance', 'instance':this.instance_id_});
326352
};
@@ -611,6 +637,7 @@ goog.exportProperty(Application.prototype, 'cancelAllOrders', Application.protot
611637
goog.exportProperty(Application.prototype, 'cancelOrder', Application.prototype.cancelOrder);
612638
goog.exportProperty(Application.prototype, 'getOrderBook', Application.prototype.getOrderBook);
613639
goog.exportProperty(Application.prototype, 'getTrades', Application.prototype.getTrades);
640+
goog.exportProperty(Application.prototype, 'getBalance', Application.prototype.getBalance);
614641
goog.exportProperty(Application.prototype, 'getParameters', Application.prototype.getParameters);
615642
goog.exportProperty(Application.prototype, 'getOpenOrders', Application.prototype.getOpenOrders);
616643
goog.exportProperty(Application.prototype, 'getMarket', Application.prototype.getMarket);

algorithm_interface.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
*/
77
var AlgorithmTradingInterface = function(application, symbol){};
88

9+
/**
10+
* @enum {string} Balance Types
11+
*/
12+
AlgorithmTradingInterface.BalanceType = {
13+
DEPOSIT : 'deposit',
14+
LOCKED: 'locked',
15+
AVAILABLE: 'available'
16+
};
917

1018
/**
1119
* Invoked when your algorithm is ready to run.
@@ -22,9 +30,11 @@ AlgorithmTradingInterface.prototype.stop = function() { };
2230

2331
/**
2432
* Invoked whenever your balance change
25-
* @param {Object.<string,string|number>} msg
33+
* @param {string} currency
34+
* @param {number} balance
35+
* @param {AlgorithmTradingInterface.BalanceType} balance_type
2636
*/
27-
AlgorithmTradingInterface.prototype.onBalanceUpdate = function(msg) { };
37+
AlgorithmTradingInterface.prototype.onBalanceUpdate = function(currency, balance, balance_type) { };
2838

2939

3040
/**

0 commit comments

Comments
 (0)