For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

chikou_span of Ichimoku indicator always return nan in IB live trading



  • The backtest of Ichimoku indicator works very well in a csv datafeed with 1 min data. The indicator can produce all the values (tenkan_sen, kijun_sen, senkou_span_a, senkou_span_b and chikou_span)

    However, when the same strategy code run in IB live trading feed, all values can be produced except chikou_span. chikou_span always returns nan value during the whole trading day. Even with the same data re-tested in backtest it has no problem at all.

    The behavior is very weird to me.

    I also read through the discussion forum & found this topic https://community.backtrader.com/topic/7/question-ichimoku-218 but my situation is not same as it because my problem only happens in IB live trading but not backtest.

    I even tried cerebro.run(tradehistory=True, live=True) but still didn't work.

    I have no idea whether I missed some important parameter in live trading. This issue stuck me many days for going further.

    Does anyone met the same issue before & how's it get resolved ? Appreciate you can share the hints

    Below is the code snippet to illustrate my problem

    import backtrader as bt
    import datetime as dt
    import os
    from datetime import datetime
    from TestIchimoku import TestIchimokuStrategy
    
    cerebro = bt.Cerebro(writer=True)
    storekwargs = dict(
            host='127.0.0.1',
            port=7497,               # 7497 is paper trade port
            clientId=7,
            timeoffset=not None,
            timeout=3,
            notifyall=None,
            _debug=False
        )
    
    ibstore = bt.stores.IBStore(**storekwargs)
    broker = ibstore.getbroker()
    cerebro.setbroker(broker)
    IBDataFactory = ibstore.getdata
    datakwargs = dict(
            historical=False,
            rtbar=True,
            qcheck=0.5,
            what=None,
            backfill_start=False,
            backfill=False,
            latethrough=False,
            tz='Asia/Hong_Kong'
        )
    
    data0 = IBDataFactory(dataname='HSIK0-FUT-HKFE-HKD', **datakwargs)
    cerebro.resampledata(data0, timeframe=bt.TimeFrame.Minutes, compression=1)
    cerebro.addstrategy (TestIchimokuStrategy)
    cerebro.run(tradehistory=True, live=True)
    
    class TestIchimokuStrategy(bt.Strategy):
        
        def __init__(self):
            self.ich = bt.indicators.Ichimoku(self.datas[0],
                                               tenkan=9,
                                               kijun=26,
                                               senkou=52,
                                               senkou_lead=26,
                                               chikou=26)
    
        def next(self):
            self.tenkan_sen = self.ich.l.tenkan_sen[0]
            self.kijun_sen = self.ich.l.kijun_sen[0]
            self.senkou_span_a = self.ich.l.senkou_span_a[0]
            self.senkou_span_b = self.ich.l.senkou_span_b[0]
            self.ichi = self.ich.l.chikou_span[0]
            
            print(
                'self.data.open[0]:{}, self.tenkan_sen:{}, self.kijun_sen:{}, self.senkou_span_a:{}, self.senkou_span_b:{}, self.ichi:{}'.format
                (self.data.open[0], self.tenkan_sen, self.kijun_sen, self.senkou_span_a, self.senkou_span_b, self.ichi))
    

    Output (nan for whole trading day)

    self.data.open[0]:24164.0, self.tenkan_sen:24155.5, self.kijun_sen:24155.0, self.senkou_span_a:24162.5, self.senkou_span_b:24193.0, self.ichi:nan
    self.data.open[0]:24166.0, self.tenkan_sen:24155.5, self.kijun_sen:24155.0, self.senkou_span_a:24164.0, self.senkou_span_b:24193.0, self.ichi:nan
    self.data.open[0]:24159.0, self.tenkan_sen:24155.5, self.kijun_sen:24154.0, self.senkou_span_a:24165.0, self.senkou_span_b:24192.0, self.ichi:nan
    self.data.open[0]:24142.0, self.tenkan_sen:24155.5, self.kijun_sen:24154.0, self.senkou_span_a:24165.0, self.senkou_span_b:24189.5, self.ichi:nan
    self.data.open[0]:24151.0, self.tenkan_sen:24155.5, self.kijun_sen:24154.0, self.senkou_span_a:24164.5, self.senkou_span_b:24189.5, self.ichi:nan
    self.data.open[0]:24150.0, self.tenkan_sen:24158.0, self.kijun_sen:24154.0, self.senkou_span_a:24160.25, self.senkou_span_b:24189.5, self.ichi:nan
    self.data.open[0]:24163.0, self.tenkan_sen:24156.0, self.kijun_sen:24154.0, self.senkou_span_a:24158.0, self.senkou_span_b:24188.0, self.ichi:nan
    self.data.open[0]:24166.0, self.tenkan_sen:24155.5, self.kijun_sen:24154.0, self.senkou_span_a:24158.0, self.senkou_span_b:24184.5, self.ichi:nan
    self.data.open[0]:24165.0, self.tenkan_sen:24155.5, self.kijun_sen:24155.0, self.senkou_span_a:24156.0, self.senkou_span_b:24177.0, self.ichi:nan
    self.data.open[0]:24166.0, self.tenkan_sen:24159.5, self.kijun_sen:24157.0, self.senkou_span_a:24156.0, self.senkou_span_b:24177.0, self.ichi:nan
    self.data.open[0]:24175.0, self.tenkan_sen:24159.5, self.kijun_sen:24157.0, self.senkou_span_a:24156.0, self.senkou_span_b:24177.0, self.ichi:nan
    self.data.open[0]:24164.0, self.tenkan_sen:24159.5, self.kijun_sen:24157.0, self.senkou_span_a:24154.5, self.senkou_span_b:24177.0, self.ichi:nan
    self.data.open[0]:24168.0, self.tenkan_sen:24161.0, self.kijun_sen:24157.0, self.senkou_span_a:24154.5, self.senkou_span_b:24176.5, self.ichi:nan
    self.data.open[0]:24150.0, self.tenkan_sen:24163.5, self.kijun_sen:24157.0, self.senkou_span_a:24153.75, self.senkou_span_b:24173.5, self.ichi:nan
    self.data.open[0]:24166.0, self.tenkan_sen:24164.5, self.kijun_sen:24157.0, self.senkou_span_a:24153.75, self.senkou_span_b:24165.0, self.ichi:nan
    self.data.open[0]:24162.0, self.tenkan_sen:24164.5, self.kijun_sen:24157.0, self.senkou_span_a:24153.75, self.senkou_span_b:24163.5, self.ichi:nan
    self.data.open[0]:24172.0, self.tenkan_sen:24164.5, self.kijun_sen:24157.0, self.senkou_span_a:24153.75, self.senkou_span_b:24162.0, self.ichi:nan
    self.data.open[0]:24168.0, self.tenkan_sen:24164.5, self.kijun_sen:24157.0, self.senkou_span_a:24154.25, self.senkou_span_b:24162.0, self.ichi:nan
    self.data.open[0]:24173.0, self.tenkan_sen:24163.5, self.kijun_sen:24157.0, self.senkou_span_a:24158.0, self.senkou_span_b:24159.0, self.ichi:nan
    self.data.open[0]:24172.0, self.tenkan_sen:24164.0, self.kijun_sen:24157.0, self.senkou_span_a:24157.5, self.senkou_span_b:24159.0, self.ichi:nan
    self.data.open[0]:24173.0, self.tenkan_sen:24165.0, self.kijun_sen:24157.5, self.senkou_span_a:24154.0, self.senkou_span_b:24157.5, self.ichi:nan
    self.data.open[0]:24179.0, self.tenkan_sen:24169.5, self.kijun_sen:24161.5, self.senkou_span_a:24153.5, self.senkou_span_b:24157.0, self.ichi:nan
    self.data.open[0]:24180.0, self.tenkan_sen:24174.0, self.kijun_sen:24164.0, self.senkou_span_a:24153.5, self.senkou_span_b:24157.0, self.ichi:nan
    self.data.open[0]:24179.0, self.tenkan_sen:24179.0, self.kijun_sen:24168.5, self.senkou_span_a:24155.75, self.senkou_span_b:24157.0, self.ichi:nan
    self.data.open[0]:24195.0, self.tenkan_sen:24182.0, self.kijun_sen:24168.5, self.senkou_span_a:24155.75, self.senkou_span_b:24157.0, self.ichi:nan
    self.data.open[0]:24188.0, self.tenkan_sen:24182.0, self.kijun_sen:24168.5, self.senkou_span_a:24155.25, self.senkou_span_b:24157.0, self.ichi:nan
    self.data.open[0]:24189.0, self.tenkan_sen:24186.5, self.kijun_sen:24171.0, self.senkou_span_a:24155.25, self.senkou_span_b:24157.0, self.ichi:nan
    self.data.open[0]:24201.0, self.tenkan_sen:24192.5, self.kijun_sen:24177.0, self.senkou_span_a:24155.25, self.senkou_span_b:24157.0, self.ichi:nan
    self.data.open[0]:24215.0, self.tenkan_sen:24193.5, self.kijun_sen:24177.5, self.senkou_span_a:24154.75, self.senkou_span_b:24157.0, self.ichi:nan
    self.data.open[0]:24213.0, self.tenkan_sen:24196.5, self.kijun_sen:24179.5, self.senkou_span_a:24154.75, self.senkou_span_b:24157.0, self.ichi:nan
    self.data.open[0]:24214.0, self.tenkan_sen:24197.5, self.kijun_sen:24183.0, self.senkou_span_a:24154.75, self.senkou_span_b:24157.0, self.ichi:nan
    self.data.open[0]:24212.0, self.tenkan_sen:24203.0, self.kijun_sen:24188.0, self.senkou_span_a:24156.0, self.senkou_span_b:24157.0, self.ichi:nan
    self.data.open[0]:24226.0, self.tenkan_sen:24211.5, self.kijun_sen:24193.5, self.senkou_span_a:24155.0, self.senkou_span_b:24157.0, self.ichi:nan
    self.data.open[0]:24227.0, self.tenkan_sen:24212.5, self.kijun_sen:24193.5, self.senkou_span_a:24154.75, self.senkou_span_b:24157.0, self.ichi:nan
    self.data.open[0]:24226.0, self.tenkan_sen:24213.5, self.kijun_sen:24193.5, self.senkou_span_a:24155.25, self.senkou_span_b:24157.0, self.ichi:nan
    self.data.open[0]:24235.0, self.tenkan_sen:24222.5, self.kijun_sen:24197.0, self.senkou_span_a:24158.25, self.senkou_span_b:24157.0, self.ichi:nan
    self.data.open[0]:24241.0, self.tenkan_sen:24226.5, self.kijun_sen:24197.0, self.senkou_span_a:24158.25, self.senkou_span_b:24157.0, self.ichi:nan
    self.data.open[0]:24244.0, self.tenkan_sen:24230.0, self.kijun_sen:24200.5, self.senkou_span_a:24158.25, self.senkou_span_b:24157.0, self.ichi:nan
    self.data.open[0]:24232.0, self.tenkan_sen:24245.5, self.kijun_sen:24215.5, self.senkou_span_a:24159.0, self.senkou_span_b:24157.0, self.ichi:nan
    self.data.open[0]:24281.0, self.tenkan_sen:24260.5, self.kijun_sen:24235.0, self.senkou_span_a:24160.25, self.senkou_span_b:24157.0, self.ichi:nan
    self.data.open[0]:24288.0, self.tenkan_sen:24266.5, self.kijun_sen:24235.5, self.senkou_span_a:24160.75, self.senkou_span_b:24157.0, self.ichi:nan
    self.data.open[0]:24294.0, self.tenkan_sen:24266.5, self.kijun_sen:24238.5, self.senkou_span_a:24160.75, self.senkou_span_b:24157.0, self.ichi:nan
    
    


  • Hi forks, I used today's live trading data to further supplement evidence of my findings. Hope it can illustrate the problem more clearly.

    Below is the output of running the above code in IB live trading feed. It prints out the OHLCV & Ichimoku values. All self.ichi showed nan

    Output in live trading

    Datetime:2020-06-05 13:25:00, Open:24338.0, High:24343.0, Low:24338.0, Close:24341.0, Volume:81.0
    Datetime:2020-06-05 13:25:00, self.data.open[0]:24338.0, self.tenkan_sen:24334.0, self.kijun_sen:24337.0, self.senkou_span_a:24306.0, self.senkou_span_b:24300.5, self.ichi:nan
    Datetime:2020-06-05 13:26:00, Open:24341.0, High:24342.0, Low:24334.0, Close:24336.0, Volume:72.0
    Datetime:2020-06-05 13:26:00, self.data.open[0]:24341.0, self.tenkan_sen:24334.0, self.kijun_sen:24337.0, self.senkou_span_a:24306.0, self.senkou_span_b:24300.5, self.ichi:nan
    Datetime:2020-06-05 13:27:00, Open:24336.0, High:24340.0, Low:24331.0, Close:24333.0, Volume:102.0
    Datetime:2020-06-05 13:27:00, self.data.open[0]:24336.0, self.tenkan_sen:24334.0, self.kijun_sen:24340.0, self.senkou_span_a:24308.0, self.senkou_span_b:24302.5, self.ichi:nan
    Datetime:2020-06-05 13:28:00, Open:24333.0, High:24333.0, Low:24324.0, Close:24329.0, Volume:105.0
    Datetime:2020-06-05 13:28:00, self.data.open[0]:24333.0, self.tenkan_sen:24334.0, self.kijun_sen:24340.0, self.senkou_span_a:24310.75, self.senkou_span_b:24305.0, self.ichi:nan
    Datetime:2020-06-05 13:29:00, Open:24330.0, High:24333.0, Low:24325.0, Close:24331.0, Volume:89.0
    Datetime:2020-06-05 13:29:00, self.data.open[0]:24330.0, self.tenkan_sen:24334.0, self.kijun_sen:24340.0, self.senkou_span_a:24315.25, self.senkou_span_b:24308.0, self.ichi:nan
    Datetime:2020-06-05 13:30:00, Open:24332.0, High:24339.0, Low:24329.0, Close:24330.0, Volume:97.0
    Datetime:2020-06-05 13:30:00, self.data.open[0]:24332.0, self.tenkan_sen:24334.0, self.kijun_sen:24340.0, self.senkou_span_a:24321.0, self.senkou_span_b:24312.5, self.ichi:nan
    

    Screen capture output in live trading
    Ichimoku-live.jpg

    After that I converted all the OHLCV to a CSV and then run the same code against the CSV

    CSV

    Dates,Open,High,Low,Close,Volume,Open Interest
    2020-06-05 11:10:00,24290,24307,24290,24295,174
    2020-06-05 11:11:00,24295,24303,24291,24303,151
    2020-06-05 11:12:00,24302,24307,24295,24300,158
    2020-06-05 11:13:00,24299,24308,24294,24299,105
    2020-06-05 11:14:00,24299,24307,24299,24300,143
    2020-06-05 11:15:00,24300,24303,24296,24302,124
    2020-06-05 11:16:00,24301,24301,24293,24295,99
    2020-06-05 11:17:00,24295,24295,24289,24291,86
    2020-06-05 11:18:00,24292,24299,24288,24295,81
    2020-06-05 11:19:00,24295,24303,24292,24294,103
    2020-06-05 11:20:00,24295,24295,24280,24285,157
    2020-06-05 11:21:00,24286,24294,24280,24284,133
    2020-06-05 11:22:00,24282,24288,24281,24287,67
    2020-06-05 11:23:00,24283,24288,24274,24277,146
    2020-06-05 11:24:00,24276,24281,24272,24276,105
    2020-06-05 11:25:00,24276,24278,24271,24277,130
    2020-06-05 11:26:00,24276,24280,24275,24277,84
    2020-06-05 11:27:00,24276,24280,24275,24277,70
    2020-06-05 11:28:00,24277,24287,24275,24282,82
    2020-06-05 11:29:00,24281,24292,24281,24289,102
    2020-06-05 11:30:00,24289,24289,24271,24286,152
    2020-06-05 11:31:00,24284,24295,24282,24282,138
    2020-06-05 11:32:00,24282,24293,24282,24290,88
    2020-06-05 11:33:00,24289,24295,24288,24290,83
    2020-06-05 11:34:00,24289,24296,24289,24296,77
    2020-06-05 11:35:00,24295,24305,24291,24302,191
    2020-06-05 11:36:00,24302,24303,24294,24296,96
    2020-06-05 11:37:00,24295,24298,24293,24297,112
    2020-06-05 11:38:00,24295,24302,24291,24299,65
    2020-06-05 11:39:00,24301,24313,24299,24302,253
    2020-06-05 11:40:00,24304,24308,24301,24304,156
    2020-06-05 11:41:00,24305,24305,24299,24302,75
    2020-06-05 11:42:00,24301,24305,24296,24298,82
    2020-06-05 11:43:00,24297,24300,24294,24296,57
    2020-06-05 11:44:00,24293,24298,24293,24294,47
    2020-06-05 11:45:00,24294,24297,24293,24297,34
    2020-06-05 11:46:00,24297,24298,24293,24295,65
    2020-06-05 11:47:00,24294,24298,24287,24288,118
    2020-06-05 11:48:00,24288,24290,24285,24287,55
    2020-06-05 11:49:00,24287,24289,24284,24286,59
    2020-06-05 11:50:00,24284,24289,24283,24286,90
    2020-06-05 11:51:00,24287,24292,24286,24291,74
    2020-06-05 11:52:00,24292,24298,24291,24291,97
    2020-06-05 11:53:00,24291,24291,24284,24286,125
    2020-06-05 11:54:00,24286,24290,24286,24288,30
    2020-06-05 11:55:00,24286,24290,24282,24289,74
    2020-06-05 11:56:00,24290,24292,24283,24287,86
    2020-06-05 11:57:00,24289,24294,24289,24294,49
    2020-06-05 11:58:00,24294,24304,24294,24303,111
    2020-06-05 11:59:00,24304,24310,24302,24303,175
    2020-06-05 12:00:00,24303,24305,24297,24299,125
    2020-06-05 12:59:00,24330,24330,24330,24330,39
    2020-06-05 13:00:00,24330,24330,24330,24330,81
    2020-06-05 13:01:00,24327,24334,24311,24321,722
    2020-06-05 13:02:00,24322,24339,24317,24336,251
    2020-06-05 13:03:00,24337,24345,24328,24341,289
    2020-06-05 13:04:00,24343,24354,24343,24351,384
    2020-06-05 13:05:00,24352,24358,24350,24353,207
    2020-06-05 13:06:00,24354,24363,24332,24336,424
    2020-06-05 13:07:00,24337,24343,24324,24327,375
    2020-06-05 13:08:00,24329,24332,24317,24321,207
    2020-06-05 13:09:00,24320,24329,24317,24324,98
    2020-06-05 13:10:00,24324,24325,24319,24319,95
    2020-06-05 13:11:00,24318,24325,24317,24325,136
    2020-06-05 13:12:00,24324,24333,24321,24330,135
    2020-06-05 13:13:00,24331,24339,24329,24337,120
    2020-06-05 13:14:00,24335,24338,24332,24338,110
    2020-06-05 13:15:00,24337,24340,24331,24331,98
    2020-06-05 13:16:00,24332,24336,24321,24327,175
    2020-06-05 13:17:00,24327,24334,24327,24332,77
    2020-06-05 13:18:00,24331,24332,24329,24330,70
    2020-06-05 13:19:00,24333,24334,24326,24329,101
    2020-06-05 13:20:00,24329,24337,24324,24332,103
    2020-06-05 13:21:00,24334,24339,24330,24337,101
    2020-06-05 13:22:00,24337,24339,24332,24335,37
    2020-06-05 13:23:00,24333,24338,24333,24337,72
    2020-06-05 13:24:00,24339,24344,24338,24338,143
    2020-06-05 13:25:00,24338,24343,24338,24341,81
    2020-06-05 13:26:00,24341,24342,24334,24336,72
    2020-06-05 13:27:00,24336,24340,24331,24333,102
    2020-06-05 13:28:00,24333,24333,24324,24329,105
    2020-06-05 13:29:00,24330,24333,24325,24331,89
    2020-06-05 13:30:00,24332,24339,24329,24330,97
    

    Output in backtest

    Datetime:2020-06-05 13:25:00, Open:24338.0, High:24343.0, Low:24338.0, Close:24341.0, Volume:81.0
    Datetime:2020-06-05 13:25:00, self.data.open[0]:24338.0, self.tenkan_sen:24334.0, self.kijun_sen:24337.0, self.senkou_span_a:24306.0, self.senkou_span_b:24300.5, self.ichi:24394.0
    Datetime:2020-06-05 13:26:00, Open:24341.0, High:24342.0, Low:24334.0, Close:24336.0, Volume:72.0
    Datetime:2020-06-05 13:26:00, self.data.open[0]:24341.0, self.tenkan_sen:24334.0, self.kijun_sen:24337.0, self.senkou_span_a:24306.0, self.senkou_span_b:24300.5, self.ichi:24411.0
    Datetime:2020-06-05 13:27:00, Open:24336.0, High:24340.0, Low:24331.0, Close:24333.0, Volume:102.0
    Datetime:2020-06-05 13:27:00, self.data.open[0]:24336.0, self.tenkan_sen:24334.0, self.kijun_sen:24340.0, self.senkou_span_a:24308.0, self.senkou_span_b:24302.5, self.ichi:24425.0
    Datetime:2020-06-05 13:28:00, Open:24333.0, High:24333.0, Low:24324.0, Close:24329.0, Volume:105.0
    Datetime:2020-06-05 13:28:00, self.data.open[0]:24333.0, self.tenkan_sen:24334.0, self.kijun_sen:24340.0, self.senkou_span_a:24310.75, self.senkou_span_b:24305.0, self.ichi:24438.0
    Datetime:2020-06-05 13:29:00, Open:24330.0, High:24333.0, Low:24325.0, Close:24331.0, Volume:89.0
    Datetime:2020-06-05 13:29:00, self.data.open[0]:24330.0, self.tenkan_sen:24334.0, self.kijun_sen:24340.0, self.senkou_span_a:24315.25, self.senkou_span_b:24308.0, self.ichi:24456.0
    Datetime:2020-06-05 13:30:00, Open:24332.0, High:24339.0, Low:24329.0, Close:24330.0, Volume:97.0
    Datetime:2020-06-05 13:30:00, self.data.open[0]:24332.0, self.tenkan_sen:24334.0, self.kijun_sen:24340.0, self.senkou_span_a:24321.0, self.senkou_span_b:24312.5, self.ichi:24463.0
    

    Screen capture output in backtest
    Ichimoku-backtest.jpg



  • Ops....forgot to paste the new code.

        def next(self):
            self.open = self.data.open[0]
            self.high = self.data.high[0]
            self.low = self.data.low[0]
            self.close = self.data.close[0]
            self.volume = self.data.volume[0]
    
            self.tenkan_sen = self.ich.l.tenkan_sen[0]
            self.kijun_sen = self.ich.l.kijun_sen[0]
            self.senkou_span_a = self.ich.l.senkou_span_a[0]
            self.senkou_span_b = self.ich.l.senkou_span_b[0]
            self.ichi = self.ich.l.chikou_span[0]
    
            print(
                'Datetime:{} {}, Open:{}, High:{}, Low:{}, Close:{}, Volume:{}'.format
                (self.datas[0].datetime.date(0), self.datas[0].datetime.time(0), self.open, self.high, self.low, self.close, self.volume))
    self.low, self.close, self.volume))
            print(
                'Datetime:{} {}, self.data.open[0]:{}, self.tenkan_sen:{}, self.kijun_sen:{}, self.senkou_span_a:{}, self.senkou_span_b:{}, self.ichi:{}'.format
                (self.datas[0].datetime.date(0), self.datas[0].datetime.time().strftime('%H:%M:%S'), self.data.open[0], self.tenkan_sen, self.kijun_sen, self.senkou_span_a, self.senkou_span_b, self.ichi))
    


  • def init(self):
    self.ich = bt.indicators.Ichimoku(self.datas[0],
    tenkan=9,
    kijun=26,
    senkou=52,
    senkou_lead=26,
    chikou=26)

    def next(self):
    self.open = self.data.open[0]
    self.high = self.data.high[0]
    self.low = self.data.low[0]
    self.close = self.data.close[0]
    self.volume = self.data.volume[0]

        self.tenkan_sen = self.ich.l.tenkan_sen[0]
        self.kijun_sen = self.ich.l.kijun_sen[0]
        self.senkou_span_a = self.ich.l.senkou_span_a[0]
        self.senkou_span_b = self.ich.l.senkou_span_b[0]
        self.ichi = self.ich.l.chikou_span[0]
    
        print(
            'Datetime:{} {}, Open:{}, High:{}, Low:{}, Close:{}, Volume:{}'.format
            (self.datas[0].datetime.date(0), self.datas[0].datetime.time(0), self.open, self.high, self.low, self.close, self.volume))
    

    self.low, self.close, self.volume))
    print(
    'Datetime:{} {}, self.data.open[0]:{}, self.tenkan_sen:{}, self.kijun_sen:{}, self.senkou_span_a:{}, self.senkou_span_b:{}, self.ichi:{}'.format
    (self.datas[0].datetime.date(0), self.datas[0].datetime.time().strftime('%H:%M:%S'), self.data.open[0], self.tenkan_sen, self.kijun_sen, self.senkou_span_a, self.senkou_span_b, self.ichi))



  • Pls ignore the above reply of bad format codes

    def __init__(self):
            self.ich = bt.indicators.Ichimoku(self.datas[0],
                                               tenkan=9,
                                               kijun=26,
                                               senkou=52,
                                               senkou_lead=26,
                                               chikou=26)
    def next(self):
            self.open = self.data.open[0]
            self.high = self.data.high[0]
            self.low = self.data.low[0]
            self.close = self.data.close[0]
            self.volume = self.data.volume[0]
    
            self.tenkan_sen = self.ich.l.tenkan_sen[0]
            self.kijun_sen = self.ich.l.kijun_sen[0]
            self.senkou_span_a = self.ich.l.senkou_span_a[0]
            self.senkou_span_b = self.ich.l.senkou_span_b[0]
            self.ichi = self.ich.l.chikou_span[0]
    
            print(
                'Datetime:{} {}, Open:{}, High:{}, Low:{}, Close:{}, Volume:{}'.format
                (self.datas[0].datetime.date(0), self.datas[0].datetime.time(0), self.open, self.high, self.low, self.close, self.volume))
    self.low, self.close, self.volume))
            print(
                'Datetime:{} {}, self.data.open[0]:{}, self.tenkan_sen:{}, self.kijun_sen:{}, self.senkou_span_a:{}, self.senkou_span_b:{}, self.ichi:{}'.format
                (self.datas[0].datetime.date(0), self.datas[0].datetime.time().strftime('%H:%M:%S'), self.data.open[0], self.tenkan_sen, self.kijun_sen, self.senkou_span_a, self.senkou_span_b, self.ichi))
    




  • I may be mistaken, but it seem that the indicator tries to look ahead - which can't be done in live trading - only in backtest when all the data is available.



  • @vladisld , thanks for your advice. I did read thread but his problem is in backtest. I have no problem in backtest at all but only in live trading

    I paste the full CSV data set here again. Sorry for my bad in previous message.

    Dates,Open,High,Low,Close,Volume,Open Interest
    2020-06-05 11:10:00,24290,24307,24290,24295,174
    2020-06-05 11:11:00,24295,24303,24291,24303,151
    2020-06-05 11:12:00,24302,24307,24295,24300,158
    2020-06-05 11:13:00,24299,24308,24294,24299,105
    2020-06-05 11:14:00,24299,24307,24299,24300,143
    2020-06-05 11:15:00,24300,24303,24296,24302,124
    2020-06-05 11:16:00,24301,24301,24293,24295,99
    2020-06-05 11:17:00,24295,24295,24289,24291,86
    2020-06-05 11:18:00,24292,24299,24288,24295,81
    2020-06-05 11:19:00,24295,24303,24292,24294,103
    2020-06-05 11:20:00,24295,24295,24280,24285,157
    2020-06-05 11:21:00,24286,24294,24280,24284,133
    2020-06-05 11:22:00,24282,24288,24281,24287,67
    2020-06-05 11:23:00,24283,24288,24274,24277,146
    2020-06-05 11:24:00,24276,24281,24272,24276,105
    2020-06-05 11:25:00,24276,24278,24271,24277,130
    2020-06-05 11:26:00,24276,24280,24275,24277,84
    2020-06-05 11:27:00,24276,24280,24275,24277,70
    2020-06-05 11:28:00,24277,24287,24275,24282,82
    2020-06-05 11:29:00,24281,24292,24281,24289,102
    2020-06-05 11:30:00,24289,24289,24271,24286,152
    2020-06-05 11:31:00,24284,24295,24282,24282,138
    2020-06-05 11:32:00,24282,24293,24282,24290,88
    2020-06-05 11:33:00,24289,24295,24288,24290,83
    2020-06-05 11:34:00,24289,24296,24289,24296,77
    2020-06-05 11:35:00,24295,24305,24291,24302,191
    2020-06-05 11:36:00,24302,24303,24294,24296,96
    2020-06-05 11:37:00,24295,24298,24293,24297,112
    2020-06-05 11:38:00,24295,24302,24291,24299,65
    2020-06-05 11:39:00,24301,24313,24299,24302,253
    2020-06-05 11:40:00,24304,24308,24301,24304,156
    2020-06-05 11:41:00,24305,24305,24299,24302,75
    2020-06-05 11:42:00,24301,24305,24296,24298,82
    2020-06-05 11:43:00,24297,24300,24294,24296,57
    2020-06-05 11:44:00,24293,24298,24293,24294,47
    2020-06-05 11:45:00,24294,24297,24293,24297,34
    2020-06-05 11:46:00,24297,24298,24293,24295,65
    2020-06-05 11:47:00,24294,24298,24287,24288,118
    2020-06-05 11:48:00,24288,24290,24285,24287,55
    2020-06-05 11:49:00,24287,24289,24284,24286,59
    2020-06-05 11:50:00,24284,24289,24283,24286,90
    2020-06-05 11:51:00,24287,24292,24286,24291,74
    2020-06-05 11:52:00,24292,24298,24291,24291,97
    2020-06-05 11:53:00,24291,24291,24284,24286,125
    2020-06-05 11:54:00,24286,24290,24286,24288,30
    2020-06-05 11:55:00,24286,24290,24282,24289,74
    2020-06-05 11:56:00,24290,24292,24283,24287,86
    2020-06-05 11:57:00,24289,24294,24289,24294,49
    2020-06-05 11:58:00,24294,24304,24294,24303,111
    2020-06-05 11:59:00,24304,24310,24302,24303,175
    2020-06-05 12:00:00,24303,24305,24297,24299,125
    2020-06-05 12:59:00,24330,24330,24330,24330,39
    2020-06-05 13:00:00,24330,24330,24330,24330,81
    2020-06-05 13:01:00,24327,24334,24311,24321,722
    2020-06-05 13:02:00,24322,24339,24317,24336,251
    2020-06-05 13:03:00,24337,24345,24328,24341,289
    2020-06-05 13:04:00,24343,24354,24343,24351,384
    2020-06-05 13:05:00,24352,24358,24350,24353,207
    2020-06-05 13:06:00,24354,24363,24332,24336,424
    2020-06-05 13:07:00,24337,24343,24324,24327,375
    2020-06-05 13:08:00,24329,24332,24317,24321,207
    2020-06-05 13:09:00,24320,24329,24317,24324,98
    2020-06-05 13:10:00,24324,24325,24319,24319,95
    2020-06-05 13:11:00,24318,24325,24317,24325,136
    2020-06-05 13:12:00,24324,24333,24321,24330,135
    2020-06-05 13:13:00,24331,24339,24329,24337,120
    2020-06-05 13:14:00,24335,24338,24332,24338,110
    2020-06-05 13:15:00,24337,24340,24331,24331,98
    2020-06-05 13:16:00,24332,24336,24321,24327,175
    2020-06-05 13:17:00,24327,24334,24327,24332,77
    2020-06-05 13:18:00,24331,24332,24329,24330,70
    2020-06-05 13:19:00,24333,24334,24326,24329,101
    2020-06-05 13:20:00,24329,24337,24324,24332,103
    2020-06-05 13:21:00,24334,24339,24330,24337,101
    2020-06-05 13:22:00,24337,24339,24332,24335,37
    2020-06-05 13:23:00,24333,24338,24333,24337,72
    2020-06-05 13:24:00,24339,24344,24338,24338,143
    2020-06-05 13:25:00,24338,24343,24338,24341,81
    2020-06-05 13:26:00,24341,24342,24334,24336,72
    2020-06-05 13:27:00,24336,24340,24331,24333,102
    2020-06-05 13:28:00,24333,24333,24324,24329,105
    2020-06-05 13:29:00,24330,24333,24325,24331,89
    2020-06-05 13:30:00,24332,24339,24329,24330,97
    2020-06-05 13:31:00,24331,24343,24331,24343,155
    2020-06-05 13:32:00,24342,24354,24341,24349,353
    2020-06-05 13:33:00,24350,24350,24341,24342,99
    2020-06-05 13:34:00,24341,24342,24332,24333,118
    2020-06-05 13:35:00,24332,24333,24320,24321,200
    2020-06-05 13:36:00,24322,24324,24313,24321,251
    2020-06-05 13:37:00,24321,24326,24320,24323,103
    2020-06-05 13:38:00,24324,24330,24319,24326,135
    2020-06-05 13:39:00,24327,24327,24319,24319,100
    2020-06-05 13:40:00,24319,24319,24307,24309,322
    2020-06-05 13:41:00,24309,24316,24305,24314,108
    2020-06-05 13:42:00,24315,24316,24309,24314,148
    2020-06-05 13:43:00,24315,24315,24303,24310,131
    2020-06-05 13:44:00,24311,24317,24307,24311,147
    2020-06-05 13:45:00,24310,24313,24306,24312,78
    2020-06-05 13:46:00,24314,24317,24308,24315,89
    2020-06-05 13:47:00,24317,24324,24316,24323,87
    2020-06-05 13:48:00,24324,24343,24322,24339,376
    2020-06-05 13:49:00,24340,24368,24337,24359,842
    2020-06-05 13:50:00,24360,24397,24360,24397,1058
    2020-06-05 13:51:00,24399,24406,24381,24394,614
    2020-06-05 13:52:00,24396,24411,24390,24411,562
    2020-06-05 13:53:00,24412,24430,24412,24425,612
    2020-06-05 13:54:00,24427,24442,24426,24438,456
    2020-06-05 13:55:00,24436,24459,24435,24456,551
    2020-06-05 13:56:00,24459,24465,24452,24463,389
    2020-06-05 13:57:00,24466,24471,24446,24447,586
    2020-06-05 13:58:00,24450,24454,24445,24448,366
    2020-06-05 13:59:00,24448,24453,24441,24446,297
    2020-06-05 14:00:00,24448,24454,24443,24448,298
    2020-06-05 14:01:00,24449,24488,24447,24486,890
    2020-06-05 14:02:00,24484,24489,24463,24473,507
    2020-06-05 14:03:00,24473,24473,24455,24460,342
    2020-06-05 14:04:00,24463,24465,24436,24439,381
    2020-06-05 14:05:00,24440,24455,24438,24453,926
    2020-06-05 14:06:00,24454,24478,24453,24470,514
    2020-06-05 14:07:00,24470,24470,24460,24461,145
    2020-06-05 14:08:00,24462,24468,24453,24467,159
    2020-06-05 14:09:00,24469,24475,24463,24473,232
    2020-06-05 14:10:00,24475,24480,24456,24462,267
    2020-06-05 14:11:00,24465,24468,24450,24459,288
    2020-06-05 14:12:00,24460,24465,24452,24461,157
    2020-06-05 14:13:00,24459,24477,24459,24462,222
    2020-06-05 14:14:00,24464,24469,24460,24463,142
    2020-06-05 14:15:00,24462,24464,24452,24463,119
    2020-06-05 14:16:00,24464,24473,24459,24467,154
    2020-06-05 14:17:00,24467,24480,24463,24480,173
    2020-06-05 14:18:00,24480,24508,24480,24504,917
    2020-06-05 14:19:00,24505,24526,24503,24503,720
    


  • @vladisld , your guess sounds reasonable. I wonder how to make chikou_span work in live trading mode.



  • @vladisld said in chikou_span of Ichimoku indicator always return nan in IB live trading:

    I may be mistaken, but it seem that the indicator tries to look ahead - which can't be done in live trading - only in backtest when all the data is available.

    @vladisld I further check the explanation from stockcharts. chikou_span should be looking back 26 bars in the past but not look ahead.

    chikou-span-past26.jpg

    chikou-span.jpg

    In the source of Ichimoku indicator, it uses +26.

    self.l.chikou_span = self.data.close(self.p.chikou)
    

    Based on your explanation that the indicator can't be done in live trading - only in backtest. It seems doesn't make sense because I believe all indicators should be working on both live trading & backtest scenario.

    Do you think it is a bug to be fixed ? Please correct me if I am wrong as I am a newbie to backtrader. Thx a lot.



  • I'm not pretending to be absolutely correct on this - just trying to understand it with you:

    chikou_span: The value of this line at any given time should equal to the value of data.close 26 (by default) bars ahead, right ?

    This means that if we are standing on bar X, the correspondent calculated value of chikou_span will be at bar X - 26. In another words, in live trading chikou_span line will have Nan values at least 26 bars in the past (since there are still no data available for data.close to calculate the correspondent chikou_span)

    So if you are printing the value of chikou_span[0] it will be always Nan, as well as chikou_span[-1] .... chikou_span[-25].

    Does it sound correct ?



  • It seems to also alight well with the diagram you've attached above. If you take a look at the ticker's last 26 bars - there are not values for Chikou Span there. The latest value of the Chikou Span is 26 bars behind the latest ticker bar.



  • So just to complete my line of though ( hoping it is correct of cause): In backtesting the situation is different. Since all bars could be available ( in case Cerebro was asked to preload it using run_once ), the value of chikou_span could be calculated using 'future' bars.

    So in this case chikou_span[0] could have a value (calculated from the 'future' data.close). However, you probably should not use it - since you are actually looking at the future ( which would not work in real life ).

    As a note - the last 26 value of chikou_span line range will obviously be Nan as well in this scenario.

    Once again, this is only right if my understanding is correct - which I'm not 100% sure as always.



  • @vladisld said in chikou_span of Ichimoku indicator always return nan in IB live trading:

    I'm not pretending to be absolutely correct on this - just trying to understand it with you:

    chikou_span: The value of this line at any given time should equal to the value of data.close 26 (by default) bars ahead, right ?

    This means that if we are standing on bar X, the correspondent calculated value of chikou_span will be at bar X - 26. In another words, in live trading chikou_span line will have Nan values at least 26 bars in the past (since there are still no data available for data.close to calculate the correspondent chikou_span)

    So if you are printing the value of chikou_span[0] it will be always Nan, as well as chikou_span[-1] .... chikou_span[-25].

    Does it sound correct ?

    @vladisld, I agree that in live trading chikou_span line will have nan values at least 26 bars in the past. It should have values in some time after 26 bars.

    However based on my live trading proof test these 2 days, the value of chikou_span is always nan from start to end of trading day. How to explain this ?



  • @vladisld said in chikou_span of Ichimoku indicator always return nan in IB live trading:

    It seems to also alight well with the diagram you've attached above. If you take a look at the ticker's last 26 bars - there are not values for Chikou Span there. The latest value of the Chikou Span is 26 bars behind the latest ticker bar.

    @vladisld , do you mean that if I need to use chikou_span value in live trading, it should be using chikou_span[-26] , which is the latest value I can access, instead of chikou_span[0] in self.data.open[0] , right ?



  • @vladisld said in chikou_span of Ichimoku indicator always return nan in IB live trading:

    So just to complete my line of though ( hoping it is correct of cause): In backtesting the situation is different. Since all bars could be available ( in case Cerebro was asked to preload it using run_once ), the value of chikou_span could be calculated using 'future' bars.

    So in this case chikou_span[0] could have a value (calculated from the 'future' data.close). However, you probably should not use it - since you are actually looking at the future ( which would not work in real life ).

    As a note - the last 26 value of chikou_span line range will obviously be Nan as well in this scenario.

    Once again, this is only right if my understanding is correct - which I'm not 100% sure as always.

    @vladisld , Thx for your detailed explanation. I finally get my concept clear why the results between live trading & backtest are different.



  • @hei-jai said in chikou_span of Ichimoku indicator always return nan in IB live trading:

    do you mean that if I need to use chikou_span value in live trading, it should be using chikou_span[-26] , which is the latest value I can access, instead of chikou_span[0] in self.data.open[0] , right ?

    right - that's also my understanding



  • @vladisld said in chikou_span of Ichimoku indicator always return nan in IB live trading:

    @hei-jai said in chikou_span of Ichimoku indicator always return nan in IB live trading:

    do you mean that if I need to use chikou_span value in live trading, it should be using chikou_span[-26] , which is the latest value I can access, instead of chikou_span[0] in self.data.open[0] , right ?

    right - that's also my understanding

    @vladisld , I just run the backtest again with runonce=False . All chikou_span[0] returns nan in backtest results which match the live trading scenaro. runonce=False plays the tricks in my previous backtest :(

    Once again thanks for backtrader team's generous contribution to make this excellent platform.



  • It should be runonce=True plays the tricks in my previous backtest :(


Log in to reply
 

});