<jupyter_start><jupyter_text>**Add targets**The target to be predicted in the $i^{th}$ day is calculated as follows:$$target_i = sign(P_{i+d} - P_i)$$Where $d$ is the number of days after which the prediction is to be made<jupyter_code>df['label'] = df.close.shift(-1) - df.close
df.label = df.label.apply(lambda x: 0 if x < 0 else 1)
df.head()<jupyter_output><empty_output><jupyter_text>**Features: Technical Indicators**<jupyter_code># Momentum indicators
df['roc'] = ta.momentum.roc(close=df.close) # Rate of Change (ROC)
df['rsi'] = ta.momentum.rsi(close=df.close) # Relative Strength Index (RSI)
df['tsi'] = ta.momentum.tsi(close=df.close) # True strength index (TSI)

# Volatility indicators
bb_indicator = ta.volatility.BollingerBands(close=df.close)
df['bb_bbhi'] = bb_indicator.bollinger_hband_indicator() # Bollinger Band high indicator
df['bb_bbli'] = bb_indicator.bollinger_lband_indicator() # Bollinger Band low indicator

# Trend indicators
aroon_indicator = ta.trend.AroonIndicator(close=df.close)
macd_indicator = ta.trend.MACD(close=df.close)
kst_indicator = ta.trend.KSTIndicator(close=df.close)
df['aroon_down'] = aroon_indicator.aroon_down() # Aroon Down Channel
df['aroon'] = aroon_indicator.aroon_indicator() # Aroon Indicator
df['aroon_up'] = aroon_indicator.aroon_up() # Aroon Up Channel
df['macd_line'] = macd_indicator.macd() # MACD Line
df['macd_hist'] = macd_indicator.macd_diff() # MACD Histogram
df['macd_signal'] = macd_indicator.macd_signal() # MACD Signal Line
df['kst'] = kst_indicator.kst() # Know Sure Thing (KST)
df['kst_diff'] = kst_indicator.kst_diff() # Diff Know Sure Thing (KST)
df['kst_signal'] = kst_indicator.kst_sig() # Signal Line Know Sure Thing (KST)
df['dpo'] = ta.trend.dpo(close=df.close) # Detrended Price Oscillator (DPO)
df['trix'] = ta.trend.trix(close=df.close) # Trix (TRIX)
df['sma_10'] = ta.trend.sma_indicator(close=df.close, window=10) # SMA n=10
df['sma_20'] = ta.trend.sma_indicator(close=df.close, window=20) # SMA n=20
df['sma_30'] = ta.trend.sma_indicator(close=df.close, window=30) # SMA n=30
df['sma_60'] = ta.trend.sma_indicator(close=df.close, window=60) # SMA n=60
df['ema_10'] = ta.trend.sma_indicator(close=df.close, window=10) # EMA n=10
df['ema_20'] = ta.trend.sma_indicator(close=df.close, window=20) # EMA n=20
df['ema_30'] = ta.trend.sma_indicator(close=df.close, window=30) # EMA n=30
df['ema_60'] = ta.trend.sma_indicator(close=df.close, window=60) # EMA n=60

# Volume indicators
df['obv'] = ta.volume.on_balance_volume(close=df.close, volume=df.volume) # On Balance Volume (OBV)
df['vpt'] = ta.volume.volume_price_trend(close=df.close, volume=df.volume) # Volume-price trend (VPT)
df['fi'] = ta.volume.force_index(close=df.close, volume=df.volume) # Force Index (FI)
df['nvi'] = ta.volume.negative_volume_index(close=df.close, volume=df.volume) # Negative Volume Index (NVI)

df.tail()<jupyter_output><empty_output><jupyter_text>**Features: Datetime cyclical encoding**$$x_{sin} = \sin(\frac{2*\pi*x}{max(x)})$$$$x_{cos} = \cos(\frac{2*\pi*x}{max(x)})$$<jupyter_code>df['datetime'] = df.index.to_pydatetime()
df['day_sin'] = np.sin(2 * np.pi * df.datetime.dt.day / 30)
df['day_cos'] = np.cos(2 * np.pi * df.datetime.dt.day / 30)
df['month_sin'] = np.sin(2 * np.pi * df.datetime.dt.month / 12)
df['month_cos'] = np.cos(2 * np.pi * df.datetime.dt.month / 12)

