Freqtrade custom stoploss functionality addendum

Do repost and rate:

This is the code addendum for the Youtube video "Freqtrade custom stoploss functionality

You can watch the video over here:

Remember the following about the way Freqtrade has implemented it's stoploss function:

Freqtrade bases its default stop loss functionality on a percentages that deviates from the original buy price. There is no way you can set this to a absolute price level at this stage.

If you have set your stop loss to minus 5% from the entry price after the strategy has given a buy signal. Then the price of an asset is, let’s say 1000 dollar, the bot’s default stop loss kicks in after a 5 percent loss, so the asset has a price of 950 dollar at that time.

The custom stop loss function is also a percentage based function. The only major difference is that it calculates the stop loss distance from the current price and not from the opening price.

So if the custom stop loss is dependent on the current price and this price is moving up, then so does this custom stop loss. In other words this custom stop loss function acts like a trailing stop loss and most of the examples given by the Freqtrade documentation also explain them as a form of stop loss that moves up with the current price.

Also a custom stop loss is always upward moving but never down. So if the price goes down, the highest calculated stop loss level remains and newer calculated lower stop loss levels will be ignored. This is also according to the documentation. 

Other things to know about the custom stoploss function

The first thing you have to know is that the custom stop loss function is activated by using the use_custom_stoploss=True within your strategy

Then you have to define the custom stop loss function and from that moment on every freqtrade iteration the bot will calculate the stop loss from the current price. If you followed my video on 04. Freqtrade configuration Bot & Exchange settings. You should know that the default freqtrade bot process runs every 5 seconds. So every time this process runs this custom stop loss is calculated. And ofcourse you can change these settings in the bot’s configuration file. You can find More on this subject in my other video on that and I will leave a link in the description below.

The custom stop loss price only can move upwards, so this acts like a trailing stop loss. And the default, initial percentage based stop loss acts as the lowest boundary for a trade.

The stop loss value is also a percentage of the current price and not an absolute price value based on the open value of the trade. And it does not matter if there is a positive of negative floating number. It will always be calculated as a positive percentage against the current price.

You should be aware that Because this custom stoploss act as a trailing stoploss , you should not confuse this with the other way Freqtrade has implemented a trailing stop loss function that you can program into your strategy. 

The freqtrade site has the following custom stop loss examples for us:

  • Trailing percentages % (WHICH I HAVE ALREADY DISCUSSED IN THE EXAMPLE I HAVE GIVEN with the 4% trailing stop loss just a minute ago)
  • Time based trailing stop
  • Different stoplosses per pair
  • Trailing stop loss with positive offset
  • Stepped stop loss
  • Custom stoploss using an indicator from dataframe example

You can watch my explainiation of these custom stoploss ideas in my video above.

Variables for creating your own custom stoploss

Below the code I made to check on all the variables that are available in the custom stoploss function. With these you can try and create your own specific stoploss funtion.

# Freqtrade default libraries (DO NOT REMOVE!). freqtradeinterface  IStrategy DataFrame# Additional libraries for dataframe, strategy and/or plotting.# Remove any when not used.abstract  freqtradeindicators  qtpylib pandas_ta  numpy # These libs are for hyperopt, remove if not used. functools  freqtradeBooleanParameter CategoricalParameter DecimalParameter IntParameter# Libs used for custom stoploss.# For simplicity reasons I added all libraries, whether I use them or not.# Remove any when not used. datetime  datetime timedelta freqtradepersistence  freqtrade stoploss_from_absolute stoploss_from_open freqtradeexchange  timeframe_to_prev_datesma_sl_indicator_fullcomment    timeframe # Both stoploss and roi are set to 100 to prevent them to give a sell signal.    stoploss     minimal_roi populate_indicators dataframe DataFrame metadata DataFrame                dataframedataframe timeperiod        # parabolic SAR for custom stoploss        dataframedataframe        # Print stuff for debugging dataframe        # print(dataframe.tail(20))         dataframe    # USE CUSTOM STOPLOSS    use_custom_stoploss custom_stoploss current_time datetime                        current_rate current_profit        """        Custom stoploss logic, returning the new distance relative to current_rate (as ratio).        e.g. returning -0.05 would create a stoploss 5% below current_rate.        The custom stoploss can never be below self.stoploss, which serves as a hard maximum loss.        For full documentation please go to https://www.freqtrade.io/en/latest/strategy-advanced/        When not implemented by a strategy, returns the initial stoploss value        Only called when use_custom_stoploss is set to True.        :param pair: Pair that's currently analyzed        :param trade: trade object.        :param current_time: datetime object, containing the current datetime        :param current_rate: Rate, calculated based on pricing settings in ask_strategy.        :param current_profit: Current profit (as ratio), calculated based on current_rate.        :param **kwargs: Ensure to keep this here so updates to this won't break your strategy.        :return float: New stoploss value, relative to the current rate        """        # You may access dataframe in various strategy functions by querying it from dataprovider.        dataframeget_analyzed_dataframe        # Obtain last available candle. Do not use current_time to look up latest candle, because        # current_time points to current incomplete candle whose data is not available.        last_candle  dataframe        # In dry/live runs trade open date will not match candle open date therefore it must be        # rounded.        trade_date  timeframe_to_prev_dateopen_date_utc        # Look up trade candle.        trade_candle  dataframedataframe trade_date        # trade_candle may be empty for trades that just opened as it is still incomplete.         trade_candle            trade_candle  trade_candle        # Use parabolic sar as absolute stoploss price        stoploss_price  last_candle        # Convert absolute price to percentage relative to current_rate         stoploss_price  current_rate            stoploss_price  current_rate        "\n Pair: "            "\n Trade: "            "\n Current time: " current_time            "\n Current rate: " current_rate            "\n Custom stoploss price: " stoploss_price            "\n Current profit: " current_profit            "\n Trade open date UTC: "open_date_utc            "\n Timedelta 120: " current_time  timedelta            "\n Timedelta 60: " timedelta            "\n Trade amount: "            "\n Trade buy tag: "            "\n Trade fee open: "fee_open            "\n Trade fee open currency: "fee_open_currency            "\n Trade initial stoploss: "initial_stop_loss            "\n Trade initial stoploss percent: "initial_stop_loss_pct            "\n Trade stoploss: "stop_loss            "\n Trade stoposs percent: "stop_loss_pct            "\n Trade open rate: "open_rate            "\n Trade open trade value: "open_trade_value            "\n Trade amount requested: "amount_requested            "\n Trade exchange: "exchange            "\n Trade is open: "            "\n Trade strategy: "            "\n Trade timeframe: "            "\n Trade stake amount: "stake_amount            "\n Trade use db: "            "\n Trade total profit: "total_profit            "\n Last candle from dataframe: " last_candle            "\n Trade date from dataframe: " trade_date            "\n Trade candle from dataframe: " trade_candle            "\n Stoploss price from dataframe: " stoploss_price            "\n Dataframe: " dataframe        # return maximum stoploss value, keeping current stoploss price unchanged        populate_buy_trend dataframe DataFrame metadata DataFrame        dataframe            # (qtpylib.crossed_above(dataframe['close'], dataframe['sma'])),            dataframe dataframe             'buy_tag''buy_signal_sma_cross'         dataframe    populate_sell_trend dataframe DataFrame metadata DataFrame        dataframe            crossed_belowdataframe dataframe                                 dataframe

Regulation and Society adoption

Ждем новостей

Нет новых страниц

Следующая новость