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

Indicator That Is Aware Of Current Date / Time



  • Hello All!

    I am attempting to write an indicator that keeps track of certain close prices at certain times. For example, in a strategy's next() method, I can do something like this:

    def next(self):
        if self.datetime.date(ago=0).hour != self.datetime.date(ago=1).hour:
            print('The hour has changed!')
    

    How can I do this in an indicator? As indicators don't receive strategy information, I'm confused about how to proceed.

    Thanks!



  • Passing the Strategy as an arg to the indicator during creation?



  • I tried to do this:

    class indicator_my_indicator(bt.Indicator):
        lines = ('myindicator',)
    
        def __init__(self, strategy):
            self.strategy = strategy
    

    Then, in each next() iteration, try to use self.strategy.datetime.time().hour or something similar, but it doesn't work. I get the following error message:

    ValueError: cannot convert float NaN to integer



  • You are (mostly sure) running in runonce=True mode (aka pseudo-vectorized) because it is the default mode.

    Read about it here: https://www.backtrader.com/docu/cerebro.html

    And disable it to avoid the indicators being pre-processed before the strategy.



  • You are absolutely correct. I changed that setting, and now it works more as expected.

    THANK YOU!