from freqtrade.strategy import merge_informative_pair, CategoricalParameter, DecimalParameter, IntParameter, \
    stoploss_from_open

from BB_RPB_TSL_Hippocritical_EmptyHull import BB_RPB_TSL_Hippocritical_EmptyHull

class BB_RPB_TSL_Hippocritical_7(BB_RPB_TSL_Hippocritical_EmptyHull):
    ###START SHORT-PARAMETERS###
    is_short_activate_pumpbreak = True
    if is_short_activate_pumpbreak:
        buy_short_pumpbreak_rmi = IntParameter(50, 70, default=65, optimize=True)
        buy_short_pumpbreak_cci = IntParameter(90, 135, default=133, optimize=True)
        buy_short_pumpbreak_srsi_fk = IntParameter(50, 80, default=75, optimize=True)
        buy_short_pumpbreak_cci_length = IntParameter(25, 45, default=25, optimize=True)
        buy_short_pumpbreak_rmi_length = IntParameter(8, 20, default=8, optimize=True)
        buy_short_pumpbreak_bb_width = DecimalParameter(0.05, 0.2, default=0.15, optimize=True)
        buy_short_pumpbreak_bb_delta = DecimalParameter(0.025, 0.08, default=0.04, optimize=True)
        buy_short_pumpbreak_bb_factor = DecimalParameter(1.001, 1.010, default=1.005, optimize=True)
        buy_short_pumpbreak_closedelta = DecimalParameter(12.0, 18.0, default=15.0, optimize=True)
    is_short_activate_localpump = True
    if is_short_activate_localpump:
        buy_short_localpump_ema_diff = DecimalParameter(0.012, 0.027, default=0.015, optimize=True)
        buy_short_localpump_bb_factor = DecimalParameter(1.001, 1.030, default=1.003, optimize=True)
        buy_short_localpump_closedelta = DecimalParameter(0.0, 18.0, default=5.0, optimize=True)
    is_short_activate_ewo = True
    if is_short_activate_ewo:
        buy_short_ewo_ema8Factor = DecimalParameter(1.01, 1.1, default=1.058, optimize=True)
        buy_short_ewo_ema16Factor = DecimalParameter(0.8, 1.05, default=0.916, optimize=True)
        buy_short_ewo_rsi_fast = IntParameter(40, 80, default=65, optimize=True)
        buy_short_ewo_rsi = IntParameter(60, 90, default=65, optimize=True)
        buy_short_ewo_ewo = DecimalParameter(-6, 5, default=-5.585, optimize=True)
    is_short_activate_ewo2 = False
    if is_short_activate_ewo2:
        buy_short_ewo2_ema_low = DecimalParameter(0.96, 0.978, default=0.96, optimize=True)
        buy_short_ewo2_ema_high = DecimalParameter(1.05, 1.2, default=1.09, optimize=True)
        buy_short_ewo2_rsi_fast = IntParameter(35, 50, default=45, optimize=True)
        buy_short_ewo2_rsi = IntParameter(15, 30, default=35, optimize=True)
        buy_short_ewo2_ewo_low = DecimalParameter(2, 12, default=3.553, optimize=True)  # added
    is_short_activate_cofi = True
    if is_short_activate_cofi:
        buy_short_cofi_ema = DecimalParameter(1.02, 1.04, default=1.03, optimize=True)
        buy_short_cofi_fastk = IntParameter(70, 80, default=80, optimize=True)
        buy_short_cofi_fastd = IntParameter(70, 80, default=80, optimize=True)
        buy_short_cofi_adx = IntParameter(70, 80, default=70, optimize=True)
        buy_short_cofi_ewo_low = DecimalParameter(2, 12, default=3.553, optimize=True)  # added
    is_short_activate_nfi32 = False
    if is_short_activate_nfi32:
        buy_short_nfi32_rsi_fast = IntParameter(50, 80, default=54, optimize=True)
        buy_short_nfi32_rsi = IntParameter(70, 90, default=81, optimize=True)
        buy_short_nfi32_sma15 = DecimalParameter(1.04, 1.6, default=1.058, optimize=True)
        buy_short_nfi32_cti = DecimalParameter(0.8, 0.9, default=0.86, optimize=True)
    is_short_activate_nfi33 = True
    if is_short_activate_nfi33:
        buy_short_nfi33_ema_13 = DecimalParameter(1.01, 1.03, default=1.022, optimize=True)
        buy_short_nfi33_ewo_low = IntParameter(3, 15, default=8, optimize=True)
        buy_short_nfi33_cti = DecimalParameter(0.75, 0.95, default=0.88, optimize=True)
        buy_short_nfi33_rsi = IntParameter(60, 80, default=68, optimize=True)
        buy_short_nfi33_r_14 = IntParameter(-10, 0, default=-2, optimize=True)
        buy_short_nfi33_volume_mean = DecimalParameter(0.5, 3, default=1, optimize=True)
    # END SHORT-PARAMETERS###
    ###START LONG-PARAMETERS###
    is_long_activate_dipbreak = True
    if is_long_activate_dipbreak:
        buy_long_dipbreak_rmi = IntParameter(30, 50, default=35, optimize=True)
        buy_long_dipbreak_cci = IntParameter(-135, -90, default=-133, optimize=True)
        buy_long_dipbreak_srsi_fk = IntParameter(30, 50, default=25, optimize=True)
        buy_long_dipbreak_cci_length = IntParameter(25, 45, default=25, optimize=True)
        buy_long_dipbreak_rmi_length = IntParameter(8, 20, default=8, optimize=True)
        buy_long_dipbreak_bb_width = DecimalParameter(0.05, 0.2, default=0.15, optimize=True)
        buy_long_dipbreak_bb_delta = DecimalParameter(0.025, 0.08, default=0.04, optimize=True)
        buy_long_dipbreak_bb_factor = DecimalParameter(0.990, 0.999, default=0.995, optimize=True)
        buy_long_dipbreak_closedelta = DecimalParameter(12.0, 18.0, default=15.0, optimize=True)
    is_long_activate_localdip = True
    if is_long_activate_localdip:
        buy_long_localdip_ema_diff = DecimalParameter(0.022, 0.027, default=0.025, optimize=True)
        buy_long_localdip_bb_factor = DecimalParameter(0.990, 0.999, default=0.995, optimize=True)
        buy_long_localdip_closedelta = DecimalParameter(12.0, 18.0, default=15.0, optimize=True)
    is_long_activate_ewo = True
    if is_long_activate_ewo:
        buy_long_ewo_ema8Factor = DecimalParameter(0.9, 0.99, default=0.942, optimize=True)
        buy_long_ewo_ema16Factor = DecimalParameter(0.95, 1.2, default=1.084, optimize=True)
        buy_long_ewo_rsi_fast = IntParameter(35, 50, default=45, optimize=True)
        buy_long_ewo_rsi = IntParameter(15, 30, default=35, optimize=True)
        buy_long_ewo_ewo = DecimalParameter(-6.0, 5, default=-5.585, optimize=True)
    is_long_activate_ewo2 = False
    if is_long_activate_ewo2:
        buy_long_ewo2_ema_low = DecimalParameter(0.96, 0.978, default=0.96, optimize=True)
        buy_long_ewo2_ema_high = DecimalParameter(1.05, 1.2, default=1.09, optimize=True)
        buy_long_ewo2_rsi_fast = IntParameter(35, 50, default=45, optimize=True)
        buy_long_ewo2_rsi = IntParameter(15, 30, default=35, optimize=True)
        buy_long_ewo2_ewo_high = DecimalParameter(-6.0, 5, default=-5.585, optimize=True)
    is_long_activate_cofi = False
    if is_long_activate_cofi:
        buy_long_cofi_ema = DecimalParameter(0.96, 0.98, default=0.97, optimize=True)
        buy_long_cofi_fastk = IntParameter(20, 30, default=20, optimize=True)
        buy_long_cofi_fastd = IntParameter(20, 30, default=20, optimize=True)
        buy_long_cofi_adx = IntParameter(20, 30, default=30, optimize=True)
        buy_long_cofi_ewo_high = DecimalParameter(2, 12, default=3.553, optimize=True)
    is_long_activate_nfi32 = False
    if is_long_activate_nfi32:
        buy_long_nfi32_rsi_fast = IntParameter(30, 50, default=46, optimize=True)
        buy_long_nfi32_rsi = IntParameter(10, 30, default=19, optimize=True)
        buy_long_nfi32_sma15 = DecimalParameter(0.7, 1.1, default=0.942, optimize=True)
        buy_long_nfi32_cti = DecimalParameter(-0.9, -0.8, default=-0.86, optimize=True)
    is_long_activate_nfi33 = True
    if is_long_activate_nfi33:
        buy_long_nfi33_ema_13 = DecimalParameter(0.95, 1.00, default=0.978, optimize=True)
        buy_long_nfi33_ewo_high = IntParameter(5, 10, default=8, optimize=True)
        buy_long_nfi33_cti = DecimalParameter(-0.9, -0.8, default=-0.88, optimize=True)
        buy_long_nfi33_rsi = IntParameter(20, 40, default=32, optimize=True)
        buy_long_nfi33_r_14 = IntParameter(-100, -90, default=-98, optimize=True)
        buy_long_nfi33_volume_mean = DecimalParameter(2, 3, default=2.5, optimize=True)
    ###END LONG-PARAMETERS###

    ## Sell params
    sell_long_ema = IntParameter(5, 80, default=24, space='sell', optimize=True)
    sell_short_mult = DecimalParameter(0.95, 1.100, default=1.009, space='sell', optimize=True)
    sell_short_mult2 = DecimalParameter(0.95, 1.200, default=1.003, space='sell', optimize=True)
    sell_short_smaFactor = DecimalParameter(1.0, 1.020, default=1.005, space='sell', optimize=True)
    sell_short_rsi = IntParameter(30, 100, default=50, space='sell', optimize=True)

    sell_short_ema = IntParameter(5, 80, default=24, space='sell', optimize=True)
    sell_long_mult = DecimalParameter(0.99, 1.5, default=0.997, space='sell', optimize=True)
    sell_long_mult2 = DecimalParameter(0.98, 1.2, default=0.991, space='sell', optimize=True)
    sell_long_smaFactor = DecimalParameter(0.95, 1.2, default=1.05, space='sell', optimize=True)
    sell_long_rsi = IntParameter(0, 70, default=50, space='sell', optimize=True)

    ## Trailing params

    # hard stoploss profit
    sell_long_pHSL = DecimalParameter(-0.100, -0.040, default=-0.05, decimals=3, space='sell', load=True)
    # profit threshold 1, trigger point, SL_1 is used
    sell_long_pPF_1 = DecimalParameter(0.008, 0.010, default=0.016, decimals=3, space='sell', load=True)
    sell_long_pSL_1_relative = DecimalParameter(0.008, 0.010, default=0.011, decimals=3, space='sell', load=True)
    # profit threshold 2, SL_2 is used
    sell_long_pPF_2 = DecimalParameter(0.040, 0.100, default=0.080, decimals=3, space='sell', load=True)
    sell_long_pSL_2_relative = DecimalParameter(0.020, 0.070, default=0.040, decimals=3, space='sell', load=True)

    # hard stoploss profit
    sell_short_pHSL = DecimalParameter(-0.100, -0.040, default=-0.05, decimals=3, space='sell', optimize=False)
    # profit threshold 1, trigger point, SL_1 is used
    sell_short_pPF_1 = DecimalParameter(0.02, 0.04, default=0.03, decimals=3, space='sell', optimize=True)
    sell_short_pSL_1_relative = DecimalParameter(0.005, 0.010, default=0.008, decimals=3, space='sell', optimize=True)

    # profit threshold 2, SL_2 is used
    sell_short_pPF_2 = DecimalParameter(0.040, 0.100, default=0.080, decimals=3, space='sell', optimize=True)
    sell_short_pSL_2_relative = DecimalParameter(0.005, 0.01, default=0.040, decimals=3, space='sell', optimize=True)