df = df.drop(['datetime'], axis=1)
print(df.shape)
df.tail()
df_na = df.dropna(axis=0)
print(df_na.shape)
df_na.head()
labels = df_na.label
df_na = df_na.drop(['label'], axis=1)<jupyter_output><empty_output><jupyter_text>**Writing data to csv**<jupyter_code>df_na.to_csv('amzn_cleaned')
df_na.columns<jupyter_output><empty_output><jupyter_text>**Splitting the dataset**<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(df_na.values, labels.values, test_size=0.05, random_state=42)
X_train.shape, X_test.shape, y_train.shape, y_test.shape<jupyter_output><empty_output><jupyter_text>**Scaling**<jupyter_code>scaler = StandardScaler()
scaler.fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)<jupyter_output><empty_output><jupyter_text>**Principal Component Analysis (PCA)**<jupyter_code>pca = PCA(n_components=0.8, random_state=42)
pca.fit(X_train_scaled)
X_train_pca = pca.transform(X_train_scaled)
X_test_pca = pca.transform(X_test_scaled)
X_train_pca.shape, X_test_pca.shape<jupyter_output><empty_output><jupyter_text>**Logistic Regression**<jupyter_code>model = LogisticRegression()
model.fit(X_train_pca, y_train)
y_pred = model.predict_proba(X_test_pca)
acc = accuracy_score(y_test, y_pred.argmax(axis=1))
f1 = f1_score(y_test, y_pred.argmax(axis=1))
roc = roc_auc_score(y_test, y_pred[:, 1])
print(f'LogisticRegression: acc={acc*100:.2f}% - f1={f1*100:.2f}% - roc={roc*100:.2f}%')<jupyter_output>LogisticRegression: acc=49.81% - f1=61.63% - roc=52.73%<jupyter_text>**Decision Trees**<jupyter_code>model = DecisionTreeClassifier()
model.fit(X_train_pca, y_train)
y_pred = model.predict_proba(X_test_pca)
acc = accuracy_score(y_test, y_pred.argmax(axis=1))
f1 = f1_score(y_test, y_pred.argmax(axis=1))
roc = roc_auc_score(y_test, y_pred[:, 1])
print(f'LogisticRegression: acc={acc*100:.2f}% - f1={f1*100:.2f}% - roc={roc*100:.2f}%')<jupyter_output>LogisticRegression: acc=54.37% - f1=55.56% - roc=54.34%<jupyter_text>**Random Forest**<jupyter_code>model = RandomForestClassifier()
model.fit(X_train_pca, y_train)
y_pred = model.predict_proba(X_test_pca)
acc = accuracy_score(y_test, y_pred.argmax(axis=1))
f1 = f1_score(y_test, y_pred.argmax(axis=1))
roc = roc_auc_score(y_test, y_pred[:, 1])
print(f'RandomForestClassifier: acc={acc*100:.2f}% - f1={f1*100:.2f}% - roc={roc*100:.2f}%')<jupyter_output>RandomForestClassifier: acc=52.09% - f1=53.68% - roc=51.85%<jupyter_text>**Gradient Boosting**<jupyter_code>model = GradientBoostingClassifier()
model.fit(X_train_pca, y_train)
y_pred = model.predict_proba(X_test_pca)
acc = accuracy_score(y_test, y_pred.argmax(axis=1))
f1 = f1_score(y_test, y_pred.argmax(axis=1))
roc = roc_auc_score(y_test, y_pred[:, 1])
print(f'GradientBoostingClassifier: acc={acc*100:.2f}% - f1={f1*100:.2f}% - roc={roc*100:.2f}%')<jupyter_output>GradientBoostingClassifier: acc=55.89% - f1=62.82% - roc=53.48%
{'fraction_non_alphanumeric': 0.17326494630549769, 'fraction_numerical': 0.03301811187690335, 'mean_word_length': 7.595041322314049, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '308567', 'license': 'MIT', 'n_tokens_mistral': 2794, 'n_tokens_neox': 2646, 'n_words': 382, 'path': 'feature-engineering/Complete Feature Engineering & Modelling v2.ipynb', 'repo_name': 'anjali-chauhan/dc_stock_portfolio_generator', 'size': '6239'}