Contents
The anatomy of a strategyThe two important callbacksWhere the well-known strategies come fromHow to compare strategies honestlyA reading order for a new strategyThe anatomy of a strategy
A freqtrade strategy is one Python file with a class in it. The class declares its indicators, defines two decisions — when to enter, when to exit — and optionally how positions are sized and how it reacts to events. That is the whole shape; everything else in the ecosystem is a variation on this file.
class MyStrategy(IStrategy):
timeframe = '5m'
minimal_roi = {{'0': 0.08, '30': 0.03, '120': 0.01}}
stoploss = -0.10
def populate_indicators(self, dataframe, metadata):
dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14)
return dataframe
def populate_entry_trend(self, dataframe, metadata):
dataframe.loc[
(dataframe['rsi'] < 30),
'enter_long'] = 1
return dataframe
def populate_exit_trend(self, dataframe, metadata):
dataframe.loc[
(dataframe['rsi'] > 70),
'exit_long'] = 1
return dataframe
Three blocks matter most: minimal_roi answers "when do we take profit", stoploss answers "when do we admit a mistake", and the two populate methods answer "what do we look at and what do we do about it".
The two important callbacks
| Method | Question it answers | Runs |
|---|---|---|
populate_indicators | What do we measure? | Once per candle set, on all pairs |
populate_entry_trend | When do we open a position? | After indicators, per pair |
populate_exit_trend | When do we close by signal? | Same pass as entry logic |
Besides these, lifecycle hooks exist for order sizing, for reacting to filled trades, and for custom exit logic that the trend table cannot express. A strategy that needs those hooks is a strategy with a story to tell — read the story before running it.
Where the well-known strategies come from
The strategies that circulate in the community mostly descend from a small set of lineages: the examples shipped with the bot, the strategy forum threads that were forked repeatedly, and the machine-learning examples that later became their own ecosystem. When you read a strategy file with a familiar feel, you are usually reading the nth descendant of one of these — which is useful to know, because the bug you are looking at may have been inherited too.
How to compare strategies honestly
- Same period, same pairs, same fees. A comparison that changes two of these is not a comparison.
- Fee assumption is a knob, not a constant. Results move when the fee model moves; check which fee the backtest assumed.
- Count trades, not just profit. Two trades that made 8% say nothing; three hundred trades that made 8% say something.
- Drawdown is the number that hurts. Total profit and maximum drawdown together describe a strategy better than either alone.
- Out-of-sample is the only sample. A curve that fits the backtest period perfectly has memorized, not learned — see the backtesting notes.
A reading order for a new strategy
- The header comment — a surprising amount of the strategy's intent and age is stated there.
timeframe— what world the strategy lives in.minimal_roiandstoploss— the exit policy.- The indicator list — what it measures, and whether those measurements fit that timeframe.
- The entry condition — is it one condition, or six stacked filters that the author never re-tested together?
- Position sizing — if absent, the strategy assumes equal stakes and should say so.