Gekko: How to access the previous candles?

Created on 19 Aug 2017  路  9Comments  路  Source: askmike/gekko

Hello guys, I'm doing a strategy here and I want to read the previous candles. Are these candles stored in a buint-in array?

Currently, I create an array inside my strategy and I'm pushing the values in the update method:
strat.candles = [];
update method:
strat.candles.push(candle);

Tks

Most helpful comment

Hey!

In most circumstances you don't have direct access to historical candles, but you can definitely do what you are doing. If you just want the previous one you can simple do:

// at the end of your `update` or `check`
this.previousCandle = candle;

That means you have access to it in any other iteration (except for the first one). But what you are doing is fine as well, just note that:

strat.candles.push(candle);

Is a memory leak, since backtesting this strat over a few months of data would probably result in an OOM error (out of memory) - since your array is ever increasing.

All 9 comments

Hey!

In most circumstances you don't have direct access to historical candles, but you can definitely do what you are doing. If you just want the previous one you can simple do:

// at the end of your `update` or `check`
this.previousCandle = candle;

That means you have access to it in any other iteration (except for the first one). But what you are doing is fine as well, just note that:

strat.candles.push(candle);

Is a memory leak, since backtesting this strat over a few months of data would probably result in an OOM error (out of memory) - since your array is ever increasing.

If you have any more questions feel free to ask!

strat.candles.push(candle);
with strat.candles.shift() isn't though.

This is exactly why candleprops should always be available to strats.

The candleprops are nested completely different. And as I proposed: why not create a config flag that when set calculates them (even without talib)?

What do you mean when you say they are nested completely differently?

They are arrays that store the last 1000 candles of data - price, open, close etc, they are so useful for a strat.

They are not stored as an array of candles, but as separate arrays per property. If you want the previous candle using the candleProps you have to do something like:

var previousCandle = {
  open: _.last(this.candleProps.open),
  close: _.last(this.candleProps.close),
  high: _.last(this.candleProps.high),
  low: _.last(this.candleProps.low),
  start: // calculate this manually.
}

@thegamecat can we please keep the discussion about the candleProps in #993, we are just making it more confusing for @evertondanilo by discussing it here (since these props are not public).

Tbf he asked for the previous candles, plural and the way to access them is through the candleprops array.

So last close price:
var previousprice = this.candleProps.close[this.candleProps.close.length-2];

The previous close to that:
var previousprice = this.candleProps.close[this.candleProps.close.length-3];

And so on.

You've then got:

var previousopen = this.candleProps.open[this.candleProps.open.length-2];
var previousvolume = this.candleProps.open[this.candleProps.volume.length-2];

And so on.

It's far better to use the objects that exist for everything that have one method for getting "last price" and then different ones for values before that. That's confusing for people.

The candleProps are not always available, this is a design decision. This
is not the place to discuss internal properties.. the discussion to make
this public is in #993 and not here.

How do we get access to candleProps in our strategies?

UPDATE: I figured it out.
Here's how you do it, as long as you are using a TALib indicator:

let candleProps = this.asyncIndicatorRunner.candleProps

This is quoted from an article which helped me:

If you want to use the candleprops array in a non talib using Strategy then do the following:

open plugins/baseTradingMethods.js and commend out the line below and the corresponding close bracket:

// if(this.asyncTick) {

Then go to your strategy and write console.log(this); in the update method and look for the candleprops arrays.

Note: baseTradingMethod is a core Gekko file so you should make this change only if you鈥檙e comfortable managing this change yourself.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

prathanbomb picture prathanbomb  路  3Comments

Oowii picture Oowii  路  4Comments

yacindou picture yacindou  路  3Comments

belemaire picture belemaire  路  5Comments

ChadTaljaardt picture ChadTaljaardt  路  5Comments