Algotopian is starting. Let it be the future of backtrader. Check it out! https://www.algotopian.com
For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

A Dynamic Indicator


  • administrators

    Use the link below to go the original post

    Click here to see the full blog post



  • Great stuff! Would someone be interested in coding the Mcginley dynamic indicator?

    http://www.tradeforextrading.com/index.php/mcginley-dynamic-indicator





  • Actually I have a problem running this. I am not sure if I am missing something but it seems that the indicator is computed all at once during strategy initialization. This is a problem since the indicator should be influenced by trades, which are computed later. This is my indicator code:
    '''
    class DynamicHighest(bt.Indicator):
    """indicates the highest high since a trade was open."""
    lines = ('dyn_highest',)

    def __init__(self):
        self._tradeopen = False
        self.addminperdiod = 2
    
    def tradeopen(self, isopen):
        self._tradeopen = isopen
        print("tradeopen {isopen}".format(isopen=isopen))
    
    def next(self):
        if self._tradeopen:
            print("trade is open")
            self.lines.dyn_highest[0] = max(self.data[0].high, self.dyn_highest[-1])
        else:
            print("trade is close")
            self.lines.dyn_highest[0] = 0 
    

    '''
    now when I run cerebro I get this output:
    trade is close
    trade is close
    trade is close
    .
    .
    .
    trade is close
    trade is close
    trade is close
    trade is close
    trade is close
    dyn_highest: 0.00 | stoplimit: -0.03 | close: 0.13
    BUY
    tradeopen True
    dyn_highest: 0.00 | stoplimit: -0.03 | close: 0.15
    dyn_highest: 0.00 | stoplimit: -0.03 | close: 0.19
    dyn_highest: 0.00 | stoplimit: -0.03 | close: 0.17
    dyn_highest: 0.00 | stoplimit: -0.03 | close: 0.19
    dyn_highest: 0.00 | stoplimit: -0.03 | close: 0.20
    dyn_highest: 0.00 | stoplimit: -0.04 | close: 0.19
    dyn_highest: 0.00 | stoplimit: -0.04 | close: 0.20
    dyn_highest: 0.00 | stoplimit: -0.04 | close: 0.19
    dyn_highest: 0.00 | stoplimit: -0.04 | close: 0.19
    dyn_highest: 0.00 | stoplimit: -0.04 | close: 0.23

    So as we see dyn_highest stays 0 the whole time.
    Is there a way to prevent precalculation of the indicator?



  • Running into the same problem as Benoit described below. Everything gets computed during the initialization of the code resulting in either full nan or 0 (depending the logic assignment you want to adopt).



  • Benoît -- have you tried runonce=False?



  • @bigdavediode Yes, that fixed the problem, thank you!


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.