The strategy below is simply to understand the signal implementation, and not to trade live...
from pandas import DataFrame
from freqtrade.strategy.interface import IStrategy
class ToggleHourlyDate(IStrategy):
"""
Enter and exit every hour on the 1 minute timeframe using the 'date' column.
"""
INTERFACE_VERSION: int = 3
minimal_roi = {"0": 10} # Close immediately at 1000% ROI.
stoploss = -1 # SL at -100%.
timeframe = '1m'
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
return dataframe
def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
dataframe.loc[(dataframe['date'].dt.hour % 2 == 0) &
(dataframe['date'].dt.minute == 0), 'enter_long'] = 1
return dataframe
def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
dataframe.loc[(dataframe['date'].dt.hour % 2 != 0) &
(dataframe['date'].dt.minute == 0), 'exit_long'] = 1
return dataframe
Explanation of the code:
dataframe.loc[(dataframe['date'].dt.hour % 2 == 0) & (dataframe['date'].dt.minute == 0), 'enter_long'] = 1
This line uses the .loc
method of the dataframe to select only the rows where the hour of the date column is even (dataframe['date'].dt.hour % 2 == 0
) and the minute of the date column is zero (dataframe['date'].dt.minute == 0
). Then it sets the value of the enter_long
column in these selected rows to 1
.
`dataframe.loc[(dataframe['date'].dt.hour % 2 != 0) & (dataframe['date'].dt.minute == 0), 'exit_long'] = 1
This line is similar to the previous one, but it selects the rows where the hour of the date column is odd (dataframe['date'].dt.hour % 2 != 0
) and the minute of the date column is zero (dataframe['date'].dt.minute == 0
), and sets the value of the exit_long
column in these selected rows to 1
.
So, every time the hour is even (e.g. 10:00, 12:00, 14:00), the entry
is set to 1
for the row with the minute of the date equal to 0
(e.g. 10:00), and every time the hour is odd (e.g. 11:00, 13:00, 15:00), the exit
is set to 1
for the row with the minute of the date equal to 0
(e.g. 11:00). This way, the enter_long
and exit_long
signals are generated only once per hour.