Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    How to get MACD of 1 minute and 5 minute

    General Discussion
    2
    5
    839
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • mr-m0nst3r
      mr-m0nst3r last edited by

      hey guys,

      It seems that there're no period parameter for bt.indicators.MACD and bt.indicators.MACDHisto like bt.indicators.SMA, question is clear:

      How should I do if I need to get MACD of 1minute and 5minute for a strategy from one date feed(ccxt live).

      Any suggestions?

      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators @mr-m0nst3r last edited by

        @mr-m0nst3r said in How to get MACD of 1 minute and 5 minute:

        It seems that there're no period parameter for bt.indicators.MACD and bt.indicators.MACDHisto

        They actually have 3 period parameters.

        Docs - Indicator Reference

        1 Reply Last reply Reply Quote 0
        • mr-m0nst3r
          mr-m0nst3r last edited by

          Hi @backtrader ,

          I noticed the three parameters:

          1 - period_me1 (12)
          
          2 - period_me2 (26)
          
          3 - period_signal (9)
          
          4 - movav (ExponentialMovingAverage)
          

          But the first three params are the standard MACD calculation params, the 4th param is MovAv.Exponential.

          the problem is how to get MACD of 5 minutes period and 15 minutes. Using other indicators like SMA we could script like this:

          // data feed's timeframe is `Minutes`
          self.sma = bt.indicators.SMA(data, period=5) // To get 5 minutes SMA
          self.sma2 = bt.indicators.SMA(data, period=15) // to get 15 minutes SMA
          

          But how to achieve this using MACD?
          self.macd = bt.indicators.MACD(data, ??=5)?

          It seems the only way is to resample data to 5 minutes timeframe then use self.macd = bt.indicators.MACD(data) to get 5 minutes MACD.

          Am I understanding it right?

          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by

            @mr-m0nst3r said in How to get MACD of 1 minute and 5 minute:

            Am I understanding it right?

            No. The MACD needs 3 period parameters and that determines the calculations. You should probably understand what the MACD is.

            @mr-m0nst3r said in How to get MACD of 1 minute and 5 minute:

            self.sma = bt.indicators.SMA(data, period=5) // To get 5 minutes SMA
            

            This is not the same as this ...

            @mr-m0nst3r said in How to get MACD of 1 minute and 5 minute:

            It seems the only way is to resample data to 5 minutes timeframe then use self.macd = bt.indicators.MACD(data) to get 5 minutes MACD

            The SMA with period 5 is NOT giving you the simple moving average of a resampled data.

            Your problem is that you are mixing timeframe and period. The timeframe applies to the data feeds. The period is what the indicators use as look-back value to understand how many bars of a given timeframe have to be used.

            1 Reply Last reply Reply Quote 1
            • mr-m0nst3r
              mr-m0nst3r last edited by mr-m0nst3r

              @backtrader said in How to get MACD of 1 minute and 5 minute:

              The period is what the indicators use as loo

              Hi @backtrader ,

              Thank you for your answer.
              First of all, this is my understanding of timeframe and period:
              timeframe is an attribute of the data feed; period is the "bar".
              If the data's timeframe is 1 minute, then 1 period is 1 minute's data (OCHLV); if data's timeframe is 5 minutes, then 1 bar is 5 minutes' data.

              I use bt.timeframe.Minutes all the time, so why bt.indicators.SMA(data, period=5) is not giving me the 5 minutes SMA value? I directly use the data, not a resampled one.
              I guess to get SMA of 5 minutes, I have to use 5 minutes timeframe data, seems clearer to me now. Thank you.

              And I used the Minutes data to resample to a 5 minutes timeframe using cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=5). Am I doing it right?

              And you're right, I'm confused when it comes to MACD.

              MACD=12-Period EMA − 26-Period EMA

              So, if the data's timeframe is 5 minutes, we'll get EMA(12) and EMA(26) of 5 minutes' timeframe, after calculation, it's the 5m MACD value, is that right?

              From the MACD equation, I see only period calculations, whether this period stands for 1 minute or 5 minutes, it's decided by the data's timeframe, which lead me to the conclusion that to get macd of 5m, the only way is to resample original 1m timeframe data to 5m timeframe data using compression=5. is that right?

              I'm stuck here, and forgive me if my expression confuses you.
              Thank you very much for your time.

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors