Skip to content

Commit

Permalink
use TOML files for configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
askmike committed Dec 3, 2016
1 parent d7e1246 commit 617ed04
Show file tree
Hide file tree
Showing 16 changed files with 146 additions and 78 deletions.
4 changes: 4 additions & 0 deletions config/adapters/mongodb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
connectionString = "mongodb://mongodb/gekko"
dependencies = [{"module"=>"mongojs", "version"=>"2.4.0"}]
path = "plugins/mongodb"
version = 0.1
4 changes: 4 additions & 0 deletions config/adapters/postgresql.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
connectionString = "postgres://user:pass@localhost:5432"
dependencies = [{"module"=>"pg", "version"=>"6.1.0"}]
path = "plugins/postgresql"
version = 0.1
7 changes: 7 additions & 0 deletions config/adapters/sqlite.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dataDirectory = "history"
path = "plugins/sqlite"
version = 0.1

[[dependencies]]
module = "sqlite3"
version = "3.1.4"
10 changes: 10 additions & 0 deletions config/general.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
debug = true

# what database should Gekko use?
adapter = 'sqlite'

[watch]
exchange = 'Bitstamp'
currency = 'USD'
asset = 'BTC'

3 changes: 1 addition & 2 deletions config/plugins/candleWriter.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
enabled = true
sqlite = "sqlite"
enabled = true
16 changes: 16 additions & 0 deletions config/plugins/pushbullet.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
enabled = false

# Send 'Gekko starting' message if true
sendMessageOnStart = true

# disable advice printout if it's soft
muteSoft = true

# your email, change it unless you are Azor Ahai
email = "jon_snow@westeros.org"

# your pushbullet API key
key = "xxx"

# will make Gekko messages start with [GEKKO]
tag = "[GEKKO]"
9 changes: 9 additions & 0 deletions config/plugins/trader.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
enabled = false

## NOTE: once you filled in the following
## never share this file with anyone!

key = ""
secret = ""
# this is only required at specific exchanges
username = ""
9 changes: 9 additions & 0 deletions config/plugins/tradingAdvisor.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
enabled = true

candleSize = 60
historySize = 25
method = "MACD"

