def execute_buy_and_hold_strategy(df, commission, data_name, start_date,
                                  end_date):
    """
    Execute buy and hold strategy on data history contained in df
    :param df: dataframe with historical data
    :param commision: commission to be paid on each operation
    :param data_name: quote data name
    :param start_date: start date of simulation
    :param end_date: end date of simulation
    :return:
        - BH_Cerebro - execution engine
        - BH_Strategy - buy and hold strategy instance
    """

    print_execution_name("Estrategia: comprar y mantener")

    strategy_name = 'comprar_y_mantener'

    info = {
        'Mercado': data_name,
        'Estrategia': strategy_name,
        'Fecha inicial': start_date,
        'Fecha final': end_date
    }

    df = df[start_date:end_date]

    BH_Strategy = BuyAndHoldStrategy
    BH_Cerebro = execute_strategy(BH_Strategy, df, commission, info)

    return BH_Cerebro, BH_Strategy
Esempio n. 2
0
def execute_moving_average_rsi_strategy(df, commission, data_name, start_date, end_date):
    """
    Execute classic strategy on data history contained in df
    :param df: dataframe with historical data
    :param commision: commission to be paid on each operation
    :param data_name: quote data name
    :param start_date: start date of simulation
    :param end_date: end date of simulation
    :return:
        - Classic_Cerebro - execution engine
        - Classic_Strategy - classic strategy instance
    """

    print_execution_name("Estrategia: clásica")

    strategy_name = 'estrategia_clasica'

    info = {
        'Mercado': data_name,
        'Estrategia': strategy_name,
        'Fecha inicial': start_date,
        'Fecha final': end_date
    }

    df = df[start_date:end_date]

    Classic_Strategy =  MovingAverageRsiStrategy
    Classic_Cerebro = execute_strategy(Classic_Strategy, df, commission, info)

    return Classic_Cerebro, Classic_Strategy
Esempio n. 3
0
def execute_moving_averages_cross_strategy(df,
                                           commission,
                                           data_name,
                                           start_date,
                                           end_date,
                                           optimize=False,
                                           **kwargs):
    """
    Execute moving averages cross strategy on data history contained in df
    :param df: dataframe with historical data
    :param commision: commission to be paid on each operation
    :param data_name: quote data name
    :param start_date: start date of simulation
    :param end_date: end date of simulation
    :param optimize: if True then optimize strategy
    :return:
        - MAC_Cerebro - execution engine
        - MAC_Strategy - moving averages cross strategy instance
    """

    print_execution_name("Estrategia: cruce de medias móviles")

    strategy_name = 'estrategia_cruce_medias_moviles'

    info = {
        'Mercado': data_name,
        'Estrategia': strategy_name,
        'Fecha inicial': start_date,
        'Fecha final': end_date
    }

    if optimize:
        print('Optimizando (esto puede tardar)...')

        # Range of values to optimize
        params = {'ma_short': range(5, 18), 'ma_long': range(20, 100, 2)}

        # Get best params in past period
        kwargs = optimize_strategy(df, commission, MovingAveragesCrossStrategy,
                                   start_date, **params)

    df = df[start_date:end_date]

    MAC_Cerebro = execute_strategy(MovingAveragesCrossStrategy, df, commission,
                                   info, **kwargs)

    return MAC_Cerebro, MovingAveragesCrossStrategy
def execute_one_moving_average_strategy(df,
                                        commission,
                                        data_name,
                                        start_date,
                                        end_date,
                                        optimize=False,
                                        **kwargs):
    """
    Execute one moving average strategy on data history contained in df
    :param df: dataframe with historical data
    :param commision: commission to be paid on each operation
    :param data_name: quote data name
    :param start_date: start date of simulation
    :param end_date: end date of simulation
    :return:
        - OMA_Cerebro - execution engine
        - OMA_Strategy - one moving average strategy instance
    """

    print_execution_name("Estrategia: media móvil")

    strategy_name = 'estrategia_media_movil'

    info = {
        'Mercado': data_name,
        'Estrategia': strategy_name,
        'Fecha inicial': start_date,
        'Fecha final': end_date
    }

    if optimize:
        print('Optimizando (esto puede tardar)...')

        params = {'ma_period': range(5, 50)}

        # Get best params in past period
        kwargs = optimize_strategy(df, commission, OneMovingAverageStrategy,
                                   start_date, **params)

    df = df[start_date:end_date]

    OMA_Strategy = OneMovingAverageStrategy
    OMA_Cerebro = execute_strategy(OMA_Strategy, df, commission, info,
                                   **kwargs)

    return OMA_Cerebro, OMA_Strategy
def execute_neural_network_strategy(df, options, commission, data_name,
                                    start_date, end_date):
    """
    Execute neural network strategy on data history contained in df
    :param df: dataframe with historical data
    :param options: dict with the following parameters
        - gain - gain threshold in simulation of labelling
        - loss - loss threshold simulation of labelling
        - n_day - number of days in simulation of labelling
        - epochs - number of epochs to train the neural network
    :param commission: commission to be paid on each operation
    :param data_name: quote data name
    :param start_date: start date of simulation
    :param end_date: end date of simulation
    :return:
        - NN_Cerebro - execution engine
        - NN_Strategy - neural network strategy instance
    """

    print_execution_name("Estrategia: red neuronal")

    strategy_name = 'red_neuronal'

    info = {
        'Mercado': data_name,
        'Estrategia': strategy_name,
        'Fecha inicial': start_date,
        'Fecha final': end_date
    }

    # Get parameters
    gain = options['gain']
    loss = options['loss']
    n_day = options['n_day']
    epochs = options['epochs']

    s_test_date = datetime.strptime(start_date, '%Y-%m-%d')
    s_train = s_test_date.replace(year=s_test_date.year - 2)
    e_train = s_test_date - timedelta(days=1)

    # Preprocess dataset
    df = func_utils.add_features(df)
    df = func_utils.add_label(df,
                              gain=gain,
                              loss=loss,
                              n_day=n_day,
                              commission=commission)

    # Split train and test
    df_train, df_test, X_train, X_test, y_train, y_test = func_utils.split_df_date(
        df, s_train, e_train, start_date, end_date)

    # Normalization
    print("Normalizando datos...")
    sc = StandardScaler()
    X_train = sc.fit_transform(X_train)
    X_test = sc.fit_transform(X_test)

    # Transform data in a correct format to use in Keras
    X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
    X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))

    # Get prediction model
    print("Entrenando red neuronal...")
    neural_network = NeuralNetwork()
    neural_network.build_model(input_shape=(X_train.shape[1], 1))
    neural_network.train(X_train, y_train, epochs=epochs)

    # Get accuraccy
    train_accuracy = neural_network.get_accuracy(X_train, y_train)
    test_accuracy = neural_network.get_accuracy(X_test, y_test)

    print("\nRESULTADOS PREDICCION:\n")
    print("TRAIN :: Porcentaje de acierto: " + str(train_accuracy))
    print("TEST  :: Porcentaje de acierto: " + str(test_accuracy))

    # ------------------------ Backtesting ------------------------ #

    # Initialize neural network memory
    neural_network.init_memory(X_train[len(X_train) - 15:len(X_train)],
                               y_train[len(y_train) - 15:len(y_train)])

    # Create an instance from NeuralNetworkStrategy class and assign parameters
    NN_Strategy = NeuralNetworkStrategy
    NN_Strategy.X_test = X_test
    NN_Strategy.y_test = y_test
    NN_Strategy.model = neural_network
    NN_Strategy.n_day = n_day

    # Execute strategy
    NN_Cerebro = execute_strategy(NN_Strategy, df_test, commission, info,
                                  options)

    return NN_Cerebro, NN_Strategy
