python类MA的实例源码

factor.py 文件源码 项目:quant 作者: yutiansut 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def MA(security_list, timeperiod=5):
    # ????????????
    if isinstance(security_list, str):
        security_list = [security_list]
    # ?? MA
    security_data = history(timeperiod * 2, '1d', 'close',
                            security_list, df=False, skip_paused=True)
    ma = {}
    for stock in security_list:
        ma[stock] = talib.MA(security_data[stock], timeperiod)
    return ma

# SMA
factor.py 文件源码 项目:quant 作者: yutiansut 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def MA_MONEY(security_list, timeperiod=5):
    # ????????????
    if isinstance(security_list, str):
        security_list = [security_list]
    # ?? N ??????
    security_data = history(timeperiod * 2, '1d', 'money',
                            security_list, df=False, skip_paused=True)
    mamoney = {}
    for stock in security_list:
        x = security_data[stock]
        mamoney[stock] = talib.MA(security_data[stock], timeperiod)
    return mamoney

# ?????
factor.py 文件源码 项目:quant 作者: yutiansut 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def MA_VOLUME(security_list, timeperiod=5):
    # ????????????
    if isinstance(security_list, str):
        security_list = [security_list]
    # ?? N ??????
    security_data = history(timeperiod * 2, '1d', 'volume',
                            security_list, df=False, skip_paused=True)
    mavol = {}
    for stock in security_list:
        x = security_data[stock]
        mavol[stock] = talib.MA(security_data[stock], timeperiod)
    return mavol

# BIAS
core.py 文件源码 项目:smart_money 作者: lpisces 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
def ma(close, p = 30):
  return talib.MA(close, p)
indicator.py 文件源码 项目:StockPredictor 作者: wallsbreaker 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def calculate_indicator(stock_df):
    periods = [3, 5, 10, 20, 30, 60]
    # MA
    for period in periods:
        stock_df['MA' + str(period)] = talib.MA(stock_df['close'].values, timeperiod=period)

    # EMA
    periods = [3, 5, 10, 20, 30, 60]
    for period in periods:
        stock_df['EMA' + str(period)] = talib.EMA(stock_df['close'].values, timeperiod=period)

    # AMTMA
    periods = [5, 10, 20]
    for period in periods:
        stock_df['AMTMA' + str(period)] = talib.MA(stock_df['amount'].values, timeperiod=period)

    # ATR
    periods = [5, 10, 20]
    for period in periods:
        stock_df['ATR' + str(period)] = talib.ATR(stock_df['high'].values, stock_df['low'].values,
                                                  stock_df['close'].values, timeperiod=period)

    # ADX
    period = 14
    stock_df['ADX' + str(period)] = talib.ADX(stock_df['high'].values, stock_df['low'].values,
                                              stock_df['close'].values, timeperiod=period)

    # MACD
    stock_df['MACD_DIFF'], stock_df['MACD_DEA'], stock_df['MACD_HIST'] = talib.MACD(
        stock_df['close'].values, fastperiod=12, slowperiod=26, signalperiod=9)

    # CCI
    period = 14
    stock_df['CCI' + str(period)] = talib.CCI(stock_df['high'].values, stock_df['low'].values,
                                              stock_df['close'].values, timeperiod=period)

    # MFI
    period = 14
    stock_df['MFI' + str(period)] = talib.MFI(stock_df['high'].values, stock_df['low'].values,
                                              stock_df['close'].values, stock_df['volume'].values,
                                              timeperiod=period)

    # ROCP
    periods = [5, 10, 20]
    for period in periods:
        stock_df['ROCP' + str(period)] = talib.ROCP(stock_df['close'].values, timeperiod=period)
