init
This commit is contained in:
63
stock/analysis/MFI.py
Normal file
63
stock/analysis/MFI.py
Normal file
@@ -0,0 +1,63 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from stock.analysis.Common import Common
|
||||
|
||||
# [청송촌놈] 파생을 알아야 시장이 보인다. 청송이 종목 고르는법! https://www.youtube.com/watch?v=weABtgZDeGg
|
||||
# 6. Pandas와 Plotly를 이용한 MACD 차트 그리기 https://excelsior-cjh.tistory.com/110
|
||||
# 첫번째. MACD 지표를 이용한 차트분석: https://post.naver.com/viewer/postView.nhn?volumeNo=7435935&memberNo=32471429
|
||||
|
||||
# MACD (Moving Average Conver gence Divergence)
|
||||
# 빨간 네모박스권으로 MACD가 MACD-Sign 을 골든크로스하며, 상승하였을때, 주가는 상승추세를 유지하며, MACD가 MACD-Sign(분홍색)을 데드크로스 할때 주가는 하락의 추세를 보이게 됩니다.
|
||||
# 즉, MSCD가 0이상에서 MACD-Sign 위에서 상승하는 그림이어야
|
||||
class MFI:
|
||||
|
||||
common = None
|
||||
|
||||
def __init__(self):
|
||||
self.common = Common()
|
||||
return
|
||||
|
||||
def apply(self, df, period = 14):
|
||||
typical_price = (df.high + df.low + df.close) / 3
|
||||
money_flow = typical_price * df.volume
|
||||
|
||||
positive_flow = []
|
||||
negative_flow = []
|
||||
|
||||
for i in range(1, len(typical_price)):
|
||||
if typical_price[i] > typical_price[i-1]:
|
||||
positive_flow.append(money_flow[i-1])
|
||||
negative_flow.append(0)
|
||||
elif typical_price[i] < typical_price[i - 1]:
|
||||
positive_flow.append(0)
|
||||
negative_flow.append(money_flow[i - 1])
|
||||
else:
|
||||
positive_flow.append(0)
|
||||
negative_flow.append(0)
|
||||
|
||||
positive_mf = []
|
||||
negative_mf = []
|
||||
|
||||
for i in range(period-1, len(positive_flow)):
|
||||
positive_mf.append(sum(positive_flow[i+1-period: i+1]))
|
||||
for i in range(period - 1, len(negative_flow)):
|
||||
negative_mf.append(sum(negative_flow[i + 1 - period: i + 1]))
|
||||
|
||||
mfi = list(100 * (np.array(positive_mf) / (np.array(positive_mf) + np.array(negative_mf))))
|
||||
mfi = list(np.repeat(np.nan, len(df)-len(mfi))) + mfi
|
||||
|
||||
#df = df.assign(macd=macd, macds=macds, macdo=macdo).dropna()
|
||||
df = df.assign(mfi=mfi)
|
||||
|
||||
return df
|
||||
|
||||
def analyze(self, stock):
|
||||
|
||||
df = pd.DataFrame()
|
||||
df = df.from_dict(stock['PRICE'])
|
||||
df = self.apply(df)
|
||||
|
||||
for i in range(len(df.macd)):
|
||||
stock['PRICE'][i]['mfi'] = df.mfi.values[i]
|
||||
|
||||
return
|
||||
Reference in New Issue
Block a user