This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NetPayOutData(bt.feeds.GenericCSVData): | |
lines = ('npy',) # add a line containing the net payout yield | |
params = dict( | |
npy=6, # npy field is in the 6th column (0 based index) | |
dtformat='%Y-%m-%d', # fix date format a yyyy-mm-dd | |
timeframe=bt.TimeFrame.Months, # fixed the timeframe | |
openinterest=-1, # -1 indicates there is no openinterest field | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
date, open, high, low, close, volume, npy | |
2001-12-31, 1.0, 1.0, 1.0, 1.0, 0.5, 3.0 | |
2002-01-31, 2.0, 2.5, 1.1, 1.2, 3.0, 5.0 | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mfiratio = bt.ind.DivByZero(flowpos, flowneg, zero=100.0) | |
self.l.mfi = 100.0 - 100.0 / (1.0 + mfiratio) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if neg_period == 0: | |
self.lines.mfi[0] = 100 | |
return | |
self.lines.mfi[0] = 100 - 100 / (1 + pos_period / neg_period) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MFI_Canonical(bt.Indicator): | |
lines = ('mfi',) | |
params = dict(period=14) | |
def __init__(self): | |
tprice = (self.data.close + self.data.low + self.data.high) / 3.0 | |
mfraw = tprice * self.data.volume | |
flowpos = bt.ind.SumN(mfraw * (tprice > tprice(-1)), period=self.p.period) | |
flowneg = bt.ind.SumN(mfraw * (tprice < tprice(-1)), period=self.p.period) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyMFIStrategy2(bt.Strategy): | |
def __init__(self): | |
MFI_MultipleInputs(self.data) | |
MFI_MultipleInputs(self.data.high, | |
self.data.low, | |
self.data.close * 5.0, | |
self.data.volume, | |
plotname='MFI Close * 5.0') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyMFIStrategy2(bt.Strategy): | |
def __init__(self): | |
MFI_Canonical(self.data) | |
MFI_MultipleInputs(self.data, plotname='MFI Single Input') | |
MFI_MultipleInputs(self.data.high, | |
self.data.low, | |
self.data.close, | |
self.data.volume, | |
plotname='MFI Multiple Inputs') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MFI_MultipleInputs(bt.Indicator): | |
lines = ('mfi',) | |
params = dict(period=14) | |
def __init__(self): | |
if len(self.datas) == 1: | |
# 1 data feed passed, must have components | |
tprice = (self.data.close + self.data.low + self.data.high) / 3.0 | |
mfraw = tprice * self.data.volume | |
else: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyMFIStrategy2(bt.Strategy): | |
def __init__(self): | |
wclose = self.data.close * 5.0 | |
mfi = bt.MFI_Canonical(self.data.high, self.data.low, wclose, self.data.volume) |