make_features.py 文件源码 项目:StockPredictor 作者: wallsbreaker 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def calculate_indicator(stock_df):
    periods = [3, 5, 10, 20, 30, 60]
    # MA
    for period in periods:
        stock_df['MA' + str(period)] = talib.MA(stock_df['close'].values, timeperiod=period)

    # EMA
    periods = [3, 5, 10, 20, 30, 60]
    for period in periods:
        stock_df['EMA' + str(period)] = talib.EMA(stock_df['close'].values, timeperiod=period)

    # AMTMA
    periods = [5, 10, 20]
    for period in periods:
        stock_df['AMTMA' + str(period)] = talib.MA(stock_df['amount'].values, timeperiod=period)

    # ATR
    periods = [5, 10, 20]
    for period in periods:
        stock_df['ATR' + str(period)] = talib.ATR(stock_df['high'].values, stock_df['low'].values,
                                                  stock_df['close'].values, timeperiod=period)

    # ADX
    period = 14
    stock_df['ADX' + str(period)] = talib.ADX(stock_df['high'].values, stock_df['low'].values,
                                              stock_df['close'].values, timeperiod=period)

    # MACD
    stock_df['MACD_DIFF'], stock_df['MACD_DEA'], stock_df['MACD_HIST'] = talib.MACD(
        stock_df['close'].values, fastperiod=12, slowperiod=26, signalperiod=9)

    # CCI
    period = 14
    stock_df['CCI' + str(period)] = talib.CCI(stock_df['high'].values, stock_df['low'].values,
                                              stock_df['close'].values, timeperiod=period)

    # MFI
    period = 14
    stock_df['MFI' + str(period)] = talib.MFI(stock_df['high'].values, stock_df['low'].values,
                                              stock_df['close'].values, stock_df['volume'].values,
                                              timeperiod=period)

    # ROCP
    periods = [5, 10, 20]
    for period in periods:
        stock_df['ROCP' + str(period)] = talib.ROCP(stock_df['close'].values, timeperiod=period)
buy_or_sale.py 文件源码 项目:DataAnalysis 作者: IMYin 项目源码 文件源码 阅读 14 收藏 0 点赞 0 评论 0
def get_kdj(sorted_data):
    close,high,low,ma5,ma10,ma20 = get_case_data(sorted_data)
    slowk, slowd = ta.STOCH(high,low,close, fastk_period=9, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0)
    slowkMA5 = ta.MA(slowk, timeperiod=5, matype=0)
    slowkMA10 = ta.MA(slowk, timeperiod=10, matype=0)
    slowkMA20 = ta.MA(slowk, timeperiod=20, matype=0)
    slowdMA5 = ta.MA(slowd, timeperiod=5, matype=0)
    slowdMA10 = ta.MA(slowd, timeperiod=10, matype=0)
    slowdMA20 = ta.MA(slowd, timeperiod=20, matype=0)
    operator = ''

    #1.K???????——???90?????????10??????D??80???????????D??20???????????
    if slowk[-1] >= 90:
        operator += 'S9'
    elif slowk[-1] <= 10:
        operator += 'B1'
    elif slowd[-1] >=80:
        operator += 'S8'
    elif slowd[-1] <= 20:
        operator += 'B2'

     #2.??????K???D??K?????D????????
    if slowk[-1] > slowd[-1] and slowk[-2] <= slowd[-2]:
        operator += 'BX'
    elif slowk[-1] < slowd[-1] and slowk[-2] >= slowd[-2]:
        operator += 'SX'

     #3.???????????????????????
    if ma5[-1] >= ma10[-1] and ma10[-1] >= ma20[-1]:  #k???
        if (slowkMA5[-1] <= slowkMA10[-1] and slowkMA10[-1] <= slowkMA20[-1]) or (slowdMA5[-1] <= slowdMA10[-1] and slowdMA10[-1] <= slowdMA20[-1]):
            operator += 'S><'
    elif ma5[-1] <= ma10[-1] and ma10[-1] <= ma20[-1]:  #k???
        if (slowkMA5[-1] >= slowkMA10[-1] and slowkMA10[-1] >= slowkMA20[-1]) or (slowdMA5[-1] >= slowdMA10[-1] and slowdMA10[-1] >= slowdMA20[-1]):
            operator += 'B><'
    return operator
second_buy_point.py 文件源码 项目:scheduler_frame 作者: f304646673 项目源码 文件源码 阅读 14 收藏 0 点赞 0 评论 0
def _get_ma_data(self, ori_data, periods):
        ret_data = {}
        float_data = [float(x) for x in ori_data]
        for period in periods:
            data = talib.MA(numpy.array(float_data), timeperiod = period)
            data_list = data.tolist()
            data_list = self._filter_data(data_list)
            ret_data["%d" % period] = data_list
        return ret_data
update_stock_daily_average_info.py 文件源码 项目:scheduler_frame 作者: f304646673 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def _get_ma_data(self, ori_data, periods):
        ret_data = {}
        float_data = [float(x) for x in ori_data]
        for period in periods:
            data = talib.MA(numpy.array(float_data), timeperiod = period)
            data_list = data.tolist()
            data_list = self._filter_data(data_list)
            ret_data["%d" % period] = data_list
        return ret_data
taLib_demo.py 文件源码 项目:base_function 作者: Rockyzsu 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def overlap_process(event):
    print(event.widget.get())
    overlap = event.widget.get()

    upperband, middleband, lowerband = ta.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)
    fig, axes = plt.subplots(2, 1, sharex=True)
    ax1, ax2 = axes[0], axes[1]
    axes[0].plot(close, 'rd-', markersize=3)
    axes[0].plot(upperband, 'y-')
    axes[0].plot(middleband, 'b-')
    axes[0].plot(lowerband, 'y-')
    axes[0].set_title(overlap, fontproperties="SimHei")

    if overlap == u'???':
        pass
    elif overlap == u'????????':
        real = ta.DEMA(close, timeperiod=30)
        axes[1].plot(real, 'r-')
    elif overlap == u'??????? ':
        real = ta.EMA(close, timeperiod=30)
        axes[1].plot(real, 'r-')
    elif overlap == u'??????——?????':
        real = ta.HT_TRENDLINE(close)
        axes[1].plot(real, 'r-')
    elif overlap == u'???????????':
        real = ta.KAMA(close, timeperiod=30)
        axes[1].plot(real, 'r-')
    elif overlap == u'?????':
        real = ta.MA(close, timeperiod=30, matype=0)
        axes[1].plot(real, 'r-')
    elif overlap == u'MESA???????':
        mama, fama = ta.MAMA(close, fastlimit=0, slowlimit=0)
        axes[1].plot(mama, 'r-')
        axes[1].plot(fama, 'g-')
    elif overlap == u'????????':
        real = ta.MAVP(close, periods, minperiod=2, maxperiod=30, matype=0)
        axes[1].plot(real, 'r-')
    elif overlap == u'???????':
        real = ta.SMA(close, timeperiod=30)
        axes[1].plot(real, 'r-')
    elif overlap == u'????????(T3)':
        real = ta.T3(close, timeperiod=5, vfactor=0)
        axes[1].plot(real, 'r-')
    elif overlap == u'????????':
        real = ta.TEMA(close, timeperiod=30)
        axes[1].plot(real, 'r-')
    elif overlap == u'?????? ':
        real = ta.TRIMA(close, timeperiod=30)
        axes[1].plot(real, 'r-')
    elif overlap == u'???????':
        real = ta.WMA(close, timeperiod=30)
        axes[1].plot(real, 'r-')
    plt.show()

# ????
strategyKingKeltner.py 文件源码 项目:future_data_analyse 作者: ipqhjjybj 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def onFiveBar(self, bar):
        """??5??K?"""
        # ?????????????????????????
        for orderID in self.orderList:
            self.cancelOrder(orderID)
        self.orderList = []

        # ??K???
        self.closeArray[0:self.bufferSize-1] = self.closeArray[1:self.bufferSize]
        self.highArray[0:self.bufferSize-1] = self.highArray[1:self.bufferSize]
        self.lowArray[0:self.bufferSize-1] = self.lowArray[1:self.bufferSize]

        self.closeArray[-1] = bar.close
        self.highArray[-1] = bar.high
        self.lowArray[-1] = bar.low

        self.bufferCount += 1
        if self.bufferCount < self.bufferSize:
            return

        # ??????
        self.atrValue = talib.ATR(self.highArray, 
                                  self.lowArray, 
                                  self.closeArray,
                                  self.kkLength)[-1]
        self.kkMid = talib.MA(self.closeArray, self.kkLength)[-1]
        self.kkUp = self.kkMid + self.atrValue * self.kkDev
        self.kkDown = self.kkMid - self.atrValue * self.kkDev

        # ?????????

        # ????????OCO????
        if self.pos == 0:
            self.intraTradeHigh = bar.high
            self.intraTradeLow = bar.low            
            self.sendOcoOrder(self.kkUp, self.kkDown, self.fixedSize)

        # ??????
        elif self.pos > 0:
            self.intraTradeHigh = max(self.intraTradeHigh, bar.high)
            self.intraTradeLow = bar.low

            orderID = self.sell(self.intraTradeHigh*(1-self.trailingPrcnt/100), 
                                abs(self.pos), True)
            self.orderList.append(orderID)

        # ??????
        elif self.pos < 0:
            self.intraTradeHigh = bar.high
            self.intraTradeLow = min(self.intraTradeLow, bar.low)

            orderID = self.cover(self.intraTradeLow*(1+self.trailingPrcnt/100),
                               abs(self.pos), True)
            self.orderList.append(orderID)

        # ????????
        self.putEvent()        

    #----------------------------------------------------------------------
m_db.py 文件源码 项目:Stock 作者: liuguoyaolgy 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def pre_data(self,stick_code, ktype='D', beginday='',endday=''):
        # ktype in ('D','W','M')
        # today='2010-01-01'
        if '' == beginday:
            begindaytmp = datetime.date.today() - datetime.timedelta(days=13)
            beginday = begindaytmp.strftime('%Y-%m-%d')
        if '' == endday:
            endday = datetime.date.today().strftime('%Y-%m-%d')

        df =''
        print(beginday,endday)
        try:
            if ktype == 'D':
                df = self.get_data(
                    "select * from t_stick_data_d \
                    where code = '" + stick_code + "'  and date > '"+beginday+"' \
                    and date <='" + endday + "' order by date asc;")  # and date>'2015-05-01'
            elif ktype == 'W':
                df = self.get_data(
                    "select * from t_stick_data_w \
                    where code = '" + stick_code + "'  and date > '"+beginday+"' \
                    and date <='" + endday + "' order by date asc;")  # and date>'2015-05-01'
            elif ktype == 'M':
                df = self.get_data(
                    "select * from t_stick_data_m \
                    where code = '" + stick_code + "'  and date > '"+beginday+"' \
                    and date <='" + endday + "' order by date asc;")  # and date>'2015-05-01'

        except Exception as e:
            # print('ERR:',e)
            return
        df['cci'] = ta.CCI(df['high'].values.astype('double'), df['low'].values.astype('double'),
                           df['close'].values.astype('double'))
        df['diff'], df['dea'], df['macd'] = ta.MACD(df['close'].values.astype('double'), fastperiod=12, slowperiod=26,
                                                    signalperiod=9)
        df['obv'] = ta.OBV(df['close'].values.astype('double'), df['vol'].values.astype('double'))
        df['volma5'] = ta.MA(df['vol'].values.astype('double'), 5);
        df['volma20'] = ta.MA(df['vol'].values.astype('double'), 20);
        df['MA20'] = ta.MA(df['close'].values.astype('double'), 20)
        df['MA60'] = ta.MA(df['close'].values.astype('double'), 60)
        df['cwbili'] = 0
        df['pricebili'] = 0
        return df
# xx=m_db2();
# df=xx.pre_data('000157',ktype='W')
# print(df)
# xx.insert_data('t_stick_data_m_test',df.head(20).as_matrix())
# xx.commit()
ta.py 文件源码 项目:dash-technical-charting 作者: plotly 项目源码 文件源码 阅读 14 收藏 0 点赞 0 评论 0
def add_MACDEXT(self, fastperiod=12, fastmatype=0,
                slowperiod=26, slowmatype=0,
                signalperiod=9, signalmatype=0,
                types=['line', 'line', 'histogram'],
                colors=['primary', 'tertiary', 'fill'],
                **kwargs):
    """Moving Average Convergence Divergence with Controllable MA Type.

    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 = 'MACDEXT({},{},{})'.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.MACDEXT(self.df[self.cl].values,
                                      fastperiod, fastmatype,
                                      slowperiod, slowmatype,
                                      signalperiod, signalmatype)
ta.py 文件源码 项目:dash-technical-charting 作者: plotly 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def add_STOCHRSI(self, timeperiod=14,
                 fastk_period=5, fastd_period=3, fastd_matype=0,
                 types=['line', 'line'],
                 colors=['primary', 'tertiary'],
                 **kwargs):
    """Stochastic Relative Strength Index.

    Note that the first argument of types and colors refers to StochRSI %K
    while second argument refers to StochRSI %D
    (signal line of %K obtained by MA).

    """
    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']] * 2
    if 'color' in kwargs:
        colors = [kwargs['color']] * 2

    name = 'STOCHRSI({},{},{})'.format(str(timeperiod),
                                       str(fastk_period),
                                       str(fastd_period))
    fastk = name + r'[%k]'
    fastd = name + r'[%d]'
    self.sec[fastk] = dict(type=types[0], color=colors[0])
    self.sec[fastd] = dict(type=types[1], color=colors[1], on=fastk)
    self.ind[fastk], self.ind[fastd] = talib.STOCHRSI(self.df[self.cl].values,
                                                      timeperiod,
                                                      fastk_period,
                                                      fastd_period,
                                                      fastd_matype)
buy_or_sale.py 文件源码 项目:DataAnalysis 作者: IMYin 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def get_macd(sorted_data):
    close,high,low,ma5,ma10,ma20 = get_case_data(sorted_data)
    macd, macdsignal, macdhist = ta.MACD(close, fastperiod=10, slowperiod=22, signalperiod=9)
#    macd, macdsignal, macdhist = ta.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9)
    SignalMA5 = ta.MA(macdsignal, timeperiod=5, matype=0)
    SignalMA10 = ta.MA(macdsignal, timeperiod=10, matype=0)
    SignalMA20 = ta.MA(macdsignal, timeperiod=20, matype=0)
    #2??? 1.DIFF?DEA????DIFF????DEA?????? 2.DIFF?DEA????DIFF????DEA??????
    operator = ''
    if macd[-1] > 0 and macdsignal[-1] > 0:
        if macd[-1] > macdsignal[-1] and macd[-2] <= macdsignal[-2]:
                operator += 'BX'  #??
    elif macd[-1] < 0 and macdsignal[-1] < 0:
        if macd[-1] <= macdsignal[-2]:
                operator += 'SX'
    #DEA??k????????????
    if ma5[-1] >= ma10[-1] and ma10[-1] >= ma20[-1]:  #k???
        if SignalMA5[-1] <= SignalMA10[-1] and SignalMA10[-1] <= SignalMA20[-1]:  #DEA??
            operator += 'S><'
    if ma5[-1] <= ma10[-1] and ma10[-1] <= ma20[-1]:  #k???
        if SignalMA5[-1] >= SignalMA10[-1] and SignalMA10[-1] >= SignalMA20[-1]:  #DEA??
            operator += 'B><'
    if macd[-1] > 0 and macdhist[-1] >0:
        if macd[-1] > macd[-2] and macdhist[-1] > macdhist[-2]:
            operator += 'B^'
    elif macd[-1] < 0 and macdhist[-1] < 0:
        if macd[-1] < macd[-2] and macdhist[-1] > macdhist[-2]:
            operator += 'S^'
    #??MACD??????????????
    if macdhist[-1] > 0:
        for i in range(1,7):
            if macdhist[-2] <= 0:
                operator += 'Bh'
                break
    if macdhist[-1] < 0:
        for i in range(1,7):
            if macdhist[-2] >= 0:
                operator += 'Sh'
                break
    return (operator)

#??KDJ?????????
saleStock.py 文件源码 项目:DataAnalysis 作者: IMYin 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def Get_MACD(df):
    #??12,26,9
    macd, macdsignal, macdhist = ta.MACD(np.array(df['close']), fastperiod=12, slowperiod=26, signalperiod=9)

    SignalMA5 = ta.MA(macdsignal, timeperiod=5, matype=0)
    SignalMA10 = ta.MA(macdsignal, timeperiod=10, matype=0)
    SignalMA20 = ta.MA(macdsignal, timeperiod=20, matype=0)
    #13-15 DIFF  DEA  DIFF-DEA       
    df['macd']=pd.Series(macd,index=df.index) #DIFF
    df['macdsignal']=pd.Series(macdsignal,index=df.index)#DEA
    df['macdhist']=pd.Series(macdhist,index=df.index)#DIFF-DEA
    dflen = df.shape[0]
    MAlen = len(SignalMA5)
    operate = 0
    #2??? 1.DIFF?DEA????DIFF????DEA?????? 2.DIFF?DEA????DIFF????DEA??????
    #???
    if df.iat[(dflen-1),13]>0:
        if df.iat[(dflen-1),14]>0:
            if df.iat[(dflen-1),13]>df.iat[(dflen-1),14] and df.iat[(dflen-2),13]<=df.iat[(dflen-2),14]:
                operate = operate + 10#??
    else:
        if df.iat[(dflen-1),14]<0:
            if df.iat[(dflen-1),13]<=df.iat[(dflen-2),14]:
                operate = operate - 10#??

    #3.DEA??K?????????????
    if df.iat[(dflen-1),7]>=df.iat[(dflen-1),8] and df.iat[(dflen-1),8]>=df.iat[(dflen-1),9]:#K???
        if SignalMA5[MAlen-1]<=SignalMA10[MAlen-1] and SignalMA10[MAlen-1]<=SignalMA20[MAlen-1]: #DEA??
            operate = operate - 1
    elif df.iat[(dflen-1),7]<=df.iat[(dflen-1),8] and df.iat[(dflen-1),8]<=df.iat[(dflen-1),9]:#K???
        if SignalMA5[MAlen-1]>=SignalMA10[MAlen-1] and SignalMA10[MAlen-1]>=SignalMA20[MAlen-1]: #DEA??
            operate = operate + 1


    #4.??MACD??????????????
    if df.iat[(dflen-1),15]>0 and dflen >30 :
        for i in range(1,26):
            if df.iat[(dflen-1-i),15]<=0:#
                operate = operate + 5
                break
            #?????????   
    if df.iat[(dflen-1),15]<0 and dflen >30 :
        for i in range(1,26):
            if df.iat[(dflen-1-i),15]>=0:#
                operate = operate - 5
                break

    return (df,operate)

#??KDJ??????
saleStock.py 文件源码 项目:DataAnalysis 作者: IMYin 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def Get_KDJ(df):
    #??9,3,3
    slowk, slowd = ta.STOCH(np.array(df['high']), np.array(df['low']), np.array(df['close']), fastk_period=9, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0)

    slowkMA5 = ta.MA(slowk, timeperiod=5, matype=0)
    slowkMA10 = ta.MA(slowk, timeperiod=10, matype=0)
    slowkMA20 = ta.MA(slowk, timeperiod=20, matype=0)
    slowdMA5 = ta.MA(slowd, timeperiod=5, matype=0)
    slowdMA10 = ta.MA(slowd, timeperiod=10, matype=0)
    slowdMA20 = ta.MA(slowd, timeperiod=20, matype=0)
    #16-17 K,D      
    df['slowk']=pd.Series(slowk,index=df.index) #K
    df['slowd']=pd.Series(slowd,index=df.index)#D
    dflen = df.shape[0]
    MAlen = len(slowkMA5)
    operate = 0
    #1.K???????——???90?????????10??????D??80???????????D??20???????????
    if df.iat[(dflen-1),16]>=90:
        operate = operate + 3
    elif df.iat[(dflen-1),16]<=10:
        operate = operate - 3

    if df.iat[(dflen-1),17]>=80:
        operate = operate + 3
    elif df.iat[(dflen-1),17]<=20:
        operate = operate - 3

    #2.??????K???D??K?????D?????????#???
    if df.iat[(dflen-1),16]> df.iat[(dflen-1),17] and df.iat[(dflen-2),16]<=df.iat[(dflen-2),17]:
        operate = operate + 10
    #??????K??D?K?????D?????????#???
    elif df.iat[(dflen-1),16]< df.iat[(dflen-1),17] and df.iat[(dflen-2),16]>=df.iat[(dflen-2),17]:
        operate = operate - 10


    #3.???????????????????????
    if df.iat[(dflen-1),7]>=df.iat[(dflen-1),8] and df.iat[(dflen-1),8]>=df.iat[(dflen-1),9]:#K???
        if (slowkMA5[MAlen-1]<=slowkMA10[MAlen-1] and slowkMA10[MAlen-1]<=slowkMA20[MAlen-1]) or \
           (slowdMA5[MAlen-1]<=slowdMA10[MAlen-1] and slowdMA10[MAlen-1]<=slowdMA20[MAlen-1]): #K,D??
            operate = operate - 1
    elif df.iat[(dflen-1),7]<=df.iat[(dflen-1),8] and df.iat[(dflen-1),8]<=df.iat[(dflen-1),9]:#K???
        if (slowkMA5[MAlen-1]>=slowkMA10[MAlen-1] and slowkMA10[MAlen-1]>=slowkMA20[MAlen-1]) or \
           (slowdMA5[MAlen-1]>=slowdMA10[MAlen-1] and slowdMA10[MAlen-1]>=slowdMA20[MAlen-1]): #K,D??
            operate = operate + 1

    return (df,operate)

#??RSI??????
saleStock.py 文件源码 项目:DataAnalysis 作者: IMYin 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def Get_RSI(df):
    #??14,5
    slowreal = ta.RSI(np.array(df['close']), timeperiod=14)
    fastreal = ta.RSI(np.array(df['close']), timeperiod=5)
    slowrealMA5 = ta.MA(slowreal, timeperiod=5, matype=0)
    slowrealMA10 = ta.MA(slowreal, timeperiod=10, matype=0)
    slowrealMA20 = ta.MA(slowreal, timeperiod=20, matype=0)
    fastrealMA5 = ta.MA(fastreal, timeperiod=5, matype=0)
    fastrealMA10 = ta.MA(fastreal, timeperiod=10, matype=0)
    fastrealMA20 = ta.MA(fastreal, timeperiod=20, matype=0)
    #18-19 ??real???real      
    df['slowreal']=pd.Series(slowreal,index=df.index) #??real 18
    df['fastreal']=pd.Series(fastreal,index=df.index)#??real 19
    dflen = df.shape[0]
    MAlen = len(slowrealMA5)
    operate = 0
    #RSI>80?????RSI<20????
    if df.iat[(dflen-1),18]>80 or df.iat[(dflen-1),19]>80:
        operate = operate - 2
    elif df.iat[(dflen-1),18]<20 or df.iat[(dflen-1),19]<20:
        operate = operate + 2

    #RSI??50???????????50????????
    if (df.iat[(dflen-2),18]<=50 and df.iat[(dflen-1),18]>50) or (df.iat[(dflen-2),19]<=50 and df.iat[(dflen-1),19]>50):
        operate = operate + 4
    elif (df.iat[(dflen-2),18]>=50 and df.iat[(dflen-1),18]<50) or (df.iat[(dflen-2),19]>=50 and df.iat[(dflen-1),19]<50):
        operate = operate - 4

    #RSI??????????RSI?????????
    if df.iat[(dflen-1),7]>=df.iat[(dflen-1),8] and df.iat[(dflen-1),8]>=df.iat[(dflen-1),9]:#K???
        if (slowrealMA5[MAlen-1]<=slowrealMA10[MAlen-1] and slowrealMA10[MAlen-1]<=slowrealMA20[MAlen-1]) or \
           (fastrealMA5[MAlen-1]<=fastrealMA10[MAlen-1] and fastrealMA10[MAlen-1]<=fastrealMA20[MAlen-1]): #RSI??
            operate = operate - 1
    elif df.iat[(dflen-1),7]<=df.iat[(dflen-1),8] and df.iat[(dflen-1),8]<=df.iat[(dflen-1),9]:#K???
        if (slowrealMA5[MAlen-1]>=slowrealMA10[MAlen-1] and slowrealMA10[MAlen-1]>=slowrealMA20[MAlen-1]) or \
           (fastrealMA5[MAlen-1]>=fastrealMA10[MAlen-1] and fastrealMA10[MAlen-1]>=fastrealMA20[MAlen-1]): #RSI??
            operate = operate + 1

    #?????????????????????????????????????????????????????????????????
    if df.iat[(dflen-1),19]> df.iat[(dflen-1),18] and df.iat[(dflen-2),19]<=df.iat[(dflen-2),18]:
        operate = operate + 10
    elif df.iat[(dflen-1),19]< df.iat[(dflen-1),18] and df.iat[(dflen-2),19]>=df.iat[(dflen-2),18]:
        operate = operate - 10      
    return (df,operate)


问题


面经


文章

微信
公众号

扫码关注公众号