{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":30894,"databundleVersionId":3147400}],"dockerImageVersionId":31328,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-04-17T09:35:18.223535Z","iopub.execute_input":"2026-04-17T09:35:18.223927Z","iopub.status.idle":"2026-04-17T09:35:18.601526Z","shell.execute_reply.started":"2026-04-17T09:35:18.223894Z","shell.execute_reply":"2026-04-17T09:35:18.600282Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\n# Veri yolunu belirleyelim\ndata_path = \"/kaggle/input/competitions/g-research-crypto-forecasting/train.csv\"\n\n# Veriyi parça parça (chunk) okuyup sadece BTC'yi ayıklıyoruz\n# Bu yöntem 2.8 GB'lık dosyayı RAM'i şişirmeden işlemeni sağlar\nchunks = pd.read_csv(data_path, chunksize=1000000)\nbtc_df = pd.concat([chunk[chunk['Asset_ID'] == 1] for chunk in chunks])\n\n# Zaman damgasını (timestamp) okunabilir tarihe çevirelim\nbtc_df['datetime'] = pd.to_datetime(btc_df['timestamp'], unit='s')\nbtc_df = btc_df.set_index('datetime')\n\nprint(f\"BTC verisi başarıyla yüklendi. Satır sayısı: {len(btc_df)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T09:35:19.986345Z","iopub.execute_input":"2026-04-17T09:35:19.986821Z","iopub.status.idle":"2026-04-17T09:36:06.645334Z","shell.execute_reply.started":"2026-04-17T09:35:19.986788Z","shell.execute_reply":"2026-04-17T09:36:06.644104Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Eksik dakikaları kontrol et ve doldur\n# reindex yaparak eksik dakikaları \"NaN\" olarak ekliyoruz, sonra dolduruyoruz\nbtc_df = btc_df.reindex(pd.date_range(start=btc_df.index.min(), end=btc_df.index.max(), freq='min'))\n\n# Fiyat verilerini bir önceki satırla doldur (forward fill)\n# Target kısmını ise 0 ile doldurabiliriz (çünkü o an işlem yoksa getiri de yoktur)\nbtc_df[['Open', 'High', 'Low', 'Close', 'Volume', 'VWAP']] = btc_df[['Open', 'High', 'Low', 'Close', 'Volume', 'VWAP']].ffill()\nbtc_df['Target'] = btc_df['Target'].fillna(0)\nbtc_df['Asset_ID'] = 1 # Reindex sonrası boşalan ID'leri düzeltelim\n\nprint(\"Eksik veriler temizlendi.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T09:36:06.647373Z","iopub.execute_input":"2026-04-17T09:36:06.647733Z","iopub.status.idle":"2026-04-17T09:36:07.398493Z","shell.execute_reply.started":"2026-04-17T09:36:06.647702Z","shell.execute_reply":"2026-04-17T09:36:07.396994Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 1. Önce WaveTrend Fonksiyonunu tanımlıyoruz (Önceki mesajdaki kod)\ndef calculate_wavetrend(df, n1=10, n2=21):\n    ap = (df['High'] + df['Low'] + df['Close']) / 3\n    esa = ap.ewm(span=n1, adjust=False).mean()\n    d = (ap - esa).abs().ewm(span=n1, adjust=False).mean()\n    ci = (ap - esa) / (0.015 * d + 1e-10)\n    wt1 = ci.ewm(span=n2, adjust=False).mean()\n    wt2 = wt1.rolling(window=4).mean()\n    return wt1, wt2\n\n# 2. Özellikleri Veri Setine Ekliyoruz\n# WaveTrend\nbtc_df['wt1'], btc_df['wt2'] = calculate_wavetrend(btc_df)\nbtc_df['wt_diff'] = btc_df['wt1'] - btc_df['wt2']\n\n# Ekstra Özellikler (Modelin daha iyi öğrenmesi için)\nbtc_df['log_ret'] = np.log(btc_df['Close'] / btc_df['Close'].shift(1)) # 1 dakikalık getiri\nbtc_df['volatility'] = btc_df['log_ret'].rolling(window=15).std() # 15 dk'lık oynaklık\nbtc_df['upper_shadow'] = btc_df['High'] - np.maximum(btc_df['Close'], btc_df['Open'])\nbtc_df['lower_shadow'] = np.minimum(btc_df['Close'], btc_df['Open']) - btc_df['Low']\n\n# 3. NaN Değerlerden Kurtulma (Hesaplamalar yüzünden oluşan ilk satırlar)\nbtc_df.dropna(inplace=True)\n\nprint(\"Özellikler başarıyla eklendi!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T09:36:07.399764Z","iopub.execute_input":"2026-04-17T09:36:07.400198Z","iopub.status.idle":"2026-04-17T09:36:08.011822Z","shell.execute_reply.started":"2026-04-17T09:36:07.400129Z","shell.execute_reply":"2026-04-17T09:36:08.010484Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Hangi sütunları model kullanacak?\nfeatures = ['Count', 'Open', 'High', 'Low', 'Close', 'Volume', 'VWAP', \n            'wt1', 'wt2', 'wt_diff', 'log_ret', 'volatility', 'upper_shadow', 'lower_shadow']\n\n# X ve y belirleme\nX = btc_df[features]\ny = btc_df['Target']\n\n# Zaman bazlı bölme (Örn: Son %20 test olsun)\nsplit = int(len(btc_df) * 0.8)\nX_train, X_test = X.iloc[:split], X.iloc[split:]\ny_train, y_test = y.iloc[:split], y.iloc[split:]\n\nprint(f\"Eğitim seti: {len(X_train)} | Test seti: {len(X_test)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T09:36:17.734806Z","iopub.execute_input":"2026-04-17T09:36:17.735275Z","iopub.status.idle":"2026-04-17T09:36:17.823483Z","shell.execute_reply.started":"2026-04-17T09:36:17.735238Z","shell.execute_reply":"2026-04-17T09:36:17.822340Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import lightgbm as lgb\n# 1. LAG FEATURES (Modelin geçmişi görmesi için en önemli adım)\n# Model \"az önce ne oldu?\" sorusuna cevap bulmalı\nX_train = X_train.copy()\nX_test = X_test.copy()\n\n# Son 1, 2 ve 3 dakikanın target değerlerini ekliyoruz (Modelin hafızası)\nX_train['lag_target_1'] = y_train.shift(1)\nX_train['lag_target_2'] = y_train.shift(2)\nX_test['lag_target_1'] = y_test.shift(1)\nX_test['lag_target_2'] = y_test.shift(2)\n\n# İlk satırlardaki boşlukları doldur\nX_train.fillna(0, inplace=True)\nX_test.fillna(0, inplace=True)\n\n# 2. TARGET SCALING (Modelin rakamları fark etmesi için)\nmultiplier = 1000\ny_train_scaled = y_train * multiplier\ny_test_scaled = y_test * multiplier\n\n# 3. LIGHTGBM DATASET (Yeni ölçekli verilerle)\ntrain_data = lgb.Dataset(X_train, label=y_train_scaled)\ntest_data = lgb.Dataset(X_test, label=y_test_scaled, reference=train_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T09:36:46.320293Z","iopub.execute_input":"2026-04-17T09:36:46.321590Z","iopub.status.idle":"2026-04-17T09:36:51.633609Z","shell.execute_reply.started":"2026-04-17T09:36:46.321546Z","shell.execute_reply":"2026-04-17T09:36:51.632498Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import lightgbm as lgb\nfrom sklearn.metrics import mean_squared_error\nfrom scipy.stats import pearsonr\nimport matplotlib.pyplot as plt\n\n# 1. Model Parametreleri\nparams = {\n    'objective': 'regression',\n    'metric': 'rmse',\n    'boosting_type': 'gbdt',\n    'learning_rate': 0.01, # Daha yavaş ve dikkatli öğrenme\n    'num_leaves': 63,      # Daha karmaşık desenleri çözebilmesi için artırdık\n    'feature_fraction': 0.8,\n    'lambda_l1': 0.1,      # Aşırı öğrenmeyi (overfitting) engellemek için\n    'lambda_l2': 0.1,\n    'verbose': -1\n}\n\n# 2. Modeli Eğitme\nprint(\"Model eğitiliyor...\")\nmodel = lgb.train(\n    params,\n    train_data,\n    valid_sets=[train_data, test_data],\n    valid_names=['train', 'valid'],\n    num_boost_round=1000,\n    callbacks=[lgb.early_stopping(stopping_rounds=50)]\n)\n\n# 3. Tahminler\ny_pred = model.predict(X_test)/1000\n\n# 4. Metriklerin Hesaplanması\nrmse = np.sqrt(mean_squared_error(y_test, y_pred))\ncorrelation, _ = pearsonr(y_test, y_pred)\n\n# Yön Doğruluğu (Yani model artış dediğinde gerçekten arttı mı?)\nactual_direction = np.sign(y_test)\npred_direction = np.sign(y_pred)\ndirection_acc = (actual_direction == pred_direction).mean()\n\nprint(\"\\n--- MODEL PERFORMANSI ---\")\nprint(f\"RMSE: {rmse:.6f}\")\nprint(f\"Korelasyon: {correlation:.4f} (1'e ne kadar yakınsa o kadar iyi)\")\nprint(f\"Yön Doğruluğu: %{direction_acc*100:.2f} (Piyasanın yönünü bilme oranı)\")\n\n# 5. Görselleştirme: Son 4 Saatlik Kesit ve Sinyaller\nplot_df = X_test.tail(240).copy()\nplot_df['actual_target'] = y_test.tail(240).values\nplot_df['pred_target'] = y_pred[-240:]\n\nplt.figure(figsize=(15, 10))\n\n# Üst Grafik: Tahmin Tutarlılığı\nplt.subplot(2, 1, 1)\nplt.plot(plot_df.index, plot_df['actual_target'], label='Gerçek Target', color='blue', alpha=0.4)\nplt.plot(plot_df.index, plot_df['pred_target'], label='Tahmin Target', color='orange', linewidth=2)\nplt.axhline(0, color='black', linestyle='--', alpha=0.3)\nplt.title('Son 4 Saat: Model Tahmin Performansı')\nplt.legend()\n\n# Alt Grafik: WaveTrend + Model Onaylı Sinyaller\nplt.subplot(2, 1, 2)\nplt.plot(plot_df.index, plot_df['wt1'], label='WT1 (Momentum)', color='green', alpha=0.7)\nplt.plot(plot_df.index, plot_df['wt2'], label='WT2 (Ortalama)', color='red', linestyle='--', alpha=0.7)\n\n# --- SİNYAL MANTIĞI ---\n# AL: Model (+) ve WT yukarı kesmiş\nbuy_cond = (plot_df['pred_target'] > 0) & (plot_df['wt1'] > plot_df['wt2']) & (plot_df['wt1'].shift(1) <= plot_df['wt2'].shift(1))\n# SAT: Model (-) ve WT aşağı kesmiş\nsell_cond = (plot_df['pred_target'] < 0) & (plot_df['wt1'] < plot_df['wt2']) & (plot_df['wt1'].shift(1) >= plot_df['wt2'].shift(1))\n\nbuy_signals = plot_df[buy_cond]\nsell_signals = plot_df[sell_cond]\n\n# Sinyalleri Grafiğe Basma\nplt.scatter(buy_signals.index, buy_signals['wt1'], marker='^', color='lime', s=150, label='GÜÇLÜ AL (Long)', edgecolors='black')\nplt.scatter(sell_signals.index, sell_signals['wt1'], marker='v', color='red', s=150, label='GÜÇLÜ SAT (Short)', edgecolors='black')\n\nplt.axhline(60, color='gray', linestyle=':', alpha=0.5)  # Overbought\nplt.axhline(-60, color='gray', linestyle=':', alpha=0.5) # Oversold\nplt.title('WaveTrend ve Model Stratejisi (Al-Sat Noktaları)')\nplt.legend()\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T09:36:58.176911Z","iopub.execute_input":"2026-04-17T09:36:58.177330Z","iopub.status.idle":"2026-04-17T09:38:06.475063Z","shell.execute_reply.started":"2026-04-17T09:36:58.177292Z","shell.execute_reply":"2026-04-17T09:38:06.473977Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Son 4 saatteki sinyalleri tablo olarak dök\nsummary_signals = plot_df[buy_cond | sell_cond][['Close', 'wt1', 'wt2', 'pred_target']]\nsummary_signals['Tipi'] = np.where(summary_signals['pred_target'] > 0, 'AL', 'SAT')\nprint(summary_signals.tail(10))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T09:38:17.677056Z","iopub.execute_input":"2026-04-17T09:38:17.677984Z","iopub.status.idle":"2026-04-17T09:38:17.694715Z","shell.execute_reply.started":"2026-04-17T09:38:17.677945Z","shell.execute_reply":"2026-04-17T09:38:17.693534Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import joblib\n\n# Modeli dışarıya aktarılabilir bir dosya olarak kaydediyoruz\n\njoblib.dump(model, 'lgbm_btc_model.pkl')\n\nprint(\"Model başarıyla kaydedildi! Sol taraftaki 'Output' kısmından indirebilirsin.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T09:38:18.995873Z","iopub.execute_input":"2026-04-17T09:38:18.996258Z","iopub.status.idle":"2026-04-17T09:38:19.095586Z","shell.execute_reply.started":"2026-04-17T09:38:18.996223Z","shell.execute_reply":"2026-04-17T09:38:19.094343Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Kaggle kodunun en sonuna bunu ekle:\n# 'final_df' senin tüm indikatörlerin (WaveTrend vb.) ve model tahminlerinin olduğu tablo olsun\nfinal_output = final_df[['timestamp', 'Close', 'wt1', 'wt2', 'prediction']].copy()\n\n# Bunu CSV olarak kaydediyoruz ki Streamlit \"Bak bu tarihte fiyat buydu, sinyal de buydu\" diyebilsin\nfinal_output.to_csv('streamlit_data.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T13:04:52.786395Z","iopub.execute_input":"2026-04-17T13:04:52.786579Z","iopub.status.idle":"2026-04-17T13:04:52.800448Z","shell.execute_reply.started":"2026-04-17T13:04:52.786558Z","shell.execute_reply":"2026-04-17T13:04:52.799167Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}