Comment
Author: Admin | 2025-04-27
Than simple moving averages, have a look at the ta-lib docs.NoteThe function definitions in this class use type hinting to define argument and return value types.This function populates our buy signal, which is triggered when the fast_MA crosses above the slow_MA in our strategy.We can accomplish this by updating populate_buy_trend() to include the following logic:class SimpleMA_strategy(IStrategy): ... def populate_indicators(...): ... def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: dataframe.loc[ ( qtpylib.crossed_above(dataframe['fast_MA'], dataframe['slow_MA']) ), 'buy'] = 1 return dataframeUsing qtpylib, we can easily find the crossover point. Essentially, the code above populates the buy column with a 1 when our buy condition (crossed_above) is triggered.In a similar fashion to the previous function, this function populates our sell signal.According to our strategy, this is when the fast_MA crosses below the slow_MA.class SimpleMA_strategy(IStrategy): ... def populate_indicators(...): ... def populate_buy_trend(...): ... def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: dataframe.loc[ ( qtpylib.crossed_below(dataframe['fast_MA'], dataframe['slow_MA']) ), 'sell' ] = 1 return dataframeWe're now using crossed_below—the opposite of crossed_above—which will populate the sell column with a 1 when our sell condition is triggered.By default, the generated freqtrade strategy file includes more options, such as ROI (Return On Investment) and stop-loss, discussed in part two of the article series. We'll disable them for now:class SimpleMA_strategy(IStrategy): ... # the following statement disables selling due to ROI minimal_roi = { "0": 100 } # the following statement disables selling due to stop-loss stoploss = -1.0 def populate_indicators(...): ... def populate_buy_trend(...): ... def populate_sell_trend(...): ...Now that we have a strategy filled out, we can test how it would have performed on past data.Having defined our simple strategy, now we want to evaluate it using historical data using backtesting, which allows us to place trades in the past to see how they would have performed.Backtesting isn't a perfect representation of how well our strategy would have performed because other factors affect returns in live markets, such as slippage.To perform backtesting with freqtrade, we can run the following command using the class and functions we just created:docker-compose run --rm freqtrade backtesting --strategy SimpleMA_strategy -i 1d -p ETH/BTC --timerange 20200101-20201231 --starting-balance 1 -c ./user_data/learndatasci-config.jsonThe parameters in the above command are as follows:-p ETH/BTC - trade the ETH/BTC coin pair, which trades our BTC for ETH--timerange 20200101-20201231 - backtest for the year 2020, from its first day 01/01/2020 to its last day 12/31/2020.c ./user_data/config-learndatasci.json we are using the configuration that we defined earlier in this article.--starting-balance 1 we start with a balance of 1 BTC.Result for strategy SimpleMA_strategy========================================================== BACKTESTING REPORT ==========================================================| Pair | Buys | Avg Profit % | Cum Profit % | Tot Profit BTC | Tot Profit % | Avg Duration | Win Draw Loss Win% ||---------+--------+----------------+----------------+------------------+----------------+-------------------+-------------------------|| ETH/BTC | 6 | 8.47 |
Add Comment