本文整理匯總了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])