def can_sell(stock, day_count=3):
DIF, DEA, macd = MACD(stock)
if DIF[-1] < DEA[-1]:
return True
result = True
for i in range(1, day_count):
result = result and DIF[-i] < DIF[-i - 1]
return result
# ??MACD??????????
python类MACD的实例源码
def MACD(stock):
prices = attribute_history(stock, 130, '1d', ('close'), fq='pre')['close'].values
# ?????????
cur_prices = attribute_history(stock, 1, '1m', ('close'), fq='pre')['close'].values
prices += cur_prices
DIF, DEA, macd = talib.MACD(prices,
fastperiod=12,
slowperiod=26,
signalperiod=9)
return DIF, DEA, macd
def add_MACD(self, fastperiod=12, slowperiod=26, signalperiod=9,
types=['line', 'line', 'histogram'],
colors=['primary', 'tertiary', 'fill'],
**kwargs):
"""Moving Average Convergence Divergence.
Note that the first argument of types and colors refers to MACD,
the second argument refers to MACD signal line and the third argument
refers to MACD histogram.
"""
if not self.has_close:
raise Exception()
utils.kwargs_check(kwargs, VALID_TA_KWARGS)
if 'kind' in kwargs:
kwargs['type'] = kwargs['kind']
if 'kinds' in kwargs:
types = kwargs['type']
if 'type' in kwargs:
types = [kwargs['type']] * 3
if 'color' in kwargs:
colors = [kwargs['color']] * 3
name = 'MACD({},{},{})'.format(str(fastperiod),
str(slowperiod),
str(signalperiod))
macd = name
smacd = name + '[Sign]'
hmacd = name + '[Hist]'
self.sec[macd] = dict(type=types[0], color=colors[0])
self.sec[smacd] = dict(type=types[1], color=colors[1], on=macd)
self.sec[hmacd] = dict(type=types[2], color=colors[2], on=macd)
(self.ind[macd],
self.ind[smacd],
self.ind[hmacd]) = talib.MACD(self.df[self.cl].values,
fastperiod, slowperiod,
signalperiod)
def calculate_macd(self, period_name, closing_prices):
macd, macd_sig, macd_hist = talib.MACD(closing_prices, fastperiod=12,
slowperiod=26, signalperiod=9)
self.current_indicators[period_name]['macd'] = macd[-1]
self.current_indicators[period_name]['macd_sig'] = macd_sig[-1]
self.current_indicators[period_name]['macd_hist'] = macd_hist[-1]
self.current_indicators[period_name]['macd_hist_diff'] = Decimal(macd_hist[-1]) - Decimal(macd_hist[-2])
def calculate_vol_macd(self, period_name, volumes):
macd, macd_sig, macd_hist = talib.MACD(volumes, fastperiod=50,
slowperiod=200, signalperiod=14)
self.current_indicators[period_name]['vol_macd'] = macd[-1]
self.current_indicators[period_name]['vol_macd_sig'] = macd_sig[-1]
self.current_indicators[period_name]['vol_macd_hist'] = macd_hist[-1]
def __init__(self, selector):
self.selector = selector
self.supported = {"ROCP", "OROCP", "HROCP", "LROCP", "MACD", "RSI", "VROCP", "BOLL", "MA", "VMA", "PRICE_VOLUME"}
self.feature = []
prep_data_03_stock_02_OHLCV_arrays_2_features_targets_arrays.py 文件源码
项目:LIE
作者: EmbraceLife
项目源码
文件源码
阅读 17
收藏 0
点赞 0
评论 0
def __init__(self, selector):
self.selector = selector
self.supported = {"ROCP", "OROCP", "HROCP", "LROCP", "MACD", "RSI", "VROCP", "BOLL", "MA", "VMA", "PRICE_VOLUME"}
self.feature = []
def __init__(self, selector):
self.selector = selector
self.supported = {"ROCP", "OROCP", "HROCP", "LROCP", "MACD", "RSI", "VROCP", "BOLL", "MA", "VMA", "PRICE_VOLUME"}
self.feature = []
def __init__(self, selector):
self.selector = selector
self.supported = {"ROCP", "OROCP", "HROCP", "LROCP", "MACD", "RSI", "VROCP", "BOLL", "MA", "VMA", "PRICE_VOLUME"}
self.feature = []
def __init__(self, selector):
self.selector = selector
self.supported = {"ROCP", "OROCP", "HROCP", "LROCP", "MACD", "RSI", "VROCP", "BOLL", "MA", "VMA", "PRICE_VOLUME"}
self.feature = []
def MACD(df, short_win=12, long_win=26, macd_win=9):
# talib??MACD
prices = np.array(df['close'])
macd_tmp = talib.MACD(prices, fastperiod=short_win, slowperiod=long_win, signalperiod=macd_win)
df['macd_dif'] = macd_tmp[0]
df['macd_dea'] = macd_tmp[1]
df['macd'] = macd_tmp[2]
return df
def macd_rule_2(df, symbol):
try: df = MACD(df)
except: return False
input_1 = -3
input_2 = -0.2
index = -1
return (df['macd_dif'][index] > input_1) & \
(df['macd_dif'][index] < input_2) & \
(df['macd_dif'][index] > df['macd_dea'][index]) & \
((df['macd_dea'][index-1] > df['macd_dif'][index-1]) | (abs(df['macd_dea'][index-1] - df['macd_dif'][index-1]) < 0.007))
def MACD(df, short_win=12, long_win=26, macd_win=9):
# talib??MACD
prices = np.array(df['close'])
macd_tmp = talib.MACD(prices, fastperiod=short_win, slowperiod=long_win, signalperiod=macd_win)
df['macd_dif'] = macd_tmp[0]
df['macd_dea'] = macd_tmp[1]
df['macd'] = macd_tmp[2]
return df
def macd_rule_1(df, index = -1):
try:
if not {'macd_dif', 'macd_dea', 'macd'}.issubset(df.columns):
df = MACD(df)
except Exception as e:
print(e)
return False
return (df['macd_dif'][index] > df['macd_dea'][index]) & \
((df['macd_dea'][index-1] > df['macd_dif'][index-1]) | (abs(df['macd_dea'][index-1] - df['macd_dif'][index-1]) < 0.007))
def macd_rule_2(df, index = -1):
try:
if not {'macd_dif', 'macd_dea', 'macd'}.issubset(df.columns):
df = MACD(df)
except Exception as e:
print(e)
return False
input = 0.05
return (df['macd_dif'][index] < input) & (df['macd_dea'][index] < input)
def MACD(df, short_win=12, long_win=26, macd_win=9):
# talib??MACD
prices = np.array(df['close'])
macd_tmp = talib.MACD(prices, fastperiod=short_win, slowperiod=long_win, signalperiod=macd_win)
df['macd_dif'] = macd_tmp[0]
df['macd_dea'] = macd_tmp[1]
df['macd'] = macd_tmp[2]
return df
def macd_rule_1(df, index = -1):
try:
if not {'macd_dif', 'macd_dea', 'macd'}.issubset(df.columns):
df = MACD(df)
except Exception as e:
print(e)
return False
return (df['macd_dif'][index] > df['macd_dea'][index]) & \
((df['macd_dea'][index-1] > df['macd_dif'][index-1]) | (abs(df['macd_dea'][index-1] - df['macd_dif'][index-1]) < 0.007))
def macd(self, fastPeriod, slowPeriod, signalPeriod, array=False):
"""MACD??"""
macd, signal, hist = talib.MACD(self.close, fastPeriod,
slowPeriod, signalPeriod)
if array:
return macd, signal, hist
return macd[-1], signal[-1], hist[-1]
# ----------------------------------------------------------------------
def MACD(self):
"""???????????????
MACD: (12-day EMA - 26-day EMA) ??? ??
Signal Line: 9-day EMA of MACD ??, ??
MACD Histogram: MACD - Signal Line ???, ???
return : macd, macdsignal, macdhist(??)"""
closes = self.getCloses()
return talib.MACD(closes)
def MACD(closes):
"""???????????????
MACD: (12-day EMA - 26-day EMA) ??? ??
Signal Line: 9-day EMA of MACD ??, ??
MACD Histogram: MACD - Signal Line ???, ???
return : macd, macdsignal, macdhist(??)"""
return talib.MACD(closes)