Skip to content
This repository has been archived by the owner on Feb 16, 2020. It is now read-only.

Commit

Permalink
rewrite vuex gekko event logic
Browse files Browse the repository at this point in the history
  • Loading branch information
askmike committed Jun 27, 2018
1 parent 4ef5299 commit 46bc376
Show file tree
Hide file tree
Showing 14 changed files with 6,526 additions and 173 deletions.
4 changes: 0 additions & 4 deletions web/routes/baseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ config.tradingAdvisor = {
config.candleWriter = {
enabled: false
}
config.adviceWriter = {
enabled: false,
muteSoft: true,
}

config.backtestResultExporter = {
enabled: false,
Expand Down
6 changes: 0 additions & 6 deletions web/routes/startGekko.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const _ = require('lodash');

const cache = require('../state/cache');
const broadcast = cache.get('broadcast');
const Logger = require('../state/logger');
const apiKeyManager= cache.get('apiKeyManager');
const gekkoManager = cache.get('gekkos');
Expand Down Expand Up @@ -34,11 +33,6 @@ module.exports = function *() {
}

const state = gekkoManager.add({config, mode});

broadcast({
type: 'new_gekko',
state
});

console.log('Gekko', state.id, 'started');

Expand Down
15 changes: 12 additions & 3 deletions web/state/gekkoManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ GekkoManager.prototype.add = function({mode, config}) {
type,
logType,
active: true,
initialEvents: {},
latestEvents: {},
events: {
initial: {},
latest: {}
},
start: moment()
}

Expand All @@ -66,6 +68,12 @@ GekkoManager.prototype.add = function({mode, config}) {
config.trader.secret = '[REDACTED]';
}

broadcast({
type: 'new_gekko',
id,
state
});

return state;
}

Expand All @@ -91,8 +99,9 @@ GekkoManager.prototype.handleRawEvent = function(id) {

GekkoManager.prototype.handleGekkoEvent = function(id, event) {
this.gekkos[id] = reduceState(this.gekkos[id], event);
console.log(event);
broadcast({
type: 'gekko_update',
type: 'gekko_event',
id,
event
});
Expand Down
35 changes: 35 additions & 0 deletions web/state/reduceState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Redux inspired reducer, reduces an event into a gekko state.
// NOTE: this does mutate.
// NOTE2: this is used by the backend as well as the frontend.

const moment = require('moment');

const skipInitialEvents = ['marketUpdate'];
const skipLatestEvents = ['marketStart'];
const trackAllEvents = ['tradeCompleted', 'advice', 'roundtrip'];

const reduce = (state, event) => {
const type = event.type;
const payload = event.payload;

state.latestUpdate = moment();

if(trackAllEvents.includes(type)) {
if(!state.events[type])
state.events[type] = [];

state.events[type].push(payload);
}

if(!state.events.initial[type] && !skipInitialEvents.includes(type)) {
state.events.initial[type] = payload;
}

if(!skipLatestEvents.includes(type)) {
state.events.latest[type] = payload;
}

return state;
}

module.exports = reduce;
Loading

0 comments on commit 46bc376

Please sign in to comment.