Advertisement
IT45200

Untitled

Apr 4th, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3.  
  4. df = pd.read_csv('../datas.csv', sep=',')
  5.  
  6. def tmf(df, n=21, fillna = False):
  7.     high = df['High']
  8.     low = df['Low']
  9.     prev_close = df['Adj Close'].shift(1)
  10.     prev_close = prev_close.fillna(df['Low'][0])
  11.     close = df['Adj Close']
  12.     volume = df['Volume']
  13.  
  14.     trh = abs(np.maximum(prev_close,high))
  15.     trl = abs(np.minimum(prev_close,low))
  16.     TR = trh-trl
  17.     mfv = (((close - trl)/TR)*2) - 1
  18.     mfv = mfv.fillna(0.0)
  19.     ad = mfv * volume
  20.  
  21.     tmf = (ad.ewm(span=((n*2)-1),min_periods=0).mean() / volume.ewm(span=((n*2)-1),min_periods=0).mean())
  22.     if fillna:
  23.         tmf = tmf.replace([np.inf, -np.inf], np.nan).fillna(0)
  24.     return tmf
  25.  
  26. def cmf(df, n=21, fillna = False):
  27.     high = df['High']
  28.     low = df['Low']
  29.     close = df['Adj Close']
  30.     volume = df['Volume']
  31.  
  32.     mfv = ((close - low - (high - close))) / (high - low)
  33.     mfv = mfv.fillna(0.0)
  34.     mfv *= volume
  35.     cmf = (mfv.rolling(n, min_periods=0).sum()
  36.     / volume.rolling(n, min_periods=0).sum()
  37.     )
  38.     if fillna:
  39.         cmf = cmf.replace([np.inf, -np.inf], np.nan).fillna(0)
  40.     return cmf
  41.  
  42.  
  43. def compare(a, b):
  44.     if a.all() == b.all():
  45.         return True
  46.     else:
  47.         return False
  48. print(compare (cmf(df), tmf(df)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement