Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yuyasugano/aa1510b82efbcff11a2a3281522c07e2 to your computer and use it in GitHub Desktop.
Save yuyasugano/aa1510b82efbcff11a2a3281522c07e2 to your computer and use it in GitHub Desktop.
feature selection for technical indexes 54 columns
# feature selection
open = pd.Series(df['open'])
high = pd.Series(df['high'])
low = pd.Series(df['low'])
close = pd.Series(df['close'])
volume = pd.Series(df['volume'])
# pct_change for new column
X['diff'] = y
## Overlap Studies Functions
# DEMA - Double Exponential Moving Average
dema = talib.DEMA(close, timeperiod=3)
dema = dema.fillna(dema.mean())
print('DEMA - Double Exponential Moving Average shape: {}'.format(dema.shape))
# EMA - Exponential Moving Average
ema = talib.EMA(close, timeperiod=3)
ema = ema.fillna(ema.mean())
print('EMA - Exponential Moving Average shape: {}'.format(ema.shape))
# HT_TRENDLINE - Hilbert Transform - Instantaneous Trendline
hilbert = talib.HT_TRENDLINE(close)
hilbert = hilbert.fillna(hilbert.mean())
print('HT_TRENDLINE - Hilbert Transform - Instantaneous Trendline shape: {}'.format(hilbert.shape))
# KAMA - Kaufman Adaptive Moving Average
kama = talib.KAMA(close, timeperiod=3)
kama = kama.fillna(kama.mean())
print('KAMA - Kaufman Adaptive Moving Average shape: {}'.format(kama.shape))
# MA - Moving average
ma = talib.MA(close, timeperiod=3, matype=0)
ma = ma.fillna(ma.mean())
print('MA - Moving average shape: {}'.format(kama.shape))
# MIDPOINT - MidPoint over period
midpoint = talib.MIDPOINT(close, timeperiod=7)
midpoint = midpoint.fillna(midpoint.mean())
print('MIDPOINT - MidPoint over period shape: {}'.format(midpoint.shape))
# MIDPRICE - Midpoint Price over period
midprice = talib.MIDPRICE(high, low, timeperiod=7)
midprice = midprice.fillna(midprice.mean())
print('MIDPRICE - Midpoint Price over period shape: {}'.format(midprice.shape))
# SAR - Parabolic SAR
sar = talib.SAR(high, low, acceleration=0, maximum=0)
sar = sar.fillna(sar.mean())
print('SAR - Parabolic SAR shape: {}'.format(sar.shape))
# SAREXT - Parabolic SAR - Extended
sarext = talib.SAREXT(high, low, startvalue=0, offsetonreverse=0, accelerationinitlong=0, accelerationlong=0, accelerationmaxlong=0, accelerationinitshort=0, accelerationshort=0, accelerationmaxshort=0)
sarext = sarext.fillna(sarext.mean())
print('SAREXT - Parabolic SAR - Extended shape: {}'.format(sarext.shape))
# SMA - Simple Moving Average
sma = talib.SMA(close, timeperiod=3)
sma = sma.fillna(sma.mean())
print('SMA - Simple Moving Average shape: {}'.format(sma.shape))
# T3 - Triple Exponential Moving Average (T3)
t3 = talib.T3(close, timeperiod=5, vfactor=0)
t3 = t3.fillna(t3.mean())
print('T3 - Triple Exponential Moving Average shape: {}'.format(t3.shape))
# TEMA - Triple Exponential Moving Average
tema = talib.TEMA(close, timeperiod=3)
tema = tema.fillna(tema.mean())
print('TEMA - Triple Exponential Moving Average shape: {}'.format(tema.shape))
# TRIMA - Triangular Moving Average
trima = talib.TRIMA(close, timeperiod=3)
trima = trima.fillna(trima.mean())
print('TRIMA - Triangular Moving Average shape: {}'.format(trima.shape))
# WMA - Weighted Moving Average
wma = talib.WMA(close, timeperiod=3)
wma = wma.fillna(wma.mean())
print('WMA - Weighted Moving Average shape: {}'.format(wma.shape))
## Momentum Indicator Functions
# ADX - Average Directional Movement Index
adx = talib.ADX(high, low, close, timeperiod=14)
adx = adx.fillna(adx.mean())
print('ADX - Average Directional Movement Index shape: {}'.format(adx.shape))
# ADXR - Average Directional Movement Index Rating
adxr = talib.ADXR(high, low, close, timeperiod=7)
adxr = adxr.fillna(adxr.mean())
print('ADXR - Average Directional Movement Index Rating shape: {}'.format(adxr.shape))
# APO - Absolute Price Oscillator
apo = talib.APO(close, fastperiod=12, slowperiod=26, matype=0)
apo = apo.fillna(apo.mean())
print('APO - Absolute Price Oscillator shape: {}'.format(apo.shape))
# AROONOSC - Aroon Oscillator
aroon = talib.AROONOSC(high, low, timeperiod=14)
aroon = aroon.fillna(aroon.mean())
print('AROONOSC - Aroon Oscillator shape: {}'.format(apo.shape))
# BOP - Balance Of Power
bop = talib.BOP(open, high, low, close)
bop = bop.fillna(bop.mean())
print('BOP - Balance Of Power shape: {}'.format(apo.shape))
# CCI - Commodity Channel Index
cci = talib.CCI(high, low, close, timeperiod=7)
cci = cci.fillna(cci.mean())
print('CCI - Commodity Channel Index shape: {}'.format(cci.shape))
# CMO - Chande Momentum Oscillator
cmo = talib.CMO(close, timeperiod=7)
cmo = cmo.fillna(cmo.mean())
print('CMO - Chande Momentum Oscillator shape: {}'.format(cmo.shape))
# DX - Directional Movement Index
dx = talib.DX(high, low, close, timeperiod=7)
dx = dx.fillna(dx.mean())
print('DX - Directional Movement Index shape: {}'.format(dx.shape))
# MFI - Money Flow Index
mfi = talib.MFI(high, low, close, volume, timeperiod=7)
mfi = mfi.fillna(mfi.mean())
print('MFI - Money Flow Index shape: {}'.format(mfi.shape))
# MINUS_DI - Minus Directional Indicator
minusdi = talib.MINUS_DI(high, low, close, timeperiod=14)
minusdi = minusdi.fillna(minusdi.mean())
print('MINUS_DI - Minus Directional Indicator shape: {}'.format(minusdi.shape))
# MINUS_DM - Minus Directional Movement
minusdm = talib.MINUS_DM(high, low, timeperiod=14)
minusdm = minusdm.fillna(minusdm.mean())
print('MINUS_DM - Minus Directional Movement shape: {}'.format(minusdm.shape))
# MOM - Momentum
mom = talib.MOM(close, timeperiod=5)
mom = mom.fillna(mom.mean())
print('MOM - Momentum shape: {}'.format(mom.shape))
# PLUS_DI - Plus Directional Indicator
plusdi = talib.PLUS_DI(high, low, close, timeperiod=14)
plusdi = plusdi.fillna(plusdi.mean())
print('PLUS_DI - Plus Directional Indicator shape: {}'.format(plusdi.shape))
# PLUS_DM - Plus Directional Movement
plusdm = talib.PLUS_DM(high, low, timeperiod=14)
plusdm = plusdm.fillna(plusdm.mean())
print('PLUS_DM - Plus Directional Movement shape: {}'.format(plusdi.shape))
# PPO - Percentage Price Oscillator
ppo = talib.PPO(close, fastperiod=12, slowperiod=26, matype=0)
ppo = ppo.fillna(ppo.mean())
print('PPO - Percentage Price Oscillator shape: {}'.format(ppo.shape))
# ROC - Rate of change:((price/prevPrice)-1)*100
roc = talib.ROC(close, timeperiod=10)
roc = roc.fillna(roc.mean())
print('ROC - Rate of change : ((price/prevPrice)-1)*100 shape: {}'.format(roc.shape))
# RSI - Relative Strength Index
rsi = talib.RSI(close, timeperiod=14)
rsi = rsi.fillna(rsi.mean())
print('RSI - Relative Strength Index shape: {}'.format(rsi.shape))
# TRIX - 1-day Rate-Of-Change (ROC) of a Triple Smooth EMA
trix = talib.TRIX(close, timeperiod=30)
trix = trix.fillna(trix.mean())
print('TRIX - 1-day Rate-Of-Change (ROC) of a Triple Smooth EMA shape: {}'.format(trix.shape))
# ULTOSC - Ultimate Oscillator
ultosc = talib.ULTOSC(high, low, close, timeperiod1=7, timeperiod2=14, timeperiod3=28)
ultosc = ultosc.fillna(ultosc.mean())
print('ULTOSC - Ultimate Oscillator shape: {}'.format(ultosc.shape))
# WILLR - Williams'%R
willr = talib.WILLR(high, low, close, timeperiod=7)
willr = willr.fillna(willr.mean())
print("WILLR - Williams'%R shape: {}".format(willr.shape))
## Volume Indicator Functions
# AD - Chaikin A/D Line
ad = talib.AD(high, low, close, volume)
ad = ad.fillna(ad.mean())
print('AD - Chaikin A/D Line shape: {}'.format(ad.shape))
# ADOSC - Chaikin A/D Oscillator
adosc = talib.ADOSC(high, low, close, volume, fastperiod=3, slowperiod=10)
adosc = adosc.fillna(adosc.mean())
print('ADOSC - Chaikin A/D Oscillator shape: {}'.format(adosc.shape))
# OBV - On Balance Volume
obv = talib.OBV(close, volume)
obv = obv.fillna(obv.mean())
print('OBV - On Balance Volume shape: {}'.format(obv.shape))
## Volatility Indicator Functions
# ATR - Average True Range
atr = talib.ATR(high, low, close, timeperiod=7)
atr = atr.fillna(atr.mean())
print('ATR - Average True Range shape: {}'.format(atr.shape))
# NATR - Normalized Average True Range
natr = talib.NATR(high, low, close, timeperiod=7)
natr = natr.fillna(natr.mean())
print('NATR - Normalized Average True Range shape: {}'.format(natr.shape))
# TRANGE - True Range
trange = talib.TRANGE(high, low, close)
trange = trange.fillna(trange.mean())
print('TRANGE - True Range shape: {}'.format(natr.shape))
## Price Transform Functions
# AVGPRICE - Average Price
avg = talib.AVGPRICE(open, high, low, close)
avg = avg.fillna(avg.mean())
print('AVGPRICE - Average Price shape: {}'.format(natr.shape))
# MEDPRICE - Median Price
medprice = talib.MEDPRICE(high, low)
medprice = medprice.fillna(medprice.mean())
print('MEDPRICE - Median Price shape: {}'.format(medprice.shape))
# TYPPRICE - Typical Price
typ = talib.TYPPRICE(high, low, close)
typ = typ.fillna(typ.mean())
print('TYPPRICE - Typical Price shape: {}'.format(typ.shape))
# WCLPRICE - Weighted Close Price
wcl = talib.WCLPRICE(high, low, close)
wcl = wcl.fillna(wcl.mean())
print('WCLPRICE - Weighted Close Price shape: {}'.format(wcl.shape))
## Cycle Indicator Functions
# HT_DCPERIOD - Hilbert Transform - Dominant Cycle Period
dcperiod = talib.HT_DCPERIOD(close)
dcperiod = dcperiod.fillna(dcperiod.mean())
print('HT_DCPERIOD - Hilbert Transform - Dominant Cycle Period shape: {}'.format(dcperiod.shape))
# HT_DCPHASE - Hilbert Transform - Dominant Cycle Phase
dcphase = talib.HT_DCPHASE(close)
dcphase = dcphase.fillna(dcphase.mean())
print('HT_DCPHASE - Hilbert Transform - Dominant Cycle Phase shape: {}'.format(dcperiod.shape))
## Statistic Functions
# BETA - Beta
beta = talib.BETA(high, low, timeperiod=3)
beta = beta.fillna(beta.mean())
print('BETA - Beta shape: {}'.format(beta.shape))
# CORREL - Pearson's Correlation Coefficient(r)
correl = talib.CORREL(high, low, timeperiod=30)
correl = correl.fillna(correl.mean())
print("CORREL - Pearson's Correlation Coefficient(r) shape: {}".format(beta.shape))
# LINEARREG - Linear Regression
linreg = talib.LINEARREG(close, timeperiod=7)
linreg = linreg.fillna(linreg.mean())
print("LINEARREG - Linear Regression shape: {}".format(linreg.shape))
# STDDEV - Standard Deviation
stddev = talib.STDDEV(close, timeperiod=5, nbdev=1)
stddev = stddev.fillna(stddev.mean())
print("STDDEV - Standard Deviation shape: {}".format(stddev.shape))
# TSF - Time Series Forecast
tsf = talib.TSF(close, timeperiod=7)
tsf = tsf.fillna(tsf.mean())
print("TSF - Time Series Forecast shape: {}".format(tsf.shape))
# VAR - Variance
var = talib.VAR(close, timeperiod=5, nbdev=1)
var = var.fillna(var.mean())
print("VAR - Variance shape: {}".format(var.shape))
## Feature DataFrame
X_full = pd.concat([X, dema, ema, hilbert, kama, ma, midpoint, midprice, sar, sarext, sma, t3, tema, trima, wma, adx, adxr,
apo, aroon, bop, cci, cmo, mfi, minusdi, minusdm, mom, plusdi, plusdm, ppo, roc, rsi, trix, ultosc, willr,
ad, adosc, obv, atr, natr, trange, avg, medprice, typ, wcl, dcperiod, dcphase, beta, correl, linreg, stddev,
tsf, var], axis=1).drop(['open', 'high', 'low', 'close'], axis=1)
X_full.columns = ['volume','diff', 'dema', 'ema', 'hilbert', 'kama', 'ma', 'midpoint', 'midprice', 'sar', 'sarext', 'sma',
't3', 'tema', 'trima', 'wma', 'adx', 'adxr', 'apo', 'aroon', 'bop', 'cci', 'cmo', 'mfi', 'minusdi', 'minusdm',
'mom', 'plusdi', 'plusdm', 'ppo', 'roc', 'rsi', 'trix', 'ultosc', 'willr', 'ad', 'adosc', 'obv', 'atr', 'natr',
'trange', 'avg', 'medprice', 'typ', 'wcl', 'dcperiod', 'dcphase', 'beta', 'correl', 'linreg', 'stddev', 'tsf', 'var']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment