def back_test(pair, date, param): tracker = TradeAnalysis(Contract(pair[0])) algo = {'class': ConstantAlgo} algo['param'] = { 'x': pair[0], 'y': pair[1], 'a': 1, 'b': 0, 'rolling': param[0], 'bollinger': param[1], 'const': param[2], 'block': 100, 'tracker': tracker } settings = { 'date': date, 'path': DATA_PATH, 'tickset': 'top', 'algo': algo, 'singletick': True } runner = PairRunner(settings) runner.run() account = runner.account history = account.history.to_dataframe(account.items) score = float(history[['pnl']].iloc[-1]) order_win = tracker.order_winning_ratio() order_profit = tracker.analyze_all_profit()[0] num_rounds = tracker.analyze_all_profit()[2] return score, order_win, order_profit, num_rounds, runner
def back_test(pair, date, param): algo = {'class': StopWinAlgo} algo['param'] = { 'x': pair[0], 'y': pair[1], 'a': 1, 'b': 0, 'rolling': param[0], 'alpha': -1, 'bollinger': param[1], 'stop_win': param[2], 'block': 100, 'bucket_size': 500, 'if_stop_win': True, 'if_ema': False, 'if_consider_spread': True, } settings = { 'date': date, 'path': DATA_PATH, 'tickset': 'top', 'algo': algo } runner = PairRunner(settings) runner.run() return runner
def back_test(pair, date, param): tracker = TradeAnalysis(Contract(pair[0])) algo = { 'class': ConstantAlgo } algo['param'] = {'x': pair[0], 'y': pair[1], 'a': 1, 'b': 0, 'rolling': param[0], 'bollinger': param[1], 'const': param[2], 'block': 100, 'tracker': tracker } settings = { 'date': date, 'path': DATA_PATH, 'tickset': 'top', 'algo': algo, 'singletick': True} runner = PairRunner(settings) runner.run() account = runner.account history = account.history.to_dataframe(account.items) score = float(history[['pnl']].iloc[-1]) order_win = tracker.order_winning_ratio() order_profit = tracker.analyze_all_profit()[0] num_rounds = tracker.analyze_all_profit()[2] return score, order_win, order_profit, num_rounds, runner
def run_simulation(param, date_list): algo = { 'class': ConstStopWinGuardAlgo } algo['param'] = {'x': 'ni1609', 'y': 'ni1701', 'a': 1, 'b': 0, 'rolling': param[0], 'bollinger': 2, 'const': param[1], 'stop_win':param[2], 'block': 100, 'tracker': None } settings = { 'date': date_list, 'path': DATA_PATH, 'tickset': 'top', 'algo': algo, 'singletick': False} settings['exe'] = PairExePlusTick(2) runner = PairRunner(settings) runner.run() report = Report(runner) temp = report.get_daily_pnl() pnl_list = list(temp.daily_pnl) return pnl_list
def back_test(pair, date, param): algo = { 'class': StopWinAlgo } algo['param'] = {'x': pair[0], 'y': pair[1], 'a': 1, 'b': 0, 'rolling': param[0], 'alpha': -1, 'bollinger': param[1], 'stop_win': param[2], 'block': 100, 'if_stop_win': True, 'if_ema': False, 'if_consider_spread': True, } settings = { 'date': date, 'path': DATA_PATH, 'tickset': 'top', 'algo': algo} runner = PairRunner(settings) runner.run() account = runner.account history = account.history.to_dataframe(account.items) score = float(history[['pnl']].iloc[-1]) return score
def back_test(pair, date, param, tracker): algo = {"class": StopWinSpreadGuardAlgo} algo["param"] = { "x": pair[0], "y": pair[1], "a": 1, "b": 0, "rolling": param[0], "bollinger": param[1], "stop_win": param[2], "block": 100, "tracker": tracker, } settings = {"date": date, "path": DATA_PATH, "tickset": "top", "algo": algo, "singletick": True} runner = PairRunner(settings) runner.run() return runner, algo
def back_test(pair, date): algo = { 'class': TestAlgo } algo['param'] = {'x': pair[0], 'y': pair[1], 'a': 1, 'b': 0, 'rolling': 4000, 'sd_coef': 3, 'block': 100, } settings = { 'date': date, 'path': DATA_PATH, 'tickset': 'top', 'algo': algo} runner = PairRunner(settings) runner.run() return runner
def back_test(pair, date): algo = { 'class': TestAlgo } algo['param'] = {'x': pair[0], 'y': pair[1], 'a': 1, 'b': 0, 'rolling': 1000, 'sd_coef': 2, 'block': 100, } settings = { 'date': date, 'path': DATA_PATH, 'tickset': 'top', 'algo': algo} runner = PairRunner(settings) runner.run() account = runner.account return account
def back_test(pair, date, param, tracker): algo = { 'class': StopWinSpreadGuardAlgo } algo['param'] = {'x': pair[0], 'y': pair[1], 'a': 1, 'b': 0, 'rolling': param[0], 'bollinger': param[1], 'stop_win': param[2], 'block': 100, 'tracker': tracker} settings = { 'date': date, 'path': DATA_PATH, 'tickset': 'top', 'algo': algo, 'singletick': True} runner = PairRunner(settings) runner.run() return runner, algo
def back_test(pair, date, param, tracker): algo = { 'class': SpreadGuardAlgo } algo['param'] = {'x': pair[0], 'y': pair[1], 'a': 1, 'b': 0, 'rolling': param[0], 'bollinger': param[1], 'block': 100, 'tracker': tracker, } settings = { 'date': date, 'path': DATA_PATH, 'tickset': 'top', 'algo': algo, 'singletick': True} runner = PairRunner(settings) runner.run() return runner, algo
def run_based_on_pre(date_list): temp = best_param(date_list[:-1]) algo = { 'class': TestAlgo } algo['param'] = {'x': pair[0], 'y': pair[1], 'a': 1, 'b': 0, 'rolling': float(temp.rolling), 'sd_coef': float(temp.sd_coef), 'block': 100} settings = { 'date': date_list[-1], 'path': DATA_PATH, 'tickset': 'top', 'algo': algo} runner = PairRunner(settings) runner.run() account = runner.account orders = account.orders.to_dataframe() history = account.history.to_dataframe(account.items) return float(history.pnl.tail(1)), len(orders)
def back_test(pair, date, param): algo = {"class": StopWinAlgo} algo["param"] = { "x": pair[0], "y": pair[1], "a": 1, "b": 0, "rolling": param[0], "alpha": -1, "bollinger": param[1], "stop_win": param[2], "block": 100, "if_stop_win": True, "if_ema": False, "if_consider_spread": True, } settings = {"date": date, "path": DATA_PATH, "tickset": "top", "algo": algo} runner = PairRunner(settings) runner.run() return runner
def back_test(pair, date, param): algo = { 'class': TestAlgo } algo['param'] = {'x': pair[0], 'y': pair[1], 'a': 1, 'b': 0, 'rolling': param[0], 'sd_coef': param[1], 'block': 100, } settings = { 'date': date, 'path': DATA_PATH, 'tickset': 'top', 'algo': algo} runner = PairRunner(settings) runner.run() account = runner.account history = account.history.to_dataframe(account.items) orders = account.orders.to_dataframe() pnl = np.asarray(history.pnl)[-1] return pnl, len(orders)
def back_test(pair, date, param): tracker = TradeAnalysis(Contract(pair[0])) algo = {"class": OUAlgo} algo["param"] = { "x": pair[0], "y": pair[1], "a": 1, "b": 0, "rolling": param[0], "bollinger": param[1], "block": 100, "tracker": tracker, } settings = {"date": date, "path": DATA_PATH, "tickset": "top", "algo": algo, "singletick": True} runner = PairRunner(settings) runner.run() account = runner.account history = account.history.to_dataframe(account.items) score = float(history[["pnl"]].iloc[-1]) order_win = tracker.order_winning_ratio() order_profit = tracker.analyze_all_profit()[0] num_rounds = tracker.analyze_all_profit()[2] return score, order_win, order_profit, num_rounds
def back_test(pair, date, param, if_param): algo = { 'class': StopWinAlgo } algo['param'] = {'x': pair[0], 'y': pair[1], 'a': 1, 'b': 0, 'rolling': param[0], 'alpha': param[1], 'bollinger': param[2], 'stop_win': param[3], 'block': 100, 'if_stop_win': if_param[0], 'if_ema': if_param[1], 'if_consider_spread': if_param[2], } settings = { 'date': date, 'path': DATA_PATH, 'tickset': 'top', 'algo': algo} runner = PairRunner(settings) runner.run() return runner
def back_test(pair, date, param): algo = {"class": StopWinAlgo} algo["param"] = { "x": pair[0], "y": pair[1], "a": 1, "b": 0, "rolling": param[0], "alpha": -1, "bollinger": param[1], "stop_win": param[2], "block": 100, "if_stop_win": True, "if_ema": False, "if_consider_spread": True, } settings = {"date": date, "path": DATA_PATH, "tickset": "top", "algo": algo} runner = PairRunner(settings) runner.run() account = runner.account history = account.history.to_dataframe(account.items) score = float(history[["pnl"]].iloc[-1]) return score
def best_param(date_list): score = [] num_trades = [] algo = { 'class': TestAlgo } algo['param'] = {'x': pair[0], 'y': pair[1], 'a': 1, 'b': 0, 'rolling': 2000, 'sd_coef': 2, 'block': 100} settings = { 'date': date_list, 'path': DATA_PATH, 'tickset': 'top', 'algo': algo} runner = PairRunner(settings) num_cores = 32 results = Parallel(n_jobs=num_cores)(delayed(run_simulation)(p, runner) for p in pars) result = pd.DataFrame({ "rolling": [p[0] for p in pars], "sd_coef": [p[1] for p in pars], "PNL": [i for i, v in results], "num_trades": [v for i, v in results]}) index = np.asarray(result.PNL).argmax() return result[index:index+1]
pair = ["m1505", "m1509"] date_list = get_trade_day(pair) algo = {"class": MyAlgo} algo["param"] = { "x": pair[0], "y": pair[1], "a": 1, "b": 0, "rolling": 4000, "rolling_sigma": 4000, "sd_coef": 3, "block": 100, "stop_win": 200, } settings = {"date": date_list, "path": DATA_PATH, "tickset": "top", "algo": algo} runner = PairRunner(settings) price_diff = get_price_diff(pair) price_diff_std = np.nanstd(price_diff) rolling_list = range(1000, 10000, 2000) rolling_sigma_list = range(1000, 10000, 2000) sd_coef_list = np.arange(2, 8) stop_win_list = price_diff_std * np.arange(0.3, 3, 0.3) final_profit = [] for r in rolling_list: for rs in rolling_sigma_list: for sd in sd_coef_list: for sw in stop_win_list: start_time = time.time() runner.run(algo_param={"rolling": r, "rolling_sigma": rs, "sd_coef": sd, "stop_win": sw}) account = runner.account history = account.history.to_dataframe(account.items)
date_list = [str(x).split(' ')[0] for x in pd.date_range('2016-03-01','2016-03-31').tolist()] algo = { 'class': TestAlgo } algo['param'] = {'x': pair[0], 'y': pair[1], 'a': 1, 'b': 0, 'rolling': 1000, 'rolling_sigma': 1000, 'sd_coef': 3, 'block': 100, } settings = { 'date': date_list, 'path': DATA_PATH, 'tickset': 'top', 'algo': algo} runner = PairRunner(settings) rolling_list = range(500,5000,500) rolling_sigma_list = range(500,5000,500) sd_coef_list = np.arange(1, 5, 0.5) final_profit = [] for r in rolling_list : for rs in rolling_sigma_list: for sd in sd_coef_list : start_time = time.time() runner.run(algo_param={'rolling': r,'rolling_sigma': rs, 'sd_coef': sd}) account = runner.account history = account.history.to_dataframe(account.items) score = float(history[['pnl']].iloc[-1]) final_profit.append(score) print("rolling {}, rolling sigma {}, sd_coef {}, backtest took {:.3f}s, score is {:.3f}".format(r, rs, sd, time.time() - start_time, score)) pars = list(itertools.product(rolling_list, rolling_sigma_list, sd_coef_list))
'b': 0, 'rolling': 1000, 'bollinger': 2, 'block': 100, 'alpha': 0.0007, 'stop_win': 1, } dates = pd.date_range('20150101', '20150228') date_list = [day.strftime("%Y-%m-%d") for day in dates] settings = { 'date': date_list, 'path': DATA_PATH, 'tickset': 'top', 'algo': algo} runner = PairRunner(settings) rolling_list = range(1000,9000,1000) bollinger_list = range(2,15,1) stop_win_list = range(1,15,1) alpha_list = np.arange(0.0001, 0.001, 0.0001) final_profit = [] num_trades = [] pars = list(itertools.product(rolling_list, bollinger_list, stop_win_list,\ alpha_list)) def run_simulation(p): runner.run(algo_param={'rolling': p[0], 'bollinger': p[1], 'stop_win': p[2],\ 'alpha': p[3]}) account = runner.account orders = account.orders.to_dataframe() history = account.history.to_dataframe(account.items)
'y': pair[1], 'a': 1, 'b': 0, 'rolling': 4000, 'sd_coef': 3, 'block': 100, 'stop_win': 200, } settings = { 'date': date_list, 'path': DATA_PATH, 'tickset': 'top', 'algo': algo } runner = PairRunner(settings) price_diff = get_price_diff(pair) price_diff_std = np.nanstd(price_diff) rolling_list = range(1000, 10000, 2000) sd_coef_list = np.arange(2, 8) stop_win_list = price_diff_std * np.arange(0.5, 3.5, 0.5) final_profit = [] for r in rolling_list: for sd in sd_coef_list: for sw in stop_win_list: start_time = time.time() runner.run(algo_param={ 'rolling': r, 'sd_coef': sd, 'stop_win': sw })
algo = { 'class': TestAlgo } algo['param'] = {'x': pair[0], 'y': pair[1], 'a': 1, 'b': 0, 'rolling': 4000, 'sd_coef': 3, 'block': 100, 'stop_win': 200, } settings = { 'date': date_list, 'path': DATA_PATH, 'tickset': 'top', 'algo': algo} runner = PairRunner(settings) price_diff = get_price_diff(pair) price_diff_std = np.nanstd(price_diff) rolling_list = range(1000,10000,2000) sd_coef_list = np.arange(2,8) stop_win_list = price_diff_std * np.arange(0.5, 3.5, 0.5) final_profit = [] for r in rolling_list : for sd in sd_coef_list : for sw in stop_win_list: start_time = time.time() runner.run(algo_param={'rolling': r, 'sd_coef': sd, 'stop_win': sw }) account = runner.account history = account.history.to_dataframe(account.items) score = float(history[['pnl']].iloc[-1]) final_profit.append(score)