本文整理汇总了Python中talib.MA属性的典型用法代码示例。如果您正苦于以下问题:Python talib.MA属性的具体用法?Python talib.MA怎么用?Python talib.MA使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类talib
的用法示例。
在下文中一共展示了talib.MA属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: reinit
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MA [as 别名]
def reinit(self,pingzhongname,filename,dailyresultname,jsonname):
"""更换品种,重新初始化"""
# K线界面
ui.loadKLineSetting(jsonname)
ui.KLtitle.setText(pingzhongname+' '+ui.StrategyName ,size='10pt',color='FFFF00')
ui.loadData(pd.DataFrame.from_csv(filename))
ui.loadData_listsig(pd.DataFrame.from_csv(dailyresultname))
# 初始化界面显示
self.SHORT_TERM_SHOW_ALL =False
self.SHORT_TERM_SHOW_FIRST =False
self.SHORT_TERM_SHOW_LIMIT =False
ui.initIndicator(u'MA SHORT')
ui.initIndicator(u'MA LONG')
ui.initIndicator(u'KLINE')
ui.initIndicator(u'SHORT TERM(First)')
ui.initIndicator(u'SHORT TERM(All)')
ui.initIndicator(u'SHORT TERM(Limit)')
if ui.signal_show == True :
ui.initIndicator(u'信号显示')
else:
ui.initIndicator(u'信号隐藏')
ui.refreshAll()
#----------------------------------------------------------------------
# 画图相关
#----------------------------------------------------------------------
示例2: handle_data
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MA [as 别名]
def handle_data(context):
if len(Close()) < p2:
return
# 使用talib计算均价
ma1 = talib.MA(Close(), p1)
ma2 = talib.MA(Close(), p2)
# 执行下单操作
if MarketPosition() <= 0 and ma1[-1] > ma2[-1]:
Buy(1, Close()[-1])
if MarketPosition() >= 0 and ma1[-1] < ma2[-1]:
SellShort(1, Close()[-1])
# 绘制指标图形
PlotNumeric("ma1", ma1[-1], RGB_Red())
PlotNumeric("ma2", ma2[-1], RGB_Green())
PlotNumeric("fit", NetProfit() + FloatProfit() - TradeCost(), RGB_Red(), False)
示例3: getAtrRatio
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MA [as 别名]
def getAtrRatio(df, period=14):
"""
平均波动率:ATR(14)/MA(14)
"""
highs = df['high']
lows = df['low']
closes = df['close']
atr = talib.ATR(highs, lows, closes, timeperiod=period)
ma = talib.MA(closes, timeperiod=period)
volatility = atr/ma
s = pd.Series(volatility, index=df.index, name='volatility').dropna()
return s
示例4: _getAtrExtreme
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MA [as 别名]
def _getAtrExtreme(cls, highs, lows, closes, atrPeriod=14, slowPeriod=30, fastPeriod=3):
"""
获取TTI ATR Exterme通道, which is based on 《Volatility-Based Technical Analysis》
TTI is 'Trading The Invisible'
@return: fasts, slows
"""
# talib 的源码,它的 ATR 不是 N 日简单平均,而是类似 EMA 的方法计算的指数平均
atr = talib.ATR(highs, lows, closes, timeperiod=atrPeriod)
highsMean = talib.EMA(highs, 5)
lowsMean = talib.EMA(lows, 5)
closesMean = talib.EMA(closes, 5)
atrExtremes = np.where(closes > closesMean,
((highs - highsMean)/closes * 100) * (atr/closes * 100),
((lows - lowsMean)/closes * 100) * (atr/closes * 100)
)
fasts = talib.MA(atrExtremes, fastPeriod)
slows = talib.EMA(atrExtremes, slowPeriod)
return fasts, slows, np.std(atrExtremes[-slowPeriod:])
示例5: ma30_cross
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MA [as 别名]
def ma30_cross(data):
MA5 = talib.MA(data.close, 5)
MA30 = talib.MA(data.close, 30)
MA30_CROSS_JX = CROSS(MA5, MA30)
MA30_CROSS_JX_Integral = Timeline_Integral_with_cross_before(MA30_CROSS_JX)
MA30_CROSS_SX = CROSS(MA30, MA5)
MA30_CROSS_SX_Integral = Timeline_Integral_with_cross_before(MA30_CROSS_SX)
MA30_CROSS = pd.DataFrame(columns=['MA30_CROSS', 'MA30_CROSS_JX', 'MA30_CROSS_SX', 'MA30_TP_CROSS_JX', 'MA30_TP_CROSS_SX'], index=data.index)
MA30_CROSS.loc[MA30_CROSS_JX == 1, 'MA30_CROSS'] = 1
MA30_CROSS.loc[MA30_CROSS_SX == 1, 'MA30_CROSS'] = -1
MA30_CROSS['MA30_CROSS_JX'] = Timeline_Integral_with_cross_before(MA30_CROSS_JX)
MA30_CROSS['MA30_CROSS_SX'] = Timeline_Integral_with_cross_before(MA30_CROSS_SX)
# MA30 前29个是 NaN,处理会抛出 Warning,使用 [29:] 则不会计算 NaN,相应的 return_index+29
MA30_tp_min, MA30_tp_max = signal.argrelextrema(MA30.values[29:], np.less)[0] + 29, signal.argrelextrema(MA30.values[29:], np.greater)[0] + 29
MA30_TP_CROSS = pd.DataFrame(columns=['MA30_TP_CROSS_JX', 'MA30_TP_CROSS_SX'], index=data.index)
MA30_TP_CROSS['MA30_TP_CROSS_SX'] = MA30_TP_CROSS['MA30_TP_CROSS_JX'] = 0
MA30_TP_CROSS.iloc[MA30_tp_min, MA30_TP_CROSS.columns.get_loc('MA30_TP_CROSS_JX')] = 1
MA30_TP_CROSS.iloc[MA30_tp_max, MA30_TP_CROSS.columns.get_loc('MA30_TP_CROSS_SX')] = 1
MA30_CROSS['MA30_TP_CROSS_JX'] = Timeline_Integral_with_cross_before(MA30_TP_CROSS['MA30_TP_CROSS_JX'])
MA30_CROSS['MA30_TP_CROSS_SX'] = Timeline_Integral_with_cross_before(MA30_TP_CROSS['MA30_TP_CROSS_SX'])
return MA30_CROSS
示例6: zscore
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MA [as 别名]
def zscore(candles: np.ndarray, period=14, matype=0, nbdev=1, source_type="close", sequential=False) -> Union[
float, np.ndarray]:
"""
zScore
:param candles: np.ndarray
:param period: int - default: 14
:param matype: int - default: 0
:param nbdev: int - default: 1
:param source_type: str - default: "close"
:param sequential: bool - default=False
:return: float | np.ndarray
"""
if not sequential and len(candles) > 240:
candles = candles[-240:]
source = get_candle_source(candles, source_type=source_type)
means = talib.MA(source, timeperiod=period, matype=matype)
sigmas = talib.STDDEV(source, timeperiod=period, nbdev=nbdev)
zScores = (source - means) / sigmas
return zScores if sequential else zScores[-1]
示例7: handle_data
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MA [as 别名]
def handle_data(context):
if len(Close()) < p2:
return;
ma1 = talib.MA(Close(), p1)
ma2 = talib.MA(Close(), p2)
his = BarStatus() != 2
if his:
his_trigger(ma1, ma2)
else:
tim_trigger(ma1, ma2)
PlotNumeric("ma1", ma1[-1], 0xFF0000)
PlotNumeric("ma2", ma2[-1], 0x00aa00)
fit = NetProfit() + FloatProfit() - TradeCost() if his else A_CoverProfit() + A_ProfitLoss() - A_Cost()
PlotNumeric("fit", fit, 0x0000FF, False)
示例8: _calc_ma_from_ta
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MA [as 别名]
def _calc_ma_from_ta(prices, time_period=10, from_calc=EMACalcType.E_MA_MA):
"""
使用talib计算ma,即透传talib.MA or talib.EMA计算结果
:param prices: 收盘价格序列,pd.Series或者np.array
:param time_period: 移动平均的N值,int
:param from_calc: EMACalcType enum对象,移动移动平均使用的方法
"""
import talib
if isinstance(prices, pd.Series):
prices = prices.values
if from_calc == EMACalcType.E_MA_MA:
ma = talib.MA(prices, timeperiod=time_period)
else:
ma = talib.EMA(prices, timeperiod=time_period)
return ma
示例9: CCI
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MA [as 别名]
def CCI(self, period: int, bars: list):
"""
Return CCI (Commodity Chanel Index) for n bars close price.
CCI = (Typical Price − MA) / 0.015 * Mean Deviation
where:
Typical Price = ∑P((H + L + C) / 3))
P = number of bars (period)
MA = Moving Average = (∑P Typical Price) / P
Mean Deviation=(∑P | Typical Price - MA |) / P
"""
self.check_bars_type(bars)
cci = ta.CCI(
bars['high'], bars['low'], bars['close'], timeperiod=period)
return cci
示例10: getRuntimeMa
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MA [as 别名]
def getRuntimeMa(self, ma_num):
"""
实时计算MA得值
:param ma_num:第几条均线, 1,对应inputMa1Len,,,,
:return:
"""
if ma_num not in [1, 2, 3]:
return None
ma_len = 1
if ma_num == 1 and self.inputMa1Len > 0:
ma_len = self.inputMa1Len
elif ma_num == 2 and self.inputMa2Len > 0:
ma_len = self.inputMa2Len
elif ma_num == 3 and self.inputMa3Len > 0:
ma_len = self.inputMa3Len
else:
return None
ma_data_len = ma_len + 2
if len(self.lineBar) < ma_data_len:
self.debugCtaLog(u'数据未充分,当前Bar数据数量:{},计算实时MA {} 需要:{}'.
format(len(self.lineBar), ma_len, ma_data_len))
return None
# 3、获取前InputN周期(不包含当前周期)的K线
listClose = [x.close for x in self.lineBar[-ma_data_len:]]
barMa = ta.MA(np.array(listClose, dtype=float), ma_len)[-1]
barMa = round(float(barMa), self.round_n)
return barMa
#----------------------------------------------------------------------
示例11: onBarRatio
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MA [as 别名]
def onBarRatio(self, bar):
"""比率线的OnBar事件"""
# 获取比率线的长度
l = len(self.lineRatio.lineStateMean)
if l > 0:
ma = self.lineRatio.lineStateMean[-1]
else:
ma = bar.close
if l > 6:
listKf = [x for x in self.lineRatio.lineStateMean[-7:-1]]
# MA():?
# numpy.array(listKf, dtype = float:转浮点型
malist = ta.MA(numpy.array(listKf, dtype=float), 5)
ma5 = malist[-1]
ma5_ref1 = malist[-2]
if ma5 <= 0 or ma5_ref1 <= 0:
self.writeCtaLog(u'卡尔曼均线异常')
return
# ?
self.m1_atan = math.atan((ma5 / ma5_ref1 - 1) * 100 * 180 / math.pi)
# M1的切线队列长度 > 10
if len(self.m1_atan_list) > 10:
del self.m1_atan_list[0]
# m1_atan_list加入m1_atan
self.m1_atan_list.append(self.m1_atan)
# displayLastBar()显示最后一个Bar的信息
self.writeCtaLog(self.lineRatio.displayLastBar())
# 残差线更新
示例12: ifup20_TA
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MA [as 别名]
def ifup20_TA(data):
# TA-lib计算
return (talib.MA(data.close, 5) - talib.MA(data.close, 20)).dropna() > 0
# 写个自定义指标 MAX_FACTOR TA-lib计算
示例13: boll_cross
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MA [as 别名]
def boll_cross(data):
BBANDS = QA.TA_BBANDS(data.close, timeperiod=20, nbdevup=2)
BOLL_CROSS = pd.DataFrame(columns=['min_peak', 'max_peak', 'BOLL_CROSS', 'BOLL_CROSS_JX', 'BOLL_CROSS_SX'], index=data.index)
# 防止插针行情突然搞乱
data['smooth_low'] = talib.MA(data.low, 2)
data['smooth_high'] = talib.MA(data.high, 2)
BOLL_CROSS['min_peak'] = data.apply(lambda x: min(x['open'], x['close'], x['smooth_low']), axis=1)
BOLL_CROSS['max_peak'] = data.apply(lambda x: max(x['open'], x['close'], x['smooth_high']), axis=1)
BOLL_CROSS_JX = CROSS(BOLL_CROSS['min_peak'], BBANDS[:,2])
BOLL_CROSS_SX = CROSS(BBANDS[:,0], BOLL_CROSS['max_peak'])
BOLL_CROSS.loc[BOLL_CROSS_JX == 1, 'BOLL_CROSS'] = 1
BOLL_CROSS.loc[BOLL_CROSS_SX == 1, 'BOLL_CROSS'] = -1
BOLL_TP_CROSS = pd.DataFrame(columns=['BOLL_TP_CROSS_JX', 'BOLL_TP_CROSS_SX'], index=data.index)
BOLL_TP_CROSS['BOLL_TP_CROSS_SX'] = BOLL_TP_CROSS['BOLL_TP_CROSS_JX'] = 0
BOLL_TP_CROSS.loc[BOLL_CROSS_JX == 1, 'BOLL_TP_CROSS_JX'] = 1
BOLL_TP_CROSS.loc[BOLL_CROSS_SX == 1, 'BOLL_TP_CROSS_SX'] = 1
BOLL_CROSS = BOLL_CROSS.assign(BOLL_UB=BBANDS[:,0])
BOLL_CROSS = BOLL_CROSS.assign(BOLL_MA=BBANDS[:,1])
BOLL_CROSS = BOLL_CROSS.assign(BOLL_LB=BBANDS[:,2])
BOLL_CROSS['BOLL_CROSS_JX'] = QA.Timeline_Integral_with_cross_before(BOLL_TP_CROSS['BOLL_TP_CROSS_JX'])
BOLL_CROSS['BOLL_CROSS_SX'] = QA.Timeline_Integral_with_cross_before(BOLL_TP_CROSS['BOLL_TP_CROSS_SX'])
return BOLL_CROSS
示例14: boll_cross_lf
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MA [as 别名]
def boll_cross_lf(data):
BBANDS = QA.TA_BBANDS(data.close, timeperiod=20, nbdevup=2)
BOLL_CROSS_JX1 = CROSS(data.open, BBANDS[:,2])
BOLL_CROSS_JX2 = CROSS(data.close, BBANDS[:,2])
BOLL_CROSS_JX3 = CROSS(talib.MA(data.low, 2), BBANDS[:,2])
BOLL_CROSS_SX1 = CROSS(BBANDS[:,0], data.open)
BOLL_CROSS_SX2 = CROSS(BBANDS[:,0], data.close)
BOLL_CROSS_SX3 = CROSS(BBANDS[:,0], talib.MA(data.high, 2))
BOLL_CROSS = pd.DataFrame(columns=['BOLL_CROSS', 'BOLL_CROSS_JX', 'BOLL_CROSS_SX'], index=data.index)
BOLL_CROSS.loc[BOLL_CROSS_JX1 == 1, 'BOLL_CROSS'] = 1
BOLL_CROSS.loc[BOLL_CROSS_JX2 == 1, 'BOLL_CROSS'] = 1
BOLL_CROSS.loc[BOLL_CROSS_JX3 == 1, 'BOLL_CROSS'] = 1
BOLL_CROSS.loc[BOLL_CROSS_SX1 == 1, 'BOLL_CROSS'] = -1
BOLL_CROSS.loc[BOLL_CROSS_SX2 == 1, 'BOLL_CROSS'] = -1
BOLL_CROSS.loc[BOLL_CROSS_SX3 == 1, 'BOLL_CROSS'] = -1
BOLL_TP_CROSS = pd.DataFrame(columns=['BOLL_TP_CROSS_JX', 'BOLL_TP_CROSS_SX'], index=data.index)
BOLL_TP_CROSS['BOLL_TP_CROSS_SX'] = BOLL_TP_CROSS['BOLL_TP_CROSS_JX'] = 0
BOLL_TP_CROSS.loc[(BOLL_CROSS_JX1 | BOLL_CROSS_JX2 | BOLL_CROSS_JX3) == 1, 'BOLL_TP_CROSS_JX'] = 1
BOLL_TP_CROSS.loc[(BOLL_CROSS_SX1 | BOLL_CROSS_SX2 | BOLL_CROSS_SX3) == 1, 'BOLL_TP_CROSS_SX'] = 1
BOLL_CROSS = BOLL_CROSS.assign(BOLL_UB=BBANDS[:,0])
BOLL_CROSS = BOLL_CROSS.assign(BOLL_MA=BBANDS[:,1])
BOLL_CROSS = BOLL_CROSS.assign(BOLL_LB=BBANDS[:,2])
BOLL_CROSS['BOLL_CROSS_JX'] = QA.Timeline_Integral_with_cross_before(BOLL_TP_CROSS['BOLL_TP_CROSS_JX'])
BOLL_CROSS['BOLL_CROSS_SX'] = QA.Timeline_Integral_with_cross_before(BOLL_TP_CROSS['BOLL_TP_CROSS_SX'])
return BOLL_CROSS
示例15: QSDD
# 需要导入模块: import talib [as 别名]
# 或者: from talib import MA [as 别名]
def QSDD(dataframe, SHORT=12, LONG=26, M=9):
"""
1.line_mid向上突破line_long,买入信号参考。
2.line_mid向下跌破line_long,卖出信号参考。
"""
OPEN = dataframe.open
HIGH = dataframe.high
LOW = dataframe.low
CLOSE = dataframe.close
# QSDD策略
# A = talib.MA(-100 * (talib.MAX(HIGH, 34) - CLOSE) / (talib.MAX(HIGH, 34) - talib.MIN(LOW, 34)), 19)
# B = -100 * (talib.MAX(HIGH, 14) - CLOSE) / (talib.MAX(HIGH, 14) - talib.MIN(LOW, 14))
# D = talib.EMA(-100 * (talib.MAX(HIGH, 34) - CLOSE) / (talib.MAX(HIGH, 34) - talib.MIN(LOW, 34)), 4)
A = QA.MA(-100 * (QA.HHV(HIGH, 34) - CLOSE) /
(QA.HHV(HIGH, 34) - QA.LLV(LOW, 34)), 19)
B = -100 * (QA.HHV(HIGH, 14) - CLOSE) / \
(QA.HHV(HIGH, 14) - QA.LLV(LOW, 14))
D = QA.EMA(-100 * (QA.HHV(HIGH, 34) - CLOSE) /
(QA.HHV(HIGH, 34) - QA.LLV(LOW, 34)), 4)
line_long = A + 100
line_short = B + 100
line_mid = D + 100 # 信号线
CROSS_JC = QA.CROSS(line_mid, line_long)
CROSS_SC = QA.CROSS(line_long, line_mid)
return pd.DataFrame({'line_mid': line_mid, 'line_long': line_long, 'CROSS_JC': CROSS_JC, 'CROSS_SC': CROSS_SC})
# create account