def sample_243(): """ 2.4.3 使用编译库提高性能 :return: """ # 买入后持股天数放大寻找范围 1 - 503 天, 间隔1天 keep_stock_list = list(range(1, 504, 1)) # 下跌买入阀值寻找范围 -0.01 - -0.99 共99个 buy_change_list = [buy_change / 100.0 for buy_change in xrange(-1, -100, -1)] def do_single_task(): task_list = list(itertools.product(keep_stock_list, buy_change_list)) print('笛卡尔积参数集合总共结果为:{}个'.format(len(task_list))) for keep_stock_threshold, buy_change_threshold in task_list: calc(keep_stock_threshold, buy_change_threshold) import time start_time = time.time() do_single_task() end_time = time.time() print('{} cost {}s'.format(do_single_task.__name__, round(end_time - start_time, 3))) import numba as nb do_single_task_nb = nb.jit(do_single_task) start_time = time.time() do_single_task_nb() end_time = time.time() print('{} cost {}s'.format(do_single_task_nb.__name__, round(end_time - start_time, 3)))
def sample_242(): """ 2.4.2 多进程 vs 多线程 :return: """ # range集合:买入后持股天数从2天-30天,间隔两天 keep_stock_list = list(range(2, 30, 2)) print('持股天数参数组:{}'.format(keep_stock_list)) # 下跌买入阀值从-0.05到-0.15,即从下跌5%到15% buy_change_list = [ buy_change / 100.0 for buy_change in xrange(-1, -100, -1) ] print('下跌阀值参数组:{}'.format(buy_change_list)) result = [] # 回调函数,通过add_done_callback任务完成后调用 def when_done(r): # when_done在主进程中运行 result.append(r.result()) """ with class_a() as a: 上下文管理器:稍后会具体讲解 """ with ProcessPoolExecutor() as pool: for keep_stock_threshold, buy_change_threshold in \ itertools.product(keep_stock_list, buy_change_list): """ submit提交任务:使用calc函数和的参数通过submit提交到独立进程 提交的任务必须是简单函数,进程并行不支持类方法、闭包等 函数参数和返回值必须兼容pickle序列化,进程间的通信需要 """ future_result = pool.submit(calc, keep_stock_threshold, buy_change_threshold) # 当进程完成任务即calc运行结束后的回调函数 future_result.add_done_callback(when_done) print('Process sorted(result)[::-1][:10]:\n', sorted(result)[::-1][:10]) result = [] def when_done(r): result.append(r.result()) with ThreadPoolExecutor(max_workers=8) as pool: for keep_stock_threshold, buy_change_threshold in \ itertools.product(keep_stock_list, buy_change_list): future_result = pool.submit(calc, keep_stock_threshold, buy_change_threshold) future_result.add_done_callback(when_done) print('Thread sorted(result)[::-1][:10]:\n', sorted(result)[::-1][:10])
def sample_242(): """ 2.4.2 多进程 vs 多线程 :return: """ # range集合:买入后持股天数从2天-30天,间隔两天 keep_stock_list = list(range(2, 30, 2)) print('持股天数参数组:{}'.format(keep_stock_list)) # 下跌买入阀值从-0.05到-0.15,即从下跌5%到15% buy_change_list = [buy_change / 100.0 for buy_change in xrange(-1, -100, -1)] print('下跌阀值参数组:{}'.format(buy_change_list)) result = [] # 回调函数,通过add_done_callback任务完成后调用 def when_done(r): # when_done在主进程中运行 result.append(r.result()) """ with class_a() as a: 上下文管理器:稍后会具体讲解 """ with ProcessPoolExecutor() as pool: for keep_stock_threshold, buy_change_threshold in \ itertools.product(keep_stock_list, buy_change_list): """ submit提交任务:使用calc函数和的参数通过submit提交到独立进程 提交的任务必须是简单函数,进程并行不支持类方法、闭包等 函数参数和返回值必须兼容pickle序列化,进程间的通信需要 """ future_result = pool.submit(calc, keep_stock_threshold, buy_change_threshold) # 当进程完成任务即calc运行结束后的回调函数 future_result.add_done_callback(when_done) print('Process sorted(result)[::-1][:10]:\n', sorted(result)[::-1][:10]) result = [] def when_done(r): result.append(r.result()) with ThreadPoolExecutor(max_workers=8) as pool: for keep_stock_threshold, buy_change_threshold in \ itertools.product(keep_stock_list, buy_change_list): future_result = pool.submit(calc, keep_stock_threshold, buy_change_threshold) future_result.add_done_callback(when_done) print('Thread sorted(result)[::-1][:10]:\n', sorted(result)[::-1][:10])
def sample_241_2(): """ 2.4.1_2 笛卡尔积最优参数 :return: """ # range集合:买入后持股天数从2天-30天,间隔两天 keep_stock_list = list(range(2, 30, 2)) print('持股天数参数组:{}'.format(keep_stock_list)) # 下跌买入阀值从-0.05到-0.15,即从下跌5%到15% buy_change_list = [buy_change / 100.0 for buy_change in xrange(-5, -16, -1)] print('下跌阀值参数组:{}'.format(buy_change_list)) result = [] for keep_stock_threshold, buy_change_threshold in itertools.product( keep_stock_list, buy_change_list): # 使用calc计算参数对应的最终盈利,结果加入result序列 result.append(calc(keep_stock_threshold, buy_change_threshold)) print('笛卡尔积参数集合总共结果为:{}个'.format(len(result))) # [::-1]将整个排序结果反转,反转后盈亏收益从最高向低排序 # [:10]取出收益最高的前10个组合查看 print(sorted(result)[::-1][:10])
trade_loop_back = TradeLoopBack(trade_days, trade_strategy2) trade_loop_back.execute_trade() # 计算回测结果的最终盈亏值profit profit = 0.0 if len(trade_loop_back.profit_array) == 0 else reduce(lambda a, b: a + b, trade_loop_back.profit_array) # 返回值profit和函数的两个输入参数 return profit, keep_stock_threshold, buy_change_threshold # 测试,使用上一节使用的参数 calc(20, -0.08) # In[66]: # range集合:买入后持股天数从2天-30天,间隔两天 keep_stock_list = list(range(2, 30, 2)) print('持股天数参数组:{}'.format(keep_stock_list)) # 下跌买入阀值从-0.05到-0.15,即从下跌5%到15% buy_change_list = [buy_change / 100.0 for buy_change in xrange(-5, -16, -1)] print('下跌阀值参数组:{}'.format(buy_change_list)) # In[58]: result = [] for keep_stock_threshold, buy_change_threshold in itertools.product( keep_stock_list, buy_change_list): # 使用calc计算参数对应的最终盈利,结果加入result序列 result.append(calc(keep_stock_threshold, buy_change_threshold)) print('笛卡尔积参数集合总共结果为:{}个'.format(len(result)))
# %% set(price_array) # %% price_array.remove('32.82') price_array # %% date_array = [] date_base = 20170118 # 这里用for只是为了计数,无用的变量python建议使用'_'声明 for _ in range(0, len(price_array)): # 本节只是简单示例,不考虑日期的进位 date_array.append(str(date_base)) date_base += 1 date_array # %% date_array = [] date_base = 20170118 price_cnt = len(price_array) - 1 while price_cnt > 0: date_array.append(str(date_base)) date_base += 1 price_cnt -= 1 date_array