Strategy for labeling trend reversals in trading data for machine learning tasks.
Timeframe
5s
Direction
Long Only
Stoploss
-0.1%
Trailing Stop
Yes
ROI
10m: 1000000.0%
Interface Version
3
Startup Candles
N/A
Indicators
1
# pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement
# flake8: noqa: F401
# isort: skip_file
# --- Standard Library Imports ---
from datetime import datetime
from typing import Optional, Union
# --- Third Party Imports ---
import numpy as np
import pandas as pd
from pandas import DataFrame
# --- Freqtrade Imports ---
from freqtrade.strategy import (
IStrategy,
IntParameter
)
# --- Technical Analysis Library Imports ---
import talib.abstract as ta
# --- Custom Strategy Class ---
class TemaTrendLabel(IStrategy):
"""
Strategy for labeling trend reversals in trading data for machine learning tasks.
"""
INTERFACE_VERSION = 3
timeframe = '5s'
can_short: bool = True
minimal_roi = {"10": 10000} # Adjusted for 5s timeframe (60s → 10s)
stoploss = -0.001 # Adjusted stoploss for faster timeframe
trailing_stop = True
startup_candle_count: int = 150 # Increased for more reliable indicators on faster timeframe
def informative_pairs(self):
"""
Define pairs for additional, informative data.
Currently returns an empty list.
"""
return []
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Add all necessary technical indicators to the DataFrame.
Currently includes Triple Exponential Moving Average (TEMA).
"""
tema_period = 150 # Increased for 5s timeframe to maintain similar time window
dataframe['tema'] = ta.TEMA(dataframe['close'], timeperiod=tema_period)
dataframe['trend'] = np.where(dataframe['tema'] > dataframe['tema'].shift(1), 'UP', 'DOWN')
dataframe['trend_duration'] = (dataframe['trend'] != dataframe['trend'].shift(1)).cumsum()
dataframe['trend_count'] = dataframe.groupby('trend_duration').cumcount() + 1
return dataframe
def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Populate the entry signals based on trend reversal logic.
Signals for long and short entries based on trend direction changes.
"""
dataframe.loc[
(dataframe['trend'] == 'UP') & (dataframe['trend'].shift(1) == 'DOWN'),
'enter_long'] = 1
dataframe.loc[
(dataframe['trend'] == 'DOWN') & (dataframe['trend'].shift(1) == 'UP'),
'enter_short'] = 1
return dataframe
def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Populate the exit signals for the strategy.
This method is left empty for further customization.
"""
return dataframe