##########################################################
    # dipbreak long:
    # 204/500:    132 trades. 88/0/44 Wins/Draws/Losses. Avg profit   3.57%. Median profit   3.65%. Total profit 589.08675056 USDT (   5.89%). Avg duration 1:29:00 min. Objective: -213.39006
    # 204/500 |      132 |     88    0   44 |        3.57% |       589.087     (5.89%) |        1:29:00 |  -213.39006 |        43.750     (0.43%)
    # pumpbreak short:
    # 344/500:    249 trades. 195/0/54 Wins/Draws/Losses. Avg profit   2.45%. Median profit   1.72%. Total profit 761.44255321 USDT (   7.61%). Avg duration 1:35:00 min. Objective: -92.09781
    # 344/500 |      249 |    195    0   54 |        2.45% |       761.443     (7.61%) |        1:35:00 |   -92.09781 |        16.544     (0.16%)
    # localdip long:
    # 115/500:    242 trades. 143/0/99 Wins/Draws/Losses. Avg profit   1.84%. Median profit   2.52%. Total profit 553.56604953 USDT (   5.54%). Avg duration 0:19:00 min. Objective: -27.24042
    # 115/500 |      242 |    143    0   99 |        1.84% |       553.566     (5.54%) |        0:19:00 |   -27.2404  |        72.219     (0.72%)
    # localpump short:
    # 13/500:    105 trades. 72/0/33 Wins/Draws/Losses. Avg profit   1.53%. Median profit   1.94%. Total profit 200.58758750 USDT (   2.01%). Avg duration 1:27:00 min. Objective: -8.80759
    # 13/500 |      105 |     72    0   33 |        1.53% |       200.588     (2.01%) |        1:27:00 |    -8.80759 |        79.805     (0.79%)
    # ewo short:
    # 268/500:    272 trades. 209/0/63 Wins/Draws/Losses. Avg profit   2.22%. Median profit   1.91%. Total profit 756.13311400 USDT (   7.56%). Avg duration 2:08:00 min. Objective: -94.56216
    # 268/500 |      272 |    209    0   63 |        2.22% |       756.133     (7.56%) |        2:08:00 |   -94.56216 |        25.082     (0.24%)
    # ewo long:
    # 3/500:    450 trades. 365/0/85 Wins/Draws/Losses. Avg profit   3.10%. Median profit   3.40%. Total profit 1743.16494063 USDT (  17.43%). Avg duration 5:15:00 min. Objective: -230.47933
    # 3/500 |      450 |    365    0   85 |        3.10% |      1743.165    (17.43%) |        5:15:00 |  -230.47933 |        41.133     (0.35%)
    # cofi short:
    # 440/500:     88 trades. 63/0/25 Wins/Draws/Losses. Avg profit   1.46%. Median profit   2.30%. Total profit 161.04111420 USDT (   1.61%). Avg duration 4:11:00 min. Objective: -13.03760
    # 440/500 |       88 |     63    0   25 |        1.46% |       161.041     (1.61%) |        4:11:00 |   -13.03760 |        26.674     (0.26%)
    # cofi long:
    # ToDo!
    #

    # nfi32 short:
    # ToDo!
    # nfi32 long:
    # ToDo!

    # nfi33 short:
    # 41/500:    348 trades. 248/0/100 Wins/Draws/Losses. Avg profit   1.39%. Median profit   1.52%. Total profit 602.68042235 USDT (   6.03%). Avg duration 2:18:00 min. Objective: -20.46272
    # 41/500 |      348 |    248    0  100 |        1.39% |       602.680     (6.03%) |        2:18:00 |   -20.46272 |        35.188     (0.34%)
    # nfi33 long:
    # 38/500:     76 trades. 51/0/25 Wins/Draws/Losses. Avg profit   1.57%. Median profit   0.88%. Total profit 149.61703632 USDT (   1.50%). Avg duration 4:02:00 min. Objective: -12.89221
    # 38/500 |       76 |     51    0   25 |        1.57% |       149.617     (1.50%) |         4:02:00 |   -12.89221 |        17.227     (0.17%)

    # Buy hyperspace params:
    buy_params = {
        "buy_long_dipbreak_bb_delta": 0.027,
        "buy_long_dipbreak_bb_factor": 0.996,
        "buy_long_dipbreak_bb_width": 0.084,
        "buy_long_dipbreak_cci": -128,
        "buy_long_dipbreak_cci_length": 27,
        "buy_long_dipbreak_closedelta": 15.573,
        "buy_long_dipbreak_rmi": 30,
        "buy_long_dipbreak_rmi_length": 13,
        "buy_long_dipbreak_srsi_fk": 37,

        "buy_short_pumpbreak_bb_delta": 0.074,
        "buy_short_pumpbreak_bb_factor": 1.0,
        "buy_short_pumpbreak_bb_width": 0.062,
        "buy_short_pumpbreak_cci": 108,
        "buy_short_pumpbreak_cci_length": 25,
        "buy_short_pumpbreak_closedelta": 12.081,
        "buy_short_pumpbreak_rmi": 69,
        "buy_short_pumpbreak_rmi_length": 18,
        "buy_short_pumpbreak_srsi_fk": 67,

        "buy_long_localdip_bb_factor": 0.993,
        "buy_long_localdip_closedelta": 15.486,
        "buy_long_localdip_ema_diff": 0.027,

        "buy_short_localpump_bb_factor": 1.027,
        "buy_short_localpump_closedelta": 2.404,
        "buy_short_localpump_ema_diff": 0.014,

        "buy_short_ewo_ema16Factor": 0.998,
        "buy_short_ewo_ema8Factor": 1.043,
        "buy_short_ewo_ewo": 0.228,
        "buy_short_ewo_rsi": 79,
        "buy_short_ewo_rsi_fast": 41,

        "buy_long_ewo_ema_high": 1.09,
        "buy_long_ewo_ema_low": 0.965,
        "buy_long_ewo_ewohigh": -0.144,
        "buy_long_ewo_rsi": 22,
        "buy_long_ewo_rsi_fast": 47,

        "buy_short_cofi_adx": 79,
        "buy_short_cofi_ema": 1.02,
        "buy_short_cofi_ewo_low": 2.111,
        "buy_short_cofi_fastd": 70,
        "buy_short_cofi_fastk": 78,

        "buy_long_cofi_adx": 25,
        "buy_long_cofi_ema": 0.975,
        "buy_long_cofi_ewo_high": 2.079,
        "buy_long_cofi_fastd": 20,
        "buy_long_cofi_fastk": 30,

        "buy_short_nfi33_cti": 0.032,
        "buy_short_nfi33_ema_13": 1.037,
        "buy_short_nfi33_ewo_low": 1,
        "buy_short_nfi33_r_14": -21,
        "buy_short_nfi33_rsi": 52,
        "buy_short_nfi33_volume_mean": 0.048,

        "buy_long_nfi33_cti": 0.868,
        "buy_long_nfi33_ema_13": 0.983,
        "buy_long_nfi33_ewo_high": 5,
        "buy_long_nfi33_r_14": -98,
        "buy_long_nfi33_rsi": 36,
        "buy_long_nfi33_volume_mean": 2.192,
    }

    # Sell hyperspace params:
    sell_params = {
        "sell_short_ema": 45,
        "sell_short_mult": 1.008,
        "sell_short_mult2": 0.963,
        "sell_short_pPF_1": 0.028,
        "sell_short_pPF_2": 0.063,
        "sell_short_pSL1_relative": 0.005,
        "sell_short_pSL2_relative": 0.01,
        "sell_short_rsi": 77,
        "sell_short_smaFactor": 1.02,
        "sell_short_pHSL": -0.05,  # value loaded from strategy

        "sell_long_pPF_1": 0.032,  # value loaded from strategy
        "sell_long_pPF_2": 0.06,  # value loaded from strategy
        "sell_long_pSL1_relative": 0.023,  # value loaded from strategy
        "sell_long_pSL2_relative": 0.01,  # value loaded from strategy
        "sell_long_ema": 24,  # value loaded from strategy
        "sell_long_mult": 0.991,  # value loaded from strategy
        "sell_long_mult2": 1.153,  # value loaded from strategy
        "sell_long_rsi": 33,  # value loaded from strategy
        "sell_long_smaFactor": 1.041,  # value loaded from strategy
        "sell_long_pHSL": -0.05,  # value loaded from strategy
    }
