Exemple #1
0
def Math_Operators(dataframe):
	#Math Operator Functions
	#ADD - Vector Arithmetic Add
	df[f'{ratio}_ADD'] = talib.ADD(High, Low)
	#c - Vector Arithmetic Div
	df[f'{ratio}_ADD'] = talib.DIV(High, Low)
	#MAX - Highest value over a specified period
	df[f'{ratio}_MAX'] = talib.MAX(Close, timeperiod=30)
	#MAXINDEX - Index of Highest value over a specified period
	#integer = MAXINDEX(Close, timeperiod=30)
	#MIN - Lowest value over a specified period
	df[f'{ratio}_MIN'] = talib.MIN(Close, timeperiod=30)
	#MININDEX - Index of Lowest value over a specified period
	integer = talib.MININDEX(Close, timeperiod=30)
	#MINMAX - Lowest and Highest values over a specified period
	min, max = talib.MINMAX(Close, timeperiod=30)
	#MINMAXINDEX - Indexes of Lowest and Highest values over a specified period
	minidx, maxidx = talib.MINMAXINDEX(Close, timeperiod=30)
	#MULT - Vector Arithmetic Mult
	df[f'{ratio}_MULT'] = talib.MULT(High, Low)
	#SUB - Vector Arithmetic Substraction
	df[f'{ratio}_SUB'] = talib.SUB(High, Low)
	#SUM - Summation
	df[f'{ratio}_SUM'] = talib.SUM(Close, timeperiod=30)

	return
Exemple #2
0
def MULT(high, low):
    ''' Vector Arithmetic Mult 向量乘法运算

    分组: Math Operator 数学运算符

    简介:

    real = MULT(high, low)
    '''
    return talib.MULT(high, low)
Exemple #3
0
def mult(client, symbol, timeframe="6m", col1="open", col2="close"):
    """This will return a dataframe of
    Vector Arithmetic Add
    for the given symbol across the given timeframe

    Args:
        client (pyEX.Client); Client
        symbol (string); Ticker
        timeframe (string); timeframe to use, for pyEX.chart
        col1 (string); column to use to calculate
        col2 (string); column to use to calculate

    Returns:
        DataFrame: result
    """
    df = client.chartDF(symbol, timeframe)
    x = t.MULT(df[col1].values, df[col2].values)
    return pd.DataFrame({col1: df[col1].values, col2: df[col2].values, "mult": x})
Exemple #4
0
def MULT(data, **kwargs):
    _check_talib_presence()
    popen, phigh, plow, pclose, pvolume = _extract_ohlc(data)
    return talib.MULT(phigh, plow, **kwargs)
