Files
MidasBot/freqtrade/user_data/strategies/IchimokuChop.py
jerem 633b033f4d MidasBot: bot trading crypto IA + stratégies Ichimoku validées
- Infra: Freqtrade (futures dry-run) + Redis + dashboard + Docker Compose
- Couche IA: ai_analyzer (Claude via abonnement, MCP TradingView, backfill biais)
- Stratégies: SampleStrategy, AiBiasStrategy, IchimokuLS (long/short, validée
  train/test + données vierges + walk-forward), MTFIchimoku, variantes hyperopt
- Arbitrage CEX (dry-run), backtesting, walk-forward, volatility targeting
- IchimokuLS en dry-run live (config_live.json)

Claude-Session: https://claude.ai/code/session_01VHETcFacdnDhQzthLpdYFR
2026-06-23 19:25:49 +02:00

116 lines
4.8 KiB
Python

# pragma pylint: disable=missing-docstring, invalid-name, too-few-public-methods
"""
IchimokuChop — IchimokuHyper + FILTRE DE RÉGIME (Choppiness Index).
Hypothèse : les folds perdants du walk-forward sont des marchés en RANGE (chop),
où un trend-follower se fait whipsaw. On ajoute un filtre Choppiness Index :
- CI élevé (>~61) = consolidation/range → on NE trade PAS.
- CI bas (<~38) = tendance forte → on trade.
Le seuil est optimisable. On valide ensuite en WALK-FORWARD (pas en hindsight).
"""
from __future__ import annotations
import numpy as np
import talib.abstract as ta
from pandas import DataFrame
from freqtrade.strategy import (
IStrategy,
IntParameter,
DecimalParameter,
BooleanParameter,
)
class IchimokuChop(IStrategy):
INTERFACE_VERSION = 3
timeframe = "1h"
can_short = True
minimal_roi = {"0": 0.08, "240": 0.04, "720": 0.02, "1440": 0}
stoploss = -0.08
trailing_stop = True
trailing_stop_positive = 0.02
trailing_stop_positive_offset = 0.03
trailing_only_offset_is_reached = True
startup_candle_count: int = 220
process_only_new_candles = True
use_exit_signal = True
buy_adx_min = IntParameter(15, 40, default=25, space="buy", optimize=True)
buy_cloud_min_pct = DecimalParameter(0.0, 2.0, default=0.3, decimals=2, space="buy", optimize=True)
require_tk_cross = BooleanParameter(default=False, space="buy", optimize=True)
# Filtre de régime : ne trade que si Choppiness Index < seuil (= marché qui tend)
buy_chop_max = IntParameter(35, 70, default=61, space="buy", optimize=True)
def leverage(self, pair, current_time, current_rate, proposed_leverage,
max_leverage, entry_tag, side, **kwargs) -> float:
return 1.0
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
high, low, close = dataframe["high"], dataframe["low"], dataframe["close"]
tenkan = (high.rolling(9).max() + low.rolling(9).min()) / 2
kijun = (high.rolling(26).max() + low.rolling(26).min()) / 2
dataframe["tenkan"] = tenkan
dataframe["kijun"] = kijun
dataframe["senkou_a"] = ((tenkan + kijun) / 2).shift(26)
dataframe["senkou_b"] = ((high.rolling(52).max() + low.rolling(52).min()) / 2).shift(26)
dataframe["cloud_top"] = dataframe[["senkou_a", "senkou_b"]].max(axis=1)
dataframe["cloud_bot"] = dataframe[["senkou_a", "senkou_b"]].min(axis=1)
dataframe["cloud_width_pct"] = (dataframe["cloud_top"] - dataframe["cloud_bot"]) / close * 100
dataframe["close_prev26"] = close.shift(26)
dataframe["adx"] = ta.ADX(dataframe, timeperiod=14)
# --- Choppiness Index (14) ---
n = 14
atr1 = ta.ATR(dataframe, timeperiod=1)
tr_sum = atr1.rolling(n).sum()
rng = high.rolling(n).max() - low.rolling(n).min()
dataframe["chop"] = 100 * np.log10(tr_sum / rng) / np.log10(n)
return dataframe
def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
adx_min = self.buy_adx_min.value
cloud_min = self.buy_cloud_min_pct.value
not_choppy = dataframe["chop"] < self.buy_chop_max.value # ← filtre régime
long_cond = (
(dataframe["close"] > dataframe["cloud_top"])
& (dataframe["tenkan"] > dataframe["kijun"])
& (dataframe["close"] > dataframe["close_prev26"])
& (dataframe["adx"] > adx_min)
& (dataframe["cloud_width_pct"] > cloud_min)
& not_choppy
& (dataframe["volume"] > 0)
)
short_cond = (
(dataframe["close"] < dataframe["cloud_bot"])
& (dataframe["tenkan"] < dataframe["kijun"])
& (dataframe["close"] < dataframe["close_prev26"])
& (dataframe["adx"] > adx_min)
& (dataframe["cloud_width_pct"] > cloud_min)
& not_choppy
& (dataframe["volume"] > 0)
)
if self.require_tk_cross.value:
long_cond &= dataframe["tenkan"].shift(1) <= dataframe["kijun"].shift(1)
short_cond &= dataframe["tenkan"].shift(1) >= dataframe["kijun"].shift(1)
dataframe.loc[long_cond, "enter_long"] = 1
dataframe.loc[short_cond, "enter_short"] = 1
return dataframe
def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
dataframe.loc[
(((dataframe["tenkan"] < dataframe["kijun"]) | (dataframe["close"] < dataframe["cloud_bot"]))
& (dataframe["volume"] > 0)),
"exit_long",
] = 1
dataframe.loc[
(((dataframe["tenkan"] > dataframe["kijun"]) | (dataframe["close"] > dataframe["cloud_top"]))
& (dataframe["volume"] > 0)),
"exit_short",
] = 1
return dataframe