iT邦幫忙

0

Day26移除極端平衡,導入機率濾網

d26
  • 分享至 

  • xImage
  •  

移除 class_weight='balanced' 參數,讓模型學習數據原本的偏斜狀態。
在 backtest_strategy 中啟用信心閾值。
同時修正一下空頭信號的浪費,一個更穩健的空頭濾網應該是當模型極度不看好多頭,且 RSI 進入超買區域時。

for i in range(1, len(df_test)):
        price_now = df_test["close"].iloc[i]
        rsi = np.nan_to_num(df_test["RSI"].iloc[i], nan=50)

        proba = df_test["Proba"].iloc[i - 1]

        atr = np.nan_to_num(df_test["ATR"].iloc[i], nan=0)

        # -------------------
        # 1️⃣ 進場邏輯
        # -------------------
        if position is None:
            if proba >= confidence_threshold and rsi > rsi_long_entry:
                position = "long"
                entry_price = price_now
                entry_capital = balance * position_size_ratio
                entry_units = entry_capital / entry_price
                balance -= entry_capital * fee_rate  # 手續費
                if debug:
                    print(f"[BUY] @ {price_now:.2f}, Proba={proba:.2f}")

            elif (1 - proba) >= confidence_threshold and rsi < rsi_short_entry:
                position = "short"
                entry_price = price_now
                entry_capital = balance * position_size_ratio
                entry_units = entry_capital / entry_price
                balance -= entry_capital * fee_rate
                if debug:
                    print(f"[SELL] @ {price_now:.2f}, Proba={proba:.2f}")

以下是修正後的結果
https://ithelp.ithome.com.tw/upload/images/20251014/20178967EhBygsSyoi.png
https://ithelp.ithome.com.tw/upload/images/20251014/20178967UBBuQDhiox.png
召回率大幅回升! 模型不再極端保守,敢於識別上漲信號了。
但是精確度下降。而這是預期行為,因為模型現在更常猜測,所以猜錯的比例增加。
下一步就是要提升精確度


圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言