def add_ta_features(df, ta_settings):
    """Add technial analysis features from typical financial dataset that
    typically include columns such as "open", "high", "low", "price" and
    "volume".

    http://mrjbq7.github.io/ta-lib/

    Args:
        df(pandas.DataFrame): original DataFrame.
        ta_settings(dict): configuration.
    Returns:
        pandas.DataFrame: DataFrame with new features included.
    """

    open = df['open']
    high = df['high']
    low = df['low']
    close = df['price']
    volume = df['volume']

    if ta_settings['overlap']:

        df['ta_overlap_bbands_upper'], df['ta_overlap_bbands_middle'], df[
            'ta_overlap_bbands_lower'] = ta.BBANDS(close,
                                                   timeperiod=5,
                                                   nbdevup=2,
                                                   nbdevdn=2,
                                                   matype=0)
        df['ta_overlap_dema'] = ta.DEMA(
            close, timeperiod=15)  # NOTE: Changed to avoid a lot of Nan values
        df['ta_overlap_ema'] = ta.EMA(close, timeperiod=30)
        df['ta_overlap_kama'] = ta.KAMA(close, timeperiod=30)
        df['ta_overlap_ma'] = ta.MA(close, timeperiod=30, matype=0)
        df['ta_overlap_mama_mama'], df['ta_overlap_mama_fama'] = ta.MAMA(close)
        period = np.random.randint(10, 20, size=len(close)).astype(float)
        df['ta_overlap_mavp'] = ta.MAVP(close,
                                        period,
                                        minperiod=2,
                                        maxperiod=30,
                                        matype=0)
        df['ta_overlap_midpoint'] = ta.MIDPOINT(close, timeperiod=14)
        df['ta_overlap_midprice'] = ta.MIDPRICE(high, low, timeperiod=14)
        df['ta_overlap_sar'] = ta.SAR(high, low, acceleration=0, maximum=0)
        df['ta_overlap_sarext'] = ta.SAREXT(high,
                                            low,
                                            startvalue=0,
                                            offsetonreverse=0,
                                            accelerationinitlong=0,
                                            accelerationlong=0,
                                            accelerationmaxlong=0,
                                            accelerationinitshort=0,
                                            accelerationshort=0,
                                            accelerationmaxshort=0)
        df['ta_overlap_sma'] = ta.SMA(close, timeperiod=30)
        df['ta_overlap_t3'] = ta.T3(close, timeperiod=5, vfactor=0)
        df['ta_overlap_tema'] = ta.TEMA(
            close, timeperiod=12)  # NOTE: Changed to avoid a lot of Nan values
        df['ta_overlap_trima'] = ta.TRIMA(close, timeperiod=30)
        df['ta_overlap_wma'] = ta.WMA(close, timeperiod=30)

        # NOTE: Commented to avoid a lot of Nan values
        # df['ta_overlap_ht_trendline'] = ta.HT_TRENDLINE(close)

    if ta_settings['momentum']:

        df['ta_momentum_adx'] = ta.ADX(high, low, close, timeperiod=14)
        df['ta_momentum_adxr'] = ta.ADXR(high, low, close, timeperiod=14)
        df['ta_momentum_apo'] = ta.APO(close,
                                       fastperiod=12,
                                       slowperiod=26,
                                       matype=0)
        df['ta_momentum_aroondown'], df['ta_momentum_aroonup'] = ta.AROON(
            high, low, timeperiod=14)
        df['ta_momentum_aroonosc'] = ta.AROONOSC(high, low, timeperiod=14)
        df['ta_momentum_bop'] = ta.BOP(open, high, low, close)
        df['ta_momentum_cci'] = ta.CCI(high, low, close, timeperiod=14)
        df['ta_momentum_cmo'] = ta.CMO(close, timeperiod=14)
        df['ta_momentum_dx'] = ta.DX(high, low, close, timeperiod=14)
        df['ta_momentum_macd_macd'], df['ta_momentum_macd_signal'], df[
            'ta_momentum_macd_hist'] = ta.MACD(close,
                                               fastperiod=12,
                                               slowperiod=26,
                                               signalperiod=9)
        df['ta_momentum_macdext_macd'], df['ta_momentum_macdext_signal'], df[
            'ta_momentum_macdext_hist'] = ta.MACDEXT(close,
                                                     fastperiod=12,
                                                     fastmatype=0,
                                                     slowperiod=26,
                                                     slowmatype=0,
                                                     signalperiod=9,
                                                     signalmatype=0)
        df['ta_momentum_macdfix_macd'], df['ta_momentum_macdfix_signal'], df[
            'ta_momentum_macdfix_hist'] = ta.MACDFIX(close, signalperiod=9)
        df['ta_momentum_mfi'] = ta.MFI(high, low, close, volume, timeperiod=14)
        df['ta_momentum_minus_di'] = ta.MINUS_DI(high,
                                                 low,
                                                 close,
                                                 timeperiod=14)
        df['ta_momentum_minus_dm'] = ta.MINUS_DM(high, low, timeperiod=14)
        df['ta_momentum_mom'] = ta.MOM(close, timeperiod=10)
        df['ta_momentum_plus_di'] = ta.PLUS_DI(high, low, close, timeperiod=14)
        df['ta_momentum_plus_dm'] = ta.PLUS_DM(high, low, timeperiod=14)
        df['ta_momentum_ppo'] = ta.PPO(close,
                                       fastperiod=12,
                                       slowperiod=26,
                                       matype=0)
        df['ta_momentum_roc'] = ta.ROC(close, timeperiod=10)
        df['ta_momentum_rocp'] = ta.ROCP(close, timeperiod=10)
        df['ta_momentum_rocr'] = ta.ROCR(close, timeperiod=10)
        df['ta_momentum_rocr100'] = ta.ROCR100(close, timeperiod=10)
        df['ta_momentum_rsi'] = ta.RSI(close, timeperiod=14)
        df['ta_momentum_slowk'], df['ta_momentum_slowd'] = ta.STOCH(
            high,
            low,
            close,
            fastk_period=5,
            slowk_period=3,
            slowk_matype=0,
            slowd_period=3,
            slowd_matype=0)
        df['ta_momentum_fastk'], df['ta_momentum_fastd'] = ta.STOCHF(
            high, low, close, fastk_period=5, fastd_period=3, fastd_matype=0)
        df['ta_momentum_fastk'], df['ta_momentum_fastd'] = ta.STOCHRSI(
            close,
            timeperiod=14,
            fastk_period=5,
            fastd_period=3,
            fastd_matype=0)
        df['ta_momentum_trix'] = ta.TRIX(
            close, timeperiod=12)  # NOTE: Changed to avoid a lot of Nan values
        df['ta_momentum_ultosc'] = ta.ULTOSC(high,
                                             low,
                                             close,
                                             timeperiod1=7,
                                             timeperiod2=14,
                                             timeperiod3=28)
        df['ta_momentum_willr'] = ta.WILLR(high, low, close, timeperiod=14)

    if ta_settings['volume']:

        df['ta_volume_ad'] = ta.AD(high, low, close, volume)
        df['ta_volume_adosc'] = ta.ADOSC(high,
                                         low,
                                         close,
                                         volume,
                                         fastperiod=3,
                                         slowperiod=10)
        df['ta_volume_obv'] = ta.OBV(close, volume)

    if ta_settings['volatility']:

        df['ta_volatility_atr'] = ta.ATR(high, low, close, timeperiod=14)
        df['ta_volatility_natr'] = ta.NATR(high, low, close, timeperiod=14)
        df['ta_volatility_trange'] = ta.TRANGE(high, low, close)

    if ta_settings['price']:

        df['ta_price_avgprice'] = ta.AVGPRICE(open, high, low, close)
        df['ta_price_medprice'] = ta.MEDPRICE(high, low)
        df['ta_price_typprice'] = ta.TYPPRICE(high, low, close)
        df['ta_price_wclprice'] = ta.WCLPRICE(high, low, close)

    if ta_settings['cycle']:

        df['ta_cycle_ht_dcperiod'] = ta.HT_DCPERIOD(close)
        df['ta_cycle_ht_phasor_inphase'], df[
            'ta_cycle_ht_phasor_quadrature'] = ta.HT_PHASOR(close)
        df['ta_cycle_ht_trendmode'] = ta.HT_TRENDMODE(close)

        # NOTE: Commented to avoid a lot of Nan values
        # df['ta_cycle_ht_dcphase'] = ta.HT_DCPHASE(close)
        # df['ta_cycle_ht_sine_sine'], df['ta_cycle_ht_sine_leadsine'] = ta.HT_SINE(close)

    if ta_settings['pattern']:

        df['ta_pattern_cdl2crows'] = ta.CDL2CROWS(open, high, low, close)
        df['ta_pattern_cdl3blackrows'] = ta.CDL3BLACKCROWS(
            open, high, low, close)
        df['ta_pattern_cdl3inside'] = ta.CDL3INSIDE(open, high, low, close)
        df['ta_pattern_cdl3linestrike'] = ta.CDL3LINESTRIKE(
            open, high, low, close)
        df['ta_pattern_cdl3outside'] = ta.CDL3OUTSIDE(open, high, low, close)
        df['ta_pattern_cdl3starsinsouth'] = ta.CDL3STARSINSOUTH(
            open, high, low, close)
        df['ta_pattern_cdl3whitesoldiers'] = ta.CDL3WHITESOLDIERS(
            open, high, low, close)
        df['ta_pattern_cdlabandonedbaby'] = ta.CDLABANDONEDBABY(open,
                                                                high,
                                                                low,
                                                                close,
                                                                penetration=0)
        df['ta_pattern_cdladvanceblock'] = ta.CDLADVANCEBLOCK(
            open, high, low, close)
        df['ta_pattern_cdlbelthold'] = ta.CDLBELTHOLD(open, high, low, close)
        df['ta_pattern_cdlbreakaway'] = ta.CDLBREAKAWAY(open, high, low, close)
        df['ta_pattern_cdlclosingmarubozu'] = ta.CDLCLOSINGMARUBOZU(
            open, high, low, close)
        df['ta_pattern_cdlconcealbabyswall'] = ta.CDLCONCEALBABYSWALL(
            open, high, low, close)
        df['ta_pattern_cdlcounterattack'] = ta.CDLCOUNTERATTACK(
            open, high, low, close)
        df['ta_pattern_cdldarkcloudcover'] = ta.CDLDARKCLOUDCOVER(
            open, high, low, close, penetration=0)
        df['ta_pattern_cdldoji'] = ta.CDLDOJI(open, high, low, close)
        df['ta_pattern_cdldojistar'] = ta.CDLDOJISTAR(open, high, low, close)
        df['ta_pattern_cdldragonflydoji'] = ta.CDLDRAGONFLYDOJI(
            open, high, low, close)
        df['ta_pattern_cdlengulfing'] = ta.CDLENGULFING(open, high, low, close)
        df['ta_pattern_cdleveningdojistar'] = ta.CDLEVENINGDOJISTAR(
            open, high, low, close, penetration=0)
        df['ta_pattern_cdleveningstar'] = ta.CDLEVENINGSTAR(open,
                                                            high,
                                                            low,
                                                            close,
                                                            penetration=0)
        df['ta_pattern_cdlgapsidesidewhite'] = ta.CDLGAPSIDESIDEWHITE(
            open, high, low, close)
        df['ta_pattern_cdlgravestonedoji'] = ta.CDLGRAVESTONEDOJI(
            open, high, low, close)
        df['ta_pattern_cdlhammer'] = ta.CDLHAMMER(open, high, low, close)
        df['ta_pattern_cdlhangingman'] = ta.CDLHANGINGMAN(
            open, high, low, close)
        df['ta_pattern_cdlharami'] = ta.CDLHARAMI(open, high, low, close)
        df['ta_pattern_cdlharamicross'] = ta.CDLHARAMICROSS(
            open, high, low, close)
        df['ta_pattern_cdlhighwave'] = ta.CDLHIGHWAVE(open, high, low, close)
        df['ta_pattern_cdlhikkake'] = ta.CDLHIKKAKE(open, high, low, close)
        df['ta_pattern_cdlhikkakemod'] = ta.CDLHIKKAKEMOD(
            open, high, low, close)
        df['ta_pattern_cdlhomingpigeon'] = ta.CDLHOMINGPIGEON(
            open, high, low, close)
        df['ta_pattern_cdlidentical3crows'] = ta.CDLIDENTICAL3CROWS(
            open, high, low, close)
        df['ta_pattern_cdlinneck'] = ta.CDLINNECK(open, high, low, close)
        df['ta_pattern_cdlinvertedhammer'] = ta.CDLINVERTEDHAMMER(
            open, high, low, close)
        df['ta_pattern_cdlkicking'] = ta.CDLKICKING(open, high, low, close)
        df['ta_pattern_cdlkickingbylength'] = ta.CDLKICKINGBYLENGTH(
            open, high, low, close)
        df['ta_pattern_cdlladderbottom'] = ta.CDLLADDERBOTTOM(
            open, high, low, close)
        df['ta_pattern_cdllongleggeddoji'] = ta.CDLLONGLEGGEDDOJI(
            open, high, low, close)
        df['ta_pattern_cdllongline'] = ta.CDLLONGLINE(open, high, low, close)
        df['ta_pattern_cdlmarubozu'] = ta.CDLMARUBOZU(open, high, low, close)
        df['ta_pattern_cdlmatchinglow'] = ta.CDLMATCHINGLOW(
            open, high, low, close)
        df['ta_pattern_cdlmathold'] = ta.CDLMATHOLD(open,
                                                    high,
                                                    low,
                                                    close,
                                                    penetration=0)
        df['ta_pattern_cdlmorningdojistar'] = ta.CDLMORNINGDOJISTAR(
            open, high, low, close, penetration=0)
        df['ta_pattern_cdlmorningstar'] = ta.CDLMORNINGSTAR(open,
                                                            high,
                                                            low,
                                                            close,
                                                            penetration=0)
        df['ta_pattern_cdllonneck'] = ta.CDLONNECK(open, high, low, close)
        df['ta_pattern_cdlpiercing'] = ta.CDLPIERCING(open, high, low, close)
        df['ta_pattern_cdlrickshawman'] = ta.CDLRICKSHAWMAN(
            open, high, low, close)
        df['ta_pattern_cdlrisefall3methods'] = ta.CDLRISEFALL3METHODS(
            open, high, low, close)
        df['ta_pattern_cdlseparatinglines'] = ta.CDLSEPARATINGLINES(
            open, high, low, close)
        df['ta_pattern_cdlshootingstar'] = ta.CDLSHOOTINGSTAR(
            open, high, low, close)
        df['ta_pattern_cdlshortline'] = ta.CDLSHORTLINE(open, high, low, close)
        df['ta_pattern_cdlspinningtop'] = ta.CDLSPINNINGTOP(
            open, high, low, close)
        df['ta_pattern_cdlstalledpattern'] = ta.CDLSTALLEDPATTERN(
            open, high, low, close)
        df['ta_pattern_cdlsticksandwich'] = ta.CDLSTICKSANDWICH(
            open, high, low, close)
        df['ta_pattern_cdltakuri'] = ta.CDLTAKURI(open, high, low, close)
        df['ta_pattern_cdltasukigap'] = ta.CDLTASUKIGAP(open, high, low, close)
        df['ta_pattern_cdlthrusting'] = ta.CDLTHRUSTING(open, high, low, close)
        df['ta_pattern_cdltristar'] = ta.CDLTRISTAR(open, high, low, close)
        df['ta_pattern_cdlunique3river'] = ta.CDLUNIQUE3RIVER(
            open, high, low, close)
        df['ta_pattern_cdlupsidegap2crows'] = ta.CDLUPSIDEGAP2CROWS(
            open, high, low, close)
        df['ta_pattern_cdlxsidegap3methods'] = ta.CDLXSIDEGAP3METHODS(
            open, high, low, close)

    if ta_settings['statistic']:

        df['ta_statistic_beta'] = ta.BETA(high, low, timeperiod=5)
        df['ta_statistic_correl'] = ta.CORREL(high, low, timeperiod=30)
        df['ta_statistic_linearreg'] = ta.LINEARREG(close, timeperiod=14)
        df['ta_statistic_linearreg_angle'] = ta.LINEARREG_ANGLE(close,
                                                                timeperiod=14)
        df['ta_statistic_linearreg_intercept'] = ta.LINEARREG_INTERCEPT(
            close, timeperiod=14)
        df['ta_statistic_linearreg_slope'] = ta.LINEARREG_SLOPE(close,
                                                                timeperiod=14)
        df['ta_statistic_stddev'] = ta.STDDEV(close, timeperiod=5, nbdev=1)
        df['ta_statistic_tsf'] = ta.TSF(close, timeperiod=14)
        df['ta_statistic_var'] = ta.VAR(close, timeperiod=5, nbdev=1)

    if ta_settings['math_transforms']:

        df['ta_math_transforms_atan'] = ta.ATAN(close)
        df['ta_math_transforms_ceil'] = ta.CEIL(close)
        df['ta_math_transforms_cos'] = ta.COS(close)
        df['ta_math_transforms_floor'] = ta.FLOOR(close)
        df['ta_math_transforms_ln'] = ta.LN(close)
        df['ta_math_transforms_log10'] = ta.LOG10(close)
        df['ta_math_transforms_sin'] = ta.SIN(close)
        df['ta_math_transforms_sqrt'] = ta.SQRT(close)
        df['ta_math_transforms_tan'] = ta.TAN(close)

    if ta_settings['math_operators']:

        df['ta_math_operators_add'] = ta.ADD(high, low)
        df['ta_math_operators_div'] = ta.DIV(high, low)
        df['ta_math_operators_min'], df['ta_math_operators_max'] = ta.MINMAX(
            close, timeperiod=30)
        df['ta_math_operators_minidx'], df[
            'ta_math_operators_maxidx'] = ta.MINMAXINDEX(close, timeperiod=30)
        df['ta_math_operators_mult'] = ta.MULT(high, low)
        df['ta_math_operators_sub'] = ta.SUB(high, low)
        df['ta_math_operators_sum'] = ta.SUM(close, timeperiod=30)

    return df
