import logging import numpy as np import pandas as pd from technical import qtpylib from pandas import DataFrame from datetime import datetime, timezone from typing import Optional from functools import reduce import talib.abstract as ta import pandas_ta as pta from freqtrade.strategy import BooleanParameter, CategoricalParameter, DecimalParameter, IStrategy, IntParameter, RealParameter, merge_informative_pair import freqtrade.vendor.qtpylib.indicators as qtpylib from freqtrade.persistence import Trade logger = logging.getLogger(__name__) class wavetrend_rsi(IStrategy): INTERFACE_VERSION = 3 ### Strategy parameters ### exit_profit_only = True ### No exiting at a loss use_custom_stoploss = True trailing_stop = True position_adjustment_enable = True ignore_roi_if_entry_signal = True use_exit_signal = True stoploss = -0.4 startup_candle_count: int = 30 timeframe = '1h' # DCA Parameters position_adjustment_enable = True max_entry_position_adjustment = 3 max_dca_multiplier = 5.5 minimal_roi = {'12000': 0.1, '600': 0.15, '300': 0.2, '180': 0.3, '120': 0.4, '60': 0.45, '0': 0.5} ### Hyperoptable parameters ### # entry optizimation max_epa = CategoricalParameter([-1, 0, 1, 3, 5, 10], default=3, space='entry', optimize=True) dcal2 = DecimalParameter(-5, -1, default=-2.5, space='entry', optimize=True) dcal3 = DecimalParameter(-8, -3, default=-5, space='entry', optimize=True) dcal4 = DecimalParameter(-15, -5, default=-10, space='entry', optimize=True) # protections cooldown_lookback = IntParameter(2, 48, default=5, space='protection', optimize=True) stop_duration = IntParameter(12, 200, default=5, space='protection', optimize=True) use_stop_protection = BooleanParameter(default=True, space='protection', optimize=True) # trading entry_rsi = IntParameter(low=15, high=30, default=25, space='entry', optimize=True, load=True) exit_rsi = IntParameter(low=50, high=70, default=55, space='exit', optimize=True, load=True) ### entry opt. ### @property def max_entry_position_adjustment(self): return self.max_epa.value ### protections ### @property def protections(self): prot = [] prot.append({'method': 'CooldownPeriod', 'stop_duration_candles': self.cooldown_lookback.value}) if self.use_stop_protection.value: prot.append({'method': 'StoplossGuard', 'lookback_period_candles': 24 * 3, 'trade_limit': 8, 'stop_duration_candles': self.stop_duration.value, 'only_per_pair': False}) return prot ### Dollar Cost Averaging ### # This is called when placing the initial order (opening trade) def custom_stake_amount(self, pair: str, current_time: datetime, current_rate: float, proposed_stake: float, min_stake: Optional[float], max_stake: float, leverage: float, entry_tag: Optional[str], side: str, **kwargs) -> float: # We need to leave most of the funds for possible further DCA orders # This also applies to fixed stakes return proposed_stake / self.max_dca_multiplier def adjust_trade_position(self, trade: Trade, current_time: datetime, current_rate: float, current_profit: float, min_stake: Optional[float], max_stake: float, current_entry_rate: float, current_exit_rate: float, current_entry_profit: float, current_exit_profit: float, **kwargs) -> Optional[float]: """ Custom trade adjustment logic, returning the stake amount that a trade should be increased or decreased. This means extra entry or exit orders with additional fees. Only called when `position_adjustment_enable` is set to True. For full documentation please go to https://www.freqtrade.io/en/latest/strategy-advanced/ When not implemented by a strategy, returns None :param trade: trade object. :param current_time: datetime object, containing the current datetime :param current_rate: Current entry rate. :param current_profit: Current profit (as ratio), calculated based on current_rate. :param min_stake: Minimal stake size allowed by exchange (for both entries and exits) :param max_stake: Maximum stake allowed (either through balance, or by exchange limits). :param current_entry_rate: Current rate using entry pricing. :param current_exit_rate: Current rate using exit pricing. :param current_entry_profit: Current profit using entry pricing. :param current_exit_profit: Current profit using exit pricing. :param **kwargs: Ensure to keep this here so updates to this won't break your strategy. :return float: Stake amount to adjust your trade, Positive values to increase position, Negative values to decrease position. Return None for no action. """ if current_profit > 0.1 and trade.nr_of_successful_exits == 0: # Take half of the profit at +5% return -(trade.stake_amount / 2) for level2 in self.dcal2.range: if current_profit > level2 and trade.nr_of_successful_entries == 1: return None for level3 in self.dcal3.range: if current_profit > level3 and trade.nr_of_successful_entries == 2: return None for level4 in self.dcal4.range: if current_profit > level4 and trade.nr_of_successful_entries == 3: return None # Obtain pair dataframe (just to show how to access it) dataframe, _ = self.dp.get_analyzed_dataframe(trade.pair, self.timeframe) filled_entries = trade.select_filled_orders(trade.entry_side) count_of_entries = trade.nr_of_successful_entries # Allow up to 3 additional increasingly larger entrys (4 in total) # Initial entry is 1x # If that falls to -5% profit, we entry more, # If that falls down to -5% again, we entry 1.5x more # If that falls once again down to -5%, we entry more # Total stake for this trade would be 1 + 1.5 + 2 + 2.5 = 7x of the initial allowed stake. # That is why max_dca_multiplier is 7 # Hope you have a deep wallet! try: # This returns first order stake size stake_amount = filled_entries[0].cost # This then calculates current safety order size stake_amount = stake_amount * (1 + count_of_entries * 0.5) return stake_amount except Exception as exception: return None return None ### Trailing Stop ### def custom_stoploss(self, pair: str, trade: 'Trade', current_time: datetime, current_rate: float, current_profit: float, **kwargs) -> float: if current_profit > 0.3: return 0.05 elif current_profit > 0.1: return 0.025 elif current_profit > 0.075: return 0.015 return self.stoploss ### NORMAL INDICATORS ### def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: # RSI dataframe['rsi'] = ta.RSI(dataframe) dataframe['rsi_ma'] = ta.SMA(dataframe['rsi'], timeperiod=10) # WaveTrend using OHLC4 or HA close - 3/21 ap = 0.25 * (dataframe['high'] + dataframe['low'] + dataframe['close'] + dataframe['open']) dataframe['esa'] = ta.EMA(ap, timeperiod=10) dataframe['d'] = ta.EMA(abs(ap - dataframe['esa']), timeperiod=10) dataframe['wave_ci'] = (ap - dataframe['esa']) / (0.015 * dataframe['d']) dataframe['wave_t1'] = ta.EMA(dataframe['wave_ci'], timeperiod=21) dataframe['wave_t2'] = ta.SMA(dataframe['wave_t1'], timeperiod=4) # SMA dataframe['200_SMA'] = ta.SMA(dataframe['close'], timeperiod=200) dataframe['50_SMA'] = ta.SMA(dataframe['close'], timeperiod=50) return dataframe ### ENTRY CONDITIONS ### def populate_entry_trend(self, df: DataFrame, metadata: dict) -> DataFrame: # Signal: RSI crosses above 30 # Guard: Wave 1 is raising # Make sure Volume is not 0 df.loc[(df['rsi'] > self.entry_rsi.value) & (df['rsi'] < 60) & (df['rsi'] > df['rsi_ma']) & (df['wave_t1'] > df['wave_t1'].shift(1)) & qtpylib.crossed_above(df['wave_t1'], df['wave_t2']) & (df['volume'] > 0), ['enter_long', 'enter_tag']] = (1, 'WT/RSI') return df ### EXIT CONDITIONS ### def populate_exit_trend(self, df: DataFrame, metadata: dict) -> DataFrame: # Signal: RSI crosses above 30 # Guard: Wave 1 is raising # Make sure Volume is not 0 df.loc[(df['rsi'] > self.exit_rsi.value) & (df['wave_t1'] < df['wave_t1'].shift(1)) & qtpylib.crossed_above(df['wave_t2'], df['wave_t1']) & (df['volume'] > 0), ['exit_long', 'exit_tag']] = (1, 'WT/RSI') return df