talib.MACD

Here are the examples of the python api talib.MACD taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

3 Examples 7

Example 1

Project: rqalpha Source File: simple_macd.py
Function: handle_bar
def handle_bar(context, bar_dict):
    # 开始编写你的主要的算法逻辑

    # bar_dict[order_book_id] 可以拿到某个证券的bar信息
    # context.portfolio 可以拿到现在的投资组合状态信息

    # 使用order_shares(id_or_ins, amount)方法进行落单

    # TODO: 开始编写你的算法吧!


    #读取历史数据,使用sma方式计算均线准确度和数据长度无关,但是在使用ema方式计算均线时建议将历史数据窗口适当放大,结果会更加准确
    prices = history(context.OBSERVATION,'1d','close')[context.s1].values

    # 用Talib计算MACD取值,得到三个时间序列数组,分别为macd,signal 和 hist
    macd,signal,hist = talib.MACD(prices,context.SHORTPERIOD,context.LONGPERIOD,context.SMOOTHPERIOD)

    plot("macd",macd[-1])
    plot("macd signal",signal[-1])

    # macd 是长短均线的差值,signal是macd的均线,使用macd策略有几种不同的方法,我们这里采用macd线突破signal线的判断方法

    # 如果macd从上往下跌破macd_signal

    if macd[-1]-signal[-1]<0 and macd[-2]-signal[-2]>0:
        # 计算现在portfolio中股票的仓位
        curPosition = context.portfolio.positions[context.s1].quantity
        #进行清仓
        if curPosition>0:
            order_target_value(context.s1,0)


    # 如果短均线从下往上突破长均线,为入场信号
    if macd[-1]-signal[-1]>0 and macd[-2]-signal[-2]<0:
    #满仓入股
        order_target_percent(context.s1, 1)

Example 2

Project: tia Source File: talib_wrapper.py
Function: macd
def MACD(series, fast=12, slow=26, signal=9):
    return _series_to_frame(series, ['MACD', 'MACD_SIGNAL', 'MACD_HIST'], talib.MACD, fast, slow, signal)

Example 3

Project: strategy Source File: macd_stock.py
Function: on_bar
    def on_bar(self, bar):
        if self.cls_mode == gm.MD_MODE_PLAYBACK:           
            if bar.strtime[0:10] != self.cur_date[0:10]:
                self.cur_date = bar.strtime[0:10] + ' 08:00:00'
                #新的交易日
                self.init_data_newday()
                
        symbol = bar.exchange + '.'+ bar.sec_id
               
        self.movement_stop_profit_loss(bar)
        self.fixation_stop_profit_loss(bar)        

        #填充价格
        if self.dict_close.has_key( symbol ):
            self.dict_close[symbol][-1] = bar.close 
            
        pos = self.get_position(bar.exchange, bar.sec_id, OrderSide_Bid )
        
        if self.dict_close.has_key( symbol ):
            close = self.dict_close[symbol]
            dif, dea, macd= talib.MACD(close, 
                                fastperiod = self.short_term, 
                                slowperiod = self.long_term, 
                                signalperiod = self.macd_term)
            if pos is None and (dif[-1] > EPS and dea[-1] > EPS and dif[-1] > dif[-2] and dif[-1] > dea[-1]):
                cash = self.get_cash()
                cur_open_vol = self.open_vol
                if cash.available/bar.close > self.open_vol:
                    cur_open_vol = self.open_vol
                else :
                    cur_open_vol = int(cash.available/bar.close/100)*100
                
                if cur_open_vol == 0:
                    print 'no available cash to buy, available cash: %.2f'%cash.available
                else :            
                    self.dict_openlong_signal[symbol] += 1
                    if self.dict_openlong_signal[symbol] == self.openlong_signal :
                        self.open_long(bar.exchange, bar.sec_id, 0, self.open_vol)
                        pos = self.get_position( bar.exchange, bar.sec_id, OrderSide_Bid)
                        self.dict_openlong_signal[symbol] = 0
                        logging.info('open long, symbol:%s, time:%s, price:%.2f '%(symbol, bar.strtime, bar.close))
            elif pos is not None and (dif[-1] < EPS and dea[-1] < EPS and dif[-1] < dif[-2] and dif[-1] < dea[-1]):
                vol = pos.volume - pos.volume_today
                if vol > 0 :
                    self.close_long(bar.exchange, bar.sec_id, 0, vol)
                    logging.info('close long, symbol:%s, time:%s, price:%.2f '%(symbol, bar.strtime, bar.close))