💬 Forum

SampleStrategy1

🏆 League #116 / 1939

ChiChasesCheese/more_money/user_data/strategies/SampleStrategy_Fixed.py · first seen 2026-07-16 · repo updated 2025-09-03

Basics mode: spot timeframe: 5m
Settings stoploss: -0.13 has minimal roi dca process only new candles startup candle count: 120 hyperopt hyperopt params: 4
Indicators RSI SMA Stochastic pandas_ta talib
Concepts dca
15 related strategies ( identical code, similar name)

Each tile is a different kind of check — from an instant code lint to full sandboxed backtests and forward tests on recent data. Not sure what a check actually proves? See the FAQ →

Source

Download Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from datetime import datetime
import talib.abstract as ta                   # TA-Lib 抽象接口(可直接对 DataFrame 使用)
import pandas_ta as pta                       # pandas_ta 指标库,这里用到 cti
from freqtrade.persistence import Trade
from freqtrade.strategy.interface import IStrategy
from pandas import DataFrame
from freqtrade.strategy import DecimalParameter, IntParameter
from functools import reduce                  # 组合多个条件用(逻辑或)

class SampleStrategy1(IStrategy):
    # ROI 表:持仓 0 分钟起要求 2% 盈利即可触发 ROI 卖出;持仓满 120 分钟后只要 1%
    minimal_roi = {
        "0": 0.02,
        "120": 0.01
    }

    timeframe = '5m'                          # 策略运行在 5 分钟线
    process_only_new_candles = True           # 只在新 K 线计算,省 CPU
    startup_candle_count = 120                # 至少准备 120 根 K,用于指标热身
    position_adjustment_enable = True         # 启用“仓位调整”(分批加减仓)

    # 订单类型设定
    order_types = {
        'entry': 'market',                    # 入场用市价
        'exit': 'market',                     # 出场用市价
        'emergency_exit': 'market',
        'force_entry': 'market',
        'force_exit': "market",
        'stoploss': 'market',                 # 止损用市价
        'stoploss_on_exchange': False,        # 不在交易所挂实体止损单(本地管)
        # 下面两项仅在 stoploss_on_exchange=True 时生效,这里相当于预留
        'stoploss_on_exchange_interval': 60,
        'stoploss_on_exchange_market_ratio': 0.99
    }

    stoploss = -0.13                          # 全局硬止损:-13%

    # —— 超参定义(可被 Hyperopt 优化)——
    is_optimize_32 = True
    buy_rsi_fast_32 = IntParameter(20, 70, default=46, space='buy', optimize=is_optimize_32)
    buy_rsi_32 = IntParameter(15, 50, default=19, space='buy', optimize=is_optimize_32)
    buy_sma15_32 = DecimalParameter(0.900, 1, default=0.942, decimals=3, space='buy', optimize=is_optimize_32)
    buy_cti_32 = DecimalParameter(-1, 0, default=-0.86, decimals=2, space='buy', optimize=is_optimize_32)

    # —— 初始下单金额控制:只用建议仓位的 70% 开仓 —— 
    def custom_stake_amount(self, pair: str, current_time: datetime, current_rate: float,
                            proposed_stake: float, min_stake: float, max_stake: float,
                            leverage: float, entry_tag: str, side: str,
                            **kwargs) -> float:
        return proposed_stake * 0.7           # 预留 30% 弹药以便后续加仓

    # —— 仓位调整(分批/加减仓)——
    def adjust_trade_position(self, trade: Trade, current_time: datetime,
                              current_rate: float, current_profit: float,
                              min_stake: float, max_stake: float,
                              current_entry_rate: float, current_exit_rate: float,
                              current_entry_profit: float, current_exit_profit: float,
                              **kwargs) -> float:

        count_of_exits = trade.nr_of_successful_exits
        # 条件1:若当前浮盈 ≥ 2%,且从未分批减仓过 → 减仓 50%
        if current_profit >= 0.02 and count_of_exits == 0:
            return -0.5 * trade.stake_amount  # 负值=减仓金额(按初始 stake 的一半)

        count_of_entries = trade.nr_of_successful_entries
        filled_entries = trade.select_filled_orders(trade.entry_side)  # 历史入场订单
        initial_stake = filled_entries[0].cost                         # 第一次入场的花费(70%)

        # 条件2:若浮亏 < -5% 且仅入场过一次 → 补仓到满仓(把剩余 30% 补上)
        if current_profit < -0.05 and count_of_entries == 1 :
            return initial_stake / 0.7 * 0.3   # 初始 70% 反推总额,再乘 30% 得到补仓额
        return None                             # 其他情况不调整

    # —— 计算技术指标 —— 
    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:

        dataframe['sma_15'] = ta.SMA(dataframe, timeperiod=15)              # 15 期简单均线
        dataframe['cti'] = pta.cti(dataframe["close"], length=20)           # CTI(20):相关性趋势指标,负值偏超卖
        dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14)                 # 标准 RSI(14)
        dataframe['rsi_fast'] = ta.RSI(dataframe, timeperiod=4)             # 快 RSI(4):更敏感
        dataframe['rsi_slow'] = ta.RSI(dataframe, timeperiod=20)            # 慢 RSI(20):趋势性

        # STOCHF:快速随机指标,这里只用 fastk(0–100)
        stoch_fast = ta.STOCHF(dataframe, 5, 3, 0, 3, 0)                    # 建议改成关键词参数以免歧义
        dataframe['fastk'] = stoch_fast['fastk']

        return dataframe

    # —— 入场条件 —— 
    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:

        conditions = []
        dataframe.loc[:, 'enter_tag'] = ''     # 记录触发的买入标签

        # buy_1 组合信号:
        buy_1 = (
                (dataframe['rsi_slow'] < dataframe['rsi_slow'].shift(1)) &      # 慢 RSI 走低(捕捉回撤)
                (dataframe['rsi_fast'] < self.buy_rsi_fast_32.value) &          # 快 RSI 低于阈值(短期超卖)
                (dataframe['rsi'] > self.buy_rsi_32.value) &                    # 但整体 RSI 不能太弱
                (dataframe['close'] < dataframe['sma_15'] * self.buy_sma15_32.value) &  # 价格低于 SMA15 的某比例
                (dataframe['cti'] < self.buy_cti_32.value)                      # CTI 足够负,倾向反弹
        )

        conditions.append(buy_1)
        dataframe.loc[buy_1, 'enter_tag'] += 'buy_1'  # 打标签方便回测分析

        # 任一买入条件满足 → 标记 enter_long = 1(Freqtrade 会据此下单)
        if conditions:
            dataframe.loc[
                reduce(lambda x, y: x | y, conditions),
                'enter_long'] = 1

        return dataframe

    # —— 出场条件 —— 
    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
#         dataframe.loc[(), ['exit_long', 'exit_tag']] = (0, 'long_out')  # 示例:可自定义出场信号
        dataframe['exit_long'] = 0
        dataframe['exit_tag'] = ''            # 这里不主动给卖出信号
                                             # 实际卖出由 ROI 表 / 全局止损 / adjust_trade_position 决定
        return dataframe