UNPKG

3.85 kBJavaScriptView Raw
1var Bitstamp = require("bitstamp");
2var util = require('../util.js');
3var _ = require('underscore');
4var moment = require('moment');
5var log = require('../log');
6
7var Trader = function(config) {
8 if(_.isObject(config)) {
9 this.user = config.user;
10 this.password = config.password;
11 }
12 this.name = 'Bitstamp';
13 this.balance;
14 this.price;
15
16 _.bindAll(this);
17
18 this.bitstamp = new Bitstamp(this.user, this.password);
19}
20
21Trader.prototype.getTrades = function(since, callback, descending) {
22 // bitstamp asks for a `deltatime`, this is the amount of seconds
23 // ago from when to fetch trades
24 if(since)
25 var deltatime = moment.duration(moment() - since).asSeconds();
26 else
27 deltatime = 600;
28
29 deltatime = Math.round(deltatime);
30
31 var self = this;
32 var args = _.toArray(arguments);
33 setTimeout(function() {
34 self.bitstamp.transactions(deltatime, function(err, data) {
35 // console.log(err, data)
36 if(err)
37 return self.retry(self.getTrades, args);
38
39 if(!data || !data.length)
40 return self.retry(self.getTrades, args);
41
42 if(descending)
43 callback(data);
44 else
45 callback(data.reverse());
46 });
47 });
48}
49
50// if the exchange errors we try the same call again after
51// waiting 10 seconds
52Trader.prototype.retry = function(method, args) {
53 var wait = +moment.duration(10, 'seconds');
54 log.debug(this.name, 'returned an error, retrying..');
55
56 var self = this;
57
58 // make sure the callback (and any other fn)
59 // is bound to Trader
60 _.each(args, function(arg, i) {
61 if(_.isFunction(arg))
62 args[i] = _.bind(arg, self);
63 });
64
65 // run the failed method again with the same
66 // arguments after wait
67 setTimeout(
68 function() { method.apply(self, args) },
69 wait
70 );
71}
72
73Trader.prototype.getPortfolio = function(callback) {
74 var set = function(err, data) {
75 var portfolio = [];
76 _.each(data, function(amount, asset) {
77 if(asset.indexOf('available') !== -1) {
78 asset = asset.substr(0, 3).toUpperCase();
79 portfolio.push({name: asset, amount: parseFloat(amount)});
80 }
81 });
82 callback(err, portfolio);
83 }
84 this.bitstamp.balance(_.bind(set, this));
85}
86
87Trader.prototype.getTicker = function(callback) {
88 this.bitstamp.ticker(callback);
89}
90
91Trader.prototype.getFee = function(callback) {
92 var set = function(err, data) {
93 callback(err, data.fee / 100);
94 }
95 this.bitstamp.balance(_.bind(set, this));
96}
97
98Trader.prototype.buy = function(amount, price, callback) {
99 console.log(amount, price);
100 var set = function(err, result) {
101 console.log(err, result);
102 if(err || result.error)
103 return log.error('unable to buy:', err, result);
104
105 callback(err, result.id);
106 };
107
108 amount *= 0.995; // remove fees
109 // prevent: Ensure that there are no more than 8 digits in total.
110 amount *= 100000000;
111 amount = Math.floor(amount);
112 amount /= 100000000;
113 // return console.log(amount);
114 this.bitstamp.buy(amount, price, _.bind(set, this));
115}
116
117Trader.prototype.sell = function(amount, price, callback) {
118 console.log(amount, price);
119 var set = function(err, result) {
120 console.log(err, result);
121 if(err || result.error)
122 return log.error('unable to sell:', err, result);
123
124 callback(err, result.id);
125 };
126
127 this.bitstamp.sell(amount, price, _.bind(set, this));
128}
129
130Trader.prototype.checkOrder = function(order, callback) {
131 var check = function(err, result) {
132 var stillThere = _.find(result, function(o) { return o.id === order });
133 callback(err, !stillThere);
134 };
135
136 this.bitstamp.open_orders(_.bind(check, this));
137}
138
139Trader.prototype.cancelOrder = function(order, callback) {
140 var cancel = function(err, result) {
141 if(err || !result)
142 log.error('unable to cancel order', order, '(', err, result, ')');
143 };
144
145 this.bitstamp.cancel_orders(order, _.bind(cancel, this));
146}
147
148
149module.exports = Trader;
\No newline at end of file