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

why the prenext and next can run many times in one bar?



  • Dear Sir or Madam:

      when I use the backtrader to backtest a future strategy with many contracts,and  the bar is 15min. when the bar is daily,it is normal.but when the bar is 15min,in some bar,it can run several times? everyone met it?
    
     for example ,when the bar datetime is 2014-12-26T23:00:00,it runs nine times。It is very terrible!
    
    below is my part log info and code. 
    
    2014-12-26T22:30:00, now is in pre_next,pre_next_num is :18141
    2014-12-26T22:30:00, now is in next, next num is :18141
    2014-12-26T22:30:00, ***********************************
    2014-12-26T22:45:00, now is in pre_next,pre_next_num is :18142
    2014-12-26T22:45:00, now is in next, next num is :18142
    2014-12-26T22:45:00, ***********************************
    2014-12-26T23:00:00, now is in pre_next,pre_next_num is :18143
    2014-12-26T23:00:00, now is in next, next num is :18143
    2014-12-26T23:00:00, ***********************************
    2014-12-26T23:00:00, now is in pre_next,pre_next_num is :18144
    2014-12-26T23:00:00, now is in next, next num is :18144
    2014-12-26T23:00:00, ***********************************
    2014-12-26T23:00:00, now is in pre_next,pre_next_num is :18145
    2014-12-26T23:00:00, now is in next, next num is :18145
    2014-12-26T23:00:00, ***********************************
    2014-12-26T23:00:00, now is in pre_next,pre_next_num is :18146
    2014-12-26T23:00:00, now is in next, next num is :18146
    2014-12-26T23:00:00, ***********************************
    2014-12-26T23:00:00, now is in pre_next,pre_next_num is :18147
    2014-12-26T23:00:00, now is in next, next num is :18147
    2014-12-26T23:00:00, ***********************************
    2014-12-26T23:00:00, now is in pre_next,pre_next_num is :18148
    2014-12-26T23:00:00, now is in next, next num is :18148
    2014-12-26T23:00:00, ***********************************
    2014-12-26T23:00:00, now is in pre_next,pre_next_num is :18149
    2014-12-26T23:00:00, now is in next, next num is :18149
    2014-12-26T23:00:00, ***********************************
    2014-12-26T23:00:00, now is in pre_next,pre_next_num is :18150
    2014-12-26T23:00:00, now is in next, next num is :18150
    2014-12-26T23:00:00, ***********************************
    2014-12-26T23:00:00, now is in pre_next,pre_next_num is :18151
    2014-12-26T23:00:00, now is in next, next num is :18151
    2014-12-26T23:00:00, ***********************************
    2014-12-29T09:15:00, now is in pre_next,pre_next_num is :18152
    2014-12-29T09:15:00, now is in next, next num is :18152
    2014-12-29T09:15:00, ***********************************
    2014-12-29T09:30:00, now is in pre_next,pre_next_num is :18153
    2014-12-29T09:30:00, now is in next, next num is :18153
    2014-12-29T09:30:00, ***********************************
    2014-12-29T09:45:00, now is in pre_next,pre_next_num is :18154
    2014-12-29T09:45:00, now is in next, next num is :18154
    2014-12-29T09:45:00, ***********************************
    2014-12-29T10:00:00, now is in pre_next,pre_next_num is :18155
    2014-12-29T10:00:00, now is in next, next num is :18155
    2014-12-29T10:00:00, ***********************************
    2014-12-29T10:15:00, now is in pre_next,pre_next_num is :18156
    2014-12-29T10:15:00, now is in next, next num is :18156
    2014-12-29T10:15:00, ***********************************
    2014-12-29T10:45:00, now is in pre_next,pre_next_num is :18157
    2014-12-29T10:45:00, now is in next, next num is :18157
    2014-12-29T10:45:00, ***********************************
    2014-12-29T11:00:00, now is in pre_next,pre_next_num is :18158
    2014-12-29T11:00:00, now is in next, next num is :18158
    2014-12-29T11:00:00, ***********************************
    2014-12-29T11:15:00, now is in pre_next,pre_next_num is :18159
    2014-12-29T11:15:00, now is in next, next num is :18159
    2014-12-29T11:15:00, ***********************************
    2014-12-29T11:30:00, now is in pre_next,pre_next_num is :18160
    2014-12-29T11:30:00, now is in next, next num is :18160
    2014-12-29T11:30:00, ***********************************
    2014-12-29T13:45:00, now is in pre_next,pre_next_num is :18161
    2014-12-29T13:45:00, now is in next, next num is :18161
    2014-12-29T13:45:00, ***********************************
    2014-12-29T14:00:00, now is in pre_next,pre_next_num is :18162
    2014-12-29T14:00:00, now is in next, next num is :18162
    2014-12-29T14:00:00, ***********************************
    2014-12-29T14:15:00, now is in pre_next,pre_next_num is :18163
    2014-12-29T14:15:00, now is in next, next num is :18163
    2014-12-29T14:15:00, ***********************************
    2014-12-29T14:30:00, now is in pre_next,pre_next_num is :18164
    2014-12-29T14:30:00, now is in next, next num is :18164
    2014-12-29T14:30:00, ***********************************
    2014-12-29T14:45:00, now is in pre_next,pre_next_num is :18165
    2014-12-29T14:45:00, now is in next, next num is :18165
    2014-12-29T14:45:00, ***********************************
    2014-12-29T15:00:00, now is in pre_next,pre_next_num is :18166
    2014-12-29T15:00:00, now is in next, next num is :18166
    2014-12-29T15:00:00, ***********************************
    2014-12-29T21:15:00, now is in pre_next,pre_next_num is :18167
    2014-12-29T21:15:00, now is in next, next num is :18167
    2014-12-29T21:15:00, ***********************************
    2014-12-29T21:30:00, now is in pre_next,pre_next_num is :18168
    2014-12-29T21:30:00, now is in next, next num is :18168
    2014-12-29T21:30:00, ***********************************
    2014-12-29T21:45:00, now is in pre_next,pre_next_num is :18169
    2014-12-29T21:45:00, now is in next, next num is :18169
    2014-12-29T21:45:00, ***********************************
    2014-12-29T22:00:00, now is in pre_next,pre_next_num is :18170
    2014-12-29T22:00:00, now is in next, next num is :18170
    2014-12-29T22:00:00, ***********************************
    2014-12-29T22:15:00, now is in pre_next,pre_next_num is :18171
    2014-12-29T22:15:00, now is in next, next num is :18171
    2014-12-29T22:15:00, ***********************************
    2014-12-29T22:30:00, now is in pre_next,pre_next_num is :18172
    2014-12-29T22:30:00, now is in next, next num is :18172
    2014-12-29T22:30:00, ***********************************
    2014-12-29T22:45:00, now is in pre_next,pre_next_num is :18173
    2014-12-29T22:45:00, now is in next, next num is :18173
    2014-12-29T22:45:00, ***********************************
    2014-12-29T23:00:00, now is in pre_next,pre_next_num is :18174
    2014-12-29T23:00:00, now is in next, next num is :18174
    2014-12-29T23:00:00, ***********************************
    2014-12-29T23:00:00, now is in pre_next,pre_next_num is :18175
    2014-12-29T23:00:00, now is in next, next num is :18175
    2014-12-29T23:00:00, ***********************************
    2014-12-29T23:00:00, now is in pre_next,pre_next_num is :18176
    2014-12-29T23:00:00, now is in next, next num is :18176
    2014-12-29T23:00:00, ***********************************
    2014-12-29T23:00:00, now is in pre_next,pre_next_num is :18177
    2014-12-29T23:00:00, now is in next, next num is :18177
    2014-12-29T23:00:00, ***********************************
    2014-12-29T23:00:00, now is in pre_next,pre_next_num is :18178
    2014-12-29T23:00:00, now is in next, next num is :18178
    2014-12-29T23:00:00, ***********************************
    2014-12-29T23:00:00, now is in pre_next,pre_next_num is :18179
    2014-12-29T23:00:00, now is in next, next num is :18179
    2014-12-29T23:00:00, ***********************************
    2014-12-29T23:00:00, now is in pre_next,pre_next_num is :18180
    2014-12-29T23:00:00, now is in next, next num is :18180
    2014-12-29T23:00:00, ***********************************
    2014-12-29T23:00:00, now is in pre_next,pre_next_num is :18181
    2014-12-29T23:00:00, now is in next, next num is :18181
    2014-12-29T23:00:00, ***********************************
    2014-12-29T23:00:00, now is in pre_next,pre_next_num is :18182
    2014-12-29T23:00:00, now is in next, next num is :18182
    2014-12-29T23:00:00, ***********************************
    
    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    import time,datetime
    import os,sys
    import pickle 
    import numpy as np
    import pandas as pd
    import random
    import statsmodels.api as sm
    
    import backtrader as bt  # backtrader
    
    
    
    ### 编写相应的策略,每个策略逻辑需要单独编写,回测和实盘直接运行策略类就行
    
    class bowling(bt.Strategy):
        # 策略作者
        author = 'yunjinqi'
        # 策略的参数
        params = (("y",200),
                  ("x",65),
                  ("m",1)
                )
        # log相应的信息
        def log(self, txt, dt=None):
            ''' Logging function fot this strategy'''
            dt = dt or self.datas[0].datetime.datetime(0)
            print('{}, {}'.format(dt.isoformat(), txt))
    
        # 初始化策略的数据
        def __init__(self):
            # 基本上常用的部分属性变量
            self.bar_num = 0                 # next运行了多少个bar
            self.current_date = None        # 当前交易日
            # next的数量
            self.next_num=0
            self.to_next=0
        def prenext(self):
            # 由于期货数据有几千个,每个期货交易日期不同,并不会自然进入next
            # 需要在每个prenext中调用next函数进行运行
            self.next_num+=1
            self.log("now is in pre_next,pre_next_num is :{}".format(self.next_num))
            self.next()
        # 在next中添加相应的策略逻辑
        def next(self):
            self.to_next+=1
            self.log("now is in next, next num is :{}".format(self.to_next))
            # 每次运行一次,bar_num自然加1,并更新交易日
            self.current_date = self.datas[0].datetime.datetime(0)
            current_date = self.current_date.strftime("%Y-%m-%d %H:%M:%S")
            self.log("***********************************")
            self.bar_num+=1
        
                
        def stop(self):
            
            pass 
    # 继承cerebro,backtrader的大脑,添加数据,策略,交易信息,运行回测和交易
    cerebro = bt.Cerebro()
    
    joinquant_day_kwargs = dict(
                # fromdate = datetime.datetime(2010, 1, 4),
                fromdate = datetime.datetime(2009, 3, 27),
                todate = datetime.datetime(2020,3,31),
                timeframe = bt.TimeFrame.Minutes,
                compression = 1,
                dtformat=('%Y-%m-%d %H:%M:%S'),
                tmformat=('%H:%M:%S'),
                datetime=0,
                high=3,
                low=4,
                open=1,
                close=2,
                volume=5,
                openinterest=7)
    
    # 虚拟合约的参数设置
    meta_data_kwargs = dict(
                fromdate = datetime.datetime(2010,1,4),
                todate = datetime.datetime(2019,12,31),
                timeframe = bt.TimeFrame.Minutes,
                compression = 1,
                dtformat=('%Y-%m-%d %H:%M:%S'),
                tmformat=('%H:%M:%S'),
                datetime=0,
                high=2,
                low=3,
                open=1,
                close=4,
                volume=5,
                openinterest=6)
    
    
    
    # 虚拟的期货合约,用于对齐交易日
    # data_path="C:/data/future_meta_data.csv"
    # # 增加元数据,包含所有的交易日期
    # meta_feed = bt.feeds.GenericCSVData(dataname = data_path, **meta_data_kwargs)
    # cerebro.adddata(meta_feed, name = 'meta')
    tbquant_kwargs = dict(
                fromdate = datetime.datetime(2010, 1, 1),
                todate = datetime.datetime(2020,3,30),
                timeframe = bt.TimeFrame.Minutes,
                compression = 1,
                # dtformat=('%Y-%m-%d %H:%M:%S'),
                # datetime='%H:%M:%S',
                high=2,
                low=3,
                open=1,
                close=4,
                volume=5,
                openinterest=6)
    
    params=dict(fromdate = datetime.datetime(2010, 1, 1),
                todate = datetime.datetime(2020,3,30))
    # 虚拟的期货合约,用于对齐交易日
    data_path="C:/Users/WIN10/Desktop/ru000.csv"
    # 增加数据,包含所有的交易日期
    feed = bt.feeds.GenericCSVData(dataname = data_path, **tbquant_kwargs)
    # 添加合约数据
    cerebro.adddata(feed, name = "ru8888.XSGE")
    
    
    # 添加RB的数据
    day_data_path="C:/data/future/min15/"
    file_list=list(os.listdir(day_data_path))
    # 剔除数据中的主力连续合约,聚宽中使用“8888"表示
    file_list=[i for i in file_list if "9999" not in i]
    file_list=[i for i in file_list if ''.join([j for j in i.split('.')[0] if j.isalpha()]).upper()=="RU"] # 保留螺纹钢的数据
    
    # 真实的合约
    file_contract_list=[i for i in file_list if "8888" not in i]
    
    # 聚宽构建的指数合约
    file_index_list=[i for i in file_list if "8888"  in i]
    
    # 获取期货合约的数据
    future_info_df=pd.read_csv("C:/data/future_info.csv",encoding='gbk')
    order_book_list=list(future_info_df['order_book_id'])
    
    count=0
    for file in file_contract_list:
        df = pd.read_csv(day_data_path+file)
        if len(df)==0:
            # print(file)
            continue
        df.columns=['datetime','open','close','high','low','volume','openinterest']
        df.index=pd.to_datetime(df['datetime'])
        df=df[['open','high','low','close','volume','openinterest']]
        feed =  bt.feeds.PandasDirectData(dataname=df,**params)
        # 读取期货合约的数据
        # 期货合约代码
        contract_name=file[:-4]
        cerebro.adddata(feed, name = contract_name)
        count+=1
    print("now has add data num is {} ".format(count))
    cerebro.broker.setcash(50000.0)
    cerebro.addstrategy(bowling)
    cerebro.run()
    


  • @tianjixuetu said in why the prenext and next can run many times in one bar?:

    timeframe = bt.TimeFrame.Minutes,
    compression = 1,

    You are loading the data and telling cerebro that you have 'minute' data at 1 minutes frequency. For 15 minute data change the compression to 15. For daily data you can just comment out the timeframe and compression lines as that is the default. Or change the timeframe to bt.TimeFrame.Days.



  • @run-out thank you very much.but it is not this reason.

    I try your suggestion,but it don't work. anyway,thank you again!

    According to the authot's documents:

    timeframe (default: TimeFrame.Days)
    
    Potential values: Ticks, Seconds, Minutes, Days, Weeks, Months and Years
    
    compression (default: 1)
    
    Number of actual bars per bar. Informative. Only effective in Data Resampling/Replaying.
    


  • interesting. if you load only one data feed to bt, does it have the same repetitive logs?



  • @ab_trader when just load the first data,it is normal.so I guess,when add more data,using prenext and next,there maybe something going wrong.



  • Your pandas data feeds goes without time frame parameter. Maybe this causes the issue.



  • @ab_trader thank you very much . It is not the pandas data feeds's question . because when I change to the csv data feed,it is also this question. when I change the data source,it is good. So , I guess,it maybe the origin data's question,it is not equal datetime.



  • @tianjixuetu said in why the prenext and next can run many times in one bar?:

    def prenext(self):
    # 由于期货数据有几千个,每个期货交易日期不同,并不会自然进入next
    # 需要在每个prenext中调用next函数进行运行
    self.next_num+=1
    self.log("now is in pre_next,pre_next_num is :{}".format(self.next_num))
    self.next()

    Try removing self.next() from prenext. The system will call next by itself.



  • @tianjixuetu

    let me rephrase - in the script shown above you specify timeframe only for the first data feed added, but second set of data feeds added using for loop doesn't have timeframe specified. So it is daily for bt. Maybe this causes the issue. I didn't get you comment about

    it is not equal datetime.

    Anyway, looks like the issue is in your data feeds, and without looking on them and understanding what data feeds causes this issue we can guess for a long time. If you really want help, please upload minimum set of data feeds (2, 3, 10 whatever number) so I can run the script by myself and check out it deeply. Don;t need the whole length of the data feeds, just date range which causes an issue.

    @run-out said in why the prenext and next can run many times in one bar?:
    Try removing self.next() from prenext. The system will call next by itself.

    looks like his data feeds have different lengthes, so he needs to work in both prenext and next.



  • @ab_trader yes,the for loop data is daily when I don't add the params which sets timeframe to be Minutes,but when I use the tbquant_kwargs, the problem is the same. So, there must be something occuring in the data that I add in the for loop.
    I guess ,mybe the bar's datetime ,it is not equal,some contract have more bar in a day,but some contract have less. but I can't understand why this will occur,I will read the source code and understand the reason in the future.



  • set the session_start and session_end params for 1st data feed, seems your intraday and daily data feeds are not in sync.



  • I get this too and I'm not sure why.

    For example, I have some code that counts a streak so it keeps upping the streak for the same piece of data, haha.

    Data0, 0012, 05-12 21:51:58, C 107.130000, O 107.120000, 12
    Data0, 0012, 05-12 21:51:58, C 107.130000, O 107.120000, 13
    Data0, 0012, 05-12 21:51:58, C 107.130000, O 107.120000, 14
    Data0, 0012, 05-12 21:51:58, C 107.130000, O 107.120000, 15
    Data0, 0012, 05-12 21:51:58, C 107.130000, O 107.120000, 16
    Data0, 0012, 05-12 21:51:58, C 107.130000, O 107.120000, 17
    

    I noticed using the length of the data to dedup like this helps, but I'm not sure if I'm supposed to be doing this or not.

        def next(self):
            # dedup
            if self.lastlen and len(self.data0) == self.lastlen: return
            self.lastlen = len(self.data0)
    


  • if you guys can provide pieces of your data and script which causes the issue, possibly we can be able to help.



  • @ab_trader said in why the prenext and next can run many times in one bar?:

    session_start and session_end params

    this two params may be used in resampling . in the normal data, fromdate todate can control the data. all the data is the same timeframe and not use resampling.



  • @booboothefool said in why the prenext and next can run many times in one bar?:

    I noticed using the length of the data to dedup like this helps, but I'm not sure if I'm supposed to be doing this or not.

    I also use this way to solve this question,also the code is not same.



  • I've ran the test with two data feeds - 15 min and daily, not resampled. No excessive repetition of the next() calls, except end of the day call - it is called once for last smaller time frame bar and once for daily bar.

    Script -

    from datetime import datetime
    import backtrader as bt
    
    class Test(bt.Strategy):
    
        def log(self, txt, dt=None):
            dt = dt or self.datas[0].datetime.datetime(0)
            print('%s, %s' % (dt.isoformat(), txt))
            
        def __init__(self):
    
            pass 
    
        def prenext(self):
    
            self.next()
    
        def next(self):
    
            self.log('C: %0.2f, data0: %d, data1: %d' % 
                    (self.data.close[0], len(self.data0), len(self.data1)))
    
    
    cerebro = bt.Cerebro()
    cerebro.addstrategy(Test)
    
    data_d = bt.feeds.GenericCSVData(dataname='sim_daily.txt', plot=True,name='D',
                                     dtformat=('%Y-%m-%d'))
    
    data_15 = bt.feeds.GenericCSVData(dataname='sim_15min.txt', plot=True,name='15',
                                      timeframe=bt.TimeFrame.Minutes,
                                      dtformat=('%Y-%m-%d %H:%M:%S'))
    
    cerebro.adddata(data_15)
    cerebro.adddata(data_d)
    
    cerebro.run()
    cerebro.plot()
    


  • Outputs -

    2020-05-04T08:15:00, C: 35.00, data0: 1, data1: 0
    2020-05-04T08:30:00, C: 35.00, data0: 2, data1: 0
    2020-05-04T08:45:00, C: 35.00, data0: 3, data1: 0
    2020-05-04T09:00:00, C: 35.00, data0: 4, data1: 0
    2020-05-04T09:15:00, C: 35.00, data0: 5, data1: 0
    2020-05-04T09:30:00, C: 35.00, data0: 6, data1: 0
    2020-05-04T09:45:00, C: 35.00, data0: 7, data1: 0
    2020-05-04T10:00:00, C: 35.00, data0: 8, data1: 0
    2020-05-04T10:15:00, C: 35.00, data0: 9, data1: 0
    2020-05-04T10:30:00, C: 35.00, data0: 10, data1: 0
    2020-05-04T10:45:00, C: 35.00, data0: 11, data1: 0
    2020-05-04T11:00:00, C: 35.00, data0: 12, data1: 0
    2020-05-04T11:15:00, C: 35.00, data0: 13, data1: 0
    2020-05-04T11:30:00, C: 35.00, data0: 14, data1: 0
    2020-05-04T11:45:00, C: 35.00, data0: 15, data1: 0
    2020-05-04T12:00:00, C: 35.00, data0: 16, data1: 0
    2020-05-04T12:00:00, C: 35.00, data0: 16, data1: 1
    2020-05-05T08:15:00, C: 35.00, data0: 17, data1: 1
    2020-05-05T08:30:00, C: 35.00, data0: 18, data1: 1
    2020-05-05T08:45:00, C: 35.00, data0: 19, data1: 1
    2020-05-05T09:00:00, C: 35.00, data0: 20, data1: 1
    2020-05-05T09:15:00, C: 35.00, data0: 21, data1: 1
    2020-05-05T09:30:00, C: 35.00, data0: 22, data1: 1
    2020-05-05T09:45:00, C: 35.00, data0: 23, data1: 1
    2020-05-05T10:00:00, C: 35.00, data0: 24, data1: 1
    2020-05-05T10:15:00, C: 35.00, data0: 25, data1: 1
    2020-05-05T10:30:00, C: 35.00, data0: 26, data1: 1
    2020-05-05T10:45:00, C: 35.00, data0: 27, data1: 1
    2020-05-05T11:00:00, C: 35.00, data0: 28, data1: 1
    2020-05-05T11:15:00, C: 35.00, data0: 29, data1: 1
    2020-05-05T11:30:00, C: 35.00, data0: 30, data1: 1
    2020-05-05T11:45:00, C: 35.00, data0: 31, data1: 1
    2020-05-05T12:00:00, C: 35.00, data0: 32, data1: 1
    2020-05-05T12:00:00, C: 35.00, data0: 32, data1: 2
    2020-05-06T08:15:00, C: 35.00, data0: 33, data1: 2
    2020-05-06T08:30:00, C: 35.00, data0: 34, data1: 2
    2020-05-06T08:45:00, C: 35.00, data0: 35, data1: 2
    2020-05-06T09:00:00, C: 35.00, data0: 36, data1: 2
    2020-05-06T09:15:00, C: 35.00, data0: 37, data1: 2
    2020-05-06T09:30:00, C: 35.00, data0: 38, data1: 2
    2020-05-06T09:45:00, C: 35.00, data0: 39, data1: 2
    2020-05-06T10:00:00, C: 35.00, data0: 40, data1: 2
    2020-05-06T10:15:00, C: 35.00, data0: 41, data1: 2
    2020-05-06T10:30:00, C: 35.00, data0: 42, data1: 2
    2020-05-06T10:45:00, C: 35.00, data0: 43, data1: 2
    2020-05-06T11:00:00, C: 35.00, data0: 44, data1: 2
    2020-05-06T11:15:00, C: 35.00, data0: 45, data1: 2
    2020-05-06T11:30:00, C: 35.00, data0: 46, data1: 2
    2020-05-06T11:45:00, C: 35.00, data0: 47, data1: 2
    2020-05-06T12:00:00, C: 35.00, data0: 48, data1: 2
    2020-05-06T12:00:00, C: 35.00, data0: 48, data1: 3
    2020-05-07T08:15:00, C: 35.00, data0: 49, data1: 3
    2020-05-07T08:30:00, C: 35.00, data0: 50, data1: 3
    2020-05-07T08:45:00, C: 35.00, data0: 51, data1: 3
    2020-05-07T09:00:00, C: 35.00, data0: 52, data1: 3
    2020-05-07T09:15:00, C: 35.00, data0: 53, data1: 3
    2020-05-07T09:30:00, C: 35.00, data0: 54, data1: 3
    2020-05-07T09:45:00, C: 35.00, data0: 55, data1: 3
    2020-05-07T10:00:00, C: 35.00, data0: 56, data1: 3
    2020-05-07T10:15:00, C: 35.00, data0: 57, data1: 3
    2020-05-07T10:30:00, C: 35.00, data0: 58, data1: 3
    2020-05-07T10:45:00, C: 35.00, data0: 59, data1: 3
    2020-05-07T11:00:00, C: 35.00, data0: 60, data1: 3
    2020-05-07T11:15:00, C: 35.00, data0: 61, data1: 3
    2020-05-07T11:30:00, C: 35.00, data0: 62, data1: 3
    2020-05-07T11:45:00, C: 35.00, data0: 63, data1: 3
    2020-05-07T12:00:00, C: 35.00, data0: 64, data1: 3
    2020-05-07T12:00:00, C: 35.00, data0: 64, data1: 4
    2020-05-08T08:15:00, C: 35.00, data0: 65, data1: 4
    2020-05-08T08:30:00, C: 35.00, data0: 66, data1: 4
    2020-05-08T08:45:00, C: 35.00, data0: 67, data1: 4
    2020-05-08T09:00:00, C: 35.00, data0: 68, data1: 4
    2020-05-08T09:15:00, C: 35.00, data0: 69, data1: 4
    2020-05-08T09:30:00, C: 35.00, data0: 70, data1: 4
    2020-05-08T09:45:00, C: 35.00, data0: 71, data1: 4
    2020-05-08T10:00:00, C: 35.00, data0: 72, data1: 4
    2020-05-08T10:15:00, C: 35.00, data0: 73, data1: 4
    2020-05-08T10:30:00, C: 35.00, data0: 74, data1: 4
    2020-05-08T10:45:00, C: 35.00, data0: 75, data1: 4
    2020-05-08T11:00:00, C: 35.00, data0: 76, data1: 4
    2020-05-08T11:15:00, C: 35.00, data0: 77, data1: 4
    2020-05-08T11:30:00, C: 35.00, data0: 78, data1: 4
    2020-05-08T11:45:00, C: 35.00, data0: 79, data1: 4
    2020-05-08T12:00:00, C: 35.00, data0: 80, data1: 4
    2020-05-08T12:00:00, C: 35.00, data0: 80, data1: 5
    


  • @ab_trader said in why the prenext and next can run many times in one bar?:

    2020-05-04T12:00:00, C: 35.00, data0: 16, data1: 0
    2020-05-04T12:00:00, C: 35.00, data0: 16, data1: 1

    this is the question. when you run strategy,if you use multi data,if some day,you run two time next,is it right for your strategy logical?

    when you use more data,you may meet more stranger question.

    do you know,why the next run two times at the end day? we may fix this bug!



  • @tianjixuetu said in why the prenext and next can run many times in one bar?:

    do you know,why the next run two times at the end day? we may fix this bug!

    I suggest you a better approach - do not try to fix the things which you don't understand, but try to figure out why is it happening. Simply assume that the person who wrote this complex tool used by a lot of people here in community and outside did thorough check and debugging.

    The thing you pointed was discussed several times long time ago. In order to make it easier to understand I reworked my data feeds a bit, so last (per session) bar of the intradady data feed has different close price compare to daily data feed.

    2020-05-04T11:45:00, C0: 35.00, data0: 15, C1: 34.00, data1: 0
    2020-05-04T12:00:00, C0: 35.00, data0: 16, C1: 34.00, data1: 0
    2020-05-04T12:00:00, C0: 35.00, data0: 16, C1: 30.00, data1: 1
    2020-05-05T08:15:00, C0: 35.00, data0: 17, C1: 30.00, data1: 1
    

    At 12pm the next() was called twice:

    • 1st time for intraday data feed - it length is 16 (16 15 min bars for 05/04), length of the daily data feed is 0 (no bars delivered yet since day is not gone yet).

    • 2nd time for daily data feed - it length is 1 now (1 bar for 05/04), length of the intraday data feed is 16, data feed not moved forward since no new intraday bar happen.



  • Here https://community.backtrader.com/topic/141/new-behavior-resampling-on-end-of-session new behavior was introduced with the sessionend parameter use. You may want to try.


Log in to reply
 

});