The strategy described in the code is a trailing buy strategy for trading. It is implemented as a class called YourStrat that inherits from IStrategy. The strategy uses various indicators and functions to determine when to initiate and exit trades.
Here is a breakdown of the important parts of the strategy:
populate_indicators: This method populates the necessary indicators for the strategy using the provided dataframe and metadata.
It calls the parent class's populate_indicators method and then executes the trailing_buy function.
confirm_trade_exit: This method confirms the exit of a trade based on the provided parameters. It calls the parent class's confirm_trade_exit method and then executes the trailing_buy function with the reinit parameter set to True. confirm_trade_entry: This method confirms the entry of a trade based on the provided parameters. It calls the parent class's confirm_trade_entry method and then executes the trailing_buy_info and trailing_buy functions. It also logs relevant information. populate_buy_trend: This method populates the buy trend for the strategy using the dataframe and metadata. It renames the "buy" column in the dataframe to "pre_buy". It checks if trailing buy orders are enabled and if the strategy is running in live or dry run mode. It retrieves the last candle's information and the current price. It sets the "buy" column to 0 initially. It checks if a trailing buy order has started and if the previous candle's "pre_buy" signal is 1. If not, it checks if there are open trades for the pair and initiates a trailing buy order if there are none. It updates the trailing buy information and logs relevant information. If a trailing buy order has started, it checks the trailing buy offset and takes appropriate actions. If the current price is lower than the trailing buy order uplimit, it updates the uplimit based on the trailing buy offset. If the current price is lower than the starting price multiplied by a maximum buy factor, it initiates a buy order. If the current price is higher than the starting price multiplied by a maximum stop factor, it stops the trailing buy. Otherwise, it updates the trailing buy information and logs relevant information. If none of the conditions are met, it does not initiate a buy order. TrailingBuyStrat2 class: This class inherits from YourStrat and overrides the populate_indicators and confirm_trade_entry methods. The overridden populate_indicators method calls the parent class's populate_indicators method and then executes the trailing_buy function. The overridden confirm_trade_entry method confirms the trade entry based on the parent class's method and additional conditions. It sets the val variable based on the trailing buy order conditions and calculates the trailing profit ratio. It updates the trailing buy information, logs relevant information, and returns the val variable. Overall, the strategy uses indicators and trailing buy orders to determine the entry and exit points for trades. It dynamically adjusts the trailing buy order uplimit based on the current price and predefined factors.