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
This commit is contained in:
158
freqtrade/user_data/strategies/IchimokuHyper2.py
Normal file
158
freqtrade/user_data/strategies/IchimokuHyper2.py
Normal file
@@ -0,0 +1,158 @@
|
||||
# pragma pylint: disable=missing-docstring, invalid-name, too-few-public-methods
|
||||
"""
|
||||
IchimokuHyper2 — Ichimoku long/short hyperoptable, AVEC contraintes de risque.
|
||||
|
||||
Améliorations vs IchimokuHyper :
|
||||
- Filtre macro EMA200 optimisable (use_macro).
|
||||
- STOP-LOSS CONTRAINT à une plage réaliste [-10% .. -2%] (le -23% précédent était le
|
||||
vrai point faible : trop large pour envisager le levier).
|
||||
- ROI plafonné à des valeurs réalistes.
|
||||
Optimisation orientée robustesse (Sharpe) + train/test strict pour éviter l'overfit.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import talib.abstract as ta
|
||||
from pandas import DataFrame
|
||||
|
||||
from freqtrade.strategy import (
|
||||
IStrategy,
|
||||
IntParameter,
|
||||
DecimalParameter,
|
||||
BooleanParameter,
|
||||
)
|
||||
from freqtrade.optimize.space import Categorical, Dimension, Integer, SKDecimal
|
||||
|
||||
|
||||
class IchimokuHyper2(IStrategy):
|
||||
INTERFACE_VERSION = 3
|
||||
timeframe = "1h"
|
||||
can_short = True
|
||||
|
||||
# Défauts (surchargés par hyperopt)
|
||||
minimal_roi = {"0": 0.08, "240": 0.04, "720": 0.02, "1440": 0}
|
||||
stoploss = -0.06
|
||||
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)
|
||||
use_macro = BooleanParameter(default=True, space="buy", optimize=True)
|
||||
|
||||
# --- Espaces hyperopt CONTRAINTS (le coeur de cette optimisation) ---
|
||||
class HyperOpt:
|
||||
@staticmethod
|
||||
def stoploss_space() -> list[Dimension]:
|
||||
# Stop réaliste : entre -2% et -10% (fini le -23%).
|
||||
return [SKDecimal(-0.10, -0.02, decimals=3, name="stoploss")]
|
||||
|
||||
@staticmethod
|
||||
def roi_space() -> list[Dimension]:
|
||||
return [
|
||||
Integer(0, 120, name="roi_t1"),
|
||||
Integer(0, 60, name="roi_t2"),
|
||||
Integer(0, 30, name="roi_t3"),
|
||||
SKDecimal(0.02, 0.12, decimals=3, name="roi_p1"),
|
||||
SKDecimal(0.01, 0.06, decimals=3, name="roi_p2"),
|
||||
SKDecimal(0.005, 0.03, decimals=3, name="roi_p3"),
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def generate_roi_table(params: dict) -> dict[int, float]:
|
||||
roi = {}
|
||||
roi[0] = params["roi_p1"] + params["roi_p2"] + params["roi_p3"]
|
||||
roi[params["roi_t3"]] = params["roi_p1"] + params["roi_p2"]
|
||||
roi[params["roi_t3"] + params["roi_t2"]] = params["roi_p1"]
|
||||
roi[params["roi_t3"] + params["roi_t2"] + params["roi_t1"]] = 0
|
||||
return roi
|
||||
|
||||
@staticmethod
|
||||
def trailing_space() -> list[Dimension]:
|
||||
return [
|
||||
Categorical([True], name="trailing_stop"),
|
||||
SKDecimal(0.01, 0.06, decimals=3, name="trailing_stop_positive"),
|
||||
SKDecimal(0.005, 0.05, decimals=3, name="trailing_stop_positive_offset_p1"),
|
||||
Categorical([True, False], name="trailing_only_offset_is_reached"),
|
||||
]
|
||||
|
||||
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)
|
||||
dataframe["ema200"] = ta.EMA(dataframe, timeperiod=200)
|
||||
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
|
||||
|
||||
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)
|
||||
& (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)
|
||||
& (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)
|
||||
if self.use_macro.value:
|
||||
long_cond &= dataframe["close"] > dataframe["ema200"]
|
||||
short_cond &= dataframe["close"] < dataframe["ema200"]
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user