本文整理汇总了Python中talib.MACD属性的典型用法代码示例。如果您正苦于以下问题:Python talib.MACD属性的具体用法?Python talib.MACD怎么用?Python talib.MACD使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类talib
的用法示例。
在下文中一共展示了talib.MACD属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: macd_rule
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MACD [as 别名]
def macd_rule(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_1 = -3
input_2 = 0.05
# df['macd_dif_1'] = df['macd_dif'].shift(1)
# df['macd_dea_1'] = df['macd_dea'].shift(1)
#(df['macd_dif'][-input_3:].min() < input_2) & \
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))
示例2: TA_MACD
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MACD [as 别名]
def TA_MACD(prices:np.ndarray,
fastperiod:int=12,
slowperiod:int=26,
signalperiod:int=9) -> np.ndarray:
'''
参数设置:
fastperiod = 12
slowperiod = 26
signalperiod = 9
返回: macd - dif, signal - dea, hist * 2 - bar, delta
'''
macd, signal, hist = talib.MACD(prices,
fastperiod=fastperiod,
slowperiod=slowperiod,
signalperiod=signalperiod)
hist = (macd - signal) * 2
delta = np.r_[np.nan, np.diff(hist)]
return np.c_[macd, signal, hist, delta]
# 定义RSI函数
示例3: isBuy
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MACD [as 别名]
def isBuy(context, analysis):
# Bullish SMA Crossover
if (getLast(analysis, 'sma_test') == 1):
# Bullish MACD
if (getLast(analysis, 'macd_test') == 1):
return True
# # Bullish Stochastics
# if(getLast(analysis, 'stoch_over_sold') == 1):
# return True
# # Bullish RSI
# if(getLast(analysis, 'rsi_over_sold') == 1):
# return True
return False
示例4: handle_data
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MACD [as 别名]
def handle_data(context):
# 等待数据就绪,否则计算果结为异常值
if len(Close()) < slow + back - 1:
return
# 计算MACD
diff, dea, macd = talib.MACD(Close(), fast, slow, back)
# 突破下单
if MarketPosition() != 1 and macd[-1] > macd_dx:
Buy(qty, Open()[-1])
elif MarketPosition() != -1 and macd[-1] < -macd_dx:
SellShort(qty, Open()[-1])
# 绘制MACD曲线
PlotStickLine('macd', 0, macd[-1], RGB_Red() if macd[-1] > 0 else RGB_Blue(), False, False)
PlotNumeric('diff', diff[-1], RGB_Red(), False, False)
PlotNumeric('dea', dea[-1], RGB_Blue(), False, False)
# 绘制盈亏曲线
PlotNumeric("profit", NetProfit() + FloatProfit() - TradeCost(), 0xcccccc, False, True)
示例5: macd_rule
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MACD [as 别名]
def macd_rule(df):
try: df = MACD(df)
except: return False
input_1 = 0.2
input_2 = -0.8
input_3 = 22 * 3
index = -1
df['macd_dif_1'] = df['macd_dif'].shift(1)
df['macd_dea_1'] = df['macd_dea'].shift(1)
return (abs(df['macd_dea'][index]) < input_1) & \
(abs(df['macd_dif'][index]) < input_1) & \
(df['macd_dif'][-input_3:].min() < input_2) & \
(df['macd_dif'][index] > df['macd_dea'][index]) & \
((df['macd_dea_1'][index] > df['macd_dif_1'][index]) | (abs(df['macd_dea_1'][index] - df['macd_dif_1'][index]) < 0.007))
示例6: macd_rule_1
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MACD [as 别名]
def macd_rule_1(df):
try: df = MACD(df)
except: return False
input_1 = 0
input_2 = -0.8
input_3 = 0.05
dif_len = len(df['macd_dif'])
if dif_len < 2: return False
if abs(df['macd_dif'][-1]) > input_3:
return False
for idx in range(dif_len-1, 1, -1):
if ((df['macd_dif'][idx] - df['macd_dif'][idx-1]) > input_1):
continue
if df['macd_dif'][idx] <= input_2:
return True
else: return False
示例7: macd
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MACD [as 别名]
def macd(candles: np.ndarray, fastperiod=12, slowperiod=26, signalperiod=9, source_type="close",
sequential=False) -> MACD:
"""
MACD - Moving Average Convergence/Divergence
:param candles: np.ndarray
:param fastperiod: int - default: 12
:param slow_period: int - default: 26
:param signal_period: int - default: 9
:param source_type: str - default: "close"
:param sequential: bool - default: False
:return: MACD(macd, signal, hist)
"""
if not sequential and len(candles) > 240:
candles = candles[-240:]
source = get_candle_source(candles, source_type=source_type)
macd, macdsignal, macdhist = talib.MACD(source, fastperiod=fastperiod, slowperiod=slowperiod,
signalperiod=signalperiod)
if sequential:
return MACD(macd, macdsignal, macdhist)
else:
return MACD(macd[-1], macdsignal[-1], macdhist[-1])
示例8: isSell
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MACD [as 别名]
def isSell(context, analysis):
# Bearish SMA Crossover
if (getLast(analysis, 'sma_test') == 0):
# Bearish MACD
if (getLast(analysis, 'macd_test') == 0):
return True
# # Bearish Stochastics
# if(getLast(analysis, 'stoch_over_bought') == 0):
# return True
# # Bearish RSI
# if(getLast(analysis, 'rsi_over_bought') == 0):
# return True
return False
示例9: handle_bar
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MACD [as 别名]
def handle_bar(context, bar_dict):
# 开始编写你的主要的算法逻辑
# 获取历史收盘价序列,history_bars函数直接返回ndarray,方便之后的有关指标计算
prices = history_bars(context.s1, context.OBSERVATION, '1d', 'close')
# 用Talib计算MACD取值,得到三个时间序列数组,分别为macd,signal 和 hist
macd, signal, hist = talib.MACD(prices, context.SHORTPERIOD,
context.LONGPERIOD, context.SMOOTHPERIOD)
# macd 是长短均线的差值,signal是macd的均线,如果短均线从下往上突破长均线,为入场信号,进行买入开仓操作
if macd[-1] - signal[-1] > 0 and macd[-2] - signal[-2] < 0:
sell_qty = context.portfolio.positions[context.s1].sell_quantity
# 先判断当前卖方仓位,如果有,则进行平仓操作
if sell_qty > 0:
buy_close(context.s1, 1)
# 买入开仓
buy_open(context.s1, 1)
if macd[-1] - signal[-1] < 0 and macd[-2] - signal[-2] > 0:
buy_qty = context.portfolio.positions[context.s1].buy_quantity
# 先判断当前买方仓位,如果有,则进行平仓操作
if buy_qty > 0:
sell_close(context.s1, 1)
# 卖出开仓
sell_open(context.s1, 1)
示例10: handle_bar
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MACD [as 别名]
def handle_bar(context, bar_dict):
# 开始编写你的主要的算法逻辑
# 获取历史收盘价序列,history_bars函数直接返回ndarray,方便之后的有关指标计算
prices = history_bars(context.s1, context.OBSERVATION, '1d', 'close')
# 用Talib计算MACD取值,得到三个时间序列数组,分别为macd,signal 和 hist
macd, signal, hist = talib.MACD(prices, context.SHORTPERIOD, context.LONGPERIOD, context.SMOOTHPERIOD)
# macd 是长短均线的差值,signal是macd的均线,如果短均线从下往上突破长均线,为入场信号,进行买入开仓操作
if (macd[-1] - signal[-1] > 0 and macd[-2] - signal[-2] < 0):
sell_qty = context.portfolio.positions[context.s1].sell_quantity
# 先判断当前卖方仓位,如果有,则进行平仓操作
if (sell_qty > 0):
buy_close(context.s1, 1)
# 买入开仓
buy_open(context.s1, 1)
if (macd[-1] - signal[-1] < 0 and macd[-2] - signal[-2] > 0):
buy_qty = context.portfolio.positions[context.s1].buy_quantity
# 先判断当前买方仓位,如果有,则进行平仓操作
if (buy_qty > 0):
sell_close(context.s1, 1)
# 卖出开仓
sell_open(context.s1, 1)
示例11: get_indicator
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MACD [as 别名]
def get_indicator(df, indicator):
ret_df = df
if 'MACD' in indicator:
macd, macdsignal, macdhist = ta.MACD(df.close.values, fastperiod=12, slowperiod=26, signalperiod=9)
ret_df = KlineData._merge_dataframe(pd.DataFrame([macd, macdsignal, macdhist]).T.rename(columns={0: "macddif", 1: "macddem", 2: "macdhist"}), ret_df)
ret_df = KlineData._merge_dataframe(line_intersections(ret_df, columns=['macddif', 'macddem']), ret_df)
if 'MFI' in indicator:
real = ta.MFI(df.high.values, df.low.values, df.close.values, df.volume.values, timeperiod=14)
ret_df = KlineData._merge_dataframe(pd.DataFrame([real]).T.rename(columns={0: "mfi"}), ret_df)
if 'ATR' in indicator:
real = ta.NATR(df.high.values, df.low.values, df.close.values, timeperiod=14)
ret_df = KlineData._merge_dataframe(pd.DataFrame([real]).T.rename(columns={0: "atr"}), ret_df)
if 'ROCR' in indicator:
real = ta.ROCR(df.close.values, timeperiod=10)
ret_df = KlineData._merge_dataframe(pd.DataFrame([real]).T.rename(columns={0: "rocr"}), ret_df)
ret_df['date'] = pd.to_datetime(ret_df['date'], format='%Y-%m-%d')
return ret_df
示例12: annotate_data
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MACD [as 别名]
def annotate_data(self, feed, timeframe):
# Get SMAs
for k, v in six.iteritems(self.sma_intervals):
feed[k] = talib.SMA(feed['closeMid'].values, v)
# Get MACD
# NOTE: talib.MACD() returns (macd, signal, hist)
feed['macd'], _, feed['macd_hist'] = talib.MACD(
feed['closeMid'].values,
fastperiod=12,
slowperiod=26,
signalperiod=9
)
# Get RSI
feed['rsi'] = talib.RSI(feed['closeMid'].values)
return feed
示例13: _calc_macd_from_ta
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MACD [as 别名]
def _calc_macd_from_ta(price, fast_period=12, slow_period=26, signal_period=9):
"""
使用talib计算macd, 即透传talib.MACD计算结果
:param price: 收盘价格序列,pd.Series或者np.array
:param fast_period: 快的加权移动均线线, 默认12,即EMA12
:param slow_period: 慢的加权移动均线, 默认26,即EMA26
:param signal_period: dif的指数移动平均线,默认9
"""
import talib
if isinstance(price, pd.Series):
price = price.values
dif, dea, bar = talib.MACD(price,
fastperiod=fast_period,
slowperiod=slow_period,
signalperiod=signal_period)
return dif, dea, bar
示例14: MACD
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MACD [as 别名]
def MACD(self, name, bars: list):
"""
Return MACD for given time series. Bars list must be 26 bars
in length (last 26 bars for period).
MACD = EMA(12) - EMA(26)
Note we only use the MACD, not signal or histogram.
"""
self.check_bars_type(bars)
macd, signal, hist = ta.MACD(
bars['close'], fastperiod=12, slowperiod=26, signalperiod=9)
return macd
示例15: calculate_macd
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MACD [as 别名]
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])