以文本方式查看主题 - 金字塔客服中心 - 专业程序化交易软件提供商 (http://222.73.7.161/bbs/index.asp) -- 策略编写求助区 (http://222.73.7.161/bbs/list.asp?boardid=11) ---- 请各大神帮忙把这个策略改为金字塔代码 (http://222.73.7.161/bbs/dispbbs.asp?boardid=11&id=161482) |
-- 作者:lyt369 -- 发布时间:2018/2/5 13:03:07 -- 请各大神帮忙把这个策略改为金字塔代码 请各大神帮忙把这个策略改为金字塔代码 套利策略:https://uqer.io/community/share/5833aa0f228e5ba297a91aeb 由于帖子内容长度不能大于116240,您已经输入了148410个字----所以分两段 universe = [\'RBM0\', \'IM0\', \'JM0\'] # 策略期货合约 2 start = \'2013-11-01\' # 回测开始时间 3 end = \'2016-11-18\' # 回测结束时间 4 capital_base = 300000 # 初试可用资金 5 refresh_rate = 1 # 调仓周期 6 freq = \'d\' # 调仓频率:m-> 分钟;d-> 日 7 diff = [] 8 def initialize(futures_account): # 初始化虚拟期货账户,一般用于设置计数器,回测辅助变量等。 9 futures_account.symbol0 = \'RB1405\' 10 futures_account.symbol1 = \'I1405\' 11 futures_account.symbol2 = \'J1405\' 12 pass 13 ? 14 def handle_data(futures_account): # 回测调仓逻辑,每个调仓周期运行一次,可在此函数内实现信号生产,生成调仓指令。 15 long_position_0 = futures_account.position.get(futures_account.symbol0, dict()).get(\'long_position\', 0) 16 short_position_0 = futures_account.position.get(futures_account.symbol0, dict()).get(\'short_position\', 0) 17 ? 18 long_position_1 = futures_account.position.get(futures_account.symbol1, dict()).get(\'long_position\', 0) 19 short_position_1 = futures_account.position.get(futures_account.symbol1, dict()).get(\'short_position\', 0) 20 ? 21 long_position_2 = futures_account.position.get(futures_account.symbol2, dict()).get(\'long_position\', 0) 22 short_position_2 = futures_account.position.get(futures_account.symbol2, dict()).get(\'short_position\', 0) 23 24 if get_symbol(universe[0]) != futures_account.symbol0 or get_symbol(universe[1]) != futures_account.symbol1 or get_symbol(universe[2]) != futures_account.symbol2: 25 if long_position_0 != 0: 26 print futures_account.current_date, \'主力更换, 平仓\' 27 order(futures_account.symbol0, -long_position_0, \'close\') 28 order(futures_account.symbol1, short_position_1, \'close\') 29 order(futures_account.symbol2, short_position_2, \'close\') 30 ? 31 if short_position_0 != 0: 32 print futures_account.current_date, \'主力更换, 平仓\' 33 order(futures_account.symbol0, short_position_0, \'close\') 34 order(futures_account.symbol1, -long_position_1, \'close\') 35 order(futures_account.symbol2, -long_position_2, \'close\') 36 37 futures_account.symbol0 = get_symbol(universe[0]) 38 futures_account.symbol1 = get_symbol(universe[1]) 39 futures_account.symbol2 = get_symbol(universe[2]) 40 |
-- 作者:lyt369 -- 发布时间:2018/2/5 13:04:32 -- 笫二段 笫二段
else: 41 futures_account.symbol0 = get_symbol(universe[0]) 42 futures_account.symbol1 = get_symbol(universe[1]) 43 futures_account.symbol2 = get_symbol(universe[2]) 44 ? 45 close_hist0 = get_symbol_history(symbol=futures_account.symbol0, field=[\'closePrice\'], time_range=1) 46 close_hist1 = get_symbol_history(symbol=futures_account.symbol1, field=[\'closePrice\'], time_range=1) 47 close_hist2 = get_symbol_history(symbol=futures_account.symbol2, field=[\'closePrice\'], time_range=1) 48 ? 49 close0 = np.array(close_hist0[futures_account.symbol0][\'closePrice\'])[-1] 50 close1 = np.array(close_hist1[futures_account.symbol1][\'closePrice\'])[-1] 51 close2 = np.array(close_hist2[futures_account.symbol2][\'closePrice\'])[-1] 52 ? 53 if short_position_0 == 0 and close0 - 1.6 * close1 - 0.5 * close2 - 1100 > 300: 54 print futures_account.current_date, \'做空螺纹\' 55 print close0 - 1.6 * close1 - 0.5 * close2 - 1100 56 order(futures_account.symbol0, -20, \'open\') 57 order(futures_account.symbol1, 3, \'open\') 58 order(futures_account.symbol2, 1, \'open\') 59 60 if long_position_0 == 0 and close0 - 1.6 * close1 - 0.5 * close2 - 1100 < -250: 61 print futures_account.current_date, \'做多螺纹\' 62 print close0 - 1.6 * close1 - 0.5 * close2 - 1100 63 order(futures_account.symbol0, 20, \'open\') 64 order(futures_account.symbol1, -3, \'open\') 65 order(futures_account.symbol2, -1, \'open\') 66 67 # 平仓 68 if close0 - 1.6 * close1 - 0.5 * close2 - 1100 < 100: 69 if short_position_0 != 0: 70 print futures_account.current_date, \'平仓\' 71 print close0 - 1.6 * close1 - 0.5 * close2 - 1100 72 order(futures_account.symbol0, short_position_0, \'close\') 73 if long_position_1 != 0: 74 order(futures_account.symbol1, -long_position_1, \'close\') 75 if long_position_2 != 0: 76 order(futures_account.symbol2, -long_position_2, \'close\') 77 if close0 - 1.6 * close1 - 0.5 * close2 - 1100 > 100: 78 if long_position_0 != 0: 79 print futures_account.current_date, \'平仓\' 80 print close0 - 1.6 * close1 - 0.5 * close2 - 1100 81 order(futures_account.symbol0, short_position_0, \'close\') 82 if short_position_1 != 0: 83 order(futures_account.symbol1, -long_position_1, \'close\') 84 if short_position_2 != 0: 85 order(futures_account.symbol2, -long_position_2, \'close\') |
-- 作者:fly -- 发布时间:2018/5/31 16:01:28 -- 这是Python语言编写的策略,软件暂时不支持。今年下半年会推出Python版,请您关注我们。 如果您想提前使用Python,请留下您的联系方式,我们一推出就会优先联系您的。
[此贴子已经被作者于2018/5/31 16:03:25编辑过]
|