Example #1
0
def algo_running_worker(args, parameter):
    strategy_module = module_loader.load_module_from_file(args.strategy)
    stock_data_provider = module_loader.load_module_from_file(args.stock_data_provider)

    create_strategy = module_loader.load_module_func(strategy_module, 'create_strategy')
    strategy = create_strategy(args)

    learn_context = None
    if args.load_optimized_parameter or args.use_default_parameter:
        learn_context = LearnContext()
        learn_context.add_feature(RsiFeature())
        learn_context.add_feature(MacdFeature())

        strategy.learn_context = learn_context


    runner = algo_runner.AlgoRunner(stock_data_provider, args.capital_base, args)
    symbols = args.stock_ids
    start_date = args.optimize_range[0]
    end_date = args.optimize_range[1]

    def tmp_analyze_func(context=None, results=None):
        pass

    strategy.current_parameter = parameter

    logging.info('running strategy:%s', strategy)

    try:
        perf_data = runner.run(strategy,
                               symbols,
                               start_date=start_date,
                               end_date=end_date,
                               analyze_func=tmp_analyze_func)

        return (parameter, perf_data, learn_context.get_learning_data() if learn_context is not None else None)
    except:
        logging.exception('running strategy:%s failed', strategy)
        return (parameter, None, None)
Example #2
0
    def __init__(
            self,
            stock_ids,
            start_date=None,
            end_date=None,
            do_normalize_data=False,
            column='close',
            stock_data_provider_name='stock_data_provider.cn_a.vip_dataset',
            **kwargs):
        self.stock_data_provider_ = module_loader.load_module_from_file(
            stock_data_provider_name)
        self.load_data_ = module_loader.load_module_func(
            self.stock_data_provider_, 'load_stock_data')

        self.all_loaded_data_ = self.load_data_(stock_ids, do_normalize_data)
        self.stock_ids_ = stock_ids
        self.start_date_ = start_date
        self.end_date_ = end_date
        self.do_normalize_data_ = do_normalize_data

        self.data_ = data = filter_dataframe(
            create_dataframe(self.all_loaded_data_, column), self.start_date_,
            self.end_date_)

        if len(data.columns) > 1:
            raise ValueError('must be single stock, but get:%s' % stock_ids)

        values = data[data.columns[0]].values

        params = {
            "prominence": (.618, None),
            "threshold": None,
            "distance": 5,
        }

        params.update(kwargs)

        self.peak_index_, _ = find_peaks(values, **params)
        self.trough_index_, _ = find_peaks(-values, **params)
Example #3
0
import bt
import matplotlib.pyplot as plt

from scipy.signal import find_peaks, medfilt, wiener, hilbert

import talib

try:
    from . import module_loader
except:
    import module_loader

sys.dont_write_bytecode = True

stock_data_provider = module_loader.load_module_from_file(
    'stock_data_provider.cn_a.vip_dataset')
load_data = module_loader.load_module_func(stock_data_provider,
                                           'load_stock_data')
data = load_data('600369')
data2 = load_data('600732')

print(data.data_frame.head())


def __create_pd_panel(all_data, name='close'):
    trading_data = {}
    for data in all_data:
        trading_data[data.stock_id] = data.data_frame[name]

    panel = pd.DataFrame(data=trading_data)
Example #4
0
def wfa_runner_main():
    args = parse_arguments()

    if args.debug is None:
        args.debug = 0

    if args.debug > 0:
        logging.getLogger('').setLevel(logging.DEBUG)
    else:
        logging.getLogger('').setLevel(logging.INFO)

    logging.debug('debug level:{}'.format(args.debug))
    logging.debug('stock_ids:{}'.format(args.stock_ids))
    logging.debug('data_range:{}'.format(args.data_range))
    logging.debug('optimize_range:{}'.format(args.optimize_range))
    logging.debug('wfa_size:{}'.format(args.wfa_size))
    logging.debug('wfa range:{}'.format([args.optimize_range[1], args.optimize_range[1] + args.wfa_size]))
    logging.debug('strategy:{}'.format(args.strategy))
    logging.debug('object_function:{}'.format(args.object_function))
    logging.debug('data_provider:{}'.format(args.stock_data_provider))

    validate_args(args)

    if args.load_optimized_parameter or args.use_default_parameter:
        args.no_pool = True

    strategy_module = module_loader.load_module_from_file(args.strategy)
    object_function_module = module_loader.load_module_from_file(args.object_function)
    stock_data_provider = module_loader.load_module_from_file(args.stock_data_provider)

    create_strategy = module_loader.load_module_func(strategy_module, 'create_strategy')
    strategy = create_strategy(args)

    analyzer = Analyzer(object_function_module)

    runner = algo_runner.AlgoRunner(stock_data_provider, args.capital_base, args)
    runner.ensure_stock_data(args.stock_ids)

    pool = mp.Pool(mp.cpu_count())

    parameter_set = strategy.parameter_set()

    if args.load_optimized_parameter:
        value = __load_optimized_parameter(args.stock_ids,
                                           strategy)

        if value is None:
            value = strategy.get_default_parameter()

            if value is None:
                logging.error('unable find optmized parameter')
                return

        parameter_set = list([value])

    if args.use_default_parameter:
        value = strategy.get_default_parameter()

        if value is None:
            logging.error('unable find default parameter')
            return

        parameter_set = list([value])

    if args.no_pool:
        for parameter in parameter_set:
            analyzer.analyze([algo_running_worker(args, parameter)])
    else:
        pool.starmap_async(algo_running_worker,
                           list((args, parameter) for parameter in parameter_set),
                           callback=analyzer.analyze,
                           error_callback=lambda x:logging.error('starmap async failed:%s', x))

        pool.close()
        pool.join()

    if analyzer.best_results_ is None:
        logging.error('non parameter of strategy:[%s] is suitable for the stock:%s', strategy, args.stock_ids)
        return

    _, perf_stats = analyzer.best_results_

    logging.info('Best results:%s', perf_stats)

    strategy.current_parameter = analyzer.parameter_
    logging.info('optimized strategy:%s', strategy)

    if not args.load_optimized_parameter:
        __save_optimized_parameter(args.stock_ids, strategy, perf_stats)

    if analyzer.learn_data_ is not None:
        logging.info('learn data:%s', analyzer.learn_data_)

        if args.learn_data_file:
            with open(args.learn_data_file, 'a+') as f:
                analyzer.learn_data_.to_csv(f)

    if args.skip_wfa:
        return

    # do wfa analyze
    wfa_begin = args.optimize_range[1]
    wfa_end = wfa_begin + args.wfa_size

    while True:
        logging.info('running wfa on out of sample data:%s=>%s', wfa_begin, wfa_end)

        try:
            runner.run(strategy,
                       args.stock_ids,
                       start_date=wfa_begin,
                       end_date=wfa_end,
                       analyze_func=None)
        except:
            logging.exception('failed running wfa on out of sample data:%s=>%s', wfa_begin, wfa_end)

        wfa_begin = wfa_end
        wfa_end = wfa_begin + args.wfa_size

        if wfa_begin >= args.data_range[1]:
            break

        if wfa_end > args.data_range[1]:
            wfa_end = args.data_range[1]
        logging.info('next wfa:%s=>%s', wfa_begin, wfa_end)