[talib]
enabled = false
version = "1.0.2"
9 changes: 1 addition & 8 deletions core/pluginUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,6 @@ var pluginHelper = {

plugin.config = config[plugin.slug];

if(!plugin.config)
log.warn(
'unable to find',
plugin.name,
'in the config. Is your config up to date?'
);

if(!plugin.config || !plugin.config.enabled)
return next();

Expand All @@ -94,7 +87,7 @@ var pluginHelper = {
return next(cannotLoad);

if(plugin.path)
var Constructor = require(pluginDir + plugin.path(plugin.config));
var Constructor = require(pluginDir + plugin.path(config));
else
var Constructor = require(pluginDir + plugin.slug);

Expand Down
38 changes: 38 additions & 0 deletions core/tools/configBuilder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const fs = require('fs');
const _ = require('lodash');
const toml = require('toml');

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


const getTOML = function(fileName) {
var raw = fs.readFileSync(fileName);
return toml.parse(raw);
}

// build a config object out of a directory of TOML files
module.exports = function() {
const configDir = util.dirs().config;

let _config = util.getTOML(configDir + 'general.toml');
fs.readdirSync(configDir + 'plugins').forEach(function(pluginFile) {
let pluginName = _.first(pluginFile.split('.'))
_config[pluginName] = util.getTOML(configDir + 'plugins/' + pluginFile);
});

// attach the proper adapter
let adapter = _config.adapter;
_config[adapter] = util.getTOML(configDir + 'adapters/' + adapter + '.toml');

if(_config.tradingAdvisor.enabled) {
// also load the strat
let strat = _config.tradingAdvisor.method;
let stratFile = configDir + 'strategies/' + strat + '.toml';
if(!fs.existsSync(stratFile))
util.die('Cannot find the strategy config file for ' + strat);
_config[strat] = util.getTOML(stratFile);
}

return _config;
}
2 changes: 1 addition & 1 deletion core/tools/dataStitcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Stitcher.prototype.prepareHistoricalData = function(done) {
return done();

var requiredHistory = config.tradingAdvisor.candleSize * config.tradingAdvisor.historySize;
var Reader = require(dirs.plugins + config.tradingAdvisor.adapter + '/reader');
var Reader = require(dirs.plugins + config.adapter + '/reader');

this.reader = new Reader;

Expand Down
20 changes: 15 additions & 5 deletions core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,24 @@ var util = {
if(_config)
return _config;

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

if(!fs.existsSync(configFile))
util.die('Cannot find a config file.');
_config = require(configFile);
return _config;
}

_config = require(configFile);
// build the config out of TOML files
var buildConfig = require(util.dirs().tools + 'configBuilder');
_config = buildConfig();
return _config;
},
getTOML: function(fileName) {
var raw = fs.readFileSync(fileName);
return toml.parse(raw);
},
// overwrite the whole config
setConfig: function(config) {
_config = config;
Expand Down Expand Up @@ -153,7 +163,7 @@ var util = {
return true;
else
return false;
},
}
}

// NOTE: those options are only used
Expand Down
4 changes: 2 additions & 2 deletions plugins/sqlite/handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ var util = require('../../core/util.js');
var config = util.getConfig();
var dirs = util.dirs();

var adapter = config.adapters.sqlite;
var adapter = config.sqlite;

// verify the correct dependencies are installed
var pluginHelper = require(dirs.core + 'pluginUtil');
var pluginMock = {
slug: 'sqlite adapter',
dependencies: config.adapters.sqlite.dependencies
dependencies: adapter.dependencies
};

var cannotLoad = pluginHelper.cannotLoad(pluginMock);
Expand Down
2 changes: 1 addition & 1 deletion plugins/sqlite/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var watch = config.watch;
var settings = {
exchange: watch.exchange,
pair: [watch.currency, watch.asset],
historyPath: config.adapters.sqlite.dataDirectory
historyPath: config.sqlite.dataDirectory
}

module.exports = {
Expand Down
26 changes: 0 additions & 26 deletions plugins/tradingAdvisor/tradingAdvisor.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ var Actor = function(done) {

this.methodName = config.tradingAdvisor.method;

this.generalizeMethodSettings();

this.setupTradingMethod();

var mode = util.gekkoMode();
Expand All @@ -35,30 +33,6 @@ var Actor = function(done) {

util.makeEventEmitter(Actor);

Actor.prototype.generalizeMethodSettings = function() {
// method settings can be either part of the main config OR a seperate
// toml configuration file. In case of the toml config file we need to
// parse and attach to main config object

// config already part of
if(config[this.methodName]) {
log.warn('\t', 'Config already has', this.methodName, 'parameters. Ignoring toml file');
return;
}

var tomlFile = dirs.config + 'strategies/' + this.methodName + '.toml';

if(!fs.existsSync(tomlFile)) {
log.warn('\t', 'toml configuration file not found.');
return;
}

var rawSettings = fs.readFileSync(tomlFile);
config[this.methodName] = toml.parse(rawSettings);

util.setConfig(config);
}

Actor.prototype.setupTradingMethod = function() {

if(!fs.existsSync(dirs.methods + this.methodName + '.js'))
Expand Down
61 changes: 28 additions & 33 deletions sample-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,43 +175,38 @@ config.adviceWriter = {
// CONFIGURING ADAPTER
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

dataDirectory: 'history',
version: 0.1,

dependencies: [{
module: 'sqlite3',
version: '3.1.4'
}]
}



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

dataDirectory: 'history',
version: 0.1,

dependencies: [{
module: 'sqlite3',
version: '3.1.4'
}]
},
// Postgres adapter example config (please note: requires postgres >= 9.5):
postgresql: {
path: 'plugins/postgresql',
version: 0.1,
connectionString: 'postgres://user:pass@localhost:5432', // if default port
dependencies: [{
module: 'pg',
version: '6.1.0'
}]
},
// Mongodb adapter, requires mongodb >= 3.3 (no version earlier tested)
mongodb: {
path: 'plugins/mongodb',
version: 0.1,
connectionString: 'mongodb://mongodb/gekko', // connection to mongodb server
dependencies: [{
module: 'mongojs',
version: '2.4.0'
}]
}
config.postgresql = {
path: 'plugins/postgresql',
version: 0.1,
connectionString: 'postgres://user:pass@localhost:5432', // if default port
dependencies: [{
module: 'pg',
version: '6.1.0'
}]
}

// Mongodb adapter, requires mongodb >= 3.3 (no version earlier tested)
config.mongodb = {
path: 'plugins/mongodb',
version: 0.1,
connectionString: 'mongodb://mongodb/gekko', // connection to mongodb server
dependencies: [{
module: 'mongojs',
version: '2.4.0'
}]
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down

0 comments on commit 617ed04

Please sign in to comment.