From 32603cb68087c02ef606ba71bd16e6d0ac66eaae Mon Sep 17 00:00:00 2001 From: Masoud Azizi Date: Sun, 8 Aug 2021 01:10:37 +0000 Subject: [PATCH] Brain Stra added --- user_data/hyperopts/brainHo.py | 154 +++++++++++++++++++++++++++++++++ user_data/strategies/brain.py | 144 ++++++++++++++++++++++++++++++ 2 files changed, 298 insertions(+) create mode 100644 user_data/hyperopts/brainHo.py create mode 100644 user_data/strategies/brain.py diff --git a/user_data/hyperopts/brainHo.py b/user_data/hyperopts/brainHo.py new file mode 100644 index 0000000..fa1f6c6 --- /dev/null +++ b/user_data/hyperopts/brainHo.py @@ -0,0 +1,154 @@ +# brain Strategy Hyperopt +# Author: @Mablue (Masoud Azizi) +# github: https://github.com/mablue/ +# IMPORTANT: INSTALL TA BEFOUR RUN: +# :~$ pip install ta +# freqtrade hyperopt --hyperopt brainHo --hyperopt-loss SharpeHyperOptLossDaily --spaces buy sell roi --strategy brain -j 3 -e 700 +# --- Do not remove these libs --- +from functools import reduce +from typing import Any, Callable, Dict, List, Reversible + +import numpy as np # noqa +import pandas as pd # noqa +from pandas import DataFrame +from skopt.space import Categorical, Dimension, Integer # noqa + +from freqtrade.optimize.hyperopt_interface import IHyperOpt + +# -------------------------------- +# Add your lib to import here +# import talib.abstract as ta # noqa +from ta import add_all_ta_features +from ta.utils import dropna +import freqtrade.vendor.qtpylib.indicators as qtpylib + +##################### SETTINGS ######################### +# this is your trading brain nodes count +# you can change it and see the results... +# Importand will same with brain.py +nodes = 4 +decimals = 2 + +#################### END SETTINGS ###################### + +# do not edit this line: +decimals = 10 ** decimals + + +PastKnowledges = ["open", "high", "low", "close", "volume", "volume_adi", "volume_obv", + "volume_cmf", "volume_fi", "volume_mfi", "volume_em", "volume_sma_em", "volume_vpt", + "volume_nvi", "volume_vwap", "volatility_atr", "volatility_bbm", "volatility_bbh", + "volatility_bbl", "volatility_bbw", "volatility_bbp", "volatility_bbhi", + "volatility_bbli", "volatility_kcc", "volatility_kch", "volatility_kcl", + "volatility_kcw", "volatility_kcp", "volatility_kchi", "volatility_kcli", + "volatility_dcl", "volatility_dch", "volatility_dcm", "volatility_dcw", + "volatility_dcp", "volatility_ui", "trend_macd", "trend_macd_signal", + "trend_macd_diff", "trend_sma_fast", "trend_sma_slow", "trend_ema_fast", + "trend_ema_slow", "trend_adx", "trend_adx_pos", "trend_adx_neg", "trend_vortex_ind_pos", + "trend_vortex_ind_neg", "trend_vortex_ind_diff", "trend_trix", + "trend_mass_index", "trend_cci", "trend_dpo", "trend_kst", + "trend_kst_sig", "trend_kst_diff", "trend_ichimoku_conv", + "trend_ichimoku_base", "trend_ichimoku_a", "trend_ichimoku_b", + "trend_visual_ichimoku_a", "trend_visual_ichimoku_b", "trend_aroon_up", + "trend_aroon_down", "trend_aroon_ind", "trend_psar_up", "trend_psar_down", + "trend_psar_up_indicator", "trend_psar_down_indicator", "trend_stc", + "momentum_rsi", "momentum_stoch_rsi", "momentum_stoch_rsi_k", + "momentum_stoch_rsi_d", "momentum_tsi", "momentum_uo", "momentum_stoch", + "momentum_stoch_signal", "momentum_wr", "momentum_ao", "momentum_kama", + "momentum_roc", "momentum_ppo", "momentum_ppo_signal", "momentum_ppo_hist", + "others_dr", "others_dlr", "others_cr"] + + +print(decimals) + + +class brainHo(IHyperOpt): + + @staticmethod + def indicator_space() -> List[Dimension]: + """ + Define your Hyperopt space for searching buy strategy parameters. + """ + brain = list() + + for i in range(nodes): + brain.append(Categorical(PastKnowledges, name=f'buy-node-input-{i}')) + brain.append(Categorical([0, 1], name=f'buy-node-enabled-{i}')) + brain.append(Categorical([-1, 1], name=f'buy-node-reversed-{i}')) + brain.append(Integer(0, decimals, name=f'buy-node-wight-{i}')) + + return brain + + @staticmethod + def buy_strategy_generator(params: Dict[str, Any]) -> Callable: + """ + Define the buy strategy parameters to be used by Hyperopt. + """ + def populate_buy_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Buy strategy Hyperopt will build and use. + """ + conditions = [] + RESULT = 0 + + for i in range(nodes): + DFINP = dataframe[params[f'buy-node-input-{i}']] + ENABLED = params[f'buy-node-enabled-{i}'] + REVERSE = params[f'buy-node-reversed-{i}'] + WIGHT = params[f'buy-node-wight-{i}']/decimals + RESULT += DFINP*ENABLED*REVERSE*WIGHT + + conditions.append(RESULT > 0) + + if conditions: + dataframe.loc[ + reduce(lambda x, y: x & y, conditions), + 'buy'] = 1 + + return dataframe + + return populate_buy_trend + + @ staticmethod + def sell_indicator_space() -> List[Dimension]: + """ + Define your Hyperopt space for searching sell strategy parameters. + """ + brain = list() + + for i in range(nodes): + brain.append(Categorical(PastKnowledges, name=f'sell-node-input-{i}')) + brain.append(Categorical([0, 1], name=f'sell-node-enabled-{i}')) + brain.append(Categorical([-1, 1], name=f'sell-node-reversed-{i}')) + brain.append(Integer(0, decimals, name=f'sell-node-wight-{i}')) + + return brain + + @ staticmethod + def sell_strategy_generator(params: Dict[str, Any]) -> Callable: + """ + Define the sell strategy parameters to be used by Hyperopt. + """ + def populate_sell_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: + """ + Sell strategy Hyperopt will build and use. + """ + conditions = [] + RESULT = 0 + for i in range(nodes): + DFINP = dataframe[params[f'sell-node-input-{i}']] + ENABLED = params[f'sell-node-enabled-{i}'] + REVERSE = params[f'sell-node-reversed-{i}'] + WIGHT = params[f'sell-node-wight-{i}']/decimals + RESULT += DFINP*ENABLED*REVERSE*WIGHT + + conditions.append(RESULT > 0) + + if conditions: + dataframe.loc[ + reduce(lambda x, y: x & y, conditions), + 'sell']=1 + + return dataframe + + return populate_sell_trend diff --git a/user_data/strategies/brain.py b/user_data/strategies/brain.py new file mode 100644 index 0000000..95258a8 --- /dev/null +++ b/user_data/strategies/brain.py @@ -0,0 +1,144 @@ +# brain Strategy +# Author: @Mablue (Masoud Azizi) +# github: https://github.com/mablue/ +# IMPORTANT: INSTALL TA BEFOUR RUN(pip install ta) +# freqtrade hyperopt --hyperopt brainHo --hyperopt-loss SharpeHyperOptLossDaily --spaces buy sell roi --strategy brain -j 3 -e 700 + +# --- Do not remove these libs --- +import logging + +from numpy.lib import math +from freqtrade.strategy.interface import IStrategy +from pandas import DataFrame +# -------------------------------- + +# Add your lib to import here +# import talib.abstract as ta +import pandas as pd +# import talib.abstract as ta +from ta import add_all_ta_features +from ta.utils import dropna +import freqtrade.vendor.qtpylib.indicators as qtpylib +from functools import reduce +import numpy as np + + +class brain(IStrategy): + ##################### SETTINGS ######################### + # this is your trading brain nodes count + # you can change it and see the results... + # Importand will same with brainHo.py + nodes = 4 + + # 1 means 1, 10 means 0.1, 100 means 0.01 + decimals = 2 + #################### END SETTINGS ###################### + + ##################### HYPEROPT RESULTS PASTE PLACE ######################### + # * 10/700: 178 trades. 103/59/16 Wins/Draws/Losses. Avg profit 1.35%. Median profit 2.30%. Total profit 0.02400559 BTC ( 24.01Σ%). Avg duration 1 day, 18:31:00 min. Objective: -5.31583 + + # Buy hyperspace params: + buy_params = { + "buy-node-input-0": "trend_mass_index", + "buy-node-enabled-0": 0, + "buy-node-reversed-0": 1, + "buy-node-wight-0": 81, + "buy-node-input-1": "momentum_ao", + "buy-node-enabled-1": 0, + "buy-node-reversed-1": -1, + "buy-node-wight-1": 36, + "buy-node-input-2": "volatility_ui", + "buy-node-enabled-2": 1, + "buy-node-reversed-2": -1, + "buy-node-wight-2": 59, + "buy-node-input-3": "volatility_kcp", + "buy-node-enabled-3": 1, + "buy-node-reversed-3": 1, + "buy-node-wight-3": 76, + } + + # Sell hyperspace params: + sell_params = { + "sell-node-input-0": "volatility_bbp", + "sell-node-enabled-0": 0, + "sell-node-reversed-0": -1, + "sell-node-wight-0": 25, + "sell-node-input-1": "trend_vortex_ind_diff", + "sell-node-enabled-1": 0, + "sell-node-reversed-1": 1, + "sell-node-wight-1": 1, + "sell-node-input-2": "trend_macd", + "sell-node-enabled-2": 0, + "sell-node-reversed-2": -1, + "sell-node-wight-2": 4, + "sell-node-input-3": "momentum_ppo_hist", + "sell-node-enabled-3": 0, + "sell-node-reversed-3": -1, + "sell-node-wight-3": 72, + } + + # ROI table: + minimal_roi = { + "0": 0.347, + "392": 0.126, + "727": 0.023, + "1411": 0 + } + # Stoploss: + stoploss = -0.256 + + #################### END HYPEROPT RESULTS PASTE PLACE ####################### + + # Buy hypers + timeframe = '1h' + + # do not edit this line: + decimals = 10 ** decimals + + def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + # Add all ta features + # dataframe = dropna(dataframe) + dataframe = add_all_ta_features( + dataframe, open="open", high="high", low="low", close="close", volume="volume", fillna=False) + return dataframe + + def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + + conditions = [] + RESULT = 0 + + for i in range(self.nodes): + DFINP = dataframe[self.buy_params[f'buy-node-input-{i}']] + ENABLED = self.buy_params[f'buy-node-enabled-{i}'] + REVERSE = self.buy_params[f'buy-node-reversed-{i}'] + WIGHT = self.buy_params[f'buy-node-wight-{i}']/self.decimals + RESULT += DFINP*ENABLED*REVERSE*WIGHT + + conditions.append(RESULT > 0) + + if conditions: + dataframe.loc[ + reduce(lambda x, y: x & y, conditions), + 'buy'] = 1 + + return dataframe + + def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + + conditions = [] + RESULT = 0 + for i in range(self.nodes): + DFINP = dataframe[self.sell_params[f'sell-node-input-{i}']] + ENABLED = self.sell_params[f'sell-node-enabled-{i}'] + REVERSE = self.sell_params[f'sell-node-reversed-{i}'] + WIGHT = self.sell_params[f'sell-node-wight-{i}']/self.decimals + RESULT += DFINP*ENABLED*REVERSE*WIGHT + + conditions.append(RESULT > 0) + + if conditions: + dataframe.loc[ + reduce(lambda x, y: x & y, conditions), + 'sell']=1 + + return dataframe