示例#1
0
        price_array, price_array_training = build_price_arrays(
            input_data,
            time_lag=TIME_LAG)

        #train_indices, validation_indices, test_indices = train_validation_test_indices(
        #    price_array_training,
        #    [0.9, 0.05, 0.0001])

        train_indices = np.linspace(0, len(price_array_training)-100, len(price_array_training)-100, dtype=int)
        test_indices = np.linspace(len(price_array_training) - 100, len(price_array_training) - 75, 25, dtype=int)
        validation_indices = np.linspace(len(price_array_training) -5, len(price_array_training)-1, 5, dtype=int)

        fitting_dictionary = tensorflow_cnn_fitting(
            train_indices,
            test_indices,
            validation_indices,
            price_array_training,
            price_array,
            load_net='model.h5')

        portfolio_value, cum_log_return = calculate_portfolio_value(
            fitting_dictionary['fitted_strategy_score'],
            price_array[test_indices, :])

        final_portfolio_value = portfolio_value[-1]
        running_portfolio_value *= final_portfolio_value

        print('Run profit fraction: ', final_portfolio_value - 1)
        print('Offset tests cumulative profit fraction:', running_portfolio_value - 1)

        from matplotlib import pyplot as plt
        if idx == int(len(price_array) / 2):
            cnn_kwargs = {
                'retrain': 80,
            }

        else:
            fit_kwargs = {'steps_per_epoch': 85}

            cnn_kwargs = {
                'retrain': 1,
                'fit_kwargs': fit_kwargs,
                'load_net': 'model.h5'
            }

        fitting_dictionary = tensorflow_cnn_fitting(train_indices,
                                                    test_indices, [],
                                                    price_array_training,
                                                    price_array, **cnn_kwargs)

        print(fitting_dictionary['fitted_strategy_score'])

        if len(portfolio_allocations) != 0:
            portfolio_allocations = np.vstack([
                portfolio_allocations,
                fitting_dictionary['fitted_strategy_score']
            ])
        else:
            portfolio_allocations = fitting_dictionary['fitted_strategy_score']

        plot_portfolio_value, cum_log_return = calculate_portfolio_value(
            portfolio_allocations, price_array[initial_idx:(idx + 1), :],
            liquidation_factor[(initial_idx + 1):(idx + 1), :])
        train_indices = np.linspace(0,
                                    len(price_array_training) - 100,
                                    len(price_array_training) - 100,
                                    dtype=int)
        test_indices = np.linspace(len(price_array_training) - 100,
                                   len(price_array_training) - 75,
                                   25,
                                   dtype=int)
        validation_indices = np.linspace(len(price_array_training) - 5,
                                         len(price_array_training) - 1,
                                         5,
                                         dtype=int)

        fitting_dictionary = tensorflow_cnn_fitting(train_indices,
                                                    test_indices,
                                                    validation_indices,
                                                    price_array_training,
                                                    price_array)

        portfolio_value, cum_log_return = calculate_portfolio_value(
            fitting_dictionary['fitted_strategy_score'],
            price_array[test_indices, :])

        final_portfolio_value = portfolio_value[-1]
        running_portfolio_value *= final_portfolio_value

        print('Run profit fraction: ', final_portfolio_value - 1)
        print('Offset tests cumulative profit fraction:',
              running_portfolio_value - 1)

        from matplotlib import pyplot as plt