def execute_pso_strategy(df,
                         options,
                         topology,
                         retrain_params,
                         commission,
                         data_name,
                         s_test,
                         e_test,
                         iters=100,
                         normalization='exponential'):
    """
    Execute particle swarm optimization strategy on data history contained in df
    :param df: dataframe with historical data
    :param options: dict with the following parameters
        - c1 - cognitive parameter with which the particle follows its personal best
        - c2 - social parameter with which the particle follows the swarm's global best position
        - w - parameter that controls the inertia of the swarm's movement
    :param commision: commission to be paid on each operation
    :param data_name: quote data name
    :param start_date: start date of simulation
    :param end_date: end date of simulation
    :return:
        - PSO_Cerebro - execution engine
        - PSO_Strategy - pso strategy instance
    """

    print_execution_name("Estrategia: particle swar optimization")

    strategy_name = 'particle_swarm_optimization'

    info = {
        'Mercado': data_name,
        'Estrategia': strategy_name,
        'Fecha inicial': s_test,
        'Fecha final': e_test
    }

    # ------------ Obtenemos los conjuntos de train y test ------------ #

    s_test_date = datetime.strptime(s_test, '%Y-%m-%d')
    s_train = s_test_date.replace(year=s_test_date.year - 2)
    #s_train = s_test_date - timedelta(days=180)
    e_train = s_test_date - timedelta(days=1)

    gen_representation = GeneticRepresentation(df, s_train, e_train, s_test,
                                               e_test)

    # ------------ Fijamos hiperparámetros ------------ #

    n_particles = topology['particles']
    num_neighbours = topology['neighbours']
    minkowski_p_norm = 2
    options['k'] = num_neighbours
    options['p'] = minkowski_p_norm
    dimensions = len(gen_representation.moving_average_rules) + 2

    if normalization == 'exponential':
        max_bound = 2.0 * np.ones(dimensions - 2)
        min_bound = -max_bound
    elif normalization == 'l1':
        max_bound = 2.0 * np.ones(dimensions - 2)
        min_bound = np.zeros(dimensions - 2)

    max_bound = np.append(max_bound, [0.9, 0.0])
    min_bound = np.append(min_bound, [0.0, -0.9])
    bounds = (min_bound, max_bound)

    # Call instance of PSO
    optimizer = ps.single.LocalBestPSO(n_particles=n_particles,
                                       dimensions=dimensions,
                                       options=options,
                                       bounds=bounds,
                                       static=True)

    # Perform optimization
    kwargs = {
        'from_date': s_train,
        'to_date': e_train,
        'normalization': normalization
    }
    best_cost, best_pos = optimizer.optimize(gen_representation.cost_function,
                                             iters=iters,
                                             n_processes=2,
                                             **kwargs)

    # Create an instance from CombinedSignalStrategy class and assign parameters
    PSO_Strategy = CombinedSignalStrategy
    w, buy_threshold, sell_threshold = get_split_w_threshold(best_pos)
    """
    print("Umbral de compra: ", buy_threshold)
    print("Umbral de venta: ", sell_threshold)

    crosses = ["(" + str(cross[0]) + ", " + str(cross[1]) + ")" for cross in gen_representation.moving_average_rules]

    y_pos = np.arange(len(crosses))
    plt.bar(y_pos, w)
    plt.xticks(y_pos, crosses)
    plt.xticks(rotation='vertical')
    plt.subplots_adjust(top=0.98, bottom=0.2, left=0.08, right=0.98, hspace=0.0, wspace=0.0)
    plt.show()
    """

    PSO_Strategy.w = w
    PSO_Strategy.buy_threshold = buy_threshold
    PSO_Strategy.sell_threshold = sell_threshold
    PSO_Strategy.moving_average_rules = gen_representation.moving_average_rules
    PSO_Strategy.moving_averages = gen_representation.moving_averages_test
    PSO_Strategy.optimizer = optimizer
    PSO_Strategy.gen_representation = gen_representation
    PSO_Strategy.normalization = normalization
    PSO_Strategy.retrain_params = retrain_params

    df_test = gen_representation.df_test
    df_train = gen_representation.df_train

    PSO_Cerebro = execute_strategy(PSO_Strategy, df_test, commission, info,
                                   retrain_params)

    return PSO_Cerebro, PSO_Strategy