Skip to content

Commit

Permalink
Update to v0.2a
Browse files Browse the repository at this point in the history
  • Loading branch information
stash86 committed Sep 5, 2021
1 parent f0fac5e commit 77745be
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 102 deletions.
17 changes: 17 additions & 0 deletions backtest results/v0.2a results.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Tested using fixed stake of 15 BUSD

2021-04-01 00:00:00 -> 2021-06-01 00:00:00 | Max open trades : 4
======================================================================== STRATEGY SUMMARY =======================================================================
| Strategy | Buys | Avg Profit % | Cum Profit % | Tot Profit BUSD | Tot Profit % | Avg Duration | Win Draw Loss Win% | Drawdown |
|---------------+--------+----------------+----------------+-------------------+----------------+----------------+-------------------------+--------------------|
| MultiMA_TSL | 939 | 1.32 | 1238.99 | 186.035 | 286.21 | 1:03:00 | 750 0 189 79.9 | 6.448 BUSD 42.94% |
| MultiMA_TSL2a | 859 | 1.58 | 1353.50 | 203.228 | 312.66 | 0:57:00 | 713 0 146 83.0 | 6.399 BUSD 42.62% |
=================================================================================================================================================================

2021-06-01 00:00:00 -> 2021-08-29 23:35:00 | Max open trades : 4
======================================================================== STRATEGY SUMMARY =======================================================================
| Strategy | Buys | Avg Profit % | Cum Profit % | Tot Profit BUSD | Tot Profit % | Avg Duration | Win Draw Loss Win% | Drawdown |
|---------------+--------+----------------+----------------+-------------------+----------------+----------------+-------------------------+--------------------|
| MultiMA_TSL | 743 | 1.07 | 795.57 | 119.455 | 183.78 | 1:10:00 | 588 0 155 79.1 | 3.983 BUSD 26.52% |
| MultiMA_TSL2a | 640 | 1.16 | 742.44 | 111.477 | 171.50 | 1:02:00 | 520 0 120 81.2 | 4.032 BUSD 26.86% |
=================================================================================================================================================================
205 changes: 103 additions & 102 deletions user_data/strategies/MultiMA_TSL.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import talib.abstract as ta
from freqtrade.strategy.interface import IStrategy
from freqtrade.strategy import (merge_informative_pair,
DecimalParameter, IntParameter, CategoricalParameter)
DecimalParameter, IntParameter, BooleanParameter, CategoricalParameter, stoploss_from_open)
from pandas import DataFrame
from functools import reduce
from freqtrade.persistence import Trade
from datetime import datetime, timedelta
from freqtrade.exchange import timeframe_to_prev_date
from technical.indicators import zema

###########################################################################################################
## MultiMA_TSL, modded by stash86, based on SMAOffsetProtectOptV1 (modded by Perkmeister) ##
Expand All @@ -20,28 +22,33 @@
# I hope you do enough testing before proceeding, either backtesting and/or dry run.
# Any profits and losses are all your responsibility

class MultiMA_TSL(IStrategy):
class MultiMA_TSL2a(IStrategy):
INTERFACE_VERSION = 2

buy_params = {
"base_nb_candles_buy_sma": 76,
"low_offset_sma": 0.959,
"rsi_buy_sma": 55,

"base_nb_candles_buy_ema": 6,
"low_offset_ema": 0.985,
"rsi_buy_ema": 61,
"base_nb_candles_buy_ema": 50,
"low_offset_ema": 1.061,

"base_nb_candles_buy_zema": 30,
"low_offset_zema": 0.963,
"rsi_buy_zema": 50,

"base_nb_candles_buy_trima": 14,
"low_offset_trima": 0.963,
"rsi_buy_trima": 50,

"buy_roc_max": 45,

"base_nb_candles_buy_trima": 6,
"low_offset_trima": 0.981,
"rsi_buy_trima": 59,
"buy_condition_trima_enable": True,
"buy_condition_zema_enable": True,
}

