Ichimoku Indicator
#5
(03-01-2018, 05:01 PM)Fizcko Wrote: Hi PatTrends,

I work too on a strategie based on Ichimoku.

This is base on the NPM package "Ichimoku" but the current version do not include "laggingspan" (chikou) who is a great indicator.

Here my starting code. Maybe somone can improve it.


First of all you have to install the npm package ichimoku on the root of the gekko folder :

Code:
npm install --save ichimoku

Then add the following file strategies/Ichimoku.js

Code:
var log = require('../core/log');
var config = require ('../core/util.js').getConfig();
var Ichimoku = require("ichimoku");

var strat = {};

// Prepare everything our method needs
strat.init = function() {
    this.ichimoku = new Ichimoku({
        conversionPeriod : this.settings.conversionPeriod,
        basePeriod       : this.settings.basePeriod,
        spanPeriod       : this.settings.spanPeriod,
        displacement     : this.settings.displacement,
        values           : []
    });

    this.requiredHistory = 82;
    this.oldIchimokuValue = null;
    this.ichimokuValue = null;
    this.wait_price_over_cloud = false;
    this.wait_price_under_cloud = false;
}

// What happens on every new candle?
strat.update = function(candle) {
    
    var updateIchimokuValue = this.ichimoku.nextValue({
       high  : candle.high,
       low   : candle.low,
       close : candle.close
   });
    
    
    this.oldIchimokuValue = (this.ichimokuValue ? this.ichimokuValue : null);
    this.ichimokuValue = {
        tenkan: (updateIchimokuValue ? updateIchimokuValue.conversion : 0),
        kijun: (updateIchimokuValue ? updateIchimokuValue.base : 0),
        spanA: (updateIchimokuValue ? updateIchimokuValue.spanA : 0),
        spanB: (updateIchimokuValue ? updateIchimokuValue.spanB : 0)
    }
    
}

strat.check = function() {

    // Check Kijun over Tenkan
    kijun_over_before_last = (this.oldIchimokuValue.tenkan >= this.oldIchimokuValue.kijun) ? false : true;
    kijun_over_last = (this.ichimokuValue.tenkan >= this.ichimokuValue.kijun) ? false : true;
    kijun_crossing_tenkan = (kijun_over_before_last == false && kijun_over_last == true) ? true : false;
    // Check Tenkan over Kijun
    tenkan_over_before_last = (this.oldIchimokuValue.kijun >= this.oldIchimokuValue.tenkan) ? false : true;
    tenkan_over_last = (this.ichimokuValue.kijun >= this.ichimokuValue.tenkan) ? false : true;
    tenkan_crossing_kijun = (tenkan_over_before_last == false && tenkan_over_last == true) ? true : false;

    var sell_signal = false;
    // SELL signal 1 : Kijun over Tenkan
    if((kijun_crossing_tenkan || this.wait_price_under_cloud)){
        if(this.candle.close < Math.max(this.ichimokuValue.spanA,this.ichimokuValue.spanB)){
            sell_signal = true;
            this.wait_price_over_cloud = false;
            this.wait_price_under_cloud = false;
            log.debug("[INFO] SELL signal - " + this.wait_price_under_cloud);
        }
        else{
            log.debug("[INFO] Kijun over Tenkan BUT wait price under the cloud");
            this.wait_price_under_cloud = true;
        }
        
    }
    // Add here other SELL signals
    // SELL actions
    if(sell_signal){    
        this.advice('short');
    }
    // Find BUY signals
    var buy_signal = false;
    // BUY signals 1 : Tenkan over Kijun and price over Span
    if((tenkan_crossing_kijun || this.wait_price_over_cloud)){
        if(this.candle.close > Math.max(this.ichimokuValue.spanA,this.ichimokuValue.spanB)){
            buy_signal = true;
            this.wait_price_over_cloud = false;
            this.wait_price_under_cloud = false;
            log.debug("[INFO] BUY signal");
        }
        /*
        else{
            log.debug("[INFO] Tenkan over Kijun BUT wait price over the cloud");
            this.wait_price_over_cloud = true;
        }
        */
    }
    // Add here other BUY signals
    // BUY actions
    if(buy_signal){        
        this.advice('long');
    }
}

module.exports = strat;

Then add the following file config/strategies/Ichimoku.toml

Code:
# Ichimoku settings
conversionPeriod = 9
basePeriod = 26
spanPeriod = 52
displacement = 26


Quote:  <-- POST /api/backtest
2018-03-09 09:19:02 (INFO):     Setting up Gekko in backtest mode
2018-03-09 09:19:02 (INFO):
2018-03-09 09:19:02 (INFO):     Setting up:
2018-03-09 09:19:02 (INFO):              Trading Advisor
2018-03-09 09:19:02 (INFO):              Calculate trading advice
2018-03-09 09:19:02 (INFO):              Using the strategy: Ichimoku
2018-03-09 09:19:02 (WARN):     TALIB indicators could not be loaded, they will be unavailable.
2018-03-09 09:19:02 (INFO):

2018-03-09 09:19:02 (INFO):     Setting up:
2018-03-09 09:19:02 (INFO):              Paper Trader
2018-03-09 09:19:02 (INFO):              Paper trader that simulates fake trades.
2018-03-09 09:19:02 (INFO):

2018-03-09 09:19:02 (INFO):     Setting up:
2018-03-09 09:19:02 (INFO):              Performance Analyzer
2018-03-09 09:19:02 (INFO):              Analyzes performances of trades
2018-03-09 09:19:02 (INFO):

2018-03-09 09:19:02 (INFO):             WARNING: BACKTESTING FEATURE NEEDS PROPER TESTING
2018-03-09 09:19:02 (INFO):             WARNING: ACT ON THESE NUMBERS AT YOUR OWN RISK!
/gekko/node_modules/sqlite3/lib/trace.js:27
                    throw err;
                    ^

TypeError: Reduce of empty array with no initial value
    at Array.reduce (<anonymous>)
    at _callee$ (/gekko/node_modules/ichimoku/lib/index.js:77:74)
    at tryCatch (/gekko/node_modules/regenerator-runtime/runtime.js:62:40)
    at Generator.invoke [as _invoke] (/gekko/node_modules/regenerator-runtime/runtime.js:296:22)
    at Generator.prototype.(anonymous function) [as next] (/gekko/node_modules/regenerator-runtime/runtime.js:114:21)
    at Ichimoku.nextValue (/gekko/node_modules/ichimoku/lib/index.js:148:26)
    at Base.strat.update (/gekko/strategies/Ichimoku.js:27:45)
    at Base.bound [as update] (/gekko/node_modules/lodash/dist/lodash.js:729:21)
    at Base.propogateTick (/gekko/plugins/tradingAdvisor/baseTradingMethod.js:218:8)
    at Base.bound [as propogateTick] (/gekko/node_modules/lodash/dist/lodash.js:729:21)
--> in Database#all('\n    SELECT * from candles_BTC_STRAT\n    WHERE start <= 1512818099 AND start >= 1512815100\n    ORDER BY start ASC\n  ', [Function])
    at Reader.get (/gekko/plugins/sqlite/reader.js:98:11)
    at Reader.bound [as get] (/gekko/node_modules/lodash/dist/lodash.js:729:21)
    at Market.get (/gekko/core/markets/backtest.js:61:15)
    at Market.bound [as get] (/gekko/node_modules/lodash/dist/lodash.js:729:21)
    at Market.processCandles (/gekko/core/markets/backtest.js:105:10)
    at bound (/gekko/node_modules/lodash/dist/lodash.js:729:21)
    at Statement.<anonymous> (/gekko/plugins/sqlite/reader.js:108:5)
  xxx POST /api/backtest 500 346ms -

  Error: non-error thrown: Child process has died.
      at Object.onerror (/gekko/node_modules/koa/lib/context.js:105:40)
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:118:7)

Getting an error with a backtest. Any help?
  Reply


Messages In This Thread
Ichimoku Indicator - by PatTrends - 02-28-2018, 10:18 PM
RE: Ichimoku Indicator - by PatTrends - 03-01-2018, 02:12 PM
RE: Ichimoku Indicator - by Fizcko - 03-01-2018, 05:01 PM
RE: Ichimoku Indicator - by muditgrover - 03-09-2018, 09:20 AM
RE: Ichimoku Indicator - by edoe - 03-10-2018, 06:09 AM
RE: Ichimoku Indicator - by PatTrends - 03-03-2018, 08:00 PM
RE: Ichimoku Indicator - by riesgo.rafael - 03-13-2018, 01:51 AM
RE: Ichimoku Indicator - by Kris191 - 03-14-2018, 05:15 AM
RE: Ichimoku Indicator - by PatTrends - 03-20-2018, 09:36 PM
RE: Ichimoku Indicator - by palofug87 - 05-27-2018, 04:19 PM
RE: Ichimoku Indicator - by PatTrends - 05-29-2018, 02:42 AM
RE: Ichimoku Indicator - by palofug87 - 05-29-2018, 09:19 PM
RE: Ichimoku Indicator - by PatTrends - 05-31-2018, 06:44 PM
RE: Ichimoku Indicator - by ardialamwijaya - 11-07-2019, 04:16 AM

Forum Jump:


Users browsing this thread: