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)



  • 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?


  • administrators

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

    2. You probably want to read the documentation about line delaying and stage-1, stage-2 operations.

    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)


Log in to reply
 

});