import numpy as np # noqa import pandas as pd # noqa from pandas import DataFrame from freqtrade.strategy.interface import IStrategy import talib.abstract as ta import freqtrade.vendor.qtpylib.indicators as qtpylib from datetime import datetime from freqtrade.persistence import Trade class custom_stoploss_with_psar_2(IStrategy): minimal_roi = { "0": -1 } """ this is an example class, implementing a PSAR based trailing stop loss you are supposed to take the `custom_stoploss()` and `populate_indicators()` parts and adapt it to your own strategy the populate_buy_trend() function is pretty nonsencial """ timeframe = '1h' stoploss = -0.2 custom_info = dict() use_custom_stoploss = True def custom_stoploss(self, pair: str, trade: 'Trade', current_time: datetime, current_rate: float, current_profit: float, **kwargs) -> float: result = 1 if self.custom_info and pair in self.custom_info and trade: relative_sl = None if self.dp: dataframe, _ = self.dp.get_analyzed_dataframe(pair=pair, timeframe=self.timeframe) last_candle = dataframe.iloc[-1].squeeze() relative_sl = last_candle['sar'] if (relative_sl is not None): new_stoploss = (current_rate - relative_sl) / current_rate result = new_stoploss - 1 return result def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: dataframe['sar'] = ta.SAR(dataframe) if self.dp.runmode.value in ('backtest', 'hyperopt'): self.custom_info[metadata['pair']] = dataframe[['date', 'sar']].copy().set_index('date') return dataframe def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ Placeholder Strategy: buys when SAR is smaller then candle before Based on TA indicators, populates the buy signal for the given dataframe :param dataframe: DataFrame :return: DataFrame with buy column """ dataframe.loc[ ( (dataframe['sar'] < dataframe['sar'].shift()) ), 'buy'] = 1 return dataframe def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ Placeholder Strategy: does nothing Based on TA indicators, populates the sell signal for the given dataframe :param dataframe: DataFrame :return: DataFrame with buy column """ dataframe.loc[:, 'sell'] = 0 return dataframe