Skip to content

Commit

Permalink
Merge branch 'xhad-develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
askmike committed Jul 9, 2016
2 parents a322e8e + e41ff50 commit 848edf1
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 90 deletions.
24 changes: 13 additions & 11 deletions core/budfox/candleCreator.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// The CandleCreator creates one minute candles based on trade batches. Note
// The CandleCreator creates one minute candles based on trade batches. Note
// that it also adds empty candles to fill gaps with no volume.
//
//
// Expects trade batches to be written like:
//
//
// {
// amount: x,
// start: (moment),
Expand All @@ -11,13 +11,13 @@
// last: (trade),
// timespan: x,
// all: [
// // batch of new trades with
// // batch of new trades with
// // moments instead of timestamps
// ]
// }
//
//
// Emits 'new candles' event with:
//
//
// [
// {
// start: (moment),
Expand All @@ -41,7 +41,7 @@
// }
// // etc.
// ]
//
//

var _ = require('lodash');
var moment = require('moment');
Expand Down Expand Up @@ -104,12 +104,14 @@ CandleCreator.prototype.fillBuckets = function(trades) {
CandleCreator.prototype.calculateCandles = function() {
var minutes = _.size(this.buckets);

// create a string referencing to minute this trade happened in
var lastMinute = this.lastTrade.date.format('YYYY-MM-DD HH:mm');
// catch error from high volume getTrades
if (this.lastTrade !== undefined)
// create a string referencing to minute this trade happened in
var lastMinute = this.lastTrade.date.format('YYYY-MM-DD HH:mm');

var candles = _.map(this.buckets, function(bucket, name) {
var candle = this.calculateCandle(bucket);

// clean all buckets, except the last one:
// this candle is not complete
if(name !== lastMinute)
Expand Down Expand Up @@ -151,7 +153,7 @@ CandleCreator.prototype.calculateCandle = function(trades) {

// Gekko expects a candle every minute, if nothing happened
// during a particilar minute Gekko will add empty candles with:
//
//
// - open, high, close, low, vwp are the same as the close of the previous candle.
// - trades, volume are 0
CandleCreator.prototype.addEmptyCandles = function(candles) {
Expand Down
3 changes: 2 additions & 1 deletion core/budfox/heart.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Heart.prototype.determineLiveTickRate = function() {
var seconds = 2;
else
var seconds = 20;

this.tickRate = +moment.duration(seconds, 's');
}

Expand All @@ -38,4 +39,4 @@ Heart.prototype.scheduleTicks = function() {
_.defer(this.tick);
}

module.exports = Heart;
module.exports = Heart;
14 changes: 7 additions & 7 deletions core/portfolioManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Manager.prototype.setPortfolio = function(callback) {
util.die(err);

this.portfolio = portfolio;

if(_.isFunction(callback))
callback();
}.bind(this);
Expand All @@ -84,7 +84,7 @@ Manager.prototype.setFee = function(callback) {

if(err)
util.die(err);

if(_.isFunction(callback))
callback();
};
Expand All @@ -94,7 +94,7 @@ Manager.prototype.setFee = function(callback) {
Manager.prototype.setTicker = function(callback) {
var set = function(err, ticker) {
this.ticker = ticker;

if(_.isFunction(callback))
callback();
}
Expand All @@ -111,11 +111,11 @@ Manager.prototype.getBalance = function(fund) {

// This function makes sure order get to the exchange
// and initiates follow up to make sure the orders will
// get executed. This is the backbone of the portfolio
// get executed. This is the backbone of the portfolio
// manager.
//
//
// How this is done depends on a couple of things:
//
//
// is this a directExchange? (does it support MKT orders)
// is this a infinityOrderExchange (does it support order
// requests bigger then the current balance?)
Expand Down Expand Up @@ -285,7 +285,7 @@ Manager.prototype.checkOrder = function() {
// an error on cex.io if they happen on the same
// unix timestamp second (nonce will not increment).
var self = this;
setTimeout(function() { self.trade(self.action); }, 1000);
setTimeout(function() { self.trade(self.action); }, 500);
return;
}

Expand Down
7 changes: 4 additions & 3 deletions exchanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,12 @@ var exchanges = [
currencies: ['BTC', 'CNY'],
assets: ['BTC', 'LTC'],
markets: [
{ pair: ['CNY', 'BTC'], minimalOrder: { amount: 0.001, unit: 'asset' } }
{ pair: ['CNY', 'BTC'], minimalOrder: { amount: 0.01, unit: 'asset' } },
{ pair: ['CNY', 'LTC'], minimalOrder: { amount: 0.01, unit: 'asset' } }
],
requires: ['key', 'secret', 'username'],
// TODO:
providesHistory: true,
providesHistory: false,
fetchTimespan: 60,
maxHistoryFetch: 600,
tid: 'date',
},
Expand Down
43 changes: 17 additions & 26 deletions exchanges/bitfinex.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ var _ = require('lodash');
var moment = require('moment');
var log = require('../core/log');

// Module-wide constants
var exchangeName = 'bitfinex';
var symbol;

var Trader = function(config) {
_.bindAll(this);
if(_.isObject(config)) {
Expand All @@ -18,9 +14,8 @@ var Trader = function(config) {
this.name = 'Bitfinex';
this.balance;
this.price;
symbol = config.asset + config.currency;
this.bitfinex = new Bitfinex(this.key, this.secret);
this.bitfinex = this.bitfinex.rest;
this.pair = config.asset + config.currency;
this.bitfinex = new Bitfinex(this.key, this.secret).rest;
}

// if the exchange errors we try the same call again after
Expand Down Expand Up @@ -49,40 +44,37 @@ Trader.prototype.retry = function(method, args) {
Trader.prototype.getPortfolio = function(callback) {
this.bitfinex.wallet_balances(function (err, data, body) {
var portfolio = _(data).filter(function(data) {
return data.type === 'exchange';
return data.type === 'exchange'
}).map(function (asset) {
return {
name: asset.currency.toUpperCase(),
// TODO: use .amount instead of .available?
amount: +asset.available
amount: asset.available
}
}).value();
callback(err, portfolio);
});
}

Trader.prototype.getTicker = function(callback) {
var args = [this.pair, process]
// the function that will handle the API callback
var process = function(err, data, body) {
if (err) {
if (err)
// on error we need to recurse this function

// however we don't want to hit any API ratelimits
// so we use this.retry since this will wait first
// before we retry.
// the arguments we need to pass the the ticker method

var args = [ symbol, process ];
return this.retry(bitfinex.ticker(args));
//>> Thanks Mike :)
return this.retry(this.bitfinex.ticker(args));
}

// whenever we reach this point we have valid
// data, the callback is still the same since
// we are inside the same javascript scope.
callback(err, {bid: +data.bid, ask: +data.ask})
}.bind(this);

this.bitfinex.ticker(symbol, process);
this.bitfinex.ticker(args);
}

// This assumes that only limit orders are being placed, so fees are the
Expand All @@ -92,11 +84,10 @@ Trader.prototype.getFee = function(callback) {
callback(false, makerFee / 100);
}

function submit_order(bfx, type, amount, price, callback) {
// TODO: Bitstamp module included the following - is it necessary?
// amount *= 0.995; // remove fees
Trader.prototype.function = function (submit_order(type, amount, price, callback) {
amount = Math.floor(amount*100000000)/100000000;
bfx.new_order(symbol, amount + '', price + '', exchangeName,

this.bitfinex.new_order(this.pair, amount + '', price + '', this.name,
type,
'exchange limit',
function (err, data, body) {
Expand All @@ -107,13 +98,13 @@ function submit_order(bfx, type, amount, price, callback) {
});
}


Trader.prototype.buy = function(amount, price, callback) {
submit_order(this.bitfinex, 'buy', amount, price, callback);
this.submit_order(this.bitfinex, 'buy', amount, price, callback);

}

Trader.prototype.sell = function(amount, price, callback) {
submit_order(this.bitfinex, 'sell', amount, price, callback);
this.submit_order(this.bitfinex, 'sell', amount, price, callback);
}

Trader.prototype.checkOrder = function(order_id, callback) {
Expand All @@ -133,7 +124,7 @@ Trader.prototype.getTrades = function(since, callback, descending) {
var args = _.toArray(arguments);
var self = this;

var path = symbol;
var path = this.pair;
if(since)
path += '?limit_trades=' + since;

Expand All @@ -154,4 +145,4 @@ Trader.prototype.getTrades = function(since, callback, descending) {
});
}

module.exports = Trader;
module.exports = Trader;
Loading

0 comments on commit 848edf1

Please sign in to comment.