sell_params = {
"base_nb_candles_sell": 30,
"high_offset_ema": 1.004,
"sl_filter_candles": 3,
"sl_filter_offset": 0.992,
"base_nb_candles_sell": 32,
"high_offset_ema": 1.002,

"base_nb_candles_sell_trima": 48,
"high_offset_trima": 1.085,
}

# ROI table:
Expand All @@ -52,45 +59,53 @@ class MultiMA_TSL(IStrategy):
stoploss = -0.15

# Multi Offset
base_nb_candles_sell = IntParameter(5, 80, default=20, load=True, space='sell', optimize=False)

base_nb_candles_buy_sma = IntParameter(5, 80, default=20, load=True, space='buy', optimize=False)
low_offset_sma = DecimalParameter(0.9, 0.99, default=0.958, load=True, space='buy', optimize=False)
rsi_buy_sma = IntParameter(30, 70, default=61, space='buy', optimize=False)
base_nb_candles_sell = IntParameter(5, 80, default=20, space='sell', optimize=False)
base_nb_candles_sell_trima = IntParameter(5, 80, default=20, space='sell', optimize=False)
high_offset_trima = DecimalParameter(0.99, 1.1, default=1.012, space='sell', optimize=False)

base_nb_candles_buy_ema = IntParameter(5, 80, default=20, load=True, space='buy', optimize=False)
low_offset_ema = DecimalParameter(0.9, 0.99, default=0.958, load=True, space='buy', optimize=False)
high_offset_ema = DecimalParameter(0.99, 1.1, default=1.012, load=True, space='sell', optimize=False)
base_nb_candles_buy_ema = IntParameter(5, 80, default=20, space='buy', optimize=False)
low_offset_ema = DecimalParameter(0.9, 1.1, default=0.958, space='buy', optimize=False)
high_offset_ema = DecimalParameter(0.99, 1.1, default=1.012, space='sell', optimize=False)
rsi_buy_ema = IntParameter(30, 70, default=61, space='buy', optimize=False)

base_nb_candles_buy_trima = IntParameter(5, 80, default=20, load=True, space='buy', optimize=False)
low_offset_trima = DecimalParameter(0.9, 0.99, default=0.958, load=True, space='buy', optimize=False)
base_nb_candles_buy_trima = IntParameter(5, 80, default=20, space='buy', optimize=False)
low_offset_trima = DecimalParameter(0.9, 0.99, default=0.958, space='buy', optimize=False)
rsi_buy_trima = IntParameter(30, 70, default=61, space='buy', optimize=False)

# Protection
ewo_low = DecimalParameter(-20.0, -8.0, default=-20.0, load=True, space='buy', optimize=False)
ewo_high = DecimalParameter(2.0, 12.0, default=6.0, load=True, space='buy', optimize=False)
fast_ewo = IntParameter(10, 50, default=50, load=True, space='buy', optimize=False)
slow_ewo = IntParameter(100, 200, default=200, load=True, space='buy', optimize=False)
base_nb_candles_buy_zema = IntParameter(5, 80, default=20, space='buy', optimize=False)
low_offset_zema = DecimalParameter(0.9, 0.99, default=0.958, space='buy', optimize=False)
rsi_buy_zema = IntParameter(30, 70, default=61, space='buy', optimize=False)

# stoploss sharp dip filter
sl_filter_candles = IntParameter(2, 10, default=5, space='sell', optimize=False, load=True)
sl_filter_offset = DecimalParameter(0.960, 0.999, default=0.989, decimals=3, space='sell', optimize=False, load=True)
buy_condition_enable_optimize = True
buy_condition_trima_enable = BooleanParameter(default=True, space='buy', optimize=buy_condition_enable_optimize)
buy_condition_zema_enable = BooleanParameter(default=True, space='buy', optimize=buy_condition_enable_optimize)

# Protection
ewo_low = DecimalParameter(-20.0, -8.0, default=-20.0, space='buy', optimize=False)
ewo_high = DecimalParameter(2.0, 12.0, default=6.0, space='buy', optimize=False)
fast_ewo = IntParameter(10, 50, default=50, space='buy', optimize=False)
slow_ewo = IntParameter(100, 200, default=200, space='buy', optimize=False)
buy_roc_max = DecimalParameter(20, 70, default=55, space='buy', optimize=False)
buy_peak_max = DecimalParameter(1, 1.1, default=1.03, decimals=3, space='buy', optimize=False)
buy_rsi_fast = IntParameter(0, 50, default=35, space='buy', optimize=False)

# Trailing stoploss (not used)
trailing_stop = False
trailing_only_offset_is_reached = True
trailing_stop_positive = 0.01
trailing_stop_positive_offset = 0.018

use_custom_stoploss = False
use_custom_stoploss = True

# Protection hyperspace params:
protection_params = {
"low_profit_lookback": 60,
"low_profit_min_req": 0.03,
"low_profit_stop_duration": 29,

"cooldown_lookback": 2, # value loaded from strategy
"stoploss_lookback": 72, # value loaded from strategy
"stoploss_stop_duration": 20, # value loaded from strategy
}

cooldown_lookback = IntParameter(2, 48, default=2, space="protection", optimize=False)
Expand Down Expand Up @@ -131,123 +146,99 @@ def protections(self):
# Number of candles the strategy requires before producing valid signals
startup_candle_count: int = 200

def get_ticker_indicator(self):
return int(self.timeframe[:-1])

def confirm_trade_entry(self, pair: str, order_type: str, amount: float, rate: float, time_in_force: str, **kwargs) -> bool:
dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
last_candle = dataframe.iloc[-1].squeeze()
#credit to Perkmeister for this custom stoploss to help the strategy ride a green candle when the sell signal triggered
def custom_stoploss(self, pair: str, trade: 'Trade', current_time: datetime,
current_rate: float, current_profit: float, **kwargs) -> float:
sl_new = 1

if ((rate > last_candle['close'])) : return False
if not self.config['runmode'].value in ('backtest', 'hyperopt'):
dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
if(len(dataframe) >= 1):
last_candle = dataframe.iloc[-1]
if((last_candle['sell_copy'] == 1) & (last_candle['buy_copy'] == 0)):
sl_new = 0.001

return True
return sl_new

def confirm_trade_exit(self, pair: str, trade: Trade, order_type: str, amount: float,
rate: float, time_in_force: str, sell_reason: str,
current_time: datetime, **kwargs) -> bool:
def get_ticker_indicator(self):
return int(self.timeframe[:-1])

# Code from Perkmeister, to check for a sudden dip
if(sell_reason == 'stop_loss'):
dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
if(len(dataframe) < 1):
return True
last_candle = dataframe.iloc[-1]
current_profit = trade.calc_profit_ratio(rate)

if(
(trade.initial_stop_loss == trade.stop_loss) &
(last_candle['ma_sl_filter_offset'] > rate)
):
# Reject hard stoploss on large dip
return False

return True

def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
if not self.config['runmode'].value == 'hyperopt':
dataframe['ma_sl_filter_offset'] = ta.EMA(dataframe, timeperiod=int(self.sl_filter_candles.value)) * self.sl_filter_offset.value

# EWO
dataframe['ewo'] = EWO(dataframe, self.fast_ewo.value, self.slow_ewo.value)

# RSI
dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14)
dataframe['rsi_fast'] = ta.RSI(dataframe, timeperiod=4)

dataframe["roc_max"] = dataframe["close"].pct_change(48).rolling(12).max() * 100

return dataframe

def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
conditions = []

dataframe['sma_offset_buy'] = ta.SMA(dataframe, int(self.base_nb_candles_buy_sma.value)) *self.low_offset_sma.value
dataframe['ema_offset_buy'] = ta.EMA(dataframe, int(self.base_nb_candles_buy_ema.value)) *self.low_offset_ema.value
dataframe['zema_offset_buy'] = zema(dataframe, int(self.base_nb_candles_buy_zema.value)) *self.low_offset_zema.value
dataframe['trima_offset_buy'] = ta.TRIMA(dataframe, int(self.base_nb_candles_buy_trima.value)) *self.low_offset_trima.value
dataframe['ema_offset_sell'] = ta.EMA(dataframe, int(self.base_nb_candles_sell.value)) *self.high_offset_ema.value


