From 2b370abc40f6e2258901d58256486e2b5de2a3e9 Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 02:33:17 -0500 Subject: [PATCH 01/28] Add files via upload --- user_data/strategies/random_strategy.py | 338 ++++++++++++++++++++++++ 1 file changed, 338 insertions(+) create mode 100644 user_data/strategies/random_strategy.py diff --git a/user_data/strategies/random_strategy.py b/user_data/strategies/random_strategy.py new file mode 100644 index 000000000..1b2a1efff --- /dev/null +++ b/user_data/strategies/random_strategy.py @@ -0,0 +1,338 @@ + +# --- Do not remove these libs --- +from freqtrade.strategy.interface import IStrategy +from typing import Dict, List +from hyperopt import hp +from functools import reduce +from pandas import DataFrame +# -------------------------------- + +# Add your lib to import here +import talib.abstract as ta +import freqtrade.vendor.qtpylib.indicators as qtpylib +import numpy # noqa + +import random + +# Update this variable if you change the class name +class_name = 'RandomStrategy' + + +# This class is a sample. Feel free to customize it. + + + + +def Select(): + param = [] + random_items = [] + param.append(str('[' + 'uptrend_long_ema' + '[' + 'enabled' + ']')) + param.append(str('[' + 'macd_below_zero' + '][' + 'enabled' + ']')) + param.append(str('[' + 'uptrend_short_ema' '][' + 'enabled'+ ']')) + param.append(str('[' + 'mfi' '][' + 'enabled'+ ']')) + param.append(str('[' + 'fastd' '][' + 'enabled'+ ']')) + param.append(str('[' + 'adx' '][' + 'enabled'+ ']')) + param.append(str('[' + 'rsi' '][' + 'enabled'+ ']')) + param.append(str('[' + 'over_sar' '][' + 'enabled'+ ']')) + param.append(str('[' + 'green_candle' '][' + 'enabled'+ ']')) + param.append(str('[' + 'uptrend_sma' '][' + 'enabled'+ ']')) + param.append(str('[' + 'closebb' '][' + 'enabled'+ ']')) + param.append(str('[' + 'temabb' '][' + 'enabled'+ ']')) + param.append(str('[' + 'fastdt' '][' + 'enabled'+ ']')) + param.append(str('[' + 'ao' '][' + 'enabled'+ ']')) + param.append(str('[' + 'ema3' '][' + 'enabled'+ ']')) + param.append(str('[' + 'macd' '][' + 'enabled'+ ']')) + param.append(str('[' + 'closesar' '][' + 'enabled'+ ']')) + param.append(str('[' + 'htsine' '][' + 'enabled'+ ']')) + param.append(str('[' + 'has' '][' + 'enabled'+ ']')) + param.append(str('[' + 'plusdi' '][' + 'enabled'+ ']')) + howmany = random.randint(1,20) + random_items = random.choices(population=param, k=howmany) + print(' ') + print('The Parameters Enabled Are As Follows!!!: ' + str(random_items)) + print(' ') + return random_items + + + + +class RandomStrategy(IStrategy): + """ + This is a test strategy to inspire you. + More information in https://github.com/gcarq/freqtrade/blob/develop/docs/bot-optimization.md + + You can: + - Rename the class name (Do not forget to update class_name) + - Add any methods you want to build your strategy + - Add any lib you need to build your strategy + + You must keep: + - the lib in the section "Do not remove these libs" + - the prototype for the methods: minimal_roi, stoploss, populate_indicators, populate_buy_trend, + populate_sell_trend, hyperopt_space, buy_strategy_generator + """ + + # Minimal ROI designed for the strategy. + # This attribute will be overridden if the config file contains "minimal_roi" + minimal_roi = { + "40": 0.0, + "30": 0.01, + "20": 0.02, + "0": 0.04 + } + + ticker_interval = 5 + + # Optimal stoploss designed for the strategy + # This attribute will be overridden if the config file contains "stoploss" + stoploss = -0.10 + + def populate_indicators(self, dataframe: DataFrame) -> DataFrame: + """ + Adds several different TA indicators to the given DataFrame + + Performance Note: For the best performance be frugal on the number of indicators + you are using. Let uncomment only the indicator you are using in your strategies + or your hyperopt configuration, otherwise you will waste your memory and CPU usage. + """ + + # Momentum Indicator + # ------------------------------------ + + # ADX + dataframe['adx'] = ta.ADX(dataframe) + + + # Awesome oscillator + dataframe['ao'] = qtpylib.awesome_oscillator(dataframe) + + # Commodity Channel Index: values Oversold:<-100, Overbought:>100 + dataframe['cci'] = ta.CCI(dataframe) + + # MACD + macd = ta.MACD(dataframe) + dataframe['macd'] = macd['macd'] + dataframe['macdsignal'] = macd['macdsignal'] + dataframe['macdhist'] = macd['macdhist'] + + # MFI + dataframe['mfi'] = ta.MFI(dataframe) + + # Minus Directional Indicator / Movement + dataframe['minus_dm'] = ta.MINUS_DM(dataframe) + dataframe['minus_di'] = ta.MINUS_DI(dataframe) + + # Plus Directional Indicator / Movement + dataframe['plus_dm'] = ta.PLUS_DM(dataframe) + dataframe['plus_di'] = ta.PLUS_DI(dataframe) + dataframe['minus_di'] = ta.MINUS_DI(dataframe) + + # ROC + dataframe['roc'] = ta.ROC(dataframe) + + # RSI + dataframe['rsi'] = ta.RSI(dataframe) + + # Inverse Fisher transform on RSI, values [-1.0, 1.0] (https://goo.gl/2JGGoy) + rsi = 0.1 * (dataframe['rsi'] - 50) + dataframe['fisher_rsi'] = (numpy.exp(2 * rsi) - 1) / (numpy.exp(2 * rsi) + 1) + + # Inverse Fisher transform on RSI normalized, value [0.0, 100.0] (https://goo.gl/2JGGoy) + dataframe['fisher_rsi_norma'] = 50 * (dataframe['fisher_rsi'] + 1) + + # Stoch + stoch = ta.STOCH(dataframe) + dataframe['slowd'] = stoch['slowd'] + dataframe['slowk'] = stoch['slowk'] + + # Stoch fast + stoch_fast = ta.STOCHF(dataframe) + dataframe['fastd'] = stoch_fast['fastd'] + dataframe['fastk'] = stoch_fast['fastk'] + + # Stoch RSI + stoch_rsi = ta.STOCHRSI(dataframe) + dataframe['fastd_rsi'] = stoch_rsi['fastd'] + dataframe['fastk_rsi'] = stoch_rsi['fastk'] + + + # Overlap Studies + # ------------------------------------ + + """ + # Previous Bollinger bands + # Because ta.BBANDS implementation is broken with small numbers, it actually + # returns middle band for all the three bands. Switch to qtpylib.bollinger_bands + # and use middle band instead. + + dataframe['blower'] = ta.BBANDS(dataframe, nbdevup=2, nbdevdn=2)['lowerband'] + """ + + # Bollinger bands + bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2) + dataframe['bb_lowerband'] = bollinger['lower'] + dataframe['bb_middleband'] = bollinger['mid'] + dataframe['bb_upperband'] = bollinger['upper'] + + + # EMA - Exponential Moving Average + dataframe['ema3'] = ta.EMA(dataframe, timeperiod=3) + dataframe['ema5'] = ta.EMA(dataframe, timeperiod=5) + dataframe['ema10'] = ta.EMA(dataframe, timeperiod=10) + dataframe['ema50'] = ta.EMA(dataframe, timeperiod=50) + dataframe['ema100'] = ta.EMA(dataframe, timeperiod=100) + + # SAR Parabol + dataframe['sar'] = ta.SAR(dataframe) + + # SMA - Simple Moving Average + dataframe['sma'] = ta.SMA(dataframe, timeperiod=40) + + + # TEMA - Triple Exponential Moving Average + dataframe['tema'] = ta.TEMA(dataframe, timeperiod=9) + + # Cycle Indicator + # ------------------------------------ + # Hilbert Transform Indicator - SineWave + hilbert = ta.HT_SINE(dataframe) + dataframe['htsine'] = hilbert['sine'] + dataframe['htleadsine'] = hilbert['leadsine'] + + # Pattern Recognition - Bullish candlestick patterns + # ------------------------------------ + + # Hammer: values [0, 100] + dataframe['CDLHAMMER'] = ta.CDLHAMMER(dataframe) + # Inverted Hammer: values [0, 100] + dataframe['CDLINVERTEDHAMMER'] = ta.CDLINVERTEDHAMMER(dataframe) + # Dragonfly Doji: values [0, 100] + dataframe['CDLDRAGONFLYDOJI'] = ta.CDLDRAGONFLYDOJI(dataframe) + # Piercing Line: values [0, 100] + dataframe['CDLPIERCING'] = ta.CDLPIERCING(dataframe) # values [0, 100] + # Morningstar: values [0, 100] + dataframe['CDLMORNINGSTAR'] = ta.CDLMORNINGSTAR(dataframe) # values [0, 100] + # Three White Soldiers: values [0, 100] + dataframe['CDL3WHITESOLDIERS'] = ta.CDL3WHITESOLDIERS(dataframe) # values [0, 100] + + + # Pattern Recognition - Bearish candlestick patterns + # ------------------------------------ + + # Hanging Man: values [0, 100] + dataframe['CDLHANGINGMAN'] = ta.CDLHANGINGMAN(dataframe) + # Shooting Star: values [0, 100] + dataframe['CDLSHOOTINGSTAR'] = ta.CDLSHOOTINGSTAR(dataframe) + # Gravestone Doji: values [0, 100] + dataframe['CDLGRAVESTONEDOJI'] = ta.CDLGRAVESTONEDOJI(dataframe) + # Dark Cloud Cover: values [0, 100] + dataframe['CDLDARKCLOUDCOVER'] = ta.CDLDARKCLOUDCOVER(dataframe) + # Evening Doji Star: values [0, 100] + dataframe['CDLEVENINGDOJISTAR'] = ta.CDLEVENINGDOJISTAR(dataframe) + # Evening Star: values [0, 100] + dataframe['CDLEVENINGSTAR'] = ta.CDLEVENINGSTAR(dataframe) + + + # Pattern Recognition - Bullish/Bearish candlestick patterns + # ------------------------------------ + + # Three Line Strike: values [0, -100, 100] + dataframe['CDL3LINESTRIKE'] = ta.CDL3LINESTRIKE(dataframe) + # Spinning Top: values [0, -100, 100] + dataframe['CDLSPINNINGTOP'] = ta.CDLSPINNINGTOP(dataframe) # values [0, -100, 100] + # Engulfing: values [0, -100, 100] + dataframe['CDLENGULFING'] = ta.CDLENGULFING(dataframe) # values [0, -100, 100] + # Harami: values [0, -100, 100] + dataframe['CDLHARAMI'] = ta.CDLHARAMI(dataframe) # values [0, -100, 100] + # Three Outside Up/Down: values [0, -100, 100] + dataframe['CDL3OUTSIDE'] = ta.CDL3OUTSIDE(dataframe) # values [0, -100, 100] + # Three Inside Up/Down: values [0, -100, 100] + dataframe['CDL3INSIDE'] = ta.CDL3INSIDE(dataframe) # values [0, -100, 100] + + + # Chart type + # ------------------------------------ + + # Heikinashi stategy + heikinashi = qtpylib.heikinashi(dataframe) + dataframe['ha_open'] = heikinashi['open'] + dataframe['ha_close'] = heikinashi['close'] + dataframe['ha_high'] = heikinashi['high'] + dataframe['ha_low'] = heikinashi['low'] + + + return dataframe + + params = Select() + valm = random.randint(1,100) + valfast = random.randint(1,100) + valadx = random.randint(1,100) + valrsi = random.randint(1,100) + def populate_buy_trend(self, dataframe: DataFrame) -> DataFrame: + + conditions = [] + # GUARDS AND TRENDS + if 'uptrend_long_ema' in str(self.params): + conditions.append(dataframe['ema50'] > dataframe['ema100']) + if 'macd_below_zero' in str(self.params): + conditions.append(dataframe['macd'] < 0) + if 'uptrend_short_ema' in str(self.params): + conditions.append(dataframe['ema5'] > dataframe['ema10']) + if 'mfi' in str(self.params): + print('MFI Value: ' + str(self.valm)) + conditions.append(dataframe['mfi'] < self.valm) + if 'fastd' in str(self.params): + print('FASTD Value :' + str(self.valfast)) + conditions.append(dataframe['fastd'] < self.valfast) + if 'adx' in str(self.params): + print('ADX Value :' + str(self.valadx)) + conditions.append(dataframe['adx'] > self.valadx) + if 'rsi' in str(self.params): + print('RSI Value :' + str(self.valrsi)) + conditions.append(dataframe['rsi'] < self.valrsi) + if 'over_sar' in str(self.params): + conditions.append(dataframe['close'] > dataframe['sar']) + if 'green_candle' in str(self.params): + conditions.append(dataframe['close'] > dataframe['open']) + if 'uptrend_sma' in str(self.params): + prevsma = dataframe['sma'].shift(1) + conditions.append(dataframe['sma'] > prevsma) + if 'closebb' in str(self.params): + conditions.append(dataframe['close'] < dataframe['bb_lowerband']) + if 'temabb' in str(self.params): + conditions.append(dataframe['tema'] < dataframe['bb_lowerband']) + if 'fastdt' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['fastd'], 10.0)) + if 'ao' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['ao'], 0.0)) + if 'ema3' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['ema3'], dataframe['ema10'])) + if 'macd' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['macd'], dataframe['macdsignal'])) + if 'closesar' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['close'], dataframe['sar'])) + if 'htsine' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['htleadsine'], dataframe['htsine'])) + if 'has' in str(self.params): + conditions.append((qtpylib.crossed_above(dataframe['ha_close'], dataframe['ha_open'])) & (dataframe['ha_low'] == dataframe['ha_open'])) + if 'plusdi' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['plus_di'], dataframe['minus_di'])) + + dataframe.loc[ + reduce(lambda x, y: x & y, conditions), + 'buy'] = 1 + + return dataframe + + def populate_sell_trend(self, dataframe: DataFrame) -> DataFrame: + """ + Based on TA indicators, populates the sell signal for the given dataframe + :param dataframe: DataFrame + :return: DataFrame with buy column + """ + dataframe.loc[ + ( + ), + 'sell'] = 1 + return dataframe + From 43e77e1fee0cc24fe459ae0e2efc5d5126cc0dd7 Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 04:49:18 -0500 Subject: [PATCH 02/28] Update and rename random_strategy.py to defauult_random_strategy.py --- .../{random_strategy.py => defauult_random_strategy.py} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename user_data/strategies/{random_strategy.py => defauult_random_strategy.py} (99%) diff --git a/user_data/strategies/random_strategy.py b/user_data/strategies/defauult_random_strategy.py similarity index 99% rename from user_data/strategies/random_strategy.py rename to user_data/strategies/defauult_random_strategy.py index 1b2a1efff..358288b2f 100644 --- a/user_data/strategies/random_strategy.py +++ b/user_data/strategies/defauult_random_strategy.py @@ -15,7 +15,7 @@ import numpy # noqa import random # Update this variable if you change the class name -class_name = 'RandomStrategy' +class_name = 'DefaultStrategy' # This class is a sample. Feel free to customize it. @@ -56,7 +56,7 @@ def Select(): -class RandomStrategy(IStrategy): +class DefaultStrategy(IStrategy): """ This is a test strategy to inspire you. More information in https://github.com/gcarq/freqtrade/blob/develop/docs/bot-optimization.md From d21c2cebeb76c172c39182ac1e25b0c30e56d745 Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 06:17:48 -0500 Subject: [PATCH 03/28] Delete defauult_random_strategy.py --- .../strategies/defauult_random_strategy.py | 338 ------------------ 1 file changed, 338 deletions(-) delete mode 100644 user_data/strategies/defauult_random_strategy.py diff --git a/user_data/strategies/defauult_random_strategy.py b/user_data/strategies/defauult_random_strategy.py deleted file mode 100644 index 358288b2f..000000000 --- a/user_data/strategies/defauult_random_strategy.py +++ /dev/null @@ -1,338 +0,0 @@ - -# --- Do not remove these libs --- -from freqtrade.strategy.interface import IStrategy -from typing import Dict, List -from hyperopt import hp -from functools import reduce -from pandas import DataFrame -# -------------------------------- - -# Add your lib to import here -import talib.abstract as ta -import freqtrade.vendor.qtpylib.indicators as qtpylib -import numpy # noqa - -import random - -# Update this variable if you change the class name -class_name = 'DefaultStrategy' - - -# This class is a sample. Feel free to customize it. - - - - -def Select(): - param = [] - random_items = [] - param.append(str('[' + 'uptrend_long_ema' + '[' + 'enabled' + ']')) - param.append(str('[' + 'macd_below_zero' + '][' + 'enabled' + ']')) - param.append(str('[' + 'uptrend_short_ema' '][' + 'enabled'+ ']')) - param.append(str('[' + 'mfi' '][' + 'enabled'+ ']')) - param.append(str('[' + 'fastd' '][' + 'enabled'+ ']')) - param.append(str('[' + 'adx' '][' + 'enabled'+ ']')) - param.append(str('[' + 'rsi' '][' + 'enabled'+ ']')) - param.append(str('[' + 'over_sar' '][' + 'enabled'+ ']')) - param.append(str('[' + 'green_candle' '][' + 'enabled'+ ']')) - param.append(str('[' + 'uptrend_sma' '][' + 'enabled'+ ']')) - param.append(str('[' + 'closebb' '][' + 'enabled'+ ']')) - param.append(str('[' + 'temabb' '][' + 'enabled'+ ']')) - param.append(str('[' + 'fastdt' '][' + 'enabled'+ ']')) - param.append(str('[' + 'ao' '][' + 'enabled'+ ']')) - param.append(str('[' + 'ema3' '][' + 'enabled'+ ']')) - param.append(str('[' + 'macd' '][' + 'enabled'+ ']')) - param.append(str('[' + 'closesar' '][' + 'enabled'+ ']')) - param.append(str('[' + 'htsine' '][' + 'enabled'+ ']')) - param.append(str('[' + 'has' '][' + 'enabled'+ ']')) - param.append(str('[' + 'plusdi' '][' + 'enabled'+ ']')) - howmany = random.randint(1,20) - random_items = random.choices(population=param, k=howmany) - print(' ') - print('The Parameters Enabled Are As Follows!!!: ' + str(random_items)) - print(' ') - return random_items - - - - -class DefaultStrategy(IStrategy): - """ - This is a test strategy to inspire you. - More information in https://github.com/gcarq/freqtrade/blob/develop/docs/bot-optimization.md - - You can: - - Rename the class name (Do not forget to update class_name) - - Add any methods you want to build your strategy - - Add any lib you need to build your strategy - - You must keep: - - the lib in the section "Do not remove these libs" - - the prototype for the methods: minimal_roi, stoploss, populate_indicators, populate_buy_trend, - populate_sell_trend, hyperopt_space, buy_strategy_generator - """ - - # Minimal ROI designed for the strategy. - # This attribute will be overridden if the config file contains "minimal_roi" - minimal_roi = { - "40": 0.0, - "30": 0.01, - "20": 0.02, - "0": 0.04 - } - - ticker_interval = 5 - - # Optimal stoploss designed for the strategy - # This attribute will be overridden if the config file contains "stoploss" - stoploss = -0.10 - - def populate_indicators(self, dataframe: DataFrame) -> DataFrame: - """ - Adds several different TA indicators to the given DataFrame - - Performance Note: For the best performance be frugal on the number of indicators - you are using. Let uncomment only the indicator you are using in your strategies - or your hyperopt configuration, otherwise you will waste your memory and CPU usage. - """ - - # Momentum Indicator - # ------------------------------------ - - # ADX - dataframe['adx'] = ta.ADX(dataframe) - - - # Awesome oscillator - dataframe['ao'] = qtpylib.awesome_oscillator(dataframe) - - # Commodity Channel Index: values Oversold:<-100, Overbought:>100 - dataframe['cci'] = ta.CCI(dataframe) - - # MACD - macd = ta.MACD(dataframe) - dataframe['macd'] = macd['macd'] - dataframe['macdsignal'] = macd['macdsignal'] - dataframe['macdhist'] = macd['macdhist'] - - # MFI - dataframe['mfi'] = ta.MFI(dataframe) - - # Minus Directional Indicator / Movement - dataframe['minus_dm'] = ta.MINUS_DM(dataframe) - dataframe['minus_di'] = ta.MINUS_DI(dataframe) - - # Plus Directional Indicator / Movement - dataframe['plus_dm'] = ta.PLUS_DM(dataframe) - dataframe['plus_di'] = ta.PLUS_DI(dataframe) - dataframe['minus_di'] = ta.MINUS_DI(dataframe) - - # ROC - dataframe['roc'] = ta.ROC(dataframe) - - # RSI - dataframe['rsi'] = ta.RSI(dataframe) - - # Inverse Fisher transform on RSI, values [-1.0, 1.0] (https://goo.gl/2JGGoy) - rsi = 0.1 * (dataframe['rsi'] - 50) - dataframe['fisher_rsi'] = (numpy.exp(2 * rsi) - 1) / (numpy.exp(2 * rsi) + 1) - - # Inverse Fisher transform on RSI normalized, value [0.0, 100.0] (https://goo.gl/2JGGoy) - dataframe['fisher_rsi_norma'] = 50 * (dataframe['fisher_rsi'] + 1) - - # Stoch - stoch = ta.STOCH(dataframe) - dataframe['slowd'] = stoch['slowd'] - dataframe['slowk'] = stoch['slowk'] - - # Stoch fast - stoch_fast = ta.STOCHF(dataframe) - dataframe['fastd'] = stoch_fast['fastd'] - dataframe['fastk'] = stoch_fast['fastk'] - - # Stoch RSI - stoch_rsi = ta.STOCHRSI(dataframe) - dataframe['fastd_rsi'] = stoch_rsi['fastd'] - dataframe['fastk_rsi'] = stoch_rsi['fastk'] - - - # Overlap Studies - # ------------------------------------ - - """ - # Previous Bollinger bands - # Because ta.BBANDS implementation is broken with small numbers, it actually - # returns middle band for all the three bands. Switch to qtpylib.bollinger_bands - # and use middle band instead. - - dataframe['blower'] = ta.BBANDS(dataframe, nbdevup=2, nbdevdn=2)['lowerband'] - """ - - # Bollinger bands - bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2) - dataframe['bb_lowerband'] = bollinger['lower'] - dataframe['bb_middleband'] = bollinger['mid'] - dataframe['bb_upperband'] = bollinger['upper'] - - - # EMA - Exponential Moving Average - dataframe['ema3'] = ta.EMA(dataframe, timeperiod=3) - dataframe['ema5'] = ta.EMA(dataframe, timeperiod=5) - dataframe['ema10'] = ta.EMA(dataframe, timeperiod=10) - dataframe['ema50'] = ta.EMA(dataframe, timeperiod=50) - dataframe['ema100'] = ta.EMA(dataframe, timeperiod=100) - - # SAR Parabol - dataframe['sar'] = ta.SAR(dataframe) - - # SMA - Simple Moving Average - dataframe['sma'] = ta.SMA(dataframe, timeperiod=40) - - - # TEMA - Triple Exponential Moving Average - dataframe['tema'] = ta.TEMA(dataframe, timeperiod=9) - - # Cycle Indicator - # ------------------------------------ - # Hilbert Transform Indicator - SineWave - hilbert = ta.HT_SINE(dataframe) - dataframe['htsine'] = hilbert['sine'] - dataframe['htleadsine'] = hilbert['leadsine'] - - # Pattern Recognition - Bullish candlestick patterns - # ------------------------------------ - - # Hammer: values [0, 100] - dataframe['CDLHAMMER'] = ta.CDLHAMMER(dataframe) - # Inverted Hammer: values [0, 100] - dataframe['CDLINVERTEDHAMMER'] = ta.CDLINVERTEDHAMMER(dataframe) - # Dragonfly Doji: values [0, 100] - dataframe['CDLDRAGONFLYDOJI'] = ta.CDLDRAGONFLYDOJI(dataframe) - # Piercing Line: values [0, 100] - dataframe['CDLPIERCING'] = ta.CDLPIERCING(dataframe) # values [0, 100] - # Morningstar: values [0, 100] - dataframe['CDLMORNINGSTAR'] = ta.CDLMORNINGSTAR(dataframe) # values [0, 100] - # Three White Soldiers: values [0, 100] - dataframe['CDL3WHITESOLDIERS'] = ta.CDL3WHITESOLDIERS(dataframe) # values [0, 100] - - - # Pattern Recognition - Bearish candlestick patterns - # ------------------------------------ - - # Hanging Man: values [0, 100] - dataframe['CDLHANGINGMAN'] = ta.CDLHANGINGMAN(dataframe) - # Shooting Star: values [0, 100] - dataframe['CDLSHOOTINGSTAR'] = ta.CDLSHOOTINGSTAR(dataframe) - # Gravestone Doji: values [0, 100] - dataframe['CDLGRAVESTONEDOJI'] = ta.CDLGRAVESTONEDOJI(dataframe) - # Dark Cloud Cover: values [0, 100] - dataframe['CDLDARKCLOUDCOVER'] = ta.CDLDARKCLOUDCOVER(dataframe) - # Evening Doji Star: values [0, 100] - dataframe['CDLEVENINGDOJISTAR'] = ta.CDLEVENINGDOJISTAR(dataframe) - # Evening Star: values [0, 100] - dataframe['CDLEVENINGSTAR'] = ta.CDLEVENINGSTAR(dataframe) - - - # Pattern Recognition - Bullish/Bearish candlestick patterns - # ------------------------------------ - - # Three Line Strike: values [0, -100, 100] - dataframe['CDL3LINESTRIKE'] = ta.CDL3LINESTRIKE(dataframe) - # Spinning Top: values [0, -100, 100] - dataframe['CDLSPINNINGTOP'] = ta.CDLSPINNINGTOP(dataframe) # values [0, -100, 100] - # Engulfing: values [0, -100, 100] - dataframe['CDLENGULFING'] = ta.CDLENGULFING(dataframe) # values [0, -100, 100] - # Harami: values [0, -100, 100] - dataframe['CDLHARAMI'] = ta.CDLHARAMI(dataframe) # values [0, -100, 100] - # Three Outside Up/Down: values [0, -100, 100] - dataframe['CDL3OUTSIDE'] = ta.CDL3OUTSIDE(dataframe) # values [0, -100, 100] - # Three Inside Up/Down: values [0, -100, 100] - dataframe['CDL3INSIDE'] = ta.CDL3INSIDE(dataframe) # values [0, -100, 100] - - - # Chart type - # ------------------------------------ - - # Heikinashi stategy - heikinashi = qtpylib.heikinashi(dataframe) - dataframe['ha_open'] = heikinashi['open'] - dataframe['ha_close'] = heikinashi['close'] - dataframe['ha_high'] = heikinashi['high'] - dataframe['ha_low'] = heikinashi['low'] - - - return dataframe - - params = Select() - valm = random.randint(1,100) - valfast = random.randint(1,100) - valadx = random.randint(1,100) - valrsi = random.randint(1,100) - def populate_buy_trend(self, dataframe: DataFrame) -> DataFrame: - - conditions = [] - # GUARDS AND TRENDS - if 'uptrend_long_ema' in str(self.params): - conditions.append(dataframe['ema50'] > dataframe['ema100']) - if 'macd_below_zero' in str(self.params): - conditions.append(dataframe['macd'] < 0) - if 'uptrend_short_ema' in str(self.params): - conditions.append(dataframe['ema5'] > dataframe['ema10']) - if 'mfi' in str(self.params): - print('MFI Value: ' + str(self.valm)) - conditions.append(dataframe['mfi'] < self.valm) - if 'fastd' in str(self.params): - print('FASTD Value :' + str(self.valfast)) - conditions.append(dataframe['fastd'] < self.valfast) - if 'adx' in str(self.params): - print('ADX Value :' + str(self.valadx)) - conditions.append(dataframe['adx'] > self.valadx) - if 'rsi' in str(self.params): - print('RSI Value :' + str(self.valrsi)) - conditions.append(dataframe['rsi'] < self.valrsi) - if 'over_sar' in str(self.params): - conditions.append(dataframe['close'] > dataframe['sar']) - if 'green_candle' in str(self.params): - conditions.append(dataframe['close'] > dataframe['open']) - if 'uptrend_sma' in str(self.params): - prevsma = dataframe['sma'].shift(1) - conditions.append(dataframe['sma'] > prevsma) - if 'closebb' in str(self.params): - conditions.append(dataframe['close'] < dataframe['bb_lowerband']) - if 'temabb' in str(self.params): - conditions.append(dataframe['tema'] < dataframe['bb_lowerband']) - if 'fastdt' in str(self.params): - conditions.append(qtpylib.crossed_above(dataframe['fastd'], 10.0)) - if 'ao' in str(self.params): - conditions.append(qtpylib.crossed_above(dataframe['ao'], 0.0)) - if 'ema3' in str(self.params): - conditions.append(qtpylib.crossed_above(dataframe['ema3'], dataframe['ema10'])) - if 'macd' in str(self.params): - conditions.append(qtpylib.crossed_above(dataframe['macd'], dataframe['macdsignal'])) - if 'closesar' in str(self.params): - conditions.append(qtpylib.crossed_above(dataframe['close'], dataframe['sar'])) - if 'htsine' in str(self.params): - conditions.append(qtpylib.crossed_above(dataframe['htleadsine'], dataframe['htsine'])) - if 'has' in str(self.params): - conditions.append((qtpylib.crossed_above(dataframe['ha_close'], dataframe['ha_open'])) & (dataframe['ha_low'] == dataframe['ha_open'])) - if 'plusdi' in str(self.params): - conditions.append(qtpylib.crossed_above(dataframe['plus_di'], dataframe['minus_di'])) - - dataframe.loc[ - reduce(lambda x, y: x & y, conditions), - 'buy'] = 1 - - return dataframe - - def populate_sell_trend(self, dataframe: DataFrame) -> DataFrame: - """ - Based on TA indicators, populates the sell signal for the given dataframe - :param dataframe: DataFrame - :return: DataFrame with buy column - """ - dataframe.loc[ - ( - ), - 'sell'] = 1 - return dataframe - From 547bc29bd168264ea4023b21a1188366b45894b0 Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 06:18:18 -0500 Subject: [PATCH 04/28] Add files via upload --- user_data/strategies/default_strategy.py | 338 +++++++++++++++++++++++ 1 file changed, 338 insertions(+) create mode 100644 user_data/strategies/default_strategy.py diff --git a/user_data/strategies/default_strategy.py b/user_data/strategies/default_strategy.py new file mode 100644 index 000000000..acedfae01 --- /dev/null +++ b/user_data/strategies/default_strategy.py @@ -0,0 +1,338 @@ + +# --- Do not remove these libs --- +from freqtrade.strategy.interface import IStrategy +from typing import Dict, List +from hyperopt import hp +from functools import reduce +from pandas import DataFrame +# -------------------------------- + +# Add your lib to import here +import talib.abstract as ta +import freqtrade.vendor.qtpylib.indicators as qtpylib +import numpy # noqa + +import random + +# Update this variable if you change the class name +class_name = 'DefaultStrategy' + + +# This class is a sample. Feel free to customize it. + + + + +def Select(): + param = [] + random_items = [] + param.append(str('[' + 'uptrend_long_ema' + '[' + 'enabled' + ']')) + param.append(str('[' + 'macd_below_zero' + '][' + 'enabled' + ']')) + param.append(str('[' + 'uptrend_short_ema' '][' + 'enabled'+ ']')) + param.append(str('[' + 'mfi' '][' + 'enabled'+ ']')) + param.append(str('[' + 'fastd' '][' + 'enabled'+ ']')) + param.append(str('[' + 'adx' '][' + 'enabled'+ ']')) + param.append(str('[' + 'rsi' '][' + 'enabled'+ ']')) + param.append(str('[' + 'over_sar' '][' + 'enabled'+ ']')) + param.append(str('[' + 'green_candle' '][' + 'enabled'+ ']')) + param.append(str('[' + 'uptrend_sma' '][' + 'enabled'+ ']')) + param.append(str('[' + 'closebb' '][' + 'enabled'+ ']')) + param.append(str('[' + 'temabb' '][' + 'enabled'+ ']')) + param.append(str('[' + 'fastdt' '][' + 'enabled'+ ']')) + param.append(str('[' + 'ao' '][' + 'enabled'+ ']')) + param.append(str('[' + 'ema3' '][' + 'enabled'+ ']')) + param.append(str('[' + 'macd' '][' + 'enabled'+ ']')) + param.append(str('[' + 'closesar' '][' + 'enabled'+ ']')) + param.append(str('[' + 'htsine' '][' + 'enabled'+ ']')) + param.append(str('[' + 'has' '][' + 'enabled'+ ']')) + param.append(str('[' + 'plusdi' '][' + 'enabled'+ ']')) + howmany = random.randint(1,20) + random_items = random.choices(population=param, k=howmany) + print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') + print('The Parameters Enabled Are As Follows!!!: ' + str(random_items)) + print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') + return random_items + + + + +class DefaultStrategy(IStrategy): + """ + This is a test strategy to inspire you. + More information in https://github.com/gcarq/freqtrade/blob/develop/docs/bot-optimization.md + + You can: + - Rename the class name (Do not forget to update class_name) + - Add any methods you want to build your strategy + - Add any lib you need to build your strategy + + You must keep: + - the lib in the section "Do not remove these libs" + - the prototype for the methods: minimal_roi, stoploss, populate_indicators, populate_buy_trend, + populate_sell_trend, hyperopt_space, buy_strategy_generator + """ + + # Minimal ROI designed for the strategy. + # This attribute will be overridden if the config file contains "minimal_roi" + minimal_roi = { + "40": 0.0, + "30": 0.01, + "20": 0.02, + "0": 0.04 + } + + ticker_interval = 5 + + # Optimal stoploss designed for the strategy + # This attribute will be overridden if the config file contains "stoploss" + stoploss = -0.10 + + def populate_indicators(self, dataframe: DataFrame) -> DataFrame: + """ + Adds several different TA indicators to the given DataFrame + + Performance Note: For the best performance be frugal on the number of indicators + you are using. Let uncomment only the indicator you are using in your strategies + or your hyperopt configuration, otherwise you will waste your memory and CPU usage. + """ + + # Momentum Indicator + # ------------------------------------ + + # ADX + dataframe['adx'] = ta.ADX(dataframe) + + + # Awesome oscillator + dataframe['ao'] = qtpylib.awesome_oscillator(dataframe) + + # Commodity Channel Index: values Oversold:<-100, Overbought:>100 + dataframe['cci'] = ta.CCI(dataframe) + + # MACD + macd = ta.MACD(dataframe) + dataframe['macd'] = macd['macd'] + dataframe['macdsignal'] = macd['macdsignal'] + dataframe['macdhist'] = macd['macdhist'] + + # MFI + dataframe['mfi'] = ta.MFI(dataframe) + + # Minus Directional Indicator / Movement + dataframe['minus_dm'] = ta.MINUS_DM(dataframe) + dataframe['minus_di'] = ta.MINUS_DI(dataframe) + + # Plus Directional Indicator / Movement + dataframe['plus_dm'] = ta.PLUS_DM(dataframe) + dataframe['plus_di'] = ta.PLUS_DI(dataframe) + dataframe['minus_di'] = ta.MINUS_DI(dataframe) + + # ROC + dataframe['roc'] = ta.ROC(dataframe) + + # RSI + dataframe['rsi'] = ta.RSI(dataframe) + + # Inverse Fisher transform on RSI, values [-1.0, 1.0] (https://goo.gl/2JGGoy) + rsi = 0.1 * (dataframe['rsi'] - 50) + dataframe['fisher_rsi'] = (numpy.exp(2 * rsi) - 1) / (numpy.exp(2 * rsi) + 1) + + # Inverse Fisher transform on RSI normalized, value [0.0, 100.0] (https://goo.gl/2JGGoy) + dataframe['fisher_rsi_norma'] = 50 * (dataframe['fisher_rsi'] + 1) + + # Stoch + stoch = ta.STOCH(dataframe) + dataframe['slowd'] = stoch['slowd'] + dataframe['slowk'] = stoch['slowk'] + + # Stoch fast + stoch_fast = ta.STOCHF(dataframe) + dataframe['fastd'] = stoch_fast['fastd'] + dataframe['fastk'] = stoch_fast['fastk'] + + # Stoch RSI + stoch_rsi = ta.STOCHRSI(dataframe) + dataframe['fastd_rsi'] = stoch_rsi['fastd'] + dataframe['fastk_rsi'] = stoch_rsi['fastk'] + + + # Overlap Studies + # ------------------------------------ + + """ + # Previous Bollinger bands + # Because ta.BBANDS implementation is broken with small numbers, it actually + # returns middle band for all the three bands. Switch to qtpylib.bollinger_bands + # and use middle band instead. + + dataframe['blower'] = ta.BBANDS(dataframe, nbdevup=2, nbdevdn=2)['lowerband'] + """ + + # Bollinger bands + bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2) + dataframe['bb_lowerband'] = bollinger['lower'] + dataframe['bb_middleband'] = bollinger['mid'] + dataframe['bb_upperband'] = bollinger['upper'] + + + # EMA - Exponential Moving Average + dataframe['ema3'] = ta.EMA(dataframe, timeperiod=3) + dataframe['ema5'] = ta.EMA(dataframe, timeperiod=5) + dataframe['ema10'] = ta.EMA(dataframe, timeperiod=10) + dataframe['ema50'] = ta.EMA(dataframe, timeperiod=50) + dataframe['ema100'] = ta.EMA(dataframe, timeperiod=100) + + # SAR Parabol + dataframe['sar'] = ta.SAR(dataframe) + + # SMA - Simple Moving Average + dataframe['sma'] = ta.SMA(dataframe, timeperiod=40) + + + # TEMA - Triple Exponential Moving Average + dataframe['tema'] = ta.TEMA(dataframe, timeperiod=9) + + # Cycle Indicator + # ------------------------------------ + # Hilbert Transform Indicator - SineWave + hilbert = ta.HT_SINE(dataframe) + dataframe['htsine'] = hilbert['sine'] + dataframe['htleadsine'] = hilbert['leadsine'] + + # Pattern Recognition - Bullish candlestick patterns + # ------------------------------------ + + # Hammer: values [0, 100] + dataframe['CDLHAMMER'] = ta.CDLHAMMER(dataframe) + # Inverted Hammer: values [0, 100] + dataframe['CDLINVERTEDHAMMER'] = ta.CDLINVERTEDHAMMER(dataframe) + # Dragonfly Doji: values [0, 100] + dataframe['CDLDRAGONFLYDOJI'] = ta.CDLDRAGONFLYDOJI(dataframe) + # Piercing Line: values [0, 100] + dataframe['CDLPIERCING'] = ta.CDLPIERCING(dataframe) # values [0, 100] + # Morningstar: values [0, 100] + dataframe['CDLMORNINGSTAR'] = ta.CDLMORNINGSTAR(dataframe) # values [0, 100] + # Three White Soldiers: values [0, 100] + dataframe['CDL3WHITESOLDIERS'] = ta.CDL3WHITESOLDIERS(dataframe) # values [0, 100] + + + # Pattern Recognition - Bearish candlestick patterns + # ------------------------------------ + + # Hanging Man: values [0, 100] + dataframe['CDLHANGINGMAN'] = ta.CDLHANGINGMAN(dataframe) + # Shooting Star: values [0, 100] + dataframe['CDLSHOOTINGSTAR'] = ta.CDLSHOOTINGSTAR(dataframe) + # Gravestone Doji: values [0, 100] + dataframe['CDLGRAVESTONEDOJI'] = ta.CDLGRAVESTONEDOJI(dataframe) + # Dark Cloud Cover: values [0, 100] + dataframe['CDLDARKCLOUDCOVER'] = ta.CDLDARKCLOUDCOVER(dataframe) + # Evening Doji Star: values [0, 100] + dataframe['CDLEVENINGDOJISTAR'] = ta.CDLEVENINGDOJISTAR(dataframe) + # Evening Star: values [0, 100] + dataframe['CDLEVENINGSTAR'] = ta.CDLEVENINGSTAR(dataframe) + + + # Pattern Recognition - Bullish/Bearish candlestick patterns + # ------------------------------------ + + # Three Line Strike: values [0, -100, 100] + dataframe['CDL3LINESTRIKE'] = ta.CDL3LINESTRIKE(dataframe) + # Spinning Top: values [0, -100, 100] + dataframe['CDLSPINNINGTOP'] = ta.CDLSPINNINGTOP(dataframe) # values [0, -100, 100] + # Engulfing: values [0, -100, 100] + dataframe['CDLENGULFING'] = ta.CDLENGULFING(dataframe) # values [0, -100, 100] + # Harami: values [0, -100, 100] + dataframe['CDLHARAMI'] = ta.CDLHARAMI(dataframe) # values [0, -100, 100] + # Three Outside Up/Down: values [0, -100, 100] + dataframe['CDL3OUTSIDE'] = ta.CDL3OUTSIDE(dataframe) # values [0, -100, 100] + # Three Inside Up/Down: values [0, -100, 100] + dataframe['CDL3INSIDE'] = ta.CDL3INSIDE(dataframe) # values [0, -100, 100] + + + # Chart type + # ------------------------------------ + + # Heikinashi stategy + heikinashi = qtpylib.heikinashi(dataframe) + dataframe['ha_open'] = heikinashi['open'] + dataframe['ha_close'] = heikinashi['close'] + dataframe['ha_high'] = heikinashi['high'] + dataframe['ha_low'] = heikinashi['low'] + + + return dataframe + + params = Select() + valm = random.randint(1,100) + valfast = random.randint(1,100) + valadx = random.randint(1,100) + valrsi = random.randint(1,100) + def populate_buy_trend(self, dataframe: DataFrame) -> DataFrame: + + conditions = [] + # GUARDS AND TRENDS + if 'uptrend_long_ema' in str(self.params): + conditions.append(dataframe['ema50'] > dataframe['ema100']) + if 'macd_below_zero' in str(self.params): + conditions.append(dataframe['macd'] < 0) + if 'uptrend_short_ema' in str(self.params): + conditions.append(dataframe['ema5'] > dataframe['ema10']) + if 'mfi' in str(self.params): + print('MFI Value :' + str(self.valm)) + conditions.append(dataframe['mfi'] < self.valm) + if 'fastd' in str(self.params): + print('FASTD Value :' + str(self.valfast)) + conditions.append(dataframe['fastd'] < self.valfast) + if 'adx' in str(self.params): + print('ADX Value :' + str(self.valadx)) + conditions.append(dataframe['adx'] > self.valadx) + if 'rsi' in str(self.params): + print('RSI Value :' + str(self.valrsi)) + conditions.append(dataframe['rsi'] < self.valrsi) + if 'over_sar' in str(self.params): + conditions.append(dataframe['close'] > dataframe['sar']) + if 'green_candle' in str(self.params): + conditions.append(dataframe['close'] > dataframe['open']) + if 'uptrend_sma' in str(self.params): + prevsma = dataframe['sma'].shift(1) + conditions.append(dataframe['sma'] > prevsma) + if 'closebb' in str(self.params): + conditions.append(dataframe['close'] < dataframe['bb_lowerband']) + if 'temabb' in str(self.params): + conditions.append(dataframe['tema'] < dataframe['bb_lowerband']) + if 'fastdt' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['fastd'], 10.0)) + if 'ao' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['ao'], 0.0)) + if 'ema3' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['ema3'], dataframe['ema10'])) + if 'macd' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['macd'], dataframe['macdsignal'])) + if 'closesar' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['close'], dataframe['sar'])) + if 'htsine' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['htleadsine'], dataframe['htsine'])) + if 'has' in str(self.params): + conditions.append((qtpylib.crossed_above(dataframe['ha_close'], dataframe['ha_open'])) & (dataframe['ha_low'] == dataframe['ha_open'])) + if 'plusdi' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['plus_di'], dataframe['minus_di'])) + + dataframe.loc[ + reduce(lambda x, y: x & y, conditions), + 'buy'] = 1 + + return dataframe + + def populate_sell_trend(self, dataframe: DataFrame) -> DataFrame: + """ + Based on TA indicators, populates the sell signal for the given dataframe + :param dataframe: DataFrame + :return: DataFrame with buy column + """ + dataframe.loc[ + ( + ), + 'sell'] = 1 + return dataframe + From b957db6d6fe644f82717f77b47cb71f5f04e0165 Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 06:18:46 -0500 Subject: [PATCH 05/28] Keeps threads full --- scripts/random.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 scripts/random.py diff --git a/scripts/random.py b/scripts/random.py new file mode 100644 index 000000000..91fb2de3a --- /dev/null +++ b/scripts/random.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +import os +import multiprocessing +from itertools import zip_longest +import subprocess +import re +PROC_COUNT = multiprocessing.cpu_count() - 1 +cwd = os.getcwd() +print(cwd) + + +limit = multiprocessing.cpu_count() - 1 +WORK_DIR = os.path.join( + os.path.sep, + os.path.abspath(os.path.dirname(__file__)), + '..', 'freqtrade', 'main.py' +) + +# Spawn workers +command = [ + 'python3.6', + WORK_DIR, + 'backtesting', +] +global current +current = 0 + +while True: + def Run(command): + global current + processes = [subprocess.Popen(command) for i in range(PROC_COUNT)] + for proc in processes: + wait = proc.communicate() + data = Run(command) From 147564eab20659a693a477400bc948164c8ae716 Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 07:44:18 -0500 Subject: [PATCH 06/28] Delete random.py --- scripts/random.py | 34 ---------------------------------- 1 file changed, 34 deletions(-) delete mode 100644 scripts/random.py diff --git a/scripts/random.py b/scripts/random.py deleted file mode 100644 index 91fb2de3a..000000000 --- a/scripts/random.py +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/env python3 -import os -import multiprocessing -from itertools import zip_longest -import subprocess -import re -PROC_COUNT = multiprocessing.cpu_count() - 1 -cwd = os.getcwd() -print(cwd) - - -limit = multiprocessing.cpu_count() - 1 -WORK_DIR = os.path.join( - os.path.sep, - os.path.abspath(os.path.dirname(__file__)), - '..', 'freqtrade', 'main.py' -) - -# Spawn workers -command = [ - 'python3.6', - WORK_DIR, - 'backtesting', -] -global current -current = 0 - -while True: - def Run(command): - global current - processes = [subprocess.Popen(command) for i in range(PROC_COUNT)] - for proc in processes: - wait = proc.communicate() - data = Run(command) From 6613190a72f1da9beae8865cf514a54182eb000a Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 07:44:35 -0500 Subject: [PATCH 07/28] Random regex, simple profit search From f02de154bf64ce73ad2b6012ba7cd2b6d2522ead Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 07:45:13 -0500 Subject: [PATCH 08/28] Delete default_strategy.py --- user_data/strategies/default_strategy.py | 338 ----------------------- 1 file changed, 338 deletions(-) delete mode 100644 user_data/strategies/default_strategy.py diff --git a/user_data/strategies/default_strategy.py b/user_data/strategies/default_strategy.py deleted file mode 100644 index acedfae01..000000000 --- a/user_data/strategies/default_strategy.py +++ /dev/null @@ -1,338 +0,0 @@ - -# --- Do not remove these libs --- -from freqtrade.strategy.interface import IStrategy -from typing import Dict, List -from hyperopt import hp -from functools import reduce -from pandas import DataFrame -# -------------------------------- - -# Add your lib to import here -import talib.abstract as ta -import freqtrade.vendor.qtpylib.indicators as qtpylib -import numpy # noqa - -import random - -# Update this variable if you change the class name -class_name = 'DefaultStrategy' - - -# This class is a sample. Feel free to customize it. - - - - -def Select(): - param = [] - random_items = [] - param.append(str('[' + 'uptrend_long_ema' + '[' + 'enabled' + ']')) - param.append(str('[' + 'macd_below_zero' + '][' + 'enabled' + ']')) - param.append(str('[' + 'uptrend_short_ema' '][' + 'enabled'+ ']')) - param.append(str('[' + 'mfi' '][' + 'enabled'+ ']')) - param.append(str('[' + 'fastd' '][' + 'enabled'+ ']')) - param.append(str('[' + 'adx' '][' + 'enabled'+ ']')) - param.append(str('[' + 'rsi' '][' + 'enabled'+ ']')) - param.append(str('[' + 'over_sar' '][' + 'enabled'+ ']')) - param.append(str('[' + 'green_candle' '][' + 'enabled'+ ']')) - param.append(str('[' + 'uptrend_sma' '][' + 'enabled'+ ']')) - param.append(str('[' + 'closebb' '][' + 'enabled'+ ']')) - param.append(str('[' + 'temabb' '][' + 'enabled'+ ']')) - param.append(str('[' + 'fastdt' '][' + 'enabled'+ ']')) - param.append(str('[' + 'ao' '][' + 'enabled'+ ']')) - param.append(str('[' + 'ema3' '][' + 'enabled'+ ']')) - param.append(str('[' + 'macd' '][' + 'enabled'+ ']')) - param.append(str('[' + 'closesar' '][' + 'enabled'+ ']')) - param.append(str('[' + 'htsine' '][' + 'enabled'+ ']')) - param.append(str('[' + 'has' '][' + 'enabled'+ ']')) - param.append(str('[' + 'plusdi' '][' + 'enabled'+ ']')) - howmany = random.randint(1,20) - random_items = random.choices(population=param, k=howmany) - print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') - print('The Parameters Enabled Are As Follows!!!: ' + str(random_items)) - print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') - return random_items - - - - -class DefaultStrategy(IStrategy): - """ - This is a test strategy to inspire you. - More information in https://github.com/gcarq/freqtrade/blob/develop/docs/bot-optimization.md - - You can: - - Rename the class name (Do not forget to update class_name) - - Add any methods you want to build your strategy - - Add any lib you need to build your strategy - - You must keep: - - the lib in the section "Do not remove these libs" - - the prototype for the methods: minimal_roi, stoploss, populate_indicators, populate_buy_trend, - populate_sell_trend, hyperopt_space, buy_strategy_generator - """ - - # Minimal ROI designed for the strategy. - # This attribute will be overridden if the config file contains "minimal_roi" - minimal_roi = { - "40": 0.0, - "30": 0.01, - "20": 0.02, - "0": 0.04 - } - - ticker_interval = 5 - - # Optimal stoploss designed for the strategy - # This attribute will be overridden if the config file contains "stoploss" - stoploss = -0.10 - - def populate_indicators(self, dataframe: DataFrame) -> DataFrame: - """ - Adds several different TA indicators to the given DataFrame - - Performance Note: For the best performance be frugal on the number of indicators - you are using. Let uncomment only the indicator you are using in your strategies - or your hyperopt configuration, otherwise you will waste your memory and CPU usage. - """ - - # Momentum Indicator - # ------------------------------------ - - # ADX - dataframe['adx'] = ta.ADX(dataframe) - - - # Awesome oscillator - dataframe['ao'] = qtpylib.awesome_oscillator(dataframe) - - # Commodity Channel Index: values Oversold:<-100, Overbought:>100 - dataframe['cci'] = ta.CCI(dataframe) - - # MACD - macd = ta.MACD(dataframe) - dataframe['macd'] = macd['macd'] - dataframe['macdsignal'] = macd['macdsignal'] - dataframe['macdhist'] = macd['macdhist'] - - # MFI - dataframe['mfi'] = ta.MFI(dataframe) - - # Minus Directional Indicator / Movement - dataframe['minus_dm'] = ta.MINUS_DM(dataframe) - dataframe['minus_di'] = ta.MINUS_DI(dataframe) - - # Plus Directional Indicator / Movement - dataframe['plus_dm'] = ta.PLUS_DM(dataframe) - dataframe['plus_di'] = ta.PLUS_DI(dataframe) - dataframe['minus_di'] = ta.MINUS_DI(dataframe) - - # ROC - dataframe['roc'] = ta.ROC(dataframe) - - # RSI - dataframe['rsi'] = ta.RSI(dataframe) - - # Inverse Fisher transform on RSI, values [-1.0, 1.0] (https://goo.gl/2JGGoy) - rsi = 0.1 * (dataframe['rsi'] - 50) - dataframe['fisher_rsi'] = (numpy.exp(2 * rsi) - 1) / (numpy.exp(2 * rsi) + 1) - - # Inverse Fisher transform on RSI normalized, value [0.0, 100.0] (https://goo.gl/2JGGoy) - dataframe['fisher_rsi_norma'] = 50 * (dataframe['fisher_rsi'] + 1) - - # Stoch - stoch = ta.STOCH(dataframe) - dataframe['slowd'] = stoch['slowd'] - dataframe['slowk'] = stoch['slowk'] - - # Stoch fast - stoch_fast = ta.STOCHF(dataframe) - dataframe['fastd'] = stoch_fast['fastd'] - dataframe['fastk'] = stoch_fast['fastk'] - - # Stoch RSI - stoch_rsi = ta.STOCHRSI(dataframe) - dataframe['fastd_rsi'] = stoch_rsi['fastd'] - dataframe['fastk_rsi'] = stoch_rsi['fastk'] - - - # Overlap Studies - # ------------------------------------ - - """ - # Previous Bollinger bands - # Because ta.BBANDS implementation is broken with small numbers, it actually - # returns middle band for all the three bands. Switch to qtpylib.bollinger_bands - # and use middle band instead. - - dataframe['blower'] = ta.BBANDS(dataframe, nbdevup=2, nbdevdn=2)['lowerband'] - """ - - # Bollinger bands - bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2) - dataframe['bb_lowerband'] = bollinger['lower'] - dataframe['bb_middleband'] = bollinger['mid'] - dataframe['bb_upperband'] = bollinger['upper'] - - - # EMA - Exponential Moving Average - dataframe['ema3'] = ta.EMA(dataframe, timeperiod=3) - dataframe['ema5'] = ta.EMA(dataframe, timeperiod=5) - dataframe['ema10'] = ta.EMA(dataframe, timeperiod=10) - dataframe['ema50'] = ta.EMA(dataframe, timeperiod=50) - dataframe['ema100'] = ta.EMA(dataframe, timeperiod=100) - - # SAR Parabol - dataframe['sar'] = ta.SAR(dataframe) - - # SMA - Simple Moving Average - dataframe['sma'] = ta.SMA(dataframe, timeperiod=40) - - - # TEMA - Triple Exponential Moving Average - dataframe['tema'] = ta.TEMA(dataframe, timeperiod=9) - - # Cycle Indicator - # ------------------------------------ - # Hilbert Transform Indicator - SineWave - hilbert = ta.HT_SINE(dataframe) - dataframe['htsine'] = hilbert['sine'] - dataframe['htleadsine'] = hilbert['leadsine'] - - # Pattern Recognition - Bullish candlestick patterns - # ------------------------------------ - - # Hammer: values [0, 100] - dataframe['CDLHAMMER'] = ta.CDLHAMMER(dataframe) - # Inverted Hammer: values [0, 100] - dataframe['CDLINVERTEDHAMMER'] = ta.CDLINVERTEDHAMMER(dataframe) - # Dragonfly Doji: values [0, 100] - dataframe['CDLDRAGONFLYDOJI'] = ta.CDLDRAGONFLYDOJI(dataframe) - # Piercing Line: values [0, 100] - dataframe['CDLPIERCING'] = ta.CDLPIERCING(dataframe) # values [0, 100] - # Morningstar: values [0, 100] - dataframe['CDLMORNINGSTAR'] = ta.CDLMORNINGSTAR(dataframe) # values [0, 100] - # Three White Soldiers: values [0, 100] - dataframe['CDL3WHITESOLDIERS'] = ta.CDL3WHITESOLDIERS(dataframe) # values [0, 100] - - - # Pattern Recognition - Bearish candlestick patterns - # ------------------------------------ - - # Hanging Man: values [0, 100] - dataframe['CDLHANGINGMAN'] = ta.CDLHANGINGMAN(dataframe) - # Shooting Star: values [0, 100] - dataframe['CDLSHOOTINGSTAR'] = ta.CDLSHOOTINGSTAR(dataframe) - # Gravestone Doji: values [0, 100] - dataframe['CDLGRAVESTONEDOJI'] = ta.CDLGRAVESTONEDOJI(dataframe) - # Dark Cloud Cover: values [0, 100] - dataframe['CDLDARKCLOUDCOVER'] = ta.CDLDARKCLOUDCOVER(dataframe) - # Evening Doji Star: values [0, 100] - dataframe['CDLEVENINGDOJISTAR'] = ta.CDLEVENINGDOJISTAR(dataframe) - # Evening Star: values [0, 100] - dataframe['CDLEVENINGSTAR'] = ta.CDLEVENINGSTAR(dataframe) - - - # Pattern Recognition - Bullish/Bearish candlestick patterns - # ------------------------------------ - - # Three Line Strike: values [0, -100, 100] - dataframe['CDL3LINESTRIKE'] = ta.CDL3LINESTRIKE(dataframe) - # Spinning Top: values [0, -100, 100] - dataframe['CDLSPINNINGTOP'] = ta.CDLSPINNINGTOP(dataframe) # values [0, -100, 100] - # Engulfing: values [0, -100, 100] - dataframe['CDLENGULFING'] = ta.CDLENGULFING(dataframe) # values [0, -100, 100] - # Harami: values [0, -100, 100] - dataframe['CDLHARAMI'] = ta.CDLHARAMI(dataframe) # values [0, -100, 100] - # Three Outside Up/Down: values [0, -100, 100] - dataframe['CDL3OUTSIDE'] = ta.CDL3OUTSIDE(dataframe) # values [0, -100, 100] - # Three Inside Up/Down: values [0, -100, 100] - dataframe['CDL3INSIDE'] = ta.CDL3INSIDE(dataframe) # values [0, -100, 100] - - - # Chart type - # ------------------------------------ - - # Heikinashi stategy - heikinashi = qtpylib.heikinashi(dataframe) - dataframe['ha_open'] = heikinashi['open'] - dataframe['ha_close'] = heikinashi['close'] - dataframe['ha_high'] = heikinashi['high'] - dataframe['ha_low'] = heikinashi['low'] - - - return dataframe - - params = Select() - valm = random.randint(1,100) - valfast = random.randint(1,100) - valadx = random.randint(1,100) - valrsi = random.randint(1,100) - def populate_buy_trend(self, dataframe: DataFrame) -> DataFrame: - - conditions = [] - # GUARDS AND TRENDS - if 'uptrend_long_ema' in str(self.params): - conditions.append(dataframe['ema50'] > dataframe['ema100']) - if 'macd_below_zero' in str(self.params): - conditions.append(dataframe['macd'] < 0) - if 'uptrend_short_ema' in str(self.params): - conditions.append(dataframe['ema5'] > dataframe['ema10']) - if 'mfi' in str(self.params): - print('MFI Value :' + str(self.valm)) - conditions.append(dataframe['mfi'] < self.valm) - if 'fastd' in str(self.params): - print('FASTD Value :' + str(self.valfast)) - conditions.append(dataframe['fastd'] < self.valfast) - if 'adx' in str(self.params): - print('ADX Value :' + str(self.valadx)) - conditions.append(dataframe['adx'] > self.valadx) - if 'rsi' in str(self.params): - print('RSI Value :' + str(self.valrsi)) - conditions.append(dataframe['rsi'] < self.valrsi) - if 'over_sar' in str(self.params): - conditions.append(dataframe['close'] > dataframe['sar']) - if 'green_candle' in str(self.params): - conditions.append(dataframe['close'] > dataframe['open']) - if 'uptrend_sma' in str(self.params): - prevsma = dataframe['sma'].shift(1) - conditions.append(dataframe['sma'] > prevsma) - if 'closebb' in str(self.params): - conditions.append(dataframe['close'] < dataframe['bb_lowerband']) - if 'temabb' in str(self.params): - conditions.append(dataframe['tema'] < dataframe['bb_lowerband']) - if 'fastdt' in str(self.params): - conditions.append(qtpylib.crossed_above(dataframe['fastd'], 10.0)) - if 'ao' in str(self.params): - conditions.append(qtpylib.crossed_above(dataframe['ao'], 0.0)) - if 'ema3' in str(self.params): - conditions.append(qtpylib.crossed_above(dataframe['ema3'], dataframe['ema10'])) - if 'macd' in str(self.params): - conditions.append(qtpylib.crossed_above(dataframe['macd'], dataframe['macdsignal'])) - if 'closesar' in str(self.params): - conditions.append(qtpylib.crossed_above(dataframe['close'], dataframe['sar'])) - if 'htsine' in str(self.params): - conditions.append(qtpylib.crossed_above(dataframe['htleadsine'], dataframe['htsine'])) - if 'has' in str(self.params): - conditions.append((qtpylib.crossed_above(dataframe['ha_close'], dataframe['ha_open'])) & (dataframe['ha_low'] == dataframe['ha_open'])) - if 'plusdi' in str(self.params): - conditions.append(qtpylib.crossed_above(dataframe['plus_di'], dataframe['minus_di'])) - - dataframe.loc[ - reduce(lambda x, y: x & y, conditions), - 'buy'] = 1 - - return dataframe - - def populate_sell_trend(self, dataframe: DataFrame) -> DataFrame: - """ - Based on TA indicators, populates the sell signal for the given dataframe - :param dataframe: DataFrame - :return: DataFrame with buy column - """ - dataframe.loc[ - ( - ), - 'sell'] = 1 - return dataframe - From 1b1a6c136b8c2422ab73fe5d70a84cfd220d2c99 Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 07:45:41 -0500 Subject: [PATCH 09/28] Create backtesting.py --- user_data/random/backtesting.py | 296 ++++++++++++++++++++++++++++++++ 1 file changed, 296 insertions(+) create mode 100644 user_data/random/backtesting.py diff --git a/user_data/random/backtesting.py b/user_data/random/backtesting.py new file mode 100644 index 000000000..e2e45f697 --- /dev/null +++ b/user_data/random/backtesting.py @@ -0,0 +1,296 @@ +# pragma pylint: disable=missing-docstring, W0212, too-many-arguments + +""" +This module contains the backtesting logic +""" +from argparse import Namespace +from typing import Dict, Tuple, Any, List, Optional + +import arrow +from pandas import DataFrame, Series +from tabulate import tabulate + +import freqtrade.optimize as optimize +from freqtrade import exchange +from freqtrade.analyze import Analyze +from freqtrade.arguments import Arguments +from freqtrade.configuration import Configuration +from freqtrade.exchange import Bittrex +from freqtrade.logger import Logger +from freqtrade.misc import file_dump_json +from freqtrade.persistence import Trade + + +class Backtesting(object): + """ + Backtesting class, this class contains all the logic to run a backtest + + To run a backtest: + backtesting = Backtesting(config) + backtesting.start() + """ + def __init__(self, config: Dict[str, Any]) -> None: + + # Init the logger + self.logging = Logger(name=__name__, level=config['loglevel']) + self.logger = self.logging.get_logger() + self.config = config + self.analyze = None + self.ticker_interval = None + self.tickerdata_to_dataframe = None + self.populate_buy_trend = None + self.populate_sell_trend = None + self._init() + + def _init(self) -> None: + """ + Init objects required for backtesting + :return: None + """ + self.analyze = Analyze(self.config) + self.ticker_interval = self.analyze.strategy.ticker_interval + self.tickerdata_to_dataframe = self.analyze.tickerdata_to_dataframe + self.populate_buy_trend = self.analyze.populate_buy_trend + self.populate_sell_trend = self.analyze.populate_sell_trend + exchange._API = Bittrex({'key': '', 'secret': ''}) + + @staticmethod + def get_timeframe(data: Dict[str, DataFrame]) -> Tuple[arrow.Arrow, arrow.Arrow]: + """ + Get the maximum timeframe for the given backtest data + :param data: dictionary with preprocessed backtesting data + :return: tuple containing min_date, max_date + """ + all_dates = Series([]) + for pair_data in data.values(): + all_dates = all_dates.append(pair_data['date']) + all_dates.sort_values(inplace=True) + return arrow.get(all_dates.iloc[0]), arrow.get(all_dates.iloc[-1]) + + def _generate_text_table(self, data: Dict[str, Dict], results: DataFrame) -> str: + """ + Generates and returns a text table for the given backtest data and the results dataframe + :return: pretty printed table with tabulate as str + """ + stake_currency = self.config.get('stake_currency') + + floatfmt = ('.8f', '.8f', '.8f', '.8f', '.1f') + tabular_data = [] + headers = ['pair', 'buy count', 'avg profit %', + 'total profit ' + stake_currency, 'avg duration', 'profit', 'loss'] + + # Append Total + tabular_data.append([ + 'TOTAL', + results.profit_BTC.sum(), + ]) + return tabulate(tabular_data, headers=headers, floatfmt=floatfmt) + + def _get_sell_trade_entry( + self, pair: str, buy_row: DataFrame, + partial_ticker: List, trade_count_lock: Dict, args: Dict) -> Optional[Tuple]: + + stake_amount = args['stake_amount'] + max_open_trades = args.get('max_open_trades', 0) + trade = Trade( + open_rate=buy_row.close, + open_date=buy_row.date, + stake_amount=stake_amount, + amount=stake_amount / buy_row.open, + fee=exchange.get_fee() + ) + + # calculate win/lose forwards from buy point + for sell_row in partial_ticker: + if max_open_trades > 0: + # Increase trade_count_lock for every iteration + trade_count_lock[sell_row.date] = trade_count_lock.get(sell_row.date, 0) + 1 + + buy_signal = sell_row.buy + if self.analyze.should_sell(trade, sell_row.close, sell_row.date, buy_signal, + sell_row.sell): + return \ + sell_row, \ + ( + pair, + trade.calc_profit_percent(rate=sell_row.close), + trade.calc_profit(rate=sell_row.close), + (sell_row.date - buy_row.date).seconds // 60 + ), \ + sell_row.date + return None + + def backtest(self, args: Dict) -> DataFrame: + """ + Implements backtesting functionality + + NOTE: This method is used by Hyperopt at each iteration. Please keep it optimized. + Of course try to not have ugly code. By some accessor are sometime slower than functions. + Avoid, logging on this method + + :param args: a dict containing: + stake_amount: btc amount to use for each trade + processed: a processed dictionary with format {pair, data} + max_open_trades: maximum number of concurrent trades (default: 0, disabled) + realistic: do we try to simulate realistic trades? (default: True) + sell_profit_only: sell if profit only + use_sell_signal: act on sell-signal + :return: DataFrame + """ + headers = ['date', 'buy', 'open', 'close', 'sell'] + processed = args['processed'] + max_open_trades = args.get('max_open_trades', 0) + realistic = args.get('realistic', False) + record = args.get('record', None) + records = [] + trades = [] + trade_count_lock = {} + for pair, pair_data in processed.items(): + pair_data['buy'], pair_data['sell'] = 0, 0 # cleanup from previous run + + ticker_data = self.populate_sell_trend(self.populate_buy_trend(pair_data))[headers] + ticker = [x for x in ticker_data.itertuples()] + + lock_pair_until = None + for index, row in enumerate(ticker): + if row.buy == 0 or row.sell == 1: + continue # skip rows where no buy signal or that would immediately sell off + + if realistic: + if lock_pair_until is not None and row.date <= lock_pair_until: + continue + if max_open_trades > 0: + # Check if max_open_trades has already been reached for the given date + if not trade_count_lock.get(row.date, 0) < max_open_trades: + continue + + trade_count_lock[row.date] = trade_count_lock.get(row.date, 0) + 1 + + ret = self._get_sell_trade_entry(pair, row, ticker[index + 1:], + trade_count_lock, args) + + if ret: + row2, trade_entry, next_date = ret + lock_pair_until = next_date + trades.append(trade_entry) + if record: + # Note, need to be json.dump friendly + # record a tuple of pair, current_profit_percent, + # entry-date, duration + records.append((pair, trade_entry[1], + row.date.strftime('%s'), + row2.date.strftime('%s'), + row.date, trade_entry[3])) + # For now export inside backtest(), maybe change so that backtest() + # returns a tuple like: (dataframe, records, logs, etc) + if record and record.find('trades') >= 0: + self.logger.info('Dumping backtest results') + file_dump_json('backtest-result.json', records) + labels = ['currency', 'profit_percent', 'profit_BTC', 'duration'] + return DataFrame.from_records(trades, columns=labels) + + def start(self) -> None: + """ + Run a backtesting end-to-end + :return: None + """ + data = {} + pairs = self.config['exchange']['pair_whitelist'] + self.logger.info('Using stake_currency: %s ...', self.config['stake_currency']) + self.logger.info('Using stake_amount: %s ...', self.config['stake_amount']) + + if self.config.get('live'): + self.logger.info('Downloading data for all pairs in whitelist ...') + for pair in pairs: + data[pair] = exchange.get_ticker_history(pair, self.ticker_interval) + else: + self.logger.info('Using local backtesting data (using whitelist in given config) ...') + + timerange = Arguments.parse_timerange(self.config.get('timerange')) + data = optimize.load_data( + self.config['datadir'], + pairs=pairs, + ticker_interval=self.ticker_interval, + refresh_pairs=self.config.get('refresh_pairs', False), + timerange=timerange + ) + + # Ignore max_open_trades in backtesting, except realistic flag was passed + if self.config.get('realistic_simulation', False): + max_open_trades = self.config['max_open_trades'] + else: + self.logger.info('Ignoring max_open_trades (realistic_simulation not set) ...') + max_open_trades = 0 + + preprocessed = self.tickerdata_to_dataframe(data) + + # Print timeframe + min_date, max_date = self.get_timeframe(preprocessed) + self.logger.info( + 'Measuring data from %s up to %s (%s days)..', + min_date.isoformat(), + max_date.isoformat(), + (max_date - min_date).days + ) + + # Execute backtest and print results + sell_profit_only = self.config.get('experimental', {}).get('sell_profit_only', False) + use_sell_signal = self.config.get('experimental', {}).get('use_sell_signal', False) + results = self.backtest( + { + 'stake_amount': self.config.get('stake_amount'), + 'processed': preprocessed, + 'max_open_trades': max_open_trades, + 'realistic': self.config.get('realistic_simulation', False), + 'sell_profit_only': sell_profit_only, + 'use_sell_signal': use_sell_signal, + 'record': self.config.get('export') + } + ) + + self.logging.set_format('%(message)s') + self.logger.info( + '\n==================================== ' + 'BACKTESTING REPORT' + ' ====================================\n' + '%s', + self._generate_text_table( + data, + results + ) + ) + + +def setup_configuration(args: Namespace) -> Dict[str, Any]: + """ + Prepare the configuration for the backtesting + :param args: Cli args from Arguments() + :return: Configuration + """ + configuration = Configuration(args) + config = configuration.get_config() + + # Ensure we do not use Exchange credentials + config['exchange']['key'] = '' + config['exchange']['secret'] = '' + + return config + + +def start(args: Namespace) -> None: + """ + Start Backtesting script + :param args: Cli args from Arguments() + :return: None + """ + + # Initialize logger + logger = Logger(name=__name__).get_logger() + logger.info('Starting freqtrade in Backtesting mode') + + # Initialize configuration + config = setup_configuration(args) + + # Initialize backtesting object + backtesting = Backtesting(config) + backtesting.start() From 0a66d7662b1921c2ec9bcb204d19511a931fd63b Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 07:45:59 -0500 Subject: [PATCH 10/28] Add files via upload --- user_data/random/default_strategy.py | 342 +++++++++++++++++++++++++++ 1 file changed, 342 insertions(+) create mode 100644 user_data/random/default_strategy.py diff --git a/user_data/random/default_strategy.py b/user_data/random/default_strategy.py new file mode 100644 index 000000000..fe642225d --- /dev/null +++ b/user_data/random/default_strategy.py @@ -0,0 +1,342 @@ + +# --- Do not remove these libs --- +from freqtrade.strategy.interface import IStrategy +from typing import Dict, List +from hyperopt import hp +from functools import reduce +from pandas import DataFrame +# -------------------------------- + +# Add your lib to import here +import talib.abstract as ta +import freqtrade.vendor.qtpylib.indicators as qtpylib +import numpy # noqa + +import random + +# Update this variable if you change the class name +class_name = 'DefaultStrategy' + + +# This class is a sample. Feel free to customize it. + + + + +def Select(): + param = [] + random_items = [] + param.append(str('[' + 'uptrend_long_ema' + '[' + 'enabled' + ']')) + param.append(str('[' + 'macd_below_zero' + '][' + 'enabled' + ']')) + param.append(str('[' + 'uptrend_short_ema' '][' + 'enabled'+ ']')) + param.append(str('[' + 'mfi' '][' + 'enabled'+ ']')) + param.append(str('[' + 'fastd' '][' + 'enabled'+ ']')) + param.append(str('[' + 'adx' '][' + 'enabled'+ ']')) + param.append(str('[' + 'rsi' '][' + 'enabled'+ ']')) + param.append(str('[' + 'over_sar' '][' + 'enabled'+ ']')) + param.append(str('[' + 'green_candle' '][' + 'enabled'+ ']')) + param.append(str('[' + 'uptrend_sma' '][' + 'enabled'+ ']')) + param.append(str('[' + 'closebb' '][' + 'enabled'+ ']')) + param.append(str('[' + 'temabb' '][' + 'enabled'+ ']')) + param.append(str('[' + 'fastdt' '][' + 'enabled'+ ']')) + param.append(str('[' + 'ao' '][' + 'enabled'+ ']')) + param.append(str('[' + 'ema3' '][' + 'enabled'+ ']')) + param.append(str('[' + 'macd' '][' + 'enabled'+ ']')) + param.append(str('[' + 'closesar' '][' + 'enabled'+ ']')) + param.append(str('[' + 'htsine' '][' + 'enabled'+ ']')) + param.append(str('[' + 'has' '][' + 'enabled'+ ']')) + param.append(str('[' + 'plusdi' '][' + 'enabled'+ ']')) + howmany = random.randint(1,20) + random_items = random.choices(population=param, k=howmany) + print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') + print('The Parameters Enabled Are As Follows!!!: ' + str(random_items)) + print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') + return random_items + + + + +class DefaultStrategy(IStrategy): + """ + This is a test strategy to inspire you. + More information in https://github.com/gcarq/freqtrade/blob/develop/docs/bot-optimization.md + + You can: + - Rename the class name (Do not forget to update class_name) + - Add any methods you want to build your strategy + - Add any lib you need to build your strategy + + You must keep: + - the lib in the section "Do not remove these libs" + - the prototype for the methods: minimal_roi, stoploss, populate_indicators, populate_buy_trend, + populate_sell_trend, hyperopt_space, buy_strategy_generator + """ + + # Minimal ROI designed for the strategy. + # This attribute will be overridden if the config file contains "minimal_roi" + minimal_roi = { + "40": 0.0, + "30": 0.01, + "20": 0.02, + "0": 0.04 + } + + ticker_interval = 5 + + # Optimal stoploss designed for the strategy + # This attribute will be overridden if the config file contains "stoploss" + stoploss = -0.10 + + def populate_indicators(self, dataframe: DataFrame) -> DataFrame: + """ + Adds several different TA indicators to the given DataFrame + + Performance Note: For the best performance be frugal on the number of indicators + you are using. Let uncomment only the indicator you are using in your strategies + or your hyperopt configuration, otherwise you will waste your memory and CPU usage. + """ + + # Momentum Indicator + # ------------------------------------ + + # ADX + dataframe['adx'] = ta.ADX(dataframe) + + + # Awesome oscillator + dataframe['ao'] = qtpylib.awesome_oscillator(dataframe) + + # Commodity Channel Index: values Oversold:<-100, Overbought:>100 + dataframe['cci'] = ta.CCI(dataframe) + + # MACD + macd = ta.MACD(dataframe) + dataframe['macd'] = macd['macd'] + dataframe['macdsignal'] = macd['macdsignal'] + dataframe['macdhist'] = macd['macdhist'] + + # MFI + dataframe['mfi'] = ta.MFI(dataframe) + + # Minus Directional Indicator / Movement + dataframe['minus_dm'] = ta.MINUS_DM(dataframe) + dataframe['minus_di'] = ta.MINUS_DI(dataframe) + + # Plus Directional Indicator / Movement + dataframe['plus_dm'] = ta.PLUS_DM(dataframe) + dataframe['plus_di'] = ta.PLUS_DI(dataframe) + dataframe['minus_di'] = ta.MINUS_DI(dataframe) + + # ROC + dataframe['roc'] = ta.ROC(dataframe) + + # RSI + dataframe['rsi'] = ta.RSI(dataframe) + + # Inverse Fisher transform on RSI, values [-1.0, 1.0] (https://goo.gl/2JGGoy) + rsi = 0.1 * (dataframe['rsi'] - 50) + dataframe['fisher_rsi'] = (numpy.exp(2 * rsi) - 1) / (numpy.exp(2 * rsi) + 1) + + # Inverse Fisher transform on RSI normalized, value [0.0, 100.0] (https://goo.gl/2JGGoy) + dataframe['fisher_rsi_norma'] = 50 * (dataframe['fisher_rsi'] + 1) + + # Stoch + stoch = ta.STOCH(dataframe) + dataframe['slowd'] = stoch['slowd'] + dataframe['slowk'] = stoch['slowk'] + + # Stoch fast + stoch_fast = ta.STOCHF(dataframe) + dataframe['fastd'] = stoch_fast['fastd'] + dataframe['fastk'] = stoch_fast['fastk'] + + # Stoch RSI + stoch_rsi = ta.STOCHRSI(dataframe) + dataframe['fastd_rsi'] = stoch_rsi['fastd'] + dataframe['fastk_rsi'] = stoch_rsi['fastk'] + + + # Overlap Studies + # ------------------------------------ + + """ + # Previous Bollinger bands + # Because ta.BBANDS implementation is broken with small numbers, it actually + # returns middle band for all the three bands. Switch to qtpylib.bollinger_bands + # and use middle band instead. + + dataframe['blower'] = ta.BBANDS(dataframe, nbdevup=2, nbdevdn=2)['lowerband'] + """ + + # Bollinger bands + bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2) + dataframe['bb_lowerband'] = bollinger['lower'] + dataframe['bb_middleband'] = bollinger['mid'] + dataframe['bb_upperband'] = bollinger['upper'] + + + # EMA - Exponential Moving Average + dataframe['ema3'] = ta.EMA(dataframe, timeperiod=3) + dataframe['ema5'] = ta.EMA(dataframe, timeperiod=5) + dataframe['ema10'] = ta.EMA(dataframe, timeperiod=10) + dataframe['ema50'] = ta.EMA(dataframe, timeperiod=50) + dataframe['ema100'] = ta.EMA(dataframe, timeperiod=100) + + # SAR Parabol + dataframe['sar'] = ta.SAR(dataframe) + + # SMA - Simple Moving Average + dataframe['sma'] = ta.SMA(dataframe, timeperiod=40) + + + # TEMA - Triple Exponential Moving Average + dataframe['tema'] = ta.TEMA(dataframe, timeperiod=9) + + # Cycle Indicator + # ------------------------------------ + # Hilbert Transform Indicator - SineWave + hilbert = ta.HT_SINE(dataframe) + dataframe['htsine'] = hilbert['sine'] + dataframe['htleadsine'] = hilbert['leadsine'] + + # Pattern Recognition - Bullish candlestick patterns + # ------------------------------------ + + # Hammer: values [0, 100] + dataframe['CDLHAMMER'] = ta.CDLHAMMER(dataframe) + # Inverted Hammer: values [0, 100] + dataframe['CDLINVERTEDHAMMER'] = ta.CDLINVERTEDHAMMER(dataframe) + # Dragonfly Doji: values [0, 100] + dataframe['CDLDRAGONFLYDOJI'] = ta.CDLDRAGONFLYDOJI(dataframe) + # Piercing Line: values [0, 100] + dataframe['CDLPIERCING'] = ta.CDLPIERCING(dataframe) # values [0, 100] + # Morningstar: values [0, 100] + dataframe['CDLMORNINGSTAR'] = ta.CDLMORNINGSTAR(dataframe) # values [0, 100] + # Three White Soldiers: values [0, 100] + dataframe['CDL3WHITESOLDIERS'] = ta.CDL3WHITESOLDIERS(dataframe) # values [0, 100] + + + # Pattern Recognition - Bearish candlestick patterns + # ------------------------------------ + + # Hanging Man: values [0, 100] + dataframe['CDLHANGINGMAN'] = ta.CDLHANGINGMAN(dataframe) + # Shooting Star: values [0, 100] + dataframe['CDLSHOOTINGSTAR'] = ta.CDLSHOOTINGSTAR(dataframe) + # Gravestone Doji: values [0, 100] + dataframe['CDLGRAVESTONEDOJI'] = ta.CDLGRAVESTONEDOJI(dataframe) + # Dark Cloud Cover: values [0, 100] + dataframe['CDLDARKCLOUDCOVER'] = ta.CDLDARKCLOUDCOVER(dataframe) + # Evening Doji Star: values [0, 100] + dataframe['CDLEVENINGDOJISTAR'] = ta.CDLEVENINGDOJISTAR(dataframe) + # Evening Star: values [0, 100] + dataframe['CDLEVENINGSTAR'] = ta.CDLEVENINGSTAR(dataframe) + + + # Pattern Recognition - Bullish/Bearish candlestick patterns + # ------------------------------------ + + # Three Line Strike: values [0, -100, 100] + dataframe['CDL3LINESTRIKE'] = ta.CDL3LINESTRIKE(dataframe) + # Spinning Top: values [0, -100, 100] + dataframe['CDLSPINNINGTOP'] = ta.CDLSPINNINGTOP(dataframe) # values [0, -100, 100] + # Engulfing: values [0, -100, 100] + dataframe['CDLENGULFING'] = ta.CDLENGULFING(dataframe) # values [0, -100, 100] + # Harami: values [0, -100, 100] + dataframe['CDLHARAMI'] = ta.CDLHARAMI(dataframe) # values [0, -100, 100] + # Three Outside Up/Down: values [0, -100, 100] + dataframe['CDL3OUTSIDE'] = ta.CDL3OUTSIDE(dataframe) # values [0, -100, 100] + # Three Inside Up/Down: values [0, -100, 100] + dataframe['CDL3INSIDE'] = ta.CDL3INSIDE(dataframe) # values [0, -100, 100] + + + # Chart type + # ------------------------------------ + + # Heikinashi stategy + heikinashi = qtpylib.heikinashi(dataframe) + dataframe['ha_open'] = heikinashi['open'] + dataframe['ha_close'] = heikinashi['close'] + dataframe['ha_high'] = heikinashi['high'] + dataframe['ha_low'] = heikinashi['low'] + + + return dataframe + + params = Select() + valm = random.randint(1,100) + print('MFI Value :' + str(valm) + ' XXX') + valfast = random.randint(1,100) + print('FASTD Value :' + str(valfast) + ' XXX') + valadx = random.randint(1,100) + print('ADX Value :' + str(valadx) + ' XXX') + valrsi = random.randint(1,100) + print('RSI Value :' + str(valrsi) + ' XXX') + def populate_buy_trend(self, dataframe: DataFrame) -> DataFrame: + + conditions = [] + # GUARDS AND TRENDS + if 'uptrend_long_ema' in str(self.params): + conditions.append(dataframe['ema50'] > dataframe['ema100']) + if 'macd_below_zero' in str(self.params): + conditions.append(dataframe['macd'] < 0) + if 'uptrend_short_ema' in str(self.params): + conditions.append(dataframe['ema5'] > dataframe['ema10']) + if 'mfi' in str(self.params): + + conditions.append(dataframe['mfi'] < self.valm) + if 'fastd' in str(self.params): + + conditions.append(dataframe['fastd'] < self.valfast) + if 'adx' in str(self.params): + + conditions.append(dataframe['adx'] > self.valadx) + if 'rsi' in str(self.params): + + conditions.append(dataframe['rsi'] < self.valrsi) + if 'over_sar' in str(self.params): + conditions.append(dataframe['close'] > dataframe['sar']) + if 'green_candle' in str(self.params): + conditions.append(dataframe['close'] > dataframe['open']) + if 'uptrend_sma' in str(self.params): + prevsma = dataframe['sma'].shift(1) + conditions.append(dataframe['sma'] > prevsma) + if 'closebb' in str(self.params): + conditions.append(dataframe['close'] < dataframe['bb_lowerband']) + if 'temabb' in str(self.params): + conditions.append(dataframe['tema'] < dataframe['bb_lowerband']) + if 'fastdt' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['fastd'], 10.0)) + if 'ao' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['ao'], 0.0)) + if 'ema3' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['ema3'], dataframe['ema10'])) + if 'macd' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['macd'], dataframe['macdsignal'])) + if 'closesar' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['close'], dataframe['sar'])) + if 'htsine' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['htleadsine'], dataframe['htsine'])) + if 'has' in str(self.params): + conditions.append((qtpylib.crossed_above(dataframe['ha_close'], dataframe['ha_open'])) & (dataframe['ha_low'] == dataframe['ha_open'])) + if 'plusdi' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['plus_di'], dataframe['minus_di'])) + + dataframe.loc[ + reduce(lambda x, y: x & y, conditions), + 'buy'] = 1 + + return dataframe + + def populate_sell_trend(self, dataframe: DataFrame) -> DataFrame: + """ + Based on TA indicators, populates the sell signal for the given dataframe + :param dataframe: DataFrame + :return: DataFrame with buy column + """ + dataframe.loc[ + ( + ), + 'sell'] = 1 + return dataframe + From 93a1c7cb2af0b3f118e51fcd11bedaa18af61b62 Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 07:50:41 -0500 Subject: [PATCH 11/28] Add files via upload From e4b30fc667c78c63dde69857e3b17f9e7fc1d0ce Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 07:54:11 -0500 Subject: [PATCH 12/28] Added missing script for backup --- user_data/random/random.py | 58 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 user_data/random/random.py diff --git a/user_data/random/random.py b/user_data/random/random.py new file mode 100644 index 000000000..40915ab3a --- /dev/null +++ b/user_data/random/random.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +import os +import multiprocessing +from itertools import zip_longest +import subprocess +import re +PROC_COUNT = multiprocessing.cpu_count() - 1 +cwd = os.getcwd() +print(cwd) + + +limit = multiprocessing.cpu_count() - 1 +WORK_DIR = os.path.join( + os.path.sep, + os.path.abspath(os.path.dirname(__file__)), + '..', 'freqtrade', 'main.py' +) + +# Spawn workers +command = [ + 'python3.6', + WORK_DIR, + 'backtesting', +] +global current +current = 0 + +DEVNULL = open(os.devnull, 'wb') + +while True: + def Run(command): + global current + processes = [subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) for i in range(PROC_COUNT)] + for proc in processes: + wait = proc.communicate() + string = str(wait) + params = re.search(r'~~~~(.*)\~~~~', string).group(1) + mfi = re.search(r'MFI Value(.*)XXX', string) + fastd = re.search(r'FASTD Value(.*)XXX', string) + adx = re.search(r'ADX Value(.*)XXX', string) + rsi = re.search(r'RSI Value(.*)XXX', string) + tot = re.search(r'TOTAL(.*)', string).group(1) + total = re.search(r'[-+]?([0-9]*\.[0-9]+|[0-9]+)', tot).group(1) + if total and (float(total) > float(current)): + current = total + print('total better profit paremeters: ') + print(total) + if params: + print(params) + if mfi: + print(mfi.group(1)) + if fastd: + print(fastd.group(1)) + if adx: + print(adx.group(1)) + if rsi: + print(rsi.group(1)) + data = Run(command) From 68a619ad0a109d4c9aaecd05a55a998b8a1d28fb Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 08:14:57 -0500 Subject: [PATCH 13/28] Update random.py --- user_data/random/random.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_data/random/random.py b/user_data/random/random.py index 40915ab3a..c9dcabc95 100644 --- a/user_data/random/random.py +++ b/user_data/random/random.py @@ -34,7 +34,7 @@ while True: for proc in processes: wait = proc.communicate() string = str(wait) - params = re.search(r'~~~~(.*)\~~~~', string).group(1) + params = re.search(r'~~~~(.*)\~~~~', string) mfi = re.search(r'MFI Value(.*)XXX', string) fastd = re.search(r'FASTD Value(.*)XXX', string) adx = re.search(r'ADX Value(.*)XXX', string) From 2cb760cac61d04792b000878d41046030c5bac28 Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 08:18:58 -0500 Subject: [PATCH 14/28] Update random.py --- user_data/random/random.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/user_data/random/random.py b/user_data/random/random.py index c9dcabc95..960eefb57 100644 --- a/user_data/random/random.py +++ b/user_data/random/random.py @@ -34,7 +34,7 @@ while True: for proc in processes: wait = proc.communicate() string = str(wait) - params = re.search(r'~~~~(.*)\~~~~', string) + params = re.search(r'~~~~(.*)~~~~', string).group(1) mfi = re.search(r'MFI Value(.*)XXX', string) fastd = re.search(r'FASTD Value(.*)XXX', string) adx = re.search(r'ADX Value(.*)XXX', string) @@ -47,6 +47,9 @@ while True: print(total) if params: print(params) + print('~~~~~~') + print('Only enable the above settings, not all settings below are used!') + print('~~~~~~') if mfi: print(mfi.group(1)) if fastd: From 8c2c3983b7e0d6088532d581e02fc4a6cfaf9261 Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 08:24:54 -0500 Subject: [PATCH 15/28] Update random.py --- user_data/random/random.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/user_data/random/random.py b/user_data/random/random.py index 960eefb57..610af04b0 100644 --- a/user_data/random/random.py +++ b/user_data/random/random.py @@ -51,11 +51,15 @@ while True: print('Only enable the above settings, not all settings below are used!') print('~~~~~~') if mfi: + print('~~~MFI~~~') print(mfi.group(1)) if fastd: + print('~~~FASTD~~~') print(fastd.group(1)) if adx: + print('~~~ADX~~~') print(adx.group(1)) if rsi: + print('~~~RSI~~~') print(rsi.group(1)) data = Run(command) From 77ea47f32b00d21a13c4fec93c6bc9f54a143dda Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 09:30:27 -0500 Subject: [PATCH 16/28] Better processing time, cut down on processing time. --- user_data/random/random.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/user_data/random/random.py b/user_data/random/random.py index 610af04b0..5df05414f 100644 --- a/user_data/random/random.py +++ b/user_data/random/random.py @@ -7,9 +7,8 @@ import re PROC_COUNT = multiprocessing.cpu_count() - 1 cwd = os.getcwd() print(cwd) - - -limit = multiprocessing.cpu_count() - 1 +global procs +limit = 24 WORK_DIR = os.path.join( os.path.sep, os.path.abspath(os.path.dirname(__file__)), @@ -24,14 +23,14 @@ command = [ ] global current current = 0 - +procs = 0 DEVNULL = open(os.devnull, 'wb') while True: - def Run(command): - global current - processes = [subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) for i in range(PROC_COUNT)] - for proc in processes: + while procs < 32: + try: + procs + 1 + proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) wait = proc.communicate() string = str(wait) params = re.search(r'~~~~(.*)~~~~', string).group(1) @@ -62,4 +61,6 @@ while True: if rsi: print('~~~RSI~~~') print(rsi.group(1)) - data = Run(command) + procs - 1 + except Exception as e: + print(e) From b63db679c1f07c6f07aef0733e1863097e6c5794 Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 09:32:05 -0500 Subject: [PATCH 17/28] Code clean-up --- user_data/random/random.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/user_data/random/random.py b/user_data/random/random.py index 5df05414f..f2f474043 100644 --- a/user_data/random/random.py +++ b/user_data/random/random.py @@ -1,14 +1,11 @@ #!/usr/bin/env python3 import os import multiprocessing -from itertools import zip_longest import subprocess import re PROC_COUNT = multiprocessing.cpu_count() - 1 cwd = os.getcwd() print(cwd) -global procs -limit = 24 WORK_DIR = os.path.join( os.path.sep, os.path.abspath(os.path.dirname(__file__)), @@ -22,12 +19,13 @@ command = [ 'backtesting', ] global current +global procs current = 0 procs = 0 DEVNULL = open(os.devnull, 'wb') while True: - while procs < 32: + while procs < PROC_COUNT: try: procs + 1 proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) From 5fbb62a5dcf506e0ddb27188a9ad862ec9c1ddf9 Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 10:17:27 -0500 Subject: [PATCH 18/28] Delete random.py --- user_data/random/random.py | 64 -------------------------------------- 1 file changed, 64 deletions(-) delete mode 100644 user_data/random/random.py diff --git a/user_data/random/random.py b/user_data/random/random.py deleted file mode 100644 index f2f474043..000000000 --- a/user_data/random/random.py +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env python3 -import os -import multiprocessing -import subprocess -import re -PROC_COUNT = multiprocessing.cpu_count() - 1 -cwd = os.getcwd() -print(cwd) -WORK_DIR = os.path.join( - os.path.sep, - os.path.abspath(os.path.dirname(__file__)), - '..', 'freqtrade', 'main.py' -) - -# Spawn workers -command = [ - 'python3.6', - WORK_DIR, - 'backtesting', -] -global current -global procs -current = 0 -procs = 0 -DEVNULL = open(os.devnull, 'wb') - -while True: - while procs < PROC_COUNT: - try: - procs + 1 - proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - wait = proc.communicate() - string = str(wait) - params = re.search(r'~~~~(.*)~~~~', string).group(1) - mfi = re.search(r'MFI Value(.*)XXX', string) - fastd = re.search(r'FASTD Value(.*)XXX', string) - adx = re.search(r'ADX Value(.*)XXX', string) - rsi = re.search(r'RSI Value(.*)XXX', string) - tot = re.search(r'TOTAL(.*)', string).group(1) - total = re.search(r'[-+]?([0-9]*\.[0-9]+|[0-9]+)', tot).group(1) - if total and (float(total) > float(current)): - current = total - print('total better profit paremeters: ') - print(total) - if params: - print(params) - print('~~~~~~') - print('Only enable the above settings, not all settings below are used!') - print('~~~~~~') - if mfi: - print('~~~MFI~~~') - print(mfi.group(1)) - if fastd: - print('~~~FASTD~~~') - print(fastd.group(1)) - if adx: - print('~~~ADX~~~') - print(adx.group(1)) - if rsi: - print('~~~RSI~~~') - print(rsi.group(1)) - procs - 1 - except Exception as e: - print(e) From b8968a4fb738a95c61a8d1251a71eacd8fe88acd Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 10:17:34 -0500 Subject: [PATCH 19/28] Add files via upload --- user_data/random/random.py | 67 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 user_data/random/random.py diff --git a/user_data/random/random.py b/user_data/random/random.py new file mode 100644 index 000000000..33d9d88f4 --- /dev/null +++ b/user_data/random/random.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +import os +import multiprocessing +from itertools import zip_longest +import subprocess +import re +PROC_COUNT = multiprocessing.cpu_count() - 1 +cwd = os.getcwd() +print(cwd) +global procs +import time +limit = 24 +WORK_DIR = os.path.join( + os.path.sep, + os.path.abspath(os.path.dirname(__file__)), + '..', 'freqtrade', 'main.py' +) + +# Spawn workers +command = [ + 'python3.6', + WORK_DIR, + 'backtesting', +] +global current +current = 0 +procs = 0 +DEVNULL = open(os.devnull, 'wb') + +while True: + while procs < 32: + try: + procs + 1 + proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + data = proc.communicate() + string = str(data) + params = re.search(r'~~~~(.*)~~~~', string).group(1) + mfi = re.search(r'MFI Value(.*)XXX', string) + fastd = re.search(r'FASTD Value(.*)XXX', string) + adx = re.search(r'ADX Value(.*)XXX', string) + rsi = re.search(r'RSI Value(.*)XXX', string) + tot = re.search(r'TOTAL(.*)', string).group(1) + total = re.search(r'[-+]?([0-9]*\.[0-9]+|[0-9]+)', tot).group(1) + if total and (float(total) > float(current)): + current = total + print('total better profit paremeters: ') + print(total) + if params: + print(params) + print('~~~~~~') + print('Only enable the above settings, not all settings below are used!') + print('~~~~~~') + if mfi: + print('~~~MFI~~~') + print(mfi.group(1)) + if fastd: + print('~~~FASTD~~~') + print(fastd.group(1)) + if adx: + print('~~~ADX~~~') + print(adx.group(1)) + if rsi: + print('~~~RSI~~~') + print(rsi.group(1)) + procs - 1 + except Exception as e: + print(e) From ef8720f84e6f65bd1ff3a9b41c30ccdfe6b8b989 Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 10:17:47 -0500 Subject: [PATCH 20/28] Delete backtesting.py --- user_data/random/backtesting.py | 296 -------------------------------- 1 file changed, 296 deletions(-) delete mode 100644 user_data/random/backtesting.py diff --git a/user_data/random/backtesting.py b/user_data/random/backtesting.py deleted file mode 100644 index e2e45f697..000000000 --- a/user_data/random/backtesting.py +++ /dev/null @@ -1,296 +0,0 @@ -# pragma pylint: disable=missing-docstring, W0212, too-many-arguments - -""" -This module contains the backtesting logic -""" -from argparse import Namespace -from typing import Dict, Tuple, Any, List, Optional - -import arrow -from pandas import DataFrame, Series -from tabulate import tabulate - -import freqtrade.optimize as optimize -from freqtrade import exchange -from freqtrade.analyze import Analyze -from freqtrade.arguments import Arguments -from freqtrade.configuration import Configuration -from freqtrade.exchange import Bittrex -from freqtrade.logger import Logger -from freqtrade.misc import file_dump_json -from freqtrade.persistence import Trade - - -class Backtesting(object): - """ - Backtesting class, this class contains all the logic to run a backtest - - To run a backtest: - backtesting = Backtesting(config) - backtesting.start() - """ - def __init__(self, config: Dict[str, Any]) -> None: - - # Init the logger - self.logging = Logger(name=__name__, level=config['loglevel']) - self.logger = self.logging.get_logger() - self.config = config - self.analyze = None - self.ticker_interval = None - self.tickerdata_to_dataframe = None - self.populate_buy_trend = None - self.populate_sell_trend = None - self._init() - - def _init(self) -> None: - """ - Init objects required for backtesting - :return: None - """ - self.analyze = Analyze(self.config) - self.ticker_interval = self.analyze.strategy.ticker_interval - self.tickerdata_to_dataframe = self.analyze.tickerdata_to_dataframe - self.populate_buy_trend = self.analyze.populate_buy_trend - self.populate_sell_trend = self.analyze.populate_sell_trend - exchange._API = Bittrex({'key': '', 'secret': ''}) - - @staticmethod - def get_timeframe(data: Dict[str, DataFrame]) -> Tuple[arrow.Arrow, arrow.Arrow]: - """ - Get the maximum timeframe for the given backtest data - :param data: dictionary with preprocessed backtesting data - :return: tuple containing min_date, max_date - """ - all_dates = Series([]) - for pair_data in data.values(): - all_dates = all_dates.append(pair_data['date']) - all_dates.sort_values(inplace=True) - return arrow.get(all_dates.iloc[0]), arrow.get(all_dates.iloc[-1]) - - def _generate_text_table(self, data: Dict[str, Dict], results: DataFrame) -> str: - """ - Generates and returns a text table for the given backtest data and the results dataframe - :return: pretty printed table with tabulate as str - """ - stake_currency = self.config.get('stake_currency') - - floatfmt = ('.8f', '.8f', '.8f', '.8f', '.1f') - tabular_data = [] - headers = ['pair', 'buy count', 'avg profit %', - 'total profit ' + stake_currency, 'avg duration', 'profit', 'loss'] - - # Append Total - tabular_data.append([ - 'TOTAL', - results.profit_BTC.sum(), - ]) - return tabulate(tabular_data, headers=headers, floatfmt=floatfmt) - - def _get_sell_trade_entry( - self, pair: str, buy_row: DataFrame, - partial_ticker: List, trade_count_lock: Dict, args: Dict) -> Optional[Tuple]: - - stake_amount = args['stake_amount'] - max_open_trades = args.get('max_open_trades', 0) - trade = Trade( - open_rate=buy_row.close, - open_date=buy_row.date, - stake_amount=stake_amount, - amount=stake_amount / buy_row.open, - fee=exchange.get_fee() - ) - - # calculate win/lose forwards from buy point - for sell_row in partial_ticker: - if max_open_trades > 0: - # Increase trade_count_lock for every iteration - trade_count_lock[sell_row.date] = trade_count_lock.get(sell_row.date, 0) + 1 - - buy_signal = sell_row.buy - if self.analyze.should_sell(trade, sell_row.close, sell_row.date, buy_signal, - sell_row.sell): - return \ - sell_row, \ - ( - pair, - trade.calc_profit_percent(rate=sell_row.close), - trade.calc_profit(rate=sell_row.close), - (sell_row.date - buy_row.date).seconds // 60 - ), \ - sell_row.date - return None - - def backtest(self, args: Dict) -> DataFrame: - """ - Implements backtesting functionality - - NOTE: This method is used by Hyperopt at each iteration. Please keep it optimized. - Of course try to not have ugly code. By some accessor are sometime slower than functions. - Avoid, logging on this method - - :param args: a dict containing: - stake_amount: btc amount to use for each trade - processed: a processed dictionary with format {pair, data} - max_open_trades: maximum number of concurrent trades (default: 0, disabled) - realistic: do we try to simulate realistic trades? (default: True) - sell_profit_only: sell if profit only - use_sell_signal: act on sell-signal - :return: DataFrame - """ - headers = ['date', 'buy', 'open', 'close', 'sell'] - processed = args['processed'] - max_open_trades = args.get('max_open_trades', 0) - realistic = args.get('realistic', False) - record = args.get('record', None) - records = [] - trades = [] - trade_count_lock = {} - for pair, pair_data in processed.items(): - pair_data['buy'], pair_data['sell'] = 0, 0 # cleanup from previous run - - ticker_data = self.populate_sell_trend(self.populate_buy_trend(pair_data))[headers] - ticker = [x for x in ticker_data.itertuples()] - - lock_pair_until = None - for index, row in enumerate(ticker): - if row.buy == 0 or row.sell == 1: - continue # skip rows where no buy signal or that would immediately sell off - - if realistic: - if lock_pair_until is not None and row.date <= lock_pair_until: - continue - if max_open_trades > 0: - # Check if max_open_trades has already been reached for the given date - if not trade_count_lock.get(row.date, 0) < max_open_trades: - continue - - trade_count_lock[row.date] = trade_count_lock.get(row.date, 0) + 1 - - ret = self._get_sell_trade_entry(pair, row, ticker[index + 1:], - trade_count_lock, args) - - if ret: - row2, trade_entry, next_date = ret - lock_pair_until = next_date - trades.append(trade_entry) - if record: - # Note, need to be json.dump friendly - # record a tuple of pair, current_profit_percent, - # entry-date, duration - records.append((pair, trade_entry[1], - row.date.strftime('%s'), - row2.date.strftime('%s'), - row.date, trade_entry[3])) - # For now export inside backtest(), maybe change so that backtest() - # returns a tuple like: (dataframe, records, logs, etc) - if record and record.find('trades') >= 0: - self.logger.info('Dumping backtest results') - file_dump_json('backtest-result.json', records) - labels = ['currency', 'profit_percent', 'profit_BTC', 'duration'] - return DataFrame.from_records(trades, columns=labels) - - def start(self) -> None: - """ - Run a backtesting end-to-end - :return: None - """ - data = {} - pairs = self.config['exchange']['pair_whitelist'] - self.logger.info('Using stake_currency: %s ...', self.config['stake_currency']) - self.logger.info('Using stake_amount: %s ...', self.config['stake_amount']) - - if self.config.get('live'): - self.logger.info('Downloading data for all pairs in whitelist ...') - for pair in pairs: - data[pair] = exchange.get_ticker_history(pair, self.ticker_interval) - else: - self.logger.info('Using local backtesting data (using whitelist in given config) ...') - - timerange = Arguments.parse_timerange(self.config.get('timerange')) - data = optimize.load_data( - self.config['datadir'], - pairs=pairs, - ticker_interval=self.ticker_interval, - refresh_pairs=self.config.get('refresh_pairs', False), - timerange=timerange - ) - - # Ignore max_open_trades in backtesting, except realistic flag was passed - if self.config.get('realistic_simulation', False): - max_open_trades = self.config['max_open_trades'] - else: - self.logger.info('Ignoring max_open_trades (realistic_simulation not set) ...') - max_open_trades = 0 - - preprocessed = self.tickerdata_to_dataframe(data) - - # Print timeframe - min_date, max_date = self.get_timeframe(preprocessed) - self.logger.info( - 'Measuring data from %s up to %s (%s days)..', - min_date.isoformat(), - max_date.isoformat(), - (max_date - min_date).days - ) - - # Execute backtest and print results - sell_profit_only = self.config.get('experimental', {}).get('sell_profit_only', False) - use_sell_signal = self.config.get('experimental', {}).get('use_sell_signal', False) - results = self.backtest( - { - 'stake_amount': self.config.get('stake_amount'), - 'processed': preprocessed, - 'max_open_trades': max_open_trades, - 'realistic': self.config.get('realistic_simulation', False), - 'sell_profit_only': sell_profit_only, - 'use_sell_signal': use_sell_signal, - 'record': self.config.get('export') - } - ) - - self.logging.set_format('%(message)s') - self.logger.info( - '\n==================================== ' - 'BACKTESTING REPORT' - ' ====================================\n' - '%s', - self._generate_text_table( - data, - results - ) - ) - - -def setup_configuration(args: Namespace) -> Dict[str, Any]: - """ - Prepare the configuration for the backtesting - :param args: Cli args from Arguments() - :return: Configuration - """ - configuration = Configuration(args) - config = configuration.get_config() - - # Ensure we do not use Exchange credentials - config['exchange']['key'] = '' - config['exchange']['secret'] = '' - - return config - - -def start(args: Namespace) -> None: - """ - Start Backtesting script - :param args: Cli args from Arguments() - :return: None - """ - - # Initialize logger - logger = Logger(name=__name__).get_logger() - logger.info('Starting freqtrade in Backtesting mode') - - # Initialize configuration - config = setup_configuration(args) - - # Initialize backtesting object - backtesting = Backtesting(config) - backtesting.start() From feb24c6153b96e066d3eb664d7973dfa165f5f43 Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 10:17:53 -0500 Subject: [PATCH 21/28] Add files via upload --- user_data/random/backtesting.py | 294 ++++++++++++++++++++++++++++++++ 1 file changed, 294 insertions(+) create mode 100644 user_data/random/backtesting.py diff --git a/user_data/random/backtesting.py b/user_data/random/backtesting.py new file mode 100644 index 000000000..5697f7bbd --- /dev/null +++ b/user_data/random/backtesting.py @@ -0,0 +1,294 @@ +# pragma pylint: disable=missing-docstring, W0212, too-many-arguments + +""" +This module contains the backtesting logic +""" +from argparse import Namespace +from typing import Dict, Tuple, Any, List, Optional + +import arrow +from pandas import DataFrame, Series +from tabulate import tabulate +import os +import freqtrade.optimize as optimize +from freqtrade import exchange +from freqtrade.analyze import Analyze +from freqtrade.arguments import Arguments +from freqtrade.configuration import Configuration +from freqtrade.exchange import Bittrex +from freqtrade.logger import Logger +from freqtrade.misc import file_dump_json +from freqtrade.persistence import Trade + + +class Backtesting(object): + """ + Backtesting class, this class contains all the logic to run a backtest + + To run a backtest: + backtesting = Backtesting(config) + backtesting.start() + """ + def __init__(self, config: Dict[str, Any]) -> None: + + # Init the logger + self.logging = Logger(name=__name__, level=config['loglevel']) + self.logger = self.logging.get_logger() + self.config = config + self.analyze = None + self.ticker_interval = None + self.tickerdata_to_dataframe = None + self.populate_buy_trend = None + self.populate_sell_trend = None + self._init() + + def _init(self) -> None: + """ + Init objects required for backtesting + :return: None + """ + self.analyze = Analyze(self.config) + self.ticker_interval = self.analyze.strategy.ticker_interval + self.tickerdata_to_dataframe = self.analyze.tickerdata_to_dataframe + self.populate_buy_trend = self.analyze.populate_buy_trend + self.populate_sell_trend = self.analyze.populate_sell_trend + exchange._API = Bittrex({'key': '', 'secret': ''}) + + @staticmethod + def get_timeframe(data: Dict[str, DataFrame]) -> Tuple[arrow.Arrow, arrow.Arrow]: + """ + Get the maximum timeframe for the given backtest data + :param data: dictionary with preprocessed backtesting data + :return: tuple containing min_date, max_date + """ + all_dates = Series([]) + for pair_data in data.values(): + all_dates = all_dates.append(pair_data['date']) + all_dates.sort_values(inplace=True) + return arrow.get(all_dates.iloc[0]), arrow.get(all_dates.iloc[-1]) + + def _generate_text_table(self, data: Dict[str, Dict], results: DataFrame) -> str: + """ + Generates and returns a text table for the given backtest data and the results dataframe + :return: pretty printed table with tabulate as str + """ + stake_currency = self.config.get('stake_currency') + + floatfmt = ('.8f', '.8f', '.8f', '.8f', '.1f') + tabular_data = [] + headers = ['total profit ' + stake_currency] + + # Append Total + tabular_data.append([ + 'TOTAL', + results.profit_BTC.sum(), + ]) + return tabulate(tabular_data, headers=headers, floatfmt=floatfmt) + + def _get_sell_trade_entry( + self, pair: str, buy_row: DataFrame, + partial_ticker: List, trade_count_lock: Dict, args: Dict) -> Optional[Tuple]: + + stake_amount = args['stake_amount'] + max_open_trades = args.get('max_open_trades', 0) + trade = Trade( + open_rate=buy_row.close, + open_date=buy_row.date, + stake_amount=stake_amount, + amount=stake_amount / buy_row.open, + fee=exchange.get_fee() + ) + + # calculate win/lose forwards from buy point + for sell_row in partial_ticker: + if max_open_trades > 0: + # Increase trade_count_lock for every iteration + trade_count_lock[sell_row.date] = trade_count_lock.get(sell_row.date, 0) + 1 + + buy_signal = sell_row.buy + if self.analyze.should_sell(trade, sell_row.close, sell_row.date, buy_signal, + sell_row.sell): + return \ + sell_row, \ + ( + pair, + trade.calc_profit_percent(rate=sell_row.close), + trade.calc_profit(rate=sell_row.close), + (sell_row.date - buy_row.date).seconds // 60 + ), \ + sell_row.date + return None + + def backtest(self, args: Dict) -> DataFrame: + """ + Implements backtesting functionality + + NOTE: This method is used by Hyperopt at each iteration. Please keep it optimized. + Of course try to not have ugly code. By some accessor are sometime slower than functions. + Avoid, logging on this method + + :param args: a dict containing: + stake_amount: btc amount to use for each trade + processed: a processed dictionary with format {pair, data} + max_open_trades: maximum number of concurrent trades (default: 0, disabled) + realistic: do we try to simulate realistic trades? (default: True) + sell_profit_only: sell if profit only + use_sell_signal: act on sell-signal + :return: DataFrame + """ + headers = ['date', 'buy', 'open', 'close', 'sell'] + processed = args['processed'] + max_open_trades = args.get('max_open_trades', 0) + realistic = args.get('realistic', False) + record = args.get('record', None) + records = [] + trades = [] + trade_count_lock = {} + for pair, pair_data in processed.items(): + pair_data['buy'], pair_data['sell'] = 0, 0 # cleanup from previous run + + ticker_data = self.populate_sell_trend(self.populate_buy_trend(pair_data))[headers] + ticker = [x for x in ticker_data.itertuples()] + + lock_pair_until = None + for index, row in enumerate(ticker): + if row.buy == 0 or row.sell == 1: + continue # skip rows where no buy signal or that would immediately sell off + + if realistic: + if lock_pair_until is not None and row.date <= lock_pair_until: + continue + if max_open_trades > 0: + # Check if max_open_trades has already been reached for the given date + if not trade_count_lock.get(row.date, 0) < max_open_trades: + continue + + trade_count_lock[row.date] = trade_count_lock.get(row.date, 0) + 1 + + ret = self._get_sell_trade_entry(pair, row, ticker[index + 1:], + trade_count_lock, args) + + if ret: + row2, trade_entry, next_date = ret + lock_pair_until = next_date + trades.append(trade_entry) + if record: + # Note, need to be json.dump friendly + # record a tuple of pair, current_profit_percent, + # entry-date, duration + records.append((pair, trade_entry[1], + row.date.strftime('%s'), + row2.date.strftime('%s'), + row.date, trade_entry[3])) + # For now export inside backtest(), maybe change so that backtest() + # returns a tuple like: (dataframe, records, logs, etc) + if record and record.find('trades') >= 0: + self.logger.info('Dumping backtest results') + file_dump_json('backtest-result.json', records) + labels = ['currency', 'profit_percent', 'profit_BTC', 'duration'] + return DataFrame.from_records(trades, columns=labels) + + def start(self) -> None: + """ + Run a backtesting end-to-end + :return: None + """ + data = {} + pairs = self.config['exchange']['pair_whitelist'] + self.logger.info('Using stake_currency: %s ...', self.config['stake_currency']) + self.logger.info('Using stake_amount: %s ...', self.config['stake_amount']) + + if self.config.get('live'): + self.logger.info('Downloading data for all pairs in whitelist ...') + for pair in pairs: + data[pair] = exchange.get_ticker_history(pair, self.ticker_interval) + else: + self.logger.info('Using local backtesting data (using whitelist in given config) ...') + + timerange = Arguments.parse_timerange(self.config.get('timerange')) + data = optimize.load_data( + self.config['datadir'], + pairs=pairs, + ticker_interval=self.ticker_interval, + refresh_pairs=self.config.get('refresh_pairs', False), + timerange=timerange + ) + + # Ignore max_open_trades in backtesting, except realistic flag was passed + if self.config.get('realistic_simulation', False): + max_open_trades = self.config['max_open_trades'] + else: + self.logger.info('Ignoring max_open_trades (realistic_simulation not set) ...') + max_open_trades = 0 + + preprocessed = self.tickerdata_to_dataframe(data) + + # Print timeframe + min_date, max_date = self.get_timeframe(preprocessed) + self.logger.info( + 'Measuring data from %s up to %s (%s days)..', + min_date.isoformat(), + max_date.isoformat(), + (max_date - min_date).days + ) + + # Execute backtest and print results + sell_profit_only = self.config.get('experimental', {}).get('sell_profit_only', False) + use_sell_signal = self.config.get('experimental', {}).get('use_sell_signal', False) + results = self.backtest( + { + 'stake_amount': self.config.get('stake_amount'), + 'processed': preprocessed, + 'max_open_trades': max_open_trades, + 'realistic': self.config.get('realistic_simulation', False), + 'sell_profit_only': sell_profit_only, + 'use_sell_signal': use_sell_signal, + 'record': self.config.get('export') + } + ) + + self.logging.set_format('%(message)s') + self.logger.info( + '\n==================================== ' + 'BACKTESTING REPORT' + ' ====================================\n' + '%s', + self._generate_text_table( + data, + results + ) + ) + +def setup_configuration(args: Namespace) -> Dict[str, Any]: + """ + Prepare the configuration for the backtesting + :param args: Cli args from Arguments() + :return: Configuration + """ + configuration = Configuration(args) + config = configuration.get_config() + + # Ensure we do not use Exchange credentials + config['exchange']['key'] = '' + config['exchange']['secret'] = '' + + return config + + +def start(args: Namespace) -> None: + """ + Start Backtesting script + :param args: Cli args from Arguments() + :return: None + """ + + # Initialize logger + logger = Logger(name=__name__).get_logger() + logger.info('Starting freqtrade in Backtesting mode') + + # Initialize configuration + config = setup_configuration(args) + + # Initialize backtesting object + backtesting = Backtesting(config) + backtesting.start() From a7b217e953a567a25b9661dc03415521ce29726d Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 10:18:08 -0500 Subject: [PATCH 22/28] Delete default_strategy.py --- user_data/random/default_strategy.py | 342 --------------------------- 1 file changed, 342 deletions(-) delete mode 100644 user_data/random/default_strategy.py diff --git a/user_data/random/default_strategy.py b/user_data/random/default_strategy.py deleted file mode 100644 index fe642225d..000000000 --- a/user_data/random/default_strategy.py +++ /dev/null @@ -1,342 +0,0 @@ - -# --- Do not remove these libs --- -from freqtrade.strategy.interface import IStrategy -from typing import Dict, List -from hyperopt import hp -from functools import reduce -from pandas import DataFrame -# -------------------------------- - -# Add your lib to import here -import talib.abstract as ta -import freqtrade.vendor.qtpylib.indicators as qtpylib -import numpy # noqa - -import random - -# Update this variable if you change the class name -class_name = 'DefaultStrategy' - - -# This class is a sample. Feel free to customize it. - - - - -def Select(): - param = [] - random_items = [] - param.append(str('[' + 'uptrend_long_ema' + '[' + 'enabled' + ']')) - param.append(str('[' + 'macd_below_zero' + '][' + 'enabled' + ']')) - param.append(str('[' + 'uptrend_short_ema' '][' + 'enabled'+ ']')) - param.append(str('[' + 'mfi' '][' + 'enabled'+ ']')) - param.append(str('[' + 'fastd' '][' + 'enabled'+ ']')) - param.append(str('[' + 'adx' '][' + 'enabled'+ ']')) - param.append(str('[' + 'rsi' '][' + 'enabled'+ ']')) - param.append(str('[' + 'over_sar' '][' + 'enabled'+ ']')) - param.append(str('[' + 'green_candle' '][' + 'enabled'+ ']')) - param.append(str('[' + 'uptrend_sma' '][' + 'enabled'+ ']')) - param.append(str('[' + 'closebb' '][' + 'enabled'+ ']')) - param.append(str('[' + 'temabb' '][' + 'enabled'+ ']')) - param.append(str('[' + 'fastdt' '][' + 'enabled'+ ']')) - param.append(str('[' + 'ao' '][' + 'enabled'+ ']')) - param.append(str('[' + 'ema3' '][' + 'enabled'+ ']')) - param.append(str('[' + 'macd' '][' + 'enabled'+ ']')) - param.append(str('[' + 'closesar' '][' + 'enabled'+ ']')) - param.append(str('[' + 'htsine' '][' + 'enabled'+ ']')) - param.append(str('[' + 'has' '][' + 'enabled'+ ']')) - param.append(str('[' + 'plusdi' '][' + 'enabled'+ ']')) - howmany = random.randint(1,20) - random_items = random.choices(population=param, k=howmany) - print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') - print('The Parameters Enabled Are As Follows!!!: ' + str(random_items)) - print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') - return random_items - - - - -class DefaultStrategy(IStrategy): - """ - This is a test strategy to inspire you. - More information in https://github.com/gcarq/freqtrade/blob/develop/docs/bot-optimization.md - - You can: - - Rename the class name (Do not forget to update class_name) - - Add any methods you want to build your strategy - - Add any lib you need to build your strategy - - You must keep: - - the lib in the section "Do not remove these libs" - - the prototype for the methods: minimal_roi, stoploss, populate_indicators, populate_buy_trend, - populate_sell_trend, hyperopt_space, buy_strategy_generator - """ - - # Minimal ROI designed for the strategy. - # This attribute will be overridden if the config file contains "minimal_roi" - minimal_roi = { - "40": 0.0, - "30": 0.01, - "20": 0.02, - "0": 0.04 - } - - ticker_interval = 5 - - # Optimal stoploss designed for the strategy - # This attribute will be overridden if the config file contains "stoploss" - stoploss = -0.10 - - def populate_indicators(self, dataframe: DataFrame) -> DataFrame: - """ - Adds several different TA indicators to the given DataFrame - - Performance Note: For the best performance be frugal on the number of indicators - you are using. Let uncomment only the indicator you are using in your strategies - or your hyperopt configuration, otherwise you will waste your memory and CPU usage. - """ - - # Momentum Indicator - # ------------------------------------ - - # ADX - dataframe['adx'] = ta.ADX(dataframe) - - - # Awesome oscillator - dataframe['ao'] = qtpylib.awesome_oscillator(dataframe) - - # Commodity Channel Index: values Oversold:<-100, Overbought:>100 - dataframe['cci'] = ta.CCI(dataframe) - - # MACD - macd = ta.MACD(dataframe) - dataframe['macd'] = macd['macd'] - dataframe['macdsignal'] = macd['macdsignal'] - dataframe['macdhist'] = macd['macdhist'] - - # MFI - dataframe['mfi'] = ta.MFI(dataframe) - - # Minus Directional Indicator / Movement - dataframe['minus_dm'] = ta.MINUS_DM(dataframe) - dataframe['minus_di'] = ta.MINUS_DI(dataframe) - - # Plus Directional Indicator / Movement - dataframe['plus_dm'] = ta.PLUS_DM(dataframe) - dataframe['plus_di'] = ta.PLUS_DI(dataframe) - dataframe['minus_di'] = ta.MINUS_DI(dataframe) - - # ROC - dataframe['roc'] = ta.ROC(dataframe) - - # RSI - dataframe['rsi'] = ta.RSI(dataframe) - - # Inverse Fisher transform on RSI, values [-1.0, 1.0] (https://goo.gl/2JGGoy) - rsi = 0.1 * (dataframe['rsi'] - 50) - dataframe['fisher_rsi'] = (numpy.exp(2 * rsi) - 1) / (numpy.exp(2 * rsi) + 1) - - # Inverse Fisher transform on RSI normalized, value [0.0, 100.0] (https://goo.gl/2JGGoy) - dataframe['fisher_rsi_norma'] = 50 * (dataframe['fisher_rsi'] + 1) - - # Stoch - stoch = ta.STOCH(dataframe) - dataframe['slowd'] = stoch['slowd'] - dataframe['slowk'] = stoch['slowk'] - - # Stoch fast - stoch_fast = ta.STOCHF(dataframe) - dataframe['fastd'] = stoch_fast['fastd'] - dataframe['fastk'] = stoch_fast['fastk'] - - # Stoch RSI - stoch_rsi = ta.STOCHRSI(dataframe) - dataframe['fastd_rsi'] = stoch_rsi['fastd'] - dataframe['fastk_rsi'] = stoch_rsi['fastk'] - - - # Overlap Studies - # ------------------------------------ - - """ - # Previous Bollinger bands - # Because ta.BBANDS implementation is broken with small numbers, it actually - # returns middle band for all the three bands. Switch to qtpylib.bollinger_bands - # and use middle band instead. - - dataframe['blower'] = ta.BBANDS(dataframe, nbdevup=2, nbdevdn=2)['lowerband'] - """ - - # Bollinger bands - bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2) - dataframe['bb_lowerband'] = bollinger['lower'] - dataframe['bb_middleband'] = bollinger['mid'] - dataframe['bb_upperband'] = bollinger['upper'] - - - # EMA - Exponential Moving Average - dataframe['ema3'] = ta.EMA(dataframe, timeperiod=3) - dataframe['ema5'] = ta.EMA(dataframe, timeperiod=5) - dataframe['ema10'] = ta.EMA(dataframe, timeperiod=10) - dataframe['ema50'] = ta.EMA(dataframe, timeperiod=50) - dataframe['ema100'] = ta.EMA(dataframe, timeperiod=100) - - # SAR Parabol - dataframe['sar'] = ta.SAR(dataframe) - - # SMA - Simple Moving Average - dataframe['sma'] = ta.SMA(dataframe, timeperiod=40) - - - # TEMA - Triple Exponential Moving Average - dataframe['tema'] = ta.TEMA(dataframe, timeperiod=9) - - # Cycle Indicator - # ------------------------------------ - # Hilbert Transform Indicator - SineWave - hilbert = ta.HT_SINE(dataframe) - dataframe['htsine'] = hilbert['sine'] - dataframe['htleadsine'] = hilbert['leadsine'] - - # Pattern Recognition - Bullish candlestick patterns - # ------------------------------------ - - # Hammer: values [0, 100] - dataframe['CDLHAMMER'] = ta.CDLHAMMER(dataframe) - # Inverted Hammer: values [0, 100] - dataframe['CDLINVERTEDHAMMER'] = ta.CDLINVERTEDHAMMER(dataframe) - # Dragonfly Doji: values [0, 100] - dataframe['CDLDRAGONFLYDOJI'] = ta.CDLDRAGONFLYDOJI(dataframe) - # Piercing Line: values [0, 100] - dataframe['CDLPIERCING'] = ta.CDLPIERCING(dataframe) # values [0, 100] - # Morningstar: values [0, 100] - dataframe['CDLMORNINGSTAR'] = ta.CDLMORNINGSTAR(dataframe) # values [0, 100] - # Three White Soldiers: values [0, 100] - dataframe['CDL3WHITESOLDIERS'] = ta.CDL3WHITESOLDIERS(dataframe) # values [0, 100] - - - # Pattern Recognition - Bearish candlestick patterns - # ------------------------------------ - - # Hanging Man: values [0, 100] - dataframe['CDLHANGINGMAN'] = ta.CDLHANGINGMAN(dataframe) - # Shooting Star: values [0, 100] - dataframe['CDLSHOOTINGSTAR'] = ta.CDLSHOOTINGSTAR(dataframe) - # Gravestone Doji: values [0, 100] - dataframe['CDLGRAVESTONEDOJI'] = ta.CDLGRAVESTONEDOJI(dataframe) - # Dark Cloud Cover: values [0, 100] - dataframe['CDLDARKCLOUDCOVER'] = ta.CDLDARKCLOUDCOVER(dataframe) - # Evening Doji Star: values [0, 100] - dataframe['CDLEVENINGDOJISTAR'] = ta.CDLEVENINGDOJISTAR(dataframe) - # Evening Star: values [0, 100] - dataframe['CDLEVENINGSTAR'] = ta.CDLEVENINGSTAR(dataframe) - - - # Pattern Recognition - Bullish/Bearish candlestick patterns - # ------------------------------------ - - # Three Line Strike: values [0, -100, 100] - dataframe['CDL3LINESTRIKE'] = ta.CDL3LINESTRIKE(dataframe) - # Spinning Top: values [0, -100, 100] - dataframe['CDLSPINNINGTOP'] = ta.CDLSPINNINGTOP(dataframe) # values [0, -100, 100] - # Engulfing: values [0, -100, 100] - dataframe['CDLENGULFING'] = ta.CDLENGULFING(dataframe) # values [0, -100, 100] - # Harami: values [0, -100, 100] - dataframe['CDLHARAMI'] = ta.CDLHARAMI(dataframe) # values [0, -100, 100] - # Three Outside Up/Down: values [0, -100, 100] - dataframe['CDL3OUTSIDE'] = ta.CDL3OUTSIDE(dataframe) # values [0, -100, 100] - # Three Inside Up/Down: values [0, -100, 100] - dataframe['CDL3INSIDE'] = ta.CDL3INSIDE(dataframe) # values [0, -100, 100] - - - # Chart type - # ------------------------------------ - - # Heikinashi stategy - heikinashi = qtpylib.heikinashi(dataframe) - dataframe['ha_open'] = heikinashi['open'] - dataframe['ha_close'] = heikinashi['close'] - dataframe['ha_high'] = heikinashi['high'] - dataframe['ha_low'] = heikinashi['low'] - - - return dataframe - - params = Select() - valm = random.randint(1,100) - print('MFI Value :' + str(valm) + ' XXX') - valfast = random.randint(1,100) - print('FASTD Value :' + str(valfast) + ' XXX') - valadx = random.randint(1,100) - print('ADX Value :' + str(valadx) + ' XXX') - valrsi = random.randint(1,100) - print('RSI Value :' + str(valrsi) + ' XXX') - def populate_buy_trend(self, dataframe: DataFrame) -> DataFrame: - - conditions = [] - # GUARDS AND TRENDS - if 'uptrend_long_ema' in str(self.params): - conditions.append(dataframe['ema50'] > dataframe['ema100']) - if 'macd_below_zero' in str(self.params): - conditions.append(dataframe['macd'] < 0) - if 'uptrend_short_ema' in str(self.params): - conditions.append(dataframe['ema5'] > dataframe['ema10']) - if 'mfi' in str(self.params): - - conditions.append(dataframe['mfi'] < self.valm) - if 'fastd' in str(self.params): - - conditions.append(dataframe['fastd'] < self.valfast) - if 'adx' in str(self.params): - - conditions.append(dataframe['adx'] > self.valadx) - if 'rsi' in str(self.params): - - conditions.append(dataframe['rsi'] < self.valrsi) - if 'over_sar' in str(self.params): - conditions.append(dataframe['close'] > dataframe['sar']) - if 'green_candle' in str(self.params): - conditions.append(dataframe['close'] > dataframe['open']) - if 'uptrend_sma' in str(self.params): - prevsma = dataframe['sma'].shift(1) - conditions.append(dataframe['sma'] > prevsma) - if 'closebb' in str(self.params): - conditions.append(dataframe['close'] < dataframe['bb_lowerband']) - if 'temabb' in str(self.params): - conditions.append(dataframe['tema'] < dataframe['bb_lowerband']) - if 'fastdt' in str(self.params): - conditions.append(qtpylib.crossed_above(dataframe['fastd'], 10.0)) - if 'ao' in str(self.params): - conditions.append(qtpylib.crossed_above(dataframe['ao'], 0.0)) - if 'ema3' in str(self.params): - conditions.append(qtpylib.crossed_above(dataframe['ema3'], dataframe['ema10'])) - if 'macd' in str(self.params): - conditions.append(qtpylib.crossed_above(dataframe['macd'], dataframe['macdsignal'])) - if 'closesar' in str(self.params): - conditions.append(qtpylib.crossed_above(dataframe['close'], dataframe['sar'])) - if 'htsine' in str(self.params): - conditions.append(qtpylib.crossed_above(dataframe['htleadsine'], dataframe['htsine'])) - if 'has' in str(self.params): - conditions.append((qtpylib.crossed_above(dataframe['ha_close'], dataframe['ha_open'])) & (dataframe['ha_low'] == dataframe['ha_open'])) - if 'plusdi' in str(self.params): - conditions.append(qtpylib.crossed_above(dataframe['plus_di'], dataframe['minus_di'])) - - dataframe.loc[ - reduce(lambda x, y: x & y, conditions), - 'buy'] = 1 - - return dataframe - - def populate_sell_trend(self, dataframe: DataFrame) -> DataFrame: - """ - Based on TA indicators, populates the sell signal for the given dataframe - :param dataframe: DataFrame - :return: DataFrame with buy column - """ - dataframe.loc[ - ( - ), - 'sell'] = 1 - return dataframe - From 79b114f551fa02b0414b9aa93a719f73f89563fe Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 10:18:34 -0500 Subject: [PATCH 23/28] Done for now, need os.close(1) somewhere. --- user_data/random/default_strategy.py | 342 +++++++++++++++++++++++++++ 1 file changed, 342 insertions(+) create mode 100644 user_data/random/default_strategy.py diff --git a/user_data/random/default_strategy.py b/user_data/random/default_strategy.py new file mode 100644 index 000000000..fe642225d --- /dev/null +++ b/user_data/random/default_strategy.py @@ -0,0 +1,342 @@ + +# --- Do not remove these libs --- +from freqtrade.strategy.interface import IStrategy +from typing import Dict, List +from hyperopt import hp +from functools import reduce +from pandas import DataFrame +# -------------------------------- + +# Add your lib to import here +import talib.abstract as ta +import freqtrade.vendor.qtpylib.indicators as qtpylib +import numpy # noqa + +import random + +# Update this variable if you change the class name +class_name = 'DefaultStrategy' + + +# This class is a sample. Feel free to customize it. + + + + +def Select(): + param = [] + random_items = [] + param.append(str('[' + 'uptrend_long_ema' + '[' + 'enabled' + ']')) + param.append(str('[' + 'macd_below_zero' + '][' + 'enabled' + ']')) + param.append(str('[' + 'uptrend_short_ema' '][' + 'enabled'+ ']')) + param.append(str('[' + 'mfi' '][' + 'enabled'+ ']')) + param.append(str('[' + 'fastd' '][' + 'enabled'+ ']')) + param.append(str('[' + 'adx' '][' + 'enabled'+ ']')) + param.append(str('[' + 'rsi' '][' + 'enabled'+ ']')) + param.append(str('[' + 'over_sar' '][' + 'enabled'+ ']')) + param.append(str('[' + 'green_candle' '][' + 'enabled'+ ']')) + param.append(str('[' + 'uptrend_sma' '][' + 'enabled'+ ']')) + param.append(str('[' + 'closebb' '][' + 'enabled'+ ']')) + param.append(str('[' + 'temabb' '][' + 'enabled'+ ']')) + param.append(str('[' + 'fastdt' '][' + 'enabled'+ ']')) + param.append(str('[' + 'ao' '][' + 'enabled'+ ']')) + param.append(str('[' + 'ema3' '][' + 'enabled'+ ']')) + param.append(str('[' + 'macd' '][' + 'enabled'+ ']')) + param.append(str('[' + 'closesar' '][' + 'enabled'+ ']')) + param.append(str('[' + 'htsine' '][' + 'enabled'+ ']')) + param.append(str('[' + 'has' '][' + 'enabled'+ ']')) + param.append(str('[' + 'plusdi' '][' + 'enabled'+ ']')) + howmany = random.randint(1,20) + random_items = random.choices(population=param, k=howmany) + print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') + print('The Parameters Enabled Are As Follows!!!: ' + str(random_items)) + print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') + return random_items + + + + +class DefaultStrategy(IStrategy): + """ + This is a test strategy to inspire you. + More information in https://github.com/gcarq/freqtrade/blob/develop/docs/bot-optimization.md + + You can: + - Rename the class name (Do not forget to update class_name) + - Add any methods you want to build your strategy + - Add any lib you need to build your strategy + + You must keep: + - the lib in the section "Do not remove these libs" + - the prototype for the methods: minimal_roi, stoploss, populate_indicators, populate_buy_trend, + populate_sell_trend, hyperopt_space, buy_strategy_generator + """ + + # Minimal ROI designed for the strategy. + # This attribute will be overridden if the config file contains "minimal_roi" + minimal_roi = { + "40": 0.0, + "30": 0.01, + "20": 0.02, + "0": 0.04 + } + + ticker_interval = 5 + + # Optimal stoploss designed for the strategy + # This attribute will be overridden if the config file contains "stoploss" + stoploss = -0.10 + + def populate_indicators(self, dataframe: DataFrame) -> DataFrame: + """ + Adds several different TA indicators to the given DataFrame + + Performance Note: For the best performance be frugal on the number of indicators + you are using. Let uncomment only the indicator you are using in your strategies + or your hyperopt configuration, otherwise you will waste your memory and CPU usage. + """ + + # Momentum Indicator + # ------------------------------------ + + # ADX + dataframe['adx'] = ta.ADX(dataframe) + + + # Awesome oscillator + dataframe['ao'] = qtpylib.awesome_oscillator(dataframe) + + # Commodity Channel Index: values Oversold:<-100, Overbought:>100 + dataframe['cci'] = ta.CCI(dataframe) + + # MACD + macd = ta.MACD(dataframe) + dataframe['macd'] = macd['macd'] + dataframe['macdsignal'] = macd['macdsignal'] + dataframe['macdhist'] = macd['macdhist'] + + # MFI + dataframe['mfi'] = ta.MFI(dataframe) + + # Minus Directional Indicator / Movement + dataframe['minus_dm'] = ta.MINUS_DM(dataframe) + dataframe['minus_di'] = ta.MINUS_DI(dataframe) + + # Plus Directional Indicator / Movement + dataframe['plus_dm'] = ta.PLUS_DM(dataframe) + dataframe['plus_di'] = ta.PLUS_DI(dataframe) + dataframe['minus_di'] = ta.MINUS_DI(dataframe) + + # ROC + dataframe['roc'] = ta.ROC(dataframe) + + # RSI + dataframe['rsi'] = ta.RSI(dataframe) + + # Inverse Fisher transform on RSI, values [-1.0, 1.0] (https://goo.gl/2JGGoy) + rsi = 0.1 * (dataframe['rsi'] - 50) + dataframe['fisher_rsi'] = (numpy.exp(2 * rsi) - 1) / (numpy.exp(2 * rsi) + 1) + + # Inverse Fisher transform on RSI normalized, value [0.0, 100.0] (https://goo.gl/2JGGoy) + dataframe['fisher_rsi_norma'] = 50 * (dataframe['fisher_rsi'] + 1) + + # Stoch + stoch = ta.STOCH(dataframe) + dataframe['slowd'] = stoch['slowd'] + dataframe['slowk'] = stoch['slowk'] + + # Stoch fast + stoch_fast = ta.STOCHF(dataframe) + dataframe['fastd'] = stoch_fast['fastd'] + dataframe['fastk'] = stoch_fast['fastk'] + + # Stoch RSI + stoch_rsi = ta.STOCHRSI(dataframe) + dataframe['fastd_rsi'] = stoch_rsi['fastd'] + dataframe['fastk_rsi'] = stoch_rsi['fastk'] + + + # Overlap Studies + # ------------------------------------ + + """ + # Previous Bollinger bands + # Because ta.BBANDS implementation is broken with small numbers, it actually + # returns middle band for all the three bands. Switch to qtpylib.bollinger_bands + # and use middle band instead. + + dataframe['blower'] = ta.BBANDS(dataframe, nbdevup=2, nbdevdn=2)['lowerband'] + """ + + # Bollinger bands + bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2) + dataframe['bb_lowerband'] = bollinger['lower'] + dataframe['bb_middleband'] = bollinger['mid'] + dataframe['bb_upperband'] = bollinger['upper'] + + + # EMA - Exponential Moving Average + dataframe['ema3'] = ta.EMA(dataframe, timeperiod=3) + dataframe['ema5'] = ta.EMA(dataframe, timeperiod=5) + dataframe['ema10'] = ta.EMA(dataframe, timeperiod=10) + dataframe['ema50'] = ta.EMA(dataframe, timeperiod=50) + dataframe['ema100'] = ta.EMA(dataframe, timeperiod=100) + + # SAR Parabol + dataframe['sar'] = ta.SAR(dataframe) + + # SMA - Simple Moving Average + dataframe['sma'] = ta.SMA(dataframe, timeperiod=40) + + + # TEMA - Triple Exponential Moving Average + dataframe['tema'] = ta.TEMA(dataframe, timeperiod=9) + + # Cycle Indicator + # ------------------------------------ + # Hilbert Transform Indicator - SineWave + hilbert = ta.HT_SINE(dataframe) + dataframe['htsine'] = hilbert['sine'] + dataframe['htleadsine'] = hilbert['leadsine'] + + # Pattern Recognition - Bullish candlestick patterns + # ------------------------------------ + + # Hammer: values [0, 100] + dataframe['CDLHAMMER'] = ta.CDLHAMMER(dataframe) + # Inverted Hammer: values [0, 100] + dataframe['CDLINVERTEDHAMMER'] = ta.CDLINVERTEDHAMMER(dataframe) + # Dragonfly Doji: values [0, 100] + dataframe['CDLDRAGONFLYDOJI'] = ta.CDLDRAGONFLYDOJI(dataframe) + # Piercing Line: values [0, 100] + dataframe['CDLPIERCING'] = ta.CDLPIERCING(dataframe) # values [0, 100] + # Morningstar: values [0, 100] + dataframe['CDLMORNINGSTAR'] = ta.CDLMORNINGSTAR(dataframe) # values [0, 100] + # Three White Soldiers: values [0, 100] + dataframe['CDL3WHITESOLDIERS'] = ta.CDL3WHITESOLDIERS(dataframe) # values [0, 100] + + + # Pattern Recognition - Bearish candlestick patterns + # ------------------------------------ + + # Hanging Man: values [0, 100] + dataframe['CDLHANGINGMAN'] = ta.CDLHANGINGMAN(dataframe) + # Shooting Star: values [0, 100] + dataframe['CDLSHOOTINGSTAR'] = ta.CDLSHOOTINGSTAR(dataframe) + # Gravestone Doji: values [0, 100] + dataframe['CDLGRAVESTONEDOJI'] = ta.CDLGRAVESTONEDOJI(dataframe) + # Dark Cloud Cover: values [0, 100] + dataframe['CDLDARKCLOUDCOVER'] = ta.CDLDARKCLOUDCOVER(dataframe) + # Evening Doji Star: values [0, 100] + dataframe['CDLEVENINGDOJISTAR'] = ta.CDLEVENINGDOJISTAR(dataframe) + # Evening Star: values [0, 100] + dataframe['CDLEVENINGSTAR'] = ta.CDLEVENINGSTAR(dataframe) + + + # Pattern Recognition - Bullish/Bearish candlestick patterns + # ------------------------------------ + + # Three Line Strike: values [0, -100, 100] + dataframe['CDL3LINESTRIKE'] = ta.CDL3LINESTRIKE(dataframe) + # Spinning Top: values [0, -100, 100] + dataframe['CDLSPINNINGTOP'] = ta.CDLSPINNINGTOP(dataframe) # values [0, -100, 100] + # Engulfing: values [0, -100, 100] + dataframe['CDLENGULFING'] = ta.CDLENGULFING(dataframe) # values [0, -100, 100] + # Harami: values [0, -100, 100] + dataframe['CDLHARAMI'] = ta.CDLHARAMI(dataframe) # values [0, -100, 100] + # Three Outside Up/Down: values [0, -100, 100] + dataframe['CDL3OUTSIDE'] = ta.CDL3OUTSIDE(dataframe) # values [0, -100, 100] + # Three Inside Up/Down: values [0, -100, 100] + dataframe['CDL3INSIDE'] = ta.CDL3INSIDE(dataframe) # values [0, -100, 100] + + + # Chart type + # ------------------------------------ + + # Heikinashi stategy + heikinashi = qtpylib.heikinashi(dataframe) + dataframe['ha_open'] = heikinashi['open'] + dataframe['ha_close'] = heikinashi['close'] + dataframe['ha_high'] = heikinashi['high'] + dataframe['ha_low'] = heikinashi['low'] + + + return dataframe + + params = Select() + valm = random.randint(1,100) + print('MFI Value :' + str(valm) + ' XXX') + valfast = random.randint(1,100) + print('FASTD Value :' + str(valfast) + ' XXX') + valadx = random.randint(1,100) + print('ADX Value :' + str(valadx) + ' XXX') + valrsi = random.randint(1,100) + print('RSI Value :' + str(valrsi) + ' XXX') + def populate_buy_trend(self, dataframe: DataFrame) -> DataFrame: + + conditions = [] + # GUARDS AND TRENDS + if 'uptrend_long_ema' in str(self.params): + conditions.append(dataframe['ema50'] > dataframe['ema100']) + if 'macd_below_zero' in str(self.params): + conditions.append(dataframe['macd'] < 0) + if 'uptrend_short_ema' in str(self.params): + conditions.append(dataframe['ema5'] > dataframe['ema10']) + if 'mfi' in str(self.params): + + conditions.append(dataframe['mfi'] < self.valm) + if 'fastd' in str(self.params): + + conditions.append(dataframe['fastd'] < self.valfast) + if 'adx' in str(self.params): + + conditions.append(dataframe['adx'] > self.valadx) + if 'rsi' in str(self.params): + + conditions.append(dataframe['rsi'] < self.valrsi) + if 'over_sar' in str(self.params): + conditions.append(dataframe['close'] > dataframe['sar']) + if 'green_candle' in str(self.params): + conditions.append(dataframe['close'] > dataframe['open']) + if 'uptrend_sma' in str(self.params): + prevsma = dataframe['sma'].shift(1) + conditions.append(dataframe['sma'] > prevsma) + if 'closebb' in str(self.params): + conditions.append(dataframe['close'] < dataframe['bb_lowerband']) + if 'temabb' in str(self.params): + conditions.append(dataframe['tema'] < dataframe['bb_lowerband']) + if 'fastdt' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['fastd'], 10.0)) + if 'ao' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['ao'], 0.0)) + if 'ema3' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['ema3'], dataframe['ema10'])) + if 'macd' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['macd'], dataframe['macdsignal'])) + if 'closesar' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['close'], dataframe['sar'])) + if 'htsine' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['htleadsine'], dataframe['htsine'])) + if 'has' in str(self.params): + conditions.append((qtpylib.crossed_above(dataframe['ha_close'], dataframe['ha_open'])) & (dataframe['ha_low'] == dataframe['ha_open'])) + if 'plusdi' in str(self.params): + conditions.append(qtpylib.crossed_above(dataframe['plus_di'], dataframe['minus_di'])) + + dataframe.loc[ + reduce(lambda x, y: x & y, conditions), + 'buy'] = 1 + + return dataframe + + def populate_sell_trend(self, dataframe: DataFrame) -> DataFrame: + """ + Based on TA indicators, populates the sell signal for the given dataframe + :param dataframe: DataFrame + :return: DataFrame with buy column + """ + dataframe.loc[ + ( + ), + 'sell'] = 1 + return dataframe + From bf61c04bf9c2937f6792a2e3b0e7cddf18164349 Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 10:24:41 -0500 Subject: [PATCH 24/28] Delete backtesting.py --- user_data/random/backtesting.py | 294 -------------------------------- 1 file changed, 294 deletions(-) delete mode 100644 user_data/random/backtesting.py diff --git a/user_data/random/backtesting.py b/user_data/random/backtesting.py deleted file mode 100644 index 5697f7bbd..000000000 --- a/user_data/random/backtesting.py +++ /dev/null @@ -1,294 +0,0 @@ -# pragma pylint: disable=missing-docstring, W0212, too-many-arguments - -""" -This module contains the backtesting logic -""" -from argparse import Namespace -from typing import Dict, Tuple, Any, List, Optional - -import arrow -from pandas import DataFrame, Series -from tabulate import tabulate -import os -import freqtrade.optimize as optimize -from freqtrade import exchange -from freqtrade.analyze import Analyze -from freqtrade.arguments import Arguments -from freqtrade.configuration import Configuration -from freqtrade.exchange import Bittrex -from freqtrade.logger import Logger -from freqtrade.misc import file_dump_json -from freqtrade.persistence import Trade - - -class Backtesting(object): - """ - Backtesting class, this class contains all the logic to run a backtest - - To run a backtest: - backtesting = Backtesting(config) - backtesting.start() - """ - def __init__(self, config: Dict[str, Any]) -> None: - - # Init the logger - self.logging = Logger(name=__name__, level=config['loglevel']) - self.logger = self.logging.get_logger() - self.config = config - self.analyze = None - self.ticker_interval = None - self.tickerdata_to_dataframe = None - self.populate_buy_trend = None - self.populate_sell_trend = None - self._init() - - def _init(self) -> None: - """ - Init objects required for backtesting - :return: None - """ - self.analyze = Analyze(self.config) - self.ticker_interval = self.analyze.strategy.ticker_interval - self.tickerdata_to_dataframe = self.analyze.tickerdata_to_dataframe - self.populate_buy_trend = self.analyze.populate_buy_trend - self.populate_sell_trend = self.analyze.populate_sell_trend - exchange._API = Bittrex({'key': '', 'secret': ''}) - - @staticmethod - def get_timeframe(data: Dict[str, DataFrame]) -> Tuple[arrow.Arrow, arrow.Arrow]: - """ - Get the maximum timeframe for the given backtest data - :param data: dictionary with preprocessed backtesting data - :return: tuple containing min_date, max_date - """ - all_dates = Series([]) - for pair_data in data.values(): - all_dates = all_dates.append(pair_data['date']) - all_dates.sort_values(inplace=True) - return arrow.get(all_dates.iloc[0]), arrow.get(all_dates.iloc[-1]) - - def _generate_text_table(self, data: Dict[str, Dict], results: DataFrame) -> str: - """ - Generates and returns a text table for the given backtest data and the results dataframe - :return: pretty printed table with tabulate as str - """ - stake_currency = self.config.get('stake_currency') - - floatfmt = ('.8f', '.8f', '.8f', '.8f', '.1f') - tabular_data = [] - headers = ['total profit ' + stake_currency] - - # Append Total - tabular_data.append([ - 'TOTAL', - results.profit_BTC.sum(), - ]) - return tabulate(tabular_data, headers=headers, floatfmt=floatfmt) - - def _get_sell_trade_entry( - self, pair: str, buy_row: DataFrame, - partial_ticker: List, trade_count_lock: Dict, args: Dict) -> Optional[Tuple]: - - stake_amount = args['stake_amount'] - max_open_trades = args.get('max_open_trades', 0) - trade = Trade( - open_rate=buy_row.close, - open_date=buy_row.date, - stake_amount=stake_amount, - amount=stake_amount / buy_row.open, - fee=exchange.get_fee() - ) - - # calculate win/lose forwards from buy point - for sell_row in partial_ticker: - if max_open_trades > 0: - # Increase trade_count_lock for every iteration - trade_count_lock[sell_row.date] = trade_count_lock.get(sell_row.date, 0) + 1 - - buy_signal = sell_row.buy - if self.analyze.should_sell(trade, sell_row.close, sell_row.date, buy_signal, - sell_row.sell): - return \ - sell_row, \ - ( - pair, - trade.calc_profit_percent(rate=sell_row.close), - trade.calc_profit(rate=sell_row.close), - (sell_row.date - buy_row.date).seconds // 60 - ), \ - sell_row.date - return None - - def backtest(self, args: Dict) -> DataFrame: - """ - Implements backtesting functionality - - NOTE: This method is used by Hyperopt at each iteration. Please keep it optimized. - Of course try to not have ugly code. By some accessor are sometime slower than functions. - Avoid, logging on this method - - :param args: a dict containing: - stake_amount: btc amount to use for each trade - processed: a processed dictionary with format {pair, data} - max_open_trades: maximum number of concurrent trades (default: 0, disabled) - realistic: do we try to simulate realistic trades? (default: True) - sell_profit_only: sell if profit only - use_sell_signal: act on sell-signal - :return: DataFrame - """ - headers = ['date', 'buy', 'open', 'close', 'sell'] - processed = args['processed'] - max_open_trades = args.get('max_open_trades', 0) - realistic = args.get('realistic', False) - record = args.get('record', None) - records = [] - trades = [] - trade_count_lock = {} - for pair, pair_data in processed.items(): - pair_data['buy'], pair_data['sell'] = 0, 0 # cleanup from previous run - - ticker_data = self.populate_sell_trend(self.populate_buy_trend(pair_data))[headers] - ticker = [x for x in ticker_data.itertuples()] - - lock_pair_until = None - for index, row in enumerate(ticker): - if row.buy == 0 or row.sell == 1: - continue # skip rows where no buy signal or that would immediately sell off - - if realistic: - if lock_pair_until is not None and row.date <= lock_pair_until: - continue - if max_open_trades > 0: - # Check if max_open_trades has already been reached for the given date - if not trade_count_lock.get(row.date, 0) < max_open_trades: - continue - - trade_count_lock[row.date] = trade_count_lock.get(row.date, 0) + 1 - - ret = self._get_sell_trade_entry(pair, row, ticker[index + 1:], - trade_count_lock, args) - - if ret: - row2, trade_entry, next_date = ret - lock_pair_until = next_date - trades.append(trade_entry) - if record: - # Note, need to be json.dump friendly - # record a tuple of pair, current_profit_percent, - # entry-date, duration - records.append((pair, trade_entry[1], - row.date.strftime('%s'), - row2.date.strftime('%s'), - row.date, trade_entry[3])) - # For now export inside backtest(), maybe change so that backtest() - # returns a tuple like: (dataframe, records, logs, etc) - if record and record.find('trades') >= 0: - self.logger.info('Dumping backtest results') - file_dump_json('backtest-result.json', records) - labels = ['currency', 'profit_percent', 'profit_BTC', 'duration'] - return DataFrame.from_records(trades, columns=labels) - - def start(self) -> None: - """ - Run a backtesting end-to-end - :return: None - """ - data = {} - pairs = self.config['exchange']['pair_whitelist'] - self.logger.info('Using stake_currency: %s ...', self.config['stake_currency']) - self.logger.info('Using stake_amount: %s ...', self.config['stake_amount']) - - if self.config.get('live'): - self.logger.info('Downloading data for all pairs in whitelist ...') - for pair in pairs: - data[pair] = exchange.get_ticker_history(pair, self.ticker_interval) - else: - self.logger.info('Using local backtesting data (using whitelist in given config) ...') - - timerange = Arguments.parse_timerange(self.config.get('timerange')) - data = optimize.load_data( - self.config['datadir'], - pairs=pairs, - ticker_interval=self.ticker_interval, - refresh_pairs=self.config.get('refresh_pairs', False), - timerange=timerange - ) - - # Ignore max_open_trades in backtesting, except realistic flag was passed - if self.config.get('realistic_simulation', False): - max_open_trades = self.config['max_open_trades'] - else: - self.logger.info('Ignoring max_open_trades (realistic_simulation not set) ...') - max_open_trades = 0 - - preprocessed = self.tickerdata_to_dataframe(data) - - # Print timeframe - min_date, max_date = self.get_timeframe(preprocessed) - self.logger.info( - 'Measuring data from %s up to %s (%s days)..', - min_date.isoformat(), - max_date.isoformat(), - (max_date - min_date).days - ) - - # Execute backtest and print results - sell_profit_only = self.config.get('experimental', {}).get('sell_profit_only', False) - use_sell_signal = self.config.get('experimental', {}).get('use_sell_signal', False) - results = self.backtest( - { - 'stake_amount': self.config.get('stake_amount'), - 'processed': preprocessed, - 'max_open_trades': max_open_trades, - 'realistic': self.config.get('realistic_simulation', False), - 'sell_profit_only': sell_profit_only, - 'use_sell_signal': use_sell_signal, - 'record': self.config.get('export') - } - ) - - self.logging.set_format('%(message)s') - self.logger.info( - '\n==================================== ' - 'BACKTESTING REPORT' - ' ====================================\n' - '%s', - self._generate_text_table( - data, - results - ) - ) - -def setup_configuration(args: Namespace) -> Dict[str, Any]: - """ - Prepare the configuration for the backtesting - :param args: Cli args from Arguments() - :return: Configuration - """ - configuration = Configuration(args) - config = configuration.get_config() - - # Ensure we do not use Exchange credentials - config['exchange']['key'] = '' - config['exchange']['secret'] = '' - - return config - - -def start(args: Namespace) -> None: - """ - Start Backtesting script - :param args: Cli args from Arguments() - :return: None - """ - - # Initialize logger - logger = Logger(name=__name__).get_logger() - logger.info('Starting freqtrade in Backtesting mode') - - # Initialize configuration - config = setup_configuration(args) - - # Initialize backtesting object - backtesting = Backtesting(config) - backtesting.start() From c9ed61cb802aa1cac65c72a1acea5234a9ac0845 Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 10:24:59 -0500 Subject: [PATCH 25/28] Added sleep 5 sys.exit() --- user_data/random/backtesting.py | 296 ++++++++++++++++++++++++++++++++ 1 file changed, 296 insertions(+) create mode 100644 user_data/random/backtesting.py diff --git a/user_data/random/backtesting.py b/user_data/random/backtesting.py new file mode 100644 index 000000000..c50f1cb2d --- /dev/null +++ b/user_data/random/backtesting.py @@ -0,0 +1,296 @@ +# pragma pylint: disable=missing-docstring, W0212, too-many-arguments + +""" +This module contains the backtesting logic +""" +from argparse import Namespace +from typing import Dict, Tuple, Any, List, Optional + +import arrow +from pandas import DataFrame, Series +from tabulate import tabulate +import freqtrade.optimize as optimize +from freqtrade import exchange +from freqtrade.analyze import Analyze +from freqtrade.arguments import Arguments +from freqtrade.configuration import Configuration +from freqtrade.exchange import Bittrex +from freqtrade.logger import Logger +from freqtrade.misc import file_dump_json +from freqtrade.persistence import Trade +import sys + +class Backtesting(object): + """ + Backtesting class, this class contains all the logic to run a backtest + + To run a backtest: + backtesting = Backtesting(config) + backtesting.start() + """ + def __init__(self, config: Dict[str, Any]) -> None: + + # Init the logger + self.logging = Logger(name=__name__, level=config['loglevel']) + self.logger = self.logging.get_logger() + self.config = config + self.analyze = None + self.ticker_interval = None + self.tickerdata_to_dataframe = None + self.populate_buy_trend = None + self.populate_sell_trend = None + self._init() + + def _init(self) -> None: + """ + Init objects required for backtesting + :return: None + """ + self.analyze = Analyze(self.config) + self.ticker_interval = self.analyze.strategy.ticker_interval + self.tickerdata_to_dataframe = self.analyze.tickerdata_to_dataframe + self.populate_buy_trend = self.analyze.populate_buy_trend + self.populate_sell_trend = self.analyze.populate_sell_trend + exchange._API = Bittrex({'key': '', 'secret': ''}) + + @staticmethod + def get_timeframe(data: Dict[str, DataFrame]) -> Tuple[arrow.Arrow, arrow.Arrow]: + """ + Get the maximum timeframe for the given backtest data + :param data: dictionary with preprocessed backtesting data + :return: tuple containing min_date, max_date + """ + all_dates = Series([]) + for pair_data in data.values(): + all_dates = all_dates.append(pair_data['date']) + all_dates.sort_values(inplace=True) + return arrow.get(all_dates.iloc[0]), arrow.get(all_dates.iloc[-1]) + + def _generate_text_table(self, data: Dict[str, Dict], results: DataFrame) -> str: + """ + Generates and returns a text table for the given backtest data and the results dataframe + :return: pretty printed table with tabulate as str + """ + stake_currency = self.config.get('stake_currency') + + floatfmt = ('.8f', '.8f', '.8f', '.8f', '.1f') + tabular_data = [] + headers = ['total profit ' + stake_currency] + + # Append Total + tabular_data.append([ + 'TOTAL', + results.profit_BTC.sum(), + ]) + return tabulate(tabular_data, headers=headers, floatfmt=floatfmt) + + def _get_sell_trade_entry( + self, pair: str, buy_row: DataFrame, + partial_ticker: List, trade_count_lock: Dict, args: Dict) -> Optional[Tuple]: + + stake_amount = args['stake_amount'] + max_open_trades = args.get('max_open_trades', 0) + trade = Trade( + open_rate=buy_row.close, + open_date=buy_row.date, + stake_amount=stake_amount, + amount=stake_amount / buy_row.open, + fee=exchange.get_fee() + ) + + # calculate win/lose forwards from buy point + for sell_row in partial_ticker: + if max_open_trades > 0: + # Increase trade_count_lock for every iteration + trade_count_lock[sell_row.date] = trade_count_lock.get(sell_row.date, 0) + 1 + + buy_signal = sell_row.buy + if self.analyze.should_sell(trade, sell_row.close, sell_row.date, buy_signal, + sell_row.sell): + return \ + sell_row, \ + ( + pair, + trade.calc_profit_percent(rate=sell_row.close), + trade.calc_profit(rate=sell_row.close), + (sell_row.date - buy_row.date).seconds // 60 + ), \ + sell_row.date + return None + + def backtest(self, args: Dict) -> DataFrame: + """ + Implements backtesting functionality + + NOTE: This method is used by Hyperopt at each iteration. Please keep it optimized. + Of course try to not have ugly code. By some accessor are sometime slower than functions. + Avoid, logging on this method + + :param args: a dict containing: + stake_amount: btc amount to use for each trade + processed: a processed dictionary with format {pair, data} + max_open_trades: maximum number of concurrent trades (default: 0, disabled) + realistic: do we try to simulate realistic trades? (default: True) + sell_profit_only: sell if profit only + use_sell_signal: act on sell-signal + :return: DataFrame + """ + headers = ['date', 'buy', 'open', 'close', 'sell'] + processed = args['processed'] + max_open_trades = args.get('max_open_trades', 0) + realistic = args.get('realistic', False) + record = args.get('record', None) + records = [] + trades = [] + trade_count_lock = {} + for pair, pair_data in processed.items(): + pair_data['buy'], pair_data['sell'] = 0, 0 # cleanup from previous run + + ticker_data = self.populate_sell_trend(self.populate_buy_trend(pair_data))[headers] + ticker = [x for x in ticker_data.itertuples()] + + lock_pair_until = None + for index, row in enumerate(ticker): + if row.buy == 0 or row.sell == 1: + continue # skip rows where no buy signal or that would immediately sell off + + if realistic: + if lock_pair_until is not None and row.date <= lock_pair_until: + continue + if max_open_trades > 0: + # Check if max_open_trades has already been reached for the given date + if not trade_count_lock.get(row.date, 0) < max_open_trades: + continue + + trade_count_lock[row.date] = trade_count_lock.get(row.date, 0) + 1 + + ret = self._get_sell_trade_entry(pair, row, ticker[index + 1:], + trade_count_lock, args) + + if ret: + row2, trade_entry, next_date = ret + lock_pair_until = next_date + trades.append(trade_entry) + if record: + # Note, need to be json.dump friendly + # record a tuple of pair, current_profit_percent, + # entry-date, duration + records.append((pair, trade_entry[1], + row.date.strftime('%s'), + row2.date.strftime('%s'), + row.date, trade_entry[3])) + # For now export inside backtest(), maybe change so that backtest() + # returns a tuple like: (dataframe, records, logs, etc) + if record and record.find('trades') >= 0: + self.logger.info('Dumping backtest results') + file_dump_json('backtest-result.json', records) + labels = ['currency', 'profit_percent', 'profit_BTC', 'duration'] + return DataFrame.from_records(trades, columns=labels) + + def start(self) -> None: + """ + Run a backtesting end-to-end + :return: None + """ + data = {} + pairs = self.config['exchange']['pair_whitelist'] + self.logger.info('Using stake_currency: %s ...', self.config['stake_currency']) + self.logger.info('Using stake_amount: %s ...', self.config['stake_amount']) + + if self.config.get('live'): + self.logger.info('Downloading data for all pairs in whitelist ...') + for pair in pairs: + data[pair] = exchange.get_ticker_history(pair, self.ticker_interval) + else: + self.logger.info('Using local backtesting data (using whitelist in given config) ...') + + timerange = Arguments.parse_timerange(self.config.get('timerange')) + data = optimize.load_data( + self.config['datadir'], + pairs=pairs, + ticker_interval=self.ticker_interval, + refresh_pairs=self.config.get('refresh_pairs', False), + timerange=timerange + ) + + # Ignore max_open_trades in backtesting, except realistic flag was passed + if self.config.get('realistic_simulation', False): + max_open_trades = self.config['max_open_trades'] + else: + self.logger.info('Ignoring max_open_trades (realistic_simulation not set) ...') + max_open_trades = 0 + + preprocessed = self.tickerdata_to_dataframe(data) + + # Print timeframe + min_date, max_date = self.get_timeframe(preprocessed) + self.logger.info( + 'Measuring data from %s up to %s (%s days)..', + min_date.isoformat(), + max_date.isoformat(), + (max_date - min_date).days + ) + + # Execute backtest and print results + sell_profit_only = self.config.get('experimental', {}).get('sell_profit_only', False) + use_sell_signal = self.config.get('experimental', {}).get('use_sell_signal', False) + results = self.backtest( + { + 'stake_amount': self.config.get('stake_amount'), + 'processed': preprocessed, + 'max_open_trades': max_open_trades, + 'realistic': self.config.get('realistic_simulation', False), + 'sell_profit_only': sell_profit_only, + 'use_sell_signal': use_sell_signal, + 'record': self.config.get('export') + } + ) + + self.logging.set_format('%(message)s') + self.logger.info( + '\n==================================== ' + 'BACKTESTING REPORT' + ' ====================================\n' + '%s', + self._generate_text_table( + data, + results + ) + ) + time.sleep(5) + os.close(1) + sys.exit() + +def setup_configuration(args: Namespace) -> Dict[str, Any]: + """ + Prepare the configuration for the backtesting + :param args: Cli args from Arguments() + :return: Configuration + """ + configuration = Configuration(args) + config = configuration.get_config() + + # Ensure we do not use Exchange credentials + config['exchange']['key'] = '' + config['exchange']['secret'] = '' + + return config + + +def start(args: Namespace) -> None: + """ + Start Backtesting script + :param args: Cli args from Arguments() + :return: None + """ + + # Initialize logger + logger = Logger(name=__name__).get_logger() + logger.info('Starting freqtrade in Backtesting mode') + + # Initialize configuration + config = setup_configuration(args) + + # Initialize backtesting object + backtesting = Backtesting(config) + backtesting.start() From 2ea1fd141c7c38211fabbdd755760f3de6e5cd5d Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 10:33:36 -0500 Subject: [PATCH 26/28] Update backtesting.py --- user_data/random/backtesting.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/user_data/random/backtesting.py b/user_data/random/backtesting.py index c50f1cb2d..4354abeb2 100644 --- a/user_data/random/backtesting.py +++ b/user_data/random/backtesting.py @@ -257,9 +257,7 @@ class Backtesting(object): results ) ) - time.sleep(5) - os.close(1) - sys.exit() + sys.exit(1) def setup_configuration(args: Namespace) -> Dict[str, Any]: """ From 4e8ed34d032047be28df311dd8b954ea4112d1fa Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 11:11:21 -0500 Subject: [PATCH 27/28] Update random.py --- user_data/random/random.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/user_data/random/random.py b/user_data/random/random.py index 33d9d88f4..7ff0be866 100644 --- a/user_data/random/random.py +++ b/user_data/random/random.py @@ -19,6 +19,7 @@ WORK_DIR = os.path.join( # Spawn workers command = [ 'python3.6', + '-u', WORK_DIR, 'backtesting', ] @@ -31,7 +32,7 @@ while True: while procs < 32: try: procs + 1 - proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1, universal_newlines=True) data = proc.communicate() string = str(data) params = re.search(r'~~~~(.*)~~~~', string).group(1) From 861fa3d9ea4129a5f7b10304f03fb41cee1910bc Mon Sep 17 00:00:00 2001 From: MoonGem <34537029+MoonGem@users.noreply.github.com> Date: Sun, 25 Mar 2018 11:14:16 -0500 Subject: [PATCH 28/28] Update backtesting.py --- user_data/random/backtesting.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/user_data/random/backtesting.py b/user_data/random/backtesting.py index 4354abeb2..2ba532610 100644 --- a/user_data/random/backtesting.py +++ b/user_data/random/backtesting.py @@ -19,7 +19,8 @@ from freqtrade.logger import Logger from freqtrade.misc import file_dump_json from freqtrade.persistence import Trade import sys - +import os +import time class Backtesting(object): """ Backtesting class, this class contains all the logic to run a backtest @@ -257,8 +258,8 @@ class Backtesting(object): results ) ) - sys.exit(1) - + time.sleep(2) + os.close(1) def setup_configuration(args: Namespace) -> Dict[str, Any]: """ Prepare the configuration for the backtesting