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/

    Money Flow Index (MFI)

    Indicators/Strategies/Analyzers
    2
    2
    284
    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.
    • Rodrigo Brito
      Rodrigo Brito last edited by

      I implemented the MFI for Backtrader, but I think this can be improved:

      #!/usr/bin/env python3
      
      import math
      import backtrader as bt
      
      class MFI(bt.Indicator):
          lines = ('mfi', 'money_flow_raw', 'typical', 'money_flow_pos', 'money_flow_neg')
      
          plotlines = dict(
              money_flow_raw=dict(_plotskip=True),
              money_flow_pos=dict(_plotskip=True),
              money_flow_neg=dict(_plotskip=True),
              typical=dict(_plotskip=True),
          )
      
          params = (
              ('period', 14),
          )
      
          def next(self):
              typical_price = (self.data.close[0] + self.data.low[0] + self.data.high[0]) / 3
              money_flow_raw = typical_price * self.data.volume[0]
              
              self.lines.typical[0] = typical_price
              self.lines.money_flow_raw[0] = money_flow_raw
      
              self.lines.money_flow_pos[0] = money_flow_raw if self.lines.typical[0] >= self.lines.typical[-1] else 0
              self.lines.money_flow_neg[0] = money_flow_raw if self.lines.typical[0] <= self.lines.typical[-1] else 0
      
              pos_period = math.fsum(self.lines.money_flow_pos.get(size=self.p.period))
              neg_period = math.fsum(self.lines.money_flow_neg.get(size=self.p.period))
              
              if neg_period == 0:
                  self.lines.mfi[0] = 100
                  return
      
              self.lines.mfi[0] =  100 - 100 / (1 +  pos_period / neg_period)
      
      

      I used two auxiliary lines to sum negative/positive values when typical_price change. There are some alternative to reduce it and compare two different lines given a period?

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

        1. A reference is always a good starting point. It does let people be on common ground. For example

          • https://school.stockcharts.com/doku.php?id=technical_indicators:money_flow_index_mfi
        2. You probably want to read the documentation about line delaying and stage-1, stage-2 operations.

          • Docs - Platform Concepts
        3. What some people would call a "canonical" implementation can be found here ... (which you probably can better grasp after seeing the Stage-1 explanation about operators)

          • https://www.backtrader.com/blog/2019-07-08-canonical-or-not/canonical-or-not/
        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
        $(document).ready(function () { app.coldLoad(); }); }