Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import numpy as np
- df = pd.read_csv('../datas.csv', sep=',')
- def tmf(df, n=21, fillna = False):
- high = df['High']
- low = df['Low']
- prev_close = df['Adj Close'].shift(1)
- prev_close = prev_close.fillna(df['Low'][0])
- close = df['Adj Close']
- volume = df['Volume']
- trh = abs(np.maximum(prev_close,high))
- trl = abs(np.minimum(prev_close,low))
- TR = trh-trl
- mfv = (((close - trl)/TR)*2) - 1
- mfv = mfv.fillna(0.0)
- ad = mfv * volume
- tmf = (ad.ewm(span=((n*2)-1),min_periods=0).mean() / volume.ewm(span=((n*2)-1),min_periods=0).mean())
- if fillna:
- tmf = tmf.replace([np.inf, -np.inf], np.nan).fillna(0)
- return tmf
- def cmf(df, n=21, fillna = False):
- high = df['High']
- low = df['Low']
- close = df['Adj Close']
- volume = df['Volume']
- mfv = ((close - low - (high - close))) / (high - low)
- mfv = mfv.fillna(0.0)
- mfv *= volume
- cmf = (mfv.rolling(n, min_periods=0).sum()
- / volume.rolling(n, min_periods=0).sum()
- )
- if fillna:
- cmf = cmf.replace([np.inf, -np.inf], np.nan).fillna(0)
- return cmf
- def compare(a, b):
- if a.all() == b.all():
- return True
- else:
- return False
- print(compare (cmf(df), tmf(df)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement