Exemple #1
0
    def create_from_file(self, filename, n_number):
        optimize_result = OptimizeResult(result_column_index=-1)
        optimize_result.load(filename)
        data = optimize_result.get_best_results(n_number)
        trade_strategy_list = []

        for i in range(n_number):
            X_list = data[i, :4]
            trade_strategy_list.append(
                TradeStrategy(X_list, self.n_max_trades_per_day, self.slippage,
                              self.courtage))

        return trade_strategy_list
    def update_worms_from_cache(self, n_number, start_day_index,
                                end_day_index):
        optimize_result = OptimizeResult()
        stockworm_cache_file = self.get_stockworm_cache_file(
            start_day_index, end_day_index)
        optimize_result.load(stockworm_cache_file)
        top_worms = optimize_result.get_best_results(n_number)

        assert (len(top_worms) == n_number)
        for i in range(n_number):
            features = top_worms[i, :15]
            strategy_features = top_worms[i, 15:21]
            md5 = top_worms[i, -1]
            model_save_path = self.get_model_save_path(start_day_index,
                                                       end_day_index, md5)
            new_worm = StockWorm(self.stock_name,
                                 self.stock_id,
                                 self.npy_files_path,
                                 model_save_path,
                                 is_future=self.is_future,
                                 slippage=self.slippage)

            if os.path.isdir(model_save_path) and new_worm.load() == True:
                pass
            else:
                total_profit, profit_daily, errors_daily = new_worm.init(
                    features,
                    start_day_index,
                    end_day_index,
                    strategy_features=strategy_features)
                new_worm.save()
                print("training finished for model {}, total_profit:{}".format(
                    i, total_profit))

            new_worm.report()

            testing_total_profit, testing_profit_daily, n_data_appended = new_worm.test(
            )
            if n_data_appended > 0:
                print(
                    "testing finished for model {}, total_profit:{} in {} days, new data for {} days appended"
                    .format(i, testing_total_profit, len(testing_profit_daily),
                            n_data_appended))
                new_worm.save()

            new_worm.report()
            self.worm_list.append(new_worm)
    def update_worms_from_cache(self, n_number, start_day_index,
                                end_day_index):
        optimize_result = OptimizeResult(result_column_index=15)
        stockworm_cache_file = self.get_stockworm_cache_file(
            start_day_index, end_day_index)
        optimize_result.load(stockworm_cache_file)
        top_worms = optimize_result.get_best_results(n_number)

        trade_strategy_factory = TradeStrategyFactory()
        strategy_cache_file = self.get_strategy_cache_file(
            start_day_index, end_day_index)
        strategy_list = trade_strategy_factory.create_from_file(
            strategy_cache_file, NUM_STRATEGIES)

        assert (len(top_worms) == n_number)
        swarm_path = self.get_swarm_path(start_day_index, end_day_index)
        for i in range(n_number):
            features = top_worms[i, :13]
            features_str = self.get_parameter_str(features)
            model_save_path = os.path.join(swarm_path, "models",
                                           md5(features_str))
            new_worm = StockWorm(self.stock_index, self.npy_files_path,
                                 model_save_path)
            if os.path.isdir(model_save_path) and new_worm.load() == True:
                pass
            else:
                total_profit, profit_daily, errors_daily = new_worm.init(
                    features, strategy_list, start_day_index, end_day_index)
                new_worm.save()
                print("training finished for model {}, total_profit:{}".format(
                    i, total_profit))

            testing_total_profit, testing_profit_daily, n_data_appended = new_worm.test(
            )
            if n_data_appended > 0:
                print(
                    "testing finished for model {}, total_profit:{} in {} days, new data for {} days appended"
                    .format(i, testing_total_profit, len(testing_profit_daily),
                            n_data_appended))
                new_worm.save()

            self.worm_list.append(new_worm)
stock_name = sys.argv[1]
stock_id = get_stock_id_by_name(stock_name)
training_start_day_index = int(sys.argv[2])
training_end_day_index = int(sys.argv[3])
number = 10
if len(sys.argv) == 5:
    number = int(sys.argv[4])

swarm_dir = get_swarm_dir(stock_name, stock_id, training_start_day_index,
                          training_end_day_index)

strategy_file = os.path.join(swarm_dir, 'stockworm_cache.txt')
worm_results = OptimizeResult(-1)
worm_results.load(strategy_file)
print("Top 10 Worms in {} results for {}: swarm: {}-{}".format(
    worm_results.get_size(), stock_name, training_start_day_index,
    training_end_day_index))

worm_results.get_best_results(number)
columns = [
    'n_neurons', 'learning_rate', 'num_layers', 'rnn_type', 'learning_period',
    'prediction_period', 'n_repeats', 'beta', 'ema', 'time_format',
    'volume_input', 'use_centralized_bid', 'split_daily_data', 'is_stateful',
    'ref_stock_id', 'buy_threshold', 'sell_threshold', 'stop_gain',
    'stop_gain', 'skip_at_beginning', 'value_ma', 'total_profit', 'days',
    'avg_error', 'avg_profit'
]
print("Columns:")
for i in range(len(columns)):
    print("{}: {}".format(i, columns[i]))
if len(sys.argv) < 5:
    print(
        "usage: python3 search-worms.py stock_name, stock_index traning_start_day_index, training_end_day_index"
    )
    sys.exit()

stock_name = sys.argv[1]
stock_index = int(sys.argv[2])
training_start_day_index = int(sys.argv[3])
training_end_day_index = int(sys.argv[4])

swarm_dir = get_swarm_dir(stock_name, stock_index, training_start_day_index,
                          training_end_day_index)

strategy_file = os.path.join(swarm_dir, 'strategy_cache.txt')
result_strategies = OptimizeResult(-1)
result_strategies.load(strategy_file)
print("Top 10 Strategies in {} results for {}: swarm: {}-{}".format(
    result_strategies.get_size(), stock_name, training_start_day_index,
    training_end_day_index))

result_strategies.get_best_results(10)

strategy_file = os.path.join(swarm_dir, 'stockworm_cache.txt')
worm_results = OptimizeResult(-2)
worm_results.load(strategy_file)
print("Top 10 Worms in {} results for {}: swarm: {}-{}".format(
    worm_results.get_size(), stock_name, training_start_day_index,
    training_end_day_index))

worm_results.get_best_results(10)
    )
    sys.exit()

stock_name = sys.argv[1]
start_day_index = int(sys.argv[2])
end_day_index = int(sys.argv[3])

if len(sys.argv) == 5:
    number = int(sys.argv[4])
else:
    number = 1

cache_file = get_cache_filename(stock_name, start_day_index, end_day_index)
optimize_result = OptimizeResult()
optimize_result.load(cache_file)
top_worms = optimize_result.get_best_results(number)
for i in range(len(top_worms)):
    features = top_worms[i, :6]
    strategy_features = top_worms[i, 6:12]
    md5 = top_worms[i, -1]
    model_path = get_model_path(stock_name, start_day_index, end_day_index)
    save_path = os.path.join(model_path, md5)
    hmm_model_filename = os.path.join(save_path, "hmm_model.pkl")
    hmm_model = None
    if os.path.isfile(hmm_model_filename):
        print("file: {} exists, load it.".format(hmm_model_filename))
        with open(hmm_model_filename, "rb") as file:
            hmm_model = pickle.load(file)

    else:
        print("file: {} does not exists, generate it.".format(