Exemple #6
0
 def MULT(High, Close):
     real = pd.DataFrame()
     for i in Close.columns:
         real[i] = ta.MULT(High[i], Close[i])
     return real
Exemple #7
0
        maxindex = ta.MAXINDEX(close, timeperiod=30)

        #MIN - Lowest value over a specified period
        minv = ta.MIN(close, timeperiod=30)

        #MININDEX - Index of lowest value over a specified period
        minindex = ta.MININDEX(close, timeperiod=30)

        #MINMAX - Lowest and highest values over a specified period
        minsp, maxsp = ta.MINMAX(close, timeperiod=30)

        #MINMAXINDEX - Indexes of lowest and highest values over a specified period
        minidx, maxidx = ta.MINMAXINDEX(close, timeperiod=30)

        #MULT - Vector Arithmetic Mult
        mult = ta.MULT(high, low)

        #SUB - Vector Arithmetic Substraction
        sub = ta.SUB(high, low)

        #SUM - Summation
        sum = ta.SUM(close, timeperiod=30)

        df_save = pd.DataFrame(
            data={
                'date': np.array(df['date']),
                'add': add,
                'div': div,
                'max': maxv,
                'maxindex': maxindex,
                'min': minv,
import tushare as ts
def get_data(code,start='2015-01-01'):
    df=ts.get_k_data(code,start)
    df.index=pd.to_datetime(df.date)
    df=df.sort_index()
    return df

#获取上证指数收盘价、最高、最低价格
df=get_data('sh')[['open','close','high','low']]

#最高价与最低价之和
df['add']=ta.ADD(df.high,df.low)
#最高价与最低价之差
df['sub']=ta.SUB(df.high,df.low)
#最高价与最低价之乘
df['mult']=ta.MULT(df.high,df.low)
#最高价与最低价之除
df['div']=ta.DIV(df.high,df.low)
#收盘价的每30日移动求和
df['sum']=ta.SUM(df.close, timeperiod=30)
#收盘价的每30日内的最大最小值
df['min'], df['max'] = ta.MINMAX(df.close, timeperiod=30)
#收盘价的每30日内的最大最小值对应的索引值(第N行)
df['minidx'], df['maxidx'] = ta.MINMAXINDEX(df.close, timeperiod=30)
df.tail()

#将上述函数计算得到的结果进行可视化
df[['close','add','sub','mult','div','sum','min','max']].plot(figsize=(12,10),
       subplots = True,
       layout=(4, 2))
plt.subplots_adjust(wspace=0,hspace=0.2)
Exemple #9
0
def data_indicator(data,time,normal=False):

    ml_datas = data.drop(data.columns, axis=1)

    open = data.open.values
    high = data.high.values
    close = data.close.values
    low = data.low.values
    volume = data.volume.values
    var = [open,high,close,low,volume]
    var_name = ['open','high','close','low','volume']




    # 单输入带时间单输出
    #为了凑数,以下候补
    #[talib.DEMA, talib.WMA, talib.MAXINDEX, talib.MININDEX, talib.TEMA ]
    #["DEMA", "WMA", "MAXINDEX", "MININDEX", "TEMA"]


    single = [talib.EMA, talib.KAMA, talib.MA, talib.MIDPOINT, talib.SMA, talib.T3, talib.TRIMA,
              talib.CMO, talib.MOM, talib.ROC, talib.ROCP, talib.ROCR, talib.ROCR100, talib.RSI, talib.TRIX, talib.MAX,
              talib.MIN, talib.SUM]
    single_name = ["EMA", "KAMA", "MA", "MIDPOINT", "SMA", "T3", "TRIMA", "CMO", "MOM", "ROC", "ROCP",
                   "ROCR", "ROCR100", "RSI", "TRIX", "MAX", "MIN", "SUM"]

    def single_output(f, x1, timeperiod):
        z = f(x1, timeperiod)
        return z

    for i in time:
        for v in range(len(var)):
            for p in range(len(single)):
                locals()[single_name[p] + str('_') + var_name[v] + str('_') + str(i)] = single_output(single[p], var[v],
                                                                                                      timeperiod=i)


    for i in time:
        for v in range(len(var)):
            for p in range(len(single)):
                ml_datas[single_name[p] + str('_') + var_name[v] + str('_') + str(i)] = pd.Series(
                    locals()[single_name[p] + str('_') + var_name[v] + str('_') + str(i)], index=data.index)


    #单输入带时间多输出
    for i in time:
        for v in range(len(var)):
            locals()['BBANDS_upper'+ str('_') + var_name[v] + str('_') + str(i)], \
            locals()['BBANDS_middle' + str('_') + var_name[v] + str('_') + str(i)],\
            locals()['BBANDS_lower'+ str('_') + var_name[v] + str('_') + str(i)] = talib.BBANDS(var[v], timeperiod=i)

            locals()['STOCHRSI_fastk' + str('_') + var_name[v] + str('_') + str(i)], \
            locals()['STOCHRSI_fastd' + str('_') + var_name[v] + str('_') + str(i)] = talib.STOCHRSI(var[v], timeperiod=i)

            locals()['MINMAX_min' + str('_') + var_name[v] + str('_') + str(i)], \
            locals()['MINMAX_max' + str('_') + var_name[v] + str('_') + str(i)] = talib.MINMAX(var[v], timeperiod=i)

            locals()['MINMAX_minidx' + str('_') + var_name[v] + str('_') + str(i)], \
            locals()['MINMAX_maxidx' + str('_') + var_name[v] + str('_') + str(i)] = talib.MINMAXINDEX(var[v], timeperiod=i)



    for i in time:
        for v in range(len(var)):
            ml_datas['BBANDS_upper'+ str('_') + var_name[v] + str('_') + str(i)] = pd.Series(
                locals()['BBANDS_upper'+ str('_') + var_name[v] + str('_') + str(i)], index=data.index)
            ml_datas['BBANDS_middle' + str('_') + var_name[v] + str('_') + str(i)] = pd.Series(
                locals()['BBANDS_middle' + str('_') + var_name[v] + str('_') + str(i)], index=data.index)
            ml_datas['BBANDS_lower' + str('_') + var_name[v] + str('_') + str(i)] = pd.Series(
                locals()['BBANDS_lower' + str('_') + var_name[v] + str('_') + str(i)], index=data.index)
            ml_datas['STOCHRSI_fastk' + str('_') + var_name[v] + str('_') + str(i)] = pd.Series(
                locals()['STOCHRSI_fastk' + str('_') + var_name[v] + str('_') + str(i)], index=data.index)
            ml_datas['STOCHRSI_fastd' + str('_') + var_name[v] + str('_') + str(i)] = pd.Series(
                locals()['STOCHRSI_fastd' + str('_') + var_name[v] + str('_') + str(i)], index=data.index)
            ml_datas['MINMAX_min' + str('_') + var_name[v] + str('_') + str(i)] = pd.Series(
                locals()['MINMAX_min' + str('_') + var_name[v] + str('_') + str(i)], index=data.index)
            ml_datas['MINMAX_max' + str('_') + var_name[v] + str('_') + str(i)] = pd.Series(
                locals()['MINMAX_max' + str('_') + var_name[v] + str('_') + str(i)], index=data.index)
            ml_datas['MINMAX_minidx' + str('_') + var_name[v] + str('_') + str(i)] = pd.Series(
               locals()['MINMAX_minidx' + str('_') + var_name[v] + str('_') + str(i)], index=data.index)
            ml_datas['MINMAX_maxidx' + str('_') + var_name[v] + str('_') + str(i)] = pd.Series(
                locals()['MINMAX_maxidx' + str('_') + var_name[v] + str('_') + str(i)], index=data.index)

    # 多输入带时间单输出
    for i in time:
        locals()['ATR' + str('_') + str(i)] = talib.ATR(high, low, close, timeperiod=i)
        locals()['NATR' + str('_') + str(i)] = talib.NATR(high, low, close, timeperiod=i)
        locals()['ADX' + str('_') + str(i)] = talib.ADX(high, low, close, timeperiod=i)
        locals()['ADXR' + str('_') + str(i)] = talib.ADXR(high, low, close, timeperiod=i)
        locals()['AROONOSC' + str('_') + str(i)] = talib.AROONOSC(high, low, timeperiod=i)
        locals()['CCI' + str('_') + str(i)] = talib.CCI(high, low, close, timeperiod=i)
        locals()['DX' + str('_') + str(i)] = talib.DX(high, low, close, timeperiod=i)
        locals()['MFI' + str('_') + str(i)] = talib.MFI(high, low, close, volume, timeperiod=i)
        locals()['MINUS_DI' + str('_') + str(i)] = talib.MINUS_DI(high, low, close, timeperiod=i)
        locals()['MINUS_DM' + str('_') + str(i)] = talib.MINUS_DM(high, low, timeperiod=i)
        locals()['PLUS_DI' + str('_') + str(i)] = talib.PLUS_DI(high, low, close, timeperiod=i)
        locals()['PLUS_DM' + str('_') + str(i)] = talib.PLUS_DM(high, low, timeperiod=i)
        locals()['WILLR' + str('_') + str(i)] = talib.WILLR(high, low, close, timeperiod=i)
        locals()['MIDPRICE' + str('_') + str(i)] = talib.MIDPRICE(high, low, timeperiod=i)
        locals()['AROON_aroondown' + str('_') + str(i)], locals()['AROON_aroonup' + str('_') + str(i)] = talib.AROON(high, low, timeperiod=i)

    for i in time:
        ml_datas['ATR'] = pd.Series(locals()['ATR' + str('_') + str(i)], index=data.index)
        ml_datas['NATR'] = pd.Series(locals()['NATR' + str('_') + str(i)], index = data.index)
        ml_datas['ADX'] = pd.Series(locals()['ADX' + str('_') + str(i)], index = data.index)
        ml_datas['ADXR'] = pd.Series(locals()['ADXR' + str('_') + str(i)], index = data.index)
        ml_datas['AROONOSC'] = pd.Series(locals()['AROONOSC' + str('_') + str(i)], index = data.index)
        ml_datas['CCI'] = pd.Series(locals()['CCI' + str('_') + str(i)], index = data.index)
        ml_datas['DX'] = pd.Series(locals()['DX' + str('_') + str(i)], index = data.index)
        ml_datas['MFI'] = pd.Series(locals()['MFI' + str('_') + str(i)], index = data.index)
        ml_datas['MINUS_DI'] = pd.Series(locals()['MINUS_DI' + str('_') + str(i)], index = data.index)
        ml_datas['MINUS_DM'] = pd.Series(locals()['MINUS_DM' + str('_') + str(i)], index = data.index)
        ml_datas['PLUS_DI'] = pd.Series(locals()['PLUS_DI' + str('_') + str(i)], index = data.index)
        ml_datas['PLUS_DM'] = pd.Series(locals()['PLUS_DM' + str('_') + str(i)], index = data.index)
        ml_datas['WILLR'] = pd.Series(locals()['WILLR' + str('_') + str(i)], index = data.index)
        ml_datas['MIDPRICE'] = pd.Series(locals()['MIDPRICE' + str('_') + str(i)], index = data.index)
        ml_datas['AROON_aroondown'] = pd.Series(locals()['AROON_aroondown' + str('_') + str(i)], index = data.index)
        ml_datas['AROON_aroonup'] = pd.Series(locals()['AROON_aroonup' + str('_') + str(i)], index = data.index)




    #单输入不带时间
    # single2 = [talib.ACOS, talib.ASIN, talib.ATAN, talib.CEIL, talib.COS, talib.COSH, talib.EXP, talib.FLOOR, talib.LN,
    #            talib.LOG10, talib.SIN, talib.SINH, talib.SQRT, talib.TAN, talib.TANH, talib.HT_DCPERIOD,
    #            talib.HT_DCPHASE, talib.HT_TRENDMODE, talib.HT_TRENDLINE, talib.APO]
    # single2_name = ["ACOS", "ASIN", "ATAN", "CEIL", "COS", "COSH", "EXP", "FLOOR", "LN", "LOG10", "SIN", "SINH", "SQRT",
    #                 "TAN", "TANH", "HT_DCPERIOD", "HT_DCPHASE", "HT_TRENDMODE", "HT_TRENDLINE", "APO"]
    #
    # def single2_output(f, x1):
    #     z = f(x1)
    #     return z
    #
    # for v in range(len(var)):
    #     for p in range(len(single2)):
    #         locals()[single2_name[p] + str('_') + var_name[v]] = single2_output(single2[p], var[v])
    #
    # for v in range(len(var)):
    #     for p in range(len(single2)):
    #         ml_datas[single2_name[p] + str('_') + var_name[v]] = pd.Series(locals()[single2_name[p] + str('_') + var_name[v]])
    #
    #






    # 模式识别类指标

    pattern = [talib.CDL2CROWS, talib.CDL3BLACKCROWS, talib.CDL3INSIDE, talib.CDL3LINESTRIKE, talib.CDL3OUTSIDE,
               talib.CDL3STARSINSOUTH, talib.CDL3WHITESOLDIERS, talib.CDLABANDONEDBABY, talib.CDLADVANCEBLOCK,
               talib.CDLBELTHOLD, talib.CDLBREAKAWAY, talib.CDLCLOSINGMARUBOZU, talib.CDLCONCEALBABYSWALL,
               talib.CDLCOUNTERATTACK, talib.CDLDARKCLOUDCOVER, talib.CDLDOJI, talib.CDLDOJISTAR,
               talib.CDLDRAGONFLYDOJI,
               talib.CDLENGULFING, talib.CDLEVENINGDOJISTAR, talib.CDLEVENINGSTAR, talib.CDLGAPSIDESIDEWHITE,
               talib.CDLGRAVESTONEDOJI, talib.CDLHAMMER, talib.CDLHANGINGMAN, talib.CDLHARAMI, talib.CDLHARAMICROSS,
               talib.CDLHIGHWAVE, talib.CDLHIKKAKE, talib.CDLHIKKAKEMOD, talib.CDLHOMINGPIGEON,
               talib.CDLIDENTICAL3CROWS,
               talib.CDLINNECK, talib.CDLINVERTEDHAMMER, talib.CDLKICKING, talib.CDLKICKINGBYLENGTH,
               talib.CDLLADDERBOTTOM,
               talib.CDLLONGLEGGEDDOJI, talib.CDLLONGLINE, talib.CDLMARUBOZU, talib.CDLMATCHINGLOW, talib.CDLMATHOLD,
               talib.CDLMORNINGDOJISTAR, talib.CDLMORNINGSTAR, talib.CDLONNECK, talib.CDLPIERCING, talib.CDLRICKSHAWMAN,
               talib.CDLRISEFALL3METHODS, talib.CDLSEPARATINGLINES, talib.CDLSHOOTINGSTAR, talib.CDLSHORTLINE,
               talib.CDLSPINNINGTOP, talib.CDLSTALLEDPATTERN, talib.CDLXSIDEGAP3METHODS, talib.CDLSTICKSANDWICH,
               talib.CDLTAKURI, talib.CDLTASUKIGAP, talib.CDLTHRUSTING, talib.CDLTRISTAR, talib.CDLUNIQUE3RIVER, talib.CDLUPSIDEGAP2CROWS]
    pattern_name = ["CDL2CROWS", "CDL3BLACKCROWS", "CDL3INSIDE", "CDL3LINESTRIKE", "CDL3OUTSIDE", "CDL3STARSINSOUTH",
                "CDL3WHITESOLDIERS", "CDLABANDONEDBABY", "CDLADVANCEBLOCK", "CDLBELTHOLD", "CDLBREAKAWAY",
                "CDLCLOSINGMARUBOZU", "CDLCONCEALBABYSWALL", "CDLCOUNTERATTACK", "CDLDARKCLOUDCOVER", "CDLDOJI",
                "CDLDOJISTAR", "CDLDRAGONFLYDOJI", "CDLENGULFING", "CDLEVENINGDOJISTAR", "CDLEVENINGSTAR",
                "CDLGAPSIDESIDEWHITE", "CDLGRAVESTONEDOJI", "CDLHAMMER", "CDLHANGINGMAN", "CDLHARAMI", "CDLHARAMICROSS",
                "CDLHIGHWAVE", "CDLHIKKAKE", "CDLHIKKAKEMOD", "CDLHOMINGPIGEON", "CDLIDENTICAL3CROWS", "CDLINNECK",
                "CDLINVERTEDHAMMER", "CDLKICKING", "CDLKICKINGBYLENGTH", "CDLLADDERBOTTOM", "CDLLONGLEGGEDDOJI",
                "CDLLONGLINE", "CDLMARUBOZU", "CDLMATCHINGLOW", "CDLMATHOLD", "CDLMORNINGDOJISTAR", "CDLMORNINGSTAR",
                "CDLONNECK", "CDLPIERCING", "CDLRICKSHAWMAN", "CDLRISEFALL3METHODS", "CDLSEPARATINGLINES",
                "CDLSHOOTINGSTAR", "CDLSHORTLINE", "CDLSPINNINGTOP", "CDLSTALLEDPATTERN","CDLXSIDEGAP3METHODS","CDLSTICKSANDWICH","CDLTAKURI", "CDLTASUKIGAP", "CDLTHRUSTING", "CDLTRISTAR", "CDLUNIQUE3RIVER", "CDLUPSIDEGAP2CROWS"]


    def Pattern_Recognition(f, x1, x2, x3, x4):
            z = f(x1, x2, x3, x4)
            return z


    for p in range(len(pattern)):
        locals()[pattern_name[p]] = Pattern_Recognition(pattern[p], open, high, low, close)

    for p in range(len(pattern)):
        ml_datas[pattern_name[p]] = pd.Series(locals()[pattern_name[p]], index=data.index)


    #杂乱指标
    #为了凑数,ULTOSC多用了一遍

    ADD = talib.ADD(high, low)
    MULT = talib.MULT(high, low)
    SUB = talib.SUB(high, low)
    TRANGE = talib.TRANGE(high, low, close)
    AD = talib.AD(high, low, close, volume)
    ADOSC = talib.ADOSC(high, low, close, volume)
    OBV = talib.OBV(close, volume)
    BOP = talib.BOP(open, high, low, close)

    ml_datas['ADD'] = pd.Series(ADD, index=data.index)
    ml_datas['MULT'] = pd.Series(MULT, index=data.index)
    ml_datas['SUB'] = pd.Series(SUB, index=data.index)
    ml_datas['TRANGE'] = pd.Series(TRANGE, index=data.index)
    ml_datas['AD'] = pd.Series(AD, index=data.index)
    ml_datas['ADOSC'] = pd.Series(ADOSC, index=data.index)
    ml_datas['OBV'] = pd.Series(OBV, index=data.index)
    ml_datas['BOP'] = pd.Series(BOP, index=data.index)


    HT_PHASOR_inphase, HT_PHASOR_quadrature = talib.HT_PHASOR(close)
    HT_SINE_sine, HT_SINE_leadsine = talib.HT_SINE(close)
    MACD_macd, MACD_macdsignal, MACD_macdhist = talib.MACD(close)
    MACDEXT_macd, MACDEXT_macdsignal, MACDEXT_macdhist = talib.MACDEXT(close)
    MACDFIX_macd, MACDFIX_macdsignal, MACDFIX_macdhist = talib.MACDFIX(close)
    PPO = talib.PPO(close)
    MAMA_mama, MAMA_fama = talib.MAMA(close)
    STOCH_slowk, STOCH_slowd = talib.STOCH(high, low, close)
    STOCHF_fastk, STOCHF_fastd = talib.STOCHF(high, low, close)
    SAR = talib.SAR(high, low)
    SAREXT = talib.SAREXT(high, low)
    ULTOSC = talib.ULTOSC(high, low, close, timeperiod1=7, timeperiod2=14, timeperiod3=28)


    ml_datas['HT_PHASOR_inphase'] = pd.Series(HT_PHASOR_inphase, index=data.index)
    ml_datas['HT_PHASOR_quadrature'] = pd.Series(HT_PHASOR_quadrature, index=data.index)
    ml_datas['HT_SINE_sine'] = pd.Series(HT_SINE_sine, index=data.index)
    ml_datas['HT_SINE_leadsine'] = pd.Series(HT_SINE_leadsine, index=data.index)
    ml_datas['MACD_macd'] = pd.Series(MACD_macd, index=data.index)
    ml_datas['MACD_macdsignal'] = pd.Series(MACD_macdsignal, index=data.index)
    ml_datas['MACD_macdhist'] = pd.Series(MACD_macdhist, index=data.index)
    ml_datas['MACDEXT_macd'] = pd.Series(MACDEXT_macd, index=data.index)
    ml_datas['MACDEXT_macdsignal'] = pd.Series(MACDEXT_macdsignal, index=data.index)
    ml_datas['MACDEXT_macdhist'] = pd.Series(MACDEXT_macdhist, index=data.index)
    ml_datas['MACDFIX_macd'] = pd.Series(MACDFIX_macd, index=data.index)
    ml_datas['MACDFIX_macdsignal'] = pd.Series(MACDFIX_macdsignal, index=data.index)
    ml_datas['MACDFIX_macdhist'] = pd.Series(MACDFIX_macdhist, index=data.index)
    ml_datas['PPO'] = pd.Series(PPO, index=data.index)
    ml_datas['MAMA_mama'] = pd.Series(MAMA_mama, index=data.index)
    ml_datas['MAMA_fama'] = pd.Series(MAMA_fama, index=data.index)
    ml_datas['STOCH_slowk'] = pd.Series(STOCH_slowk, index=data.index)
    ml_datas['STOCH_slowd'] = pd.Series(STOCH_slowd, index=data.index)
    ml_datas['STOCHF_fastk'] = pd.Series(STOCHF_fastk, index=data.index)
    ml_datas['STOCHF_fastd'] = pd.Series(STOCHF_fastd, index=data.index)
    ml_datas['SAR'] = pd.Series(SAR, index=data.index)
    ml_datas['SAREXT'] = pd.Series(SAREXT, index=data.index)
    ml_datas['ULTOSC'] = pd.Series(ULTOSC, index=data.index)
    ml_datas['ULTOSC_VAR'] = pd.Series(ULTOSC, index=data.index)






    # 将原始数据集的数据移动一天,使每天收盘价数据的特征训练的时候用前一天的信息
    ml_datas = ml_datas.shift(1)
    ml_datas['target'] = close*100



    #var_datas = ml_datas.drop(ml_datas.columns, axis=1)

    #var_datas['target'] = var_datas.sum(axis=1) * 100

    #ml_datas['target'] = var_datas['target']




    #ml_datas = ml_datas.dropna(how='all', axis=1) #删掉都是NA的列
    ml_datas = ml_datas.dropna(how='any', axis=0)

    if normal:
        X_ori = ml_datas.drop(['target'], axis=1)
        scaler = preprocessing.StandardScaler().fit(X_ori)
        X = scaler.transform(X_ori)
        X_ori = pd.DataFrame(X,index=X_ori.index,columns=X_ori.columns)

        format = lambda x: '%.1f' % x
        X_ori['target'] = ml_datas['target'].map(format).map(float) #保留n位小数,然后转回float

        #X_ori['target'] = pd.Series(ml_datas['target'],dtype=str)

        ml_datas = X_ori.copy()

    return ml_datas
Exemple #10
0
 def MULT(self, name, **parameters):
     data = self.__data[name]
     return talib.MULT(data, **parameters)
Exemple #11
0
def main():
    # read csv file and transform it to datafeed (df):
    df = pd.read_csv(current_dir + "/" + base_dir + "/" + in_dir + "/" +
                     in_dir + '_' + stock_symbol + '.csv')

    # set numpy datafeed from df:
    df_numpy = {
        'date': np.array(df['date']),
        'open': np.array(df['open'], dtype='float'),
        'high': np.array(df['high'], dtype='float'),
        'low': np.array(df['low'], dtype='float'),
        'close': np.array(df['close'], dtype='float'),
        'volume': np.array(df['volume'], dtype='float')
    }

    date = df_numpy['date']
    openp = df_numpy['open']
    high = df_numpy['high']
    low = df_numpy['low']
    close = df_numpy['close']
    volume = df_numpy['volume']

    #########################################
    #####  Math Operator Functions ######
    #########################################

    #ADD - Vector Arithmetic Add
    add = ta.ADD(high, low)

    #DIV - Vector Arithmetic Div
    div = ta.DIV(high, low)

    #MAX - Highest value over a specified period
    maxv = ta.MAX(close, timeperiod=30)

    #MAXINDEX - Index of highest value over a specified period
    maxindex = ta.MAXINDEX(close, timeperiod=30)

    #MIN - Lowest value over a specified period
    minv = ta.MIN(close, timeperiod=30)

    #MININDEX - Index of lowest value over a specified period
    minindex = ta.MININDEX(close, timeperiod=30)

    #MINMAX - Lowest and highest values over a specified period
    minsp, maxsp = ta.MINMAX(close, timeperiod=30)

    #MINMAXINDEX - Indexes of lowest and highest values over a specified period
    minidx, maxidx = ta.MINMAXINDEX(close, timeperiod=30)

    #MULT - Vector Arithmetic Mult
    mult = ta.MULT(high, low)

    #SUB - Vector Arithmetic Substraction
    sub = ta.SUB(high, low)

    #SUM - Summation
    sum = ta.SUM(close, timeperiod=30)

    df_save = pd.DataFrame(
        data={
            'date': np.array(df['date']),
            'add': add,
            'div': div,
            'max': maxv,
            'maxindex': maxindex,
            'min': minv,
            'minindex': minindex,
            'min_spec_period': minsp,
            'max_spec_period': maxsp,
            'minidx': minidx,
            'maxidx': maxidx,
            'mult': mult,
            'sub': sub,
            'sum': sum
        })

    df_save.to_csv(current_dir + "/" + base_dir + "/" + out_dir + '/' +
                   stock_symbol + "/" + out_dir + '_ta_math_operator_' +
                   stock_symbol + '.csv',
                   index=False)