dataframe.loc[:, 'buy_tag'] = ''
dataframe.loc[:, 'buy_copy'] = 0
dataframe.loc[:, 'buy'] = 0

buy_offset_sma = (
(dataframe['close'] < dataframe['sma_offset_buy']) &
(
(dataframe['ewo'] < self.ewo_low.value)
|
(
(dataframe['ewo'] > self.ewo_high.value)
&
(dataframe['rsi'] < self.rsi_buy_sma.value)
)
)
)
dataframe.loc[buy_offset_sma, 'buy_tag'] += 'sma '
conditions.append(buy_offset_sma)

buy_offset_ema = (
(dataframe['close'] < dataframe['ema_offset_buy']) &
buy_offset_trima = (
self.buy_condition_trima_enable.value &
(dataframe['close'] < dataframe['trima_offset_buy']) &
(
(dataframe['ewo'] < self.ewo_low.value)
|
(
(dataframe['ewo'] > self.ewo_high.value)
&
(dataframe['rsi'] < self.rsi_buy_ema.value)
(dataframe['rsi'] < self.rsi_buy_trima.value)
)
)
)
dataframe.loc[buy_offset_ema, 'buy_tag'] += 'ema '
conditions.append(buy_offset_ema)
dataframe.loc[buy_offset_trima, 'buy_tag'] += 'trima '
conditions.append(buy_offset_trima)

buy_offset_trima = (
(dataframe['close'] < dataframe['trima_offset_buy']) &
buy_offset_zema = (
self.buy_condition_zema_enable.value &
(dataframe['close'] < dataframe['zema_offset_buy']) &
(
(dataframe['ewo'] < self.ewo_low.value)
|
(
(dataframe['ewo'] > self.ewo_high.value)
&
(dataframe['rsi'] < self.rsi_buy_trima.value)
(dataframe['rsi'] < self.rsi_buy_zema.value)
)
)
)
dataframe.loc[buy_offset_trima, 'buy_tag'] += 'trima '
conditions.append(buy_offset_trima)
dataframe.loc[buy_offset_zema, 'buy_tag'] += 'zema '
conditions.append(buy_offset_zema)

add_check = (
(dataframe['rsi_fast'] < 35)
(dataframe['rsi_fast'] < self.buy_rsi_fast.value)
&
(dataframe['close'] < dataframe['ema_offset_sell'])
(dataframe['close'] < dataframe['ema_offset_buy'])
&
(dataframe['volume'] > 0)
)

if conditions:
dataframe.loc[:, 'buy'] = add_check & reduce(lambda x, y: x | y, conditions)
dataframe.loc[
(add_check & reduce(lambda x, y: x | y, conditions)),
['buy_copy','buy']
]=(1,1)

return dataframe

def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
if self.config['runmode'].value == 'hyperopt':
dataframe['ema_offset_sell'] = ta.EMA(dataframe, int(self.base_nb_candles_sell.value)) *self.high_offset_ema.value
dataframe['ma_sl_filter_offset'] = ta.EMA(dataframe, timeperiod=int(self.sl_filter_candles.value)) * self.sl_filter_offset.value
dataframe.loc[:, 'sell_copy'] = 0

dataframe['ema_offset_sell'] = ta.EMA(dataframe, int(self.base_nb_candles_sell.value)) *self.high_offset_ema.value
dataframe['trima_offset_sell'] = ta.TRIMA(dataframe, int(self.base_nb_candles_sell_trima.value)) *self.high_offset_trima.value

conditions = []

Expand All @@ -258,11 +249,21 @@ def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame
)
)

conditions.append(
(
(dataframe['close'] > dataframe['trima_offset_sell']) &
(dataframe['volume'] > 0)
)
)

if conditions:
dataframe.loc[
reduce(lambda x, y: x | y, conditions),
'sell'
]=1
['sell_copy', 'sell']
]=(1,1)

if not self.config['runmode'].value in ('backtest', 'hyperopt'):
dataframe.loc[:, 'sell'] = 0

return dataframe

Expand Down

0 comments on commit 77745be

Please sign in to comment.