<commit_before>var _ = require('lodash');
var Promise = require('bluebird');
var Client = require('bitstamp');    

module.exports = function(conf, trader){
    _.extend(this, conf);
    var self = this;

    var initDeferred = Promise.defer();
    this.initialized = initDeferred.promise;

    this.client = new Client(this.key, this.secret, this.customer_id);

    // var commands = ['transactions', 'ticker', 'order_book', 'bitinstant', 'eur_usd',
    //                 'balance', 'user_transactions', 'open_orders', 'cancel_order', 'buy',
    //                 'sell', 'withdrawal_requests', 'bitcoin_withdrawal', 'bitcoin_deposit_address',
    //                 'unconfirmed_btc', 'ripple_withdrawal', 'ripple_address'
    // ];

    var commands = ['transactions', 'ticker', 'order_book', 'bitinstant', 'eur_usd',
                    'balance', 'user_transactions', 'open_orders', 'cancel_order', 'buy',
                    'sell'];


    commands.forEach(function(command){
        self.client[command] = Promise.promisify(self.client[command]);
    })

    this.getSpread = function(currency){ // only USD ATM.
        return self.client.order_book('btcusd').then(function(data){
            _.extend(data, {exchange: 'bitstamp', currency: currency});
            trader.emit('spread_data', data);
            return data;
        });

    };

    function balanceMapper(data){
        return {
            'BTC' : parseFloat(data.btc_available),
            'USD' : parseFloat(data.usd_available)
        };
    }
    this.watch = function(currency, eventEmitter){
        var Pusher = require('pusher-client');
        var socket = new Pusher('de504dc5763aeef9ff52');
        var channel = socket.subscribe('order_book');
        channel.bind('data', function(data) {
            _.extend(data, {exchange: 'bitstamp', currency: 'USD'});
            eventEmitter.emit('spread_data', data);
            // storeAndProcessData(data, 'bitstamp');
            // console.log('bitstamp_data');
            // detectArbitrageForExchange('bitstamp');
        });
    }
    this.getBalance = function(){
        return self.client.balance().then(function(data) {
            self.fee = data.fee / 100 || self.fee;
            data = balanceMapper(data);
            self.balance = data;
            if(_.isNaN(self.balance.USD) || _.isNaN(self.balance.BTC)){
                throw new Error('Data received for Bitstamp balance is NaN');
            }

            console.log('bitstamp balance: ', data);
            return data;
        });
    }
    this.getBalance().then(function(){
        initDeferred.resolve();
    });

}<commit_msg>more bug handling for bitstamp<commit_after>var _ = require('lodash');
var Promise = require('bluebird');
var Client = require('bitstamp');    

module.exports = function(conf, trader){
    _.extend(this, conf);
    var self = this;

    var initDeferred = Promise.defer();
    this.initialized = initDeferred.promise;

    this.client = new Client(this.key, this.secret, this.customer_id);

    // var commands = ['transactions', 'ticker', 'order_book', 'bitinstant', 'eur_usd',
    //                 'balance', 'user_transactions', 'open_orders', 'cancel_order', 'buy',
    //                 'sell', 'withdrawal_requests', 'bitcoin_withdrawal', 'bitcoin_deposit_address',
    //                 'unconfirmed_btc', 'ripple_withdrawal', 'ripple_address'
    // ];

    var commands = ['transactions', 'ticker', 'order_book', 'bitinstant', 'eur_usd',
                    'balance', 'user_transactions', 'open_orders', 'cancel_order', 'buy',
                    'sell'];


    commands.forEach(function(command){
        self.client[command] = Promise.promisify(self.client[command]);
    })

    this.getSpread = function(currency){ // only USD ATM.
        return self.client.order_book('btcusd').then(function(data){
            _.extend(data, {exchange: 'bitstamp', currency: currency});
            trader.emit('spread_data', data);
            return data;
        });

    };

    function balanceMapper(data){
        return {
            'BTC' : parseFloat(data.btc_available),
            'USD' : parseFloat(data.usd_available)
        };
    }
    this.watch = function(currency, eventEmitter){
        var Pusher = require('pusher-client');
        var socket = new Pusher('de504dc5763aeef9ff52');
        var channel = socket.subscribe('order_book');
        channel.bind('data', function(data) {
            _.extend(data, {exchange: 'bitstamp', currency: 'USD'});
            eventEmitter.emit('spread_data', data);
            // storeAndProcessData(data, 'bitstamp');
            // console.log('bitstamp_data');
            // detectArbitrageForExchange('bitstamp');
        });
    }
    this.getBalance = function(){
        return self.client.balance().then(function(data) {
            self.fee = data.fee / 100 || self.fee;
            self.balance = balanceMapper(data);
            if(_.isNaN(self.balance.USD) || _.isNaN(self.balance.BTC)){
                console.log('error in bitstamp balance; ', data);
                throw new Error('Data received for Bitstamp balance is NaN');
            }
            return data;
        });
    }
    this.getBalance().then(function(){
        initDeferred.resolve();
    }).catch(Error, function(e){
        initDeferred.reject(e);
    }).error(function(e){
        initDeferred.reject(e);
    });

}
{'fraction_non_alphanumeric': 0.15008307181096547, 'fraction_numerical': 0.004799704633561012, 'mean_word_length': 2.424778761061947, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2911436', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1569, 'n_words': 289}