Strategy structure and comparison notes

How open trading-bot strategies are structured: the anatomy of a strategy file, the callbacks that matter, and how to compare strategies without fooling yourself.

The 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

MethodQuestion it answersRuns
populate_indicatorsWhat do we measure?Once per candle set, on all pairs
populate_entry_trendWhen do we open a position?After indicators, per pair
populate_exit_trendWhen 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

A reading order for a new strategy

  1. The header comment — a surprising amount of the strategy's intent and age is stated there.
  2. timeframe — what world the strategy lives in.
  3. minimal_roi and stoploss — the exit policy.
  4. The indicator list — what it measures, and whether those measurements fit that timeframe.
  5. The entry condition — is it one condition, or six stacked filters that the author never re-tested together?
  6. Position sizing — if absent, the strategy assumes equal stakes and should say so.
FreqDesk
An independent reference desk for open trading bots.
desk@freqdesk.pro
About this desk
Privacy notice
Terms of use

FreqDesk is an educational publication about open-source trading software. No software is hosted or distributed on this domain, and no trading advice is given.