Skip to content

Instantly share code, notes, and snippets.

@yuyasugano
yuyasugano / Matplotlib Moving Average TaLib
Created August 31, 2019 03:20
Matplotlib Moving Average sample wih TaLib
def SMA_TaLib(df):
df1 = df.copy()
df1["ma5"] = talib.SMA(df1['close'], timeperiod=5)
df1["ma15"] = talib.SMA(df1['close'], timeperiod=15)
df1["diff"] = df1.ma5 - df1.ma15
df1["unixtime"] = [datetime.timestamp(t) for t in df1.index]
# line and Moving Average
fig = plt.figure(figsize=(15,5))
ax = fig.add_subplot(1, 1, 1)
@yuyasugano
yuyasugano / Matplotlib distribution for pct_change
Created September 11, 2019 12:58
Matplotlib distribution for pct_change
# pct_change
f = lambda x: 1 if x>0.0001 else -1 if x<-0.0001 else 0 if -0.0001<=x<=0.0001 else np.nan
y = df.rename(columns={'close': 'y'}).loc[:, 'y'].pct_change(1).shift(-1).fillna(0)
X = df.copy()
y_ = pd.DataFrame(y.map(f), columns=['y'])
df_ = pd.concat([df, y_], axis=1)
# check the shape
print('----------------------------------------------------------------------------------------')
print('X shape: (%i,%i)' % X.shape)
@yuyasugano
yuyasugano / Scikit-learn for OHLC data
Last active September 13, 2019 08:26
Scikit-learn for OHLC data sample with bitbank.cc API
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, f1_score
X_train, X_test, y_train, y_test = train_test_split(X_, y_, test_size=0.33, random_state=42)
print('X_train shape: {}'.format(X_train.shape))
@yuyasugano
yuyasugano / Technical index for features with Ta-Lib
Created October 1, 2019 11:41
Technical index for features with Ta-Lib
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
# Exponential Moving Average
@yuyasugano
yuyasugano / anaconda3 of continuumio
Created October 8, 2019 12:27
conda list for anaconda3 of continuumio
_ipyw_jlab_nb_ext_conf 0.1.0 py37_0
_libgcc_mutex 0.1 main
alabaster 0.7.12 py37_0
anaconda 2019.07 py37_0
anaconda-client 1.7.2 py37_0
anaconda-navigator 1.9.7 py37_0
anaconda-project 0.8.3 py_0
asn1crypto 0.24.0 py37_0
astroid 2.2.5 py37_0
astropy 3.2.1 py37h7b6447c_0
_ipyw_jlab_nb_ext_conf 0.1.0 py37_0
_libgcc_mutex 0.1 main
alabaster 0.7.12 py37_0
anaconda 2019.07 py37_0
anaconda-client 1.7.2 py37_0
anaconda-navigator 1.9.7 py37_0
anaconda-project 0.8.3 py_0
asn1crypto 0.24.0 py37_0
astroid 2.2.5 py37_0
astropy 3.2.1 py37h7b6447c_0
@yuyasugano
yuyasugano / Technical index for feature selection 54 columns
Created October 30, 2019 10:42
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
@yuyasugano
yuyasugano / Univariate Statistics
Created October 30, 2019 11:32
Univariate Statistics
# Univariate Statistics
from sklearn.feature_selection import SelectPercentile
select = SelectPercentile(percentile=25)
select.fit(X_train_full, y_train_full.values.ravel())
X_train_selected = select.transform(X_train_full)
X_test_selected = select.transform(X_test_full)
mask = select.get_support()
print(mask)
plt.matshow(mask.reshape(1, -1), cmap='gray_r')
plt.xlabel("Technical Indexes")
@yuyasugano
yuyasugano / Model-based Selection
Last active October 30, 2019 12:18
Model-based Selection
# Model-based Selection
from sklearn.feature_selection import SelectFromModel
select = SelectFromModel(RandomForestClassifier(n_estimators=100, random_state=42),
threshold="1.25*mean")
select.fit(X_train_full, y_train_full.values.ravel())
X_train_model = select.transform(X_train_full)
print(X_train_model.shape)
X_test_model = select.transform(X_test_full)
mask = select.get_support()
print(mask)
@yuyasugano
yuyasugano / Recursive Feature Elimination
Created October 30, 2019 12:27
Recursive Feature Elimination
# Recursive Feature Elimination
from sklearn.feature_selection import RFE
select = RFE(RandomForestClassifier(n_estimators=100, random_state=42),
n_features_to_select=15)
select.fit(X_train_full, y_train_full.values.ravel())
X_train_rfe = select.transform(X_train_full)
X_test_rfe = select.transform(X_test_full)
mask = select.get_support()
print(mask)
plt.matshow(mask.reshape(1, -1), cmap='gray_r')