@@ -33,6 +33,7 @@ var Application = function( instance_id, websocket_url, symbol, open_orders, alg
33
33
this . status_started_ = false ;
34
34
this . trade_history_ = [ ] ;
35
35
this . order_book_ = { } ;
36
+ this . balance_ = { } ;
36
37
37
38
this . ws_ = new WebSocket ( this . websocket_url_ ) ;
38
39
@@ -57,7 +58,7 @@ Application.prototype.instance_;
57
58
* @return {number } Returns the clientOrderId for this order.
58
59
*/
59
60
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 ) ;
61
62
62
63
postMessage ( { 'rep' :'new_order_limited' ,
63
64
'instance' :this . instance_id_ ,
@@ -109,7 +110,7 @@ Application.prototype.cancelAllOrders = function() {
109
110
* @return {number } Returns the clientOrderId for this order.
110
111
*/
111
112
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 ) ;
113
114
114
115
postMessage ( { 'rep' :'new_order_limited' ,
115
116
'instance' :this . instance_id_ ,
@@ -136,6 +137,24 @@ Application.prototype.getOrderBook = function() {
136
137
return this . order_book_ [ this . selected_symbol_ ] ;
137
138
} ;
138
139
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
+
139
158
/**
140
159
* Return an array containing all trades that occurred in the past 24 hours
141
160
* @return {Array.<Object> }
@@ -319,8 +338,15 @@ Application.prototype.terminate_ = function(error_message) {
319
338
* @param {Object } msg
320
339
*/
321
340
Application . prototype . processBalanceMsg_ = function ( msg ) {
341
+ goog . object . extend ( this . balance_ , msg ) ;
322
342
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 ) ;
324
350
} catch ( e ) { }
325
351
postMessage ( { 'rep' :'balance' , 'instance' :this . instance_id_ } ) ;
326
352
} ;
@@ -611,6 +637,7 @@ goog.exportProperty(Application.prototype, 'cancelAllOrders', Application.protot
611
637
goog . exportProperty ( Application . prototype , 'cancelOrder' , Application . prototype . cancelOrder ) ;
612
638
goog . exportProperty ( Application . prototype , 'getOrderBook' , Application . prototype . getOrderBook ) ;
613
639
goog . exportProperty ( Application . prototype , 'getTrades' , Application . prototype . getTrades ) ;
640
+ goog . exportProperty ( Application . prototype , 'getBalance' , Application . prototype . getBalance ) ;
614
641
goog . exportProperty ( Application . prototype , 'getParameters' , Application . prototype . getParameters ) ;
615
642
goog . exportProperty ( Application . prototype , 'getOpenOrders' , Application . prototype . getOpenOrders ) ;
616
643
goog . exportProperty ( Application . prototype , 'getMarket' , Application . prototype . getMarket ) ;
0 commit comments