@@ -64,7 +64,7 @@ Trader.prototype.getPortfolio = function(callback) {
64
64
if ( err ) return callback ( err ) ;
65
65
66
66
// We are only interested in funds in the "exchange" wallet
67
- data = data . filter ( c => c . type === 'exchange ' ) ;
67
+ data = data . filter ( c => c . type === 'trading ' ) ;
68
68
69
69
const asset = _ . find ( data , c => c . currency . toUpperCase ( ) === this . asset ) ;
70
70
const currency = _ . find ( data , c => c . currency . toUpperCase ( ) === this . currency ) ;
@@ -85,15 +85,30 @@ Trader.prototype.getPortfolio = function(callback) {
85
85
currencyAmount = 0 ;
86
86
}
87
87
88
- const portfolio = [
89
- { name : this . asset , amount : assetAmount } ,
90
- { name : this . currency , amount : currencyAmount } ,
91
- ] ;
92
-
93
- callback ( undefined , portfolio ) ;
88
+ let processPositions = ( err , operations ) => {
89
+ if ( err ) return callback ( err ) ;
90
+ if ( operations == undefined ) {
91
+ log . error ( "got undefined operations list" , err ) ;
92
+ return callback ( )
93
+ }
94
+
95
+ operations . forEach ( ( operation ) => {
96
+ if ( operation . symbol . toUpperCase ( ) . substr ( 0 , 3 ) == this . asset ) {
97
+ assetAmount += parseFloat ( operation . amount )
98
+ }
99
+ } )
100
+
101
+ const portfolio = [
102
+ { name : this . asset , amount : assetAmount } ,
103
+ { name : this . currency , amount : currencyAmount } ,
104
+ ] ;
105
+
106
+ callback ( undefined , portfolio ) ;
107
+ } ;
108
+ this . bitfinex . active_positions ( this . handleResponse ( 'getPortfolio' , processPositions ) )
94
109
} ;
95
110
96
- let handler = ( cb ) => this . bitfinex . wallet_balances ( this . handleResponse ( 'getPortfolio' , cb ) ) ;
111
+ let handler = ( cb ) => this . bitfinex . wallet_balances ( this . handleResponse ( 'getPortfolio' , cb ) )
97
112
util . retryCustom ( retryForever , _ . bind ( handler , this ) , _ . bind ( process , this ) ) ;
98
113
}
99
114
@@ -118,32 +133,33 @@ Trader.prototype.getFee = function(callback) {
118
133
callback ( undefined , makerFee / 100 ) ;
119
134
}
120
135
121
- Trader . prototype . submit_order = function ( type , amount , price , callback ) {
136
+ Trader . prototype . submit_order = function ( side , amount , price , callback , type ) {
122
137
let process = ( err , data ) => {
123
138
if ( err ) return callback ( err ) ;
124
139
125
140
callback ( err , data . order_id ) ;
126
141
}
127
142
143
+
128
144
amount = Math . floor ( amount * 100000000 ) / 100000000 ;
129
145
let handler = ( cb ) => this . bitfinex . new_order ( this . pair ,
130
146
amount + '' ,
131
147
price + '' ,
132
148
this . name . toLowerCase ( ) ,
149
+ side ,
133
150
type ,
134
- 'exchange limit' ,
135
151
this . handleResponse ( 'submitOrder' , cb )
136
152
) ;
137
153
138
154
util . retryCustom ( retryCritical , _ . bind ( handler , this ) , _ . bind ( process , this ) ) ;
139
155
}
140
156
141
157
Trader . prototype . buy = function ( amount , price , callback ) {
142
- this . submit_order ( 'buy' , amount , price , callback ) ;
158
+ this . submit_order ( 'buy' , amount , price , callback , 'limit' ) ;
143
159
}
144
160
145
161
Trader . prototype . sell = function ( amount , price , callback ) {
146
- this . submit_order ( 'sell' , amount , price , callback ) ;
162
+ this . submit_order ( 'sell' , amount , price , callback , 'limit' ) ;
147
163
}
148
164
149
165
Trader . prototype . checkOrder = function ( order_id , callback ) {
0 commit comments