Skip to content

Commit

Permalink
add init leech market
Browse files Browse the repository at this point in the history
  • Loading branch information
askmike committed Dec 4, 2016
1 parent f53a9c0 commit 33dd4b6
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 12 deletions.
88 changes: 88 additions & 0 deletions core/markets/leech.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// a leech market is "semi-realtime" and pulls out candles of a
// database (which is expected to be updated reguraly, like with a
// realtime market running in parralel).

const _ = require('lodash');
const moment = require('moment');

const util = require('../util');
const dirs = util.dirs();
const config = util.getConfig();

const adapter = config[config.adapter];
const Reader = require(dirs.gekko + adapter.path + '/reader');

const TICKINTERVAL = 20 * 1000; // 20 seconds

const exchanges = require(dirs.gekko + 'exchanges');
const exchange = _.find(exchanges, function(e) {
return e.slug === config.watch.exchange.toLowerCase();
});

if(!exchange)
util.die(`Unsupported exchange: ${config.watch.exchange.toLowerCase()}`)

const exchangeChecker = require(util.dirs().core + 'exchangeChecker');

const error = exchangeChecker.cantMonitor(config.watch);
if(error)
util.die(error, true);

if(config.market.from)
const fromTs = moment.utc(config.market.from).unix();
else
const fromTs = moment().startOf('minute').unix();

var Market = function() {

_.bindAll(this);

Readable.call(this, {objectMode: true});

this.reader = new Reader();
this.latestTs = fromTs;

setInterval(
this.get,
TICKINTERVAL
);
}

var Readable = require('stream').Readable;
Market.prototype = Object.create(Readable.prototype, {
constructor: { value: Market }
});

Market.prototype._read = _.once(function() {
this.get();
});

Market.prototype.get = function() {
var future = moment().add(1, 'minute').unix();

this.reader.get(
this.latestTs,
future,
'full',
this.processCandles
)
}

Market.prototype.processCandles = function(err, candles) {

var amount = _.size(candles);

if(amount === 0) {
// no new candles!
return;
}

_.each(candles, function(c, i) {
c.start = moment.unix(c.start).utc();
this.push(c);
}, this);

this.latestTs = _.last(candles).start + 1;
}

module.exports = Market;
10 changes: 8 additions & 2 deletions core/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,14 @@ var pipeline = (settings) => {
prepareMarket
],
function() {
// load a market based on the mode
var Market = require(dirs.markets + mode);
// load a market based on the config (or fallback to mode)
let marketType;
if(config.market)
marketType = config.market.type;
else
marketType = mode;

var Market = require(dirs.markets + marketType);

var market = new Market(config);
var gekko = new GekkoStream(candleConsumers);
Expand Down
5 changes: 3 additions & 2 deletions core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ var util = {
return _config;

if(program.config) {

// we will use one single config file
if(!fs.existsSync(configFile))
if(!fs.existsSync(util.dirs().gekko + program.config))
util.die('Cannot find the specified config file.');

_config = require(configFile);
_config = require(util.dirs().gekko + program.config);
return _config;
}

Expand Down
5 changes: 3 additions & 2 deletions plugins/tradingAdvisor/tradingAdvisor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var log = require(dirs.core + 'log');
var CandleBatcher = require(dirs.core + 'candleBatcher');

var moment = require('moment');
var isLeecher = config.market && config.market.type === 'leech';

var Actor = function(done) {
_.bindAll(this);
Expand All @@ -23,11 +24,11 @@ var Actor = function(done) {

var mode = util.gekkoMode();

if(mode === 'realtime') {
if(mode === 'realtime' && !isLeecher) {
var Stitcher = require(dirs.tools + 'dataStitcher');
var stitcher = new Stitcher(this.batcher);
stitcher.prepareHistoricalData(done);
} else if(mode === 'backtest')
} else
done();
}

Expand Down
28 changes: 22 additions & 6 deletions sample-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ config.debug = true; // for additional logging / debugging
config.watch = {

// see https://github.com/askmike/gekko#supported-exchanges
exchange: 'Bitstamp',
currency: 'USD',
exchange: 'Poloniex',
currency: 'USDT',
asset: 'BTC'
}

Expand All @@ -38,6 +38,22 @@ config.tradingAdvisor = {
}
}

config.MACD = {
// EMA weight (α)
// the higher the weight, the more smooth (and delayed) the line
short: 10,
long: 21,
signal: 9,
// the difference between the EMAs (to act as triggers)
thresholds: {
down: -0.025,
up: 0.025,
// How many candle intervals should a trend persist
// before we consider it real?
persistence: 1
}
};

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CONFIGURING PLUGINS
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -53,7 +69,7 @@ config.trader = {
}

config.adviceLogger = {
enabled: true,
enabled: false,
muteSoft: true // disable advice printout if it's soft
}

Expand Down Expand Up @@ -161,12 +177,10 @@ config.redisBeacon = {
}

config.candleWriter = {
adapter: 'sqlite',
enabled: true
enabled: false
}

config.adviceWriter = {
adapter: 'mongodb',
enabled: false,
muteSoft: true,
}
Expand All @@ -175,6 +189,8 @@ config.adviceWriter = {
// CONFIGURING ADAPTER
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

config.adapter = 'sqlite';

config.sqlite = {
path: 'plugins/sqlite',

Expand Down

0 comments on commit 33dd4b6

Please sign in to comment.