UNPKG

3.34 kBJavaScriptView Raw
1var BTCE = require('btc-e');
2
3var moment = require('moment');
4var util = require('../util');
5var _ = require('underscore');
6var log = require('../log')
7
8var Trader = function(config) {
9 this.key = config.key;
10 this.secret = config.secret;
11 this.pair = 'btc_' + config.currency.toLowerCase();
12 this.name = 'BTC-E';
13
14 _.bindAll(this);
15
16 this.btce = new BTCE(this.key, this.secret);
17}
18
19Trader.prototype.buy = function(amount, price, callback) {
20 // Prevent "You incorrectly entered one of fields."
21 // because of more than 8 decimals.
22 amount *= 100000000;
23 amount = Math.floor(amount);
24 amount /= 100000000;
25
26 var set = function(err, data) {
27 if(err)
28 log.error('unable to buy:', err);
29
30 callback(data.order_id);
31 };
32
33 // workaround for nonce error
34 setTimeout(_.bind(function() {
35 this.btce.trade(this.pair, 'buy', price, amount, _.bind(set, this));
36 }, this), 1000);
37}
38
39Trader.prototype.sell = function(amount, price, callback) {
40 // Prevent "You incorrectly entered one of fields."
41 // because of more than 8 decimals.
42 amount *= 100000000;
43 amount = Math.ceil(amount);
44 amount /= 100000000;
45
46 var set = function(err, data) {
47 if(err)
48 log.error('unable to sell:', err);
49
50 callback(err, data.order_id);
51 };
52
53 // workaround for nonce error
54 setTimeout(_.bind(function() {
55 this.btce.trade(this.pair, 'sell', price, amount, _.bind(set, this));
56 }, this), 1000);
57}
58
59// if BTC-e errors we try the same call again after
60// 5 seconds or half a second if there is haste
61Trader.prototype.retry = function(method, callback, haste) {
62 var wait = +moment.duration(haste ? 0.5 : 5, 'seconds');
63 log.debug(this.name , 'returned an error, retrying..');
64 setTimeout(
65 _.bind(method, this),
66 wait,
67 _.bind(callback, this)
68 );
69}
70
71Trader.prototype.getPortfolio = function(callback) {
72 var calculate = function(err, data) {
73 if(err)
74 return this.retry(this.btce.getInfo, calculate);
75
76 var portfolio = [];
77 _.each(data.funds, function(amount, asset) {
78 portfolio.push({name: asset.toUpperCase(), amount: amount});
79 });
80 callback(err, portfolio);
81 }
82 this.btce.getInfo(_.bind(calculate, this));
83}
84
85Trader.prototype.getTicker = function(callback) {
86 // BTCE-e doesn't state asks and bids in its
87 // ticker
88 var set = function(err, data) {
89 var ticker = _.extend(data.ticker, {
90 ask: data.ticker.buy,
91 bid: data.ticker.sell
92 });
93 callback(err, ticker);
94 }
95 this.btce.ticker(this.pair, _.bind(set, this));
96}
97
98Trader.prototype.getFee = function(callback) {
99 // BTCE-e doesn't have different fees based on orders
100 // at this moment it is always 0.2%
101 callback(false, 0.002);
102}
103
104Trader.prototype.checkOrder = function(order, callback) {
105 var check = function(err, result) {
106 // btce returns an error when you have no open trades
107 // right now we assume on every error that the order
108 // was filled.
109 //
110 // TODO: check whether the error stats that there are no
111 // open trades or that there is something else.
112 if(err)
113 callback(false, true);
114 else
115 callback(err, !result[order]);
116 };
117
118 this.btce.orderList({}, _.bind(check, this));
119}
120
121Trader.prototype.cancelOrder = function(order) {
122 // TODO: properly test
123 var devNull = function() {}
124 this.btce.orderList(order, devNull);
125}
126
127module.exports = Trader;
\No newline at end of file