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

Commit

Permalink
force defer of ticks when using both sync and async indicators, fix #837
Browse files Browse the repository at this point in the history
  • Loading branch information
askmike committed Jul 19, 2017
1 parent 5e3757b commit a3cc7d4
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions plugins/tradingAdvisor/baseTradingMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ var Base = function() {
this.talibIndicators = {};
this.asyncTick = false;
this.candlePropsCacheSize = 1000;
this.deferredTicks = [];

this._prevAdvice;

Expand Down Expand Up @@ -120,14 +121,31 @@ var Base = function() {

if(_.size(this.talibIndicators))
this.asyncTick = true;

if(_.size(this.indicators))
this.hasSyncIndicators = true;
}

// teach our base trading method events
util.makeEventEmitter(Base);

Base.prototype.tick = function(candle) {

if(
this.asyncTick &&
this.hasSyncIndicators &&
this.age !== this.processedTicks
) {
// Gekko will call talib and run strat
// functions when talib is done, but by
// this time the sync indicators might be
// updated with future candles.
//
// See @link: https://github.com/askmike/gekko/issues/837#issuecomment-316549691
return this.deferredTicks.push(candle);
}

this.age++;
this.candle = candle;

if(this.asyncTick) {
this.candleProps.open.push(candle.open);
Expand Down Expand Up @@ -155,12 +173,13 @@ Base.prototype.tick = function(candle) {
},this);

// update the trading method
if(!this.asyncTick || this.requiredHistory > this.age) {
if(!this.asyncTick) {
this.propogateTick(candle);
} else {

var next = _.after(
_.size(this.talibIndicators),
this.propogateTick
() => this.propogateTick(candle)
);

var basectx = this;
Expand All @@ -185,9 +204,6 @@ Base.prototype.tick = function(candle) {
);
}

// update previous price
this.lastPrice = price;

this.propogateCustomCandle(candle);
}

Expand All @@ -205,6 +221,8 @@ if(ENV !== 'child-process') {
}

Base.prototype.propogateTick = function(candle) {
this.candle = candle;

this.update(candle);

var isAllowedToCheck = this.requiredHistory <= this.age;
Expand All @@ -224,6 +242,14 @@ Base.prototype.propogateTick = function(candle) {
}
this.processedTicks++;

if(
this.asyncTick &&
this.hasSyncIndicators &&
this.deferredTicks.length
) {
return this.tick(this.deferredTicks.shift())
}

// are we totally finished?
var done = this.age === this.processedTicks;
if(done && this.finishCb)
Expand Down Expand Up @@ -261,7 +287,7 @@ Base.prototype.addIndicator = function(name, type, parameters) {
this.indicators[name].input = Indicators[type].input;
}

Base.prototype.advice = function(newPosition) {
Base.prototype.advice = function(newPosition, _candle) {
// ignore soft advice coming from legacy
// strategies.
if(!newPosition)
Expand All @@ -271,13 +297,19 @@ Base.prototype.advice = function(newPosition) {
if(newPosition === this._prevAdvice)
return;

// cache the candle this advice is based on
if(_candle)
var candle = _candle;
else
var candle = this.candle;

this._prevAdvice = newPosition;

_.defer(function() {
this.emit('advice', {
recommendation: newPosition,
portfolio: 1,
candle: this.candle
candle
});
}.bind(this));
}
Expand Down

0 comments on commit a3cc7d4

Please sign in to comment.