空最弱,多最强?
我们经常能看到某些操盘手说,操作期货时要多最强,空最弱.但是从回溯来看,并不是这样的.
回溯采用的BTC,ETH,XRP,ETC,LTC,DASH的5分钟数据线(20160815-20181204).每次都是多最强,空最弱的(1小时内),盈利3%止盈,亏1.5%止损.
运行结果是亏个精光
import h5py import numpy as np import pandas as pd import matplotlib.pyplot as plt #空最弱,多最强策略 history=None with h5py.File('coin_history_5min_6_20170815_20181204.h5', 'r') as f: history = f['history'][:] history= np.moveaxis(history, 0, -1) #btc,eth, xrp, etc, ltc,dash #coinxlenght*close high low open offset=5*12 EMPTY=0 LONG=1 SHORT=2 def reset_action(): global prev_coin, prev_price, prev_action prev_coin = -1 prev_price = -1 prev_action = EMPTY prev_index = -1 reset_action() portfolio=3000 def calc_delta(max, current, max_idx, current_idx): delta_y = (max - current) / max * 100 return delta_y win_long_count=0 win_short_count=0 lose_long_count=0 lose_short_count=0 long_count=0 short_count=0 bias_threshold=[50,50,50,50,50,50] def calc_reward(reward): global win_long_count, win_short_count, lose_long_count,lose_short_count, portfolio if (reward > 3 ) : # 止盈 #print(" 止盈 {}".format(reward)) portfolio = portfolio * (1 + reward / 100) * 0.997 if (prev_action==LONG): win_long_count += 1 else: win_short_count+=1 reset_action() elif reward < -1.5: # 止损 portfolio = portfolio * (1 + reward / 100) * 0.997 if (prev_action==LONG): lose_long_count += 1 else: lose_short_count+=1 reset_action() for i in range(0, history.shape[1]-offset): uplist=[] downlist=[] #for i in range(0,200): max_up=0 max_down=0 up_coin=0 down_coin=0 total_up=0 total_down=0 for coin in range(0, history.shape[0]): data = history[coin, i:offset+i, :] low=np.min(data[:, 2]) low_idx=np.argmin(data[:,2]) high=np.max(data[:,1]) high_idx=np.argmax(data[:,1]) close=data[-1,0] #计算上升斜率 rate=calc_delta(close, low, low_idx, offset-1) assert rate >=0 total_up+=rate if rate>max_up: max_up=rate up_coin=coin #计算下降斜率 rate= calc_delta(close, high, high_idx,offset-1) assert rate<=0 if rate<max_down: max_down=rate down_coin=coin total_down+=rate avg_up=total_up/history.shape[0] avg_down=total_down/history.shape[0] for coin in range(0, history.shape[0]): data = history[coin, i:offset+i, :] low=np.min(data[:, 2]) low_idx=np.argmin(data[:,2]) high=np.max(data[:,1]) high_idx=np.argmax(data[:,1]) close=data[-1,0] #计算上升斜率 rate=calc_delta(close, low, low_idx, offset-1) assert rate >=0 #上升趋势检查 if coin==up_coin and max_up>avg_up+3 and avg_up>0 and prev_action==EMPTY: #上升趋势,多 prev_action=LONG prev_coin=coin prev_price=close prev_index=i+offset-1 long_count+=1 #计算下降斜率 rate= calc_delta(close, high, high_idx,offset-1) assert rate<=0 # 下降趋势检查 if coin==down_coin and rate < avg_down-3 and avg_down <0 and prev_action == EMPTY: # 下降趋势,空 prev_action = SHORT prev_coin = coin prev_price = close prev_index = i + offset - 1 short_count+=1 #止盈,止损检查 if prev_coin==coin: if prev_action==LONG: reward=(close-prev_price)/prev_price*100 calc_reward(reward) elif prev_action==SHORT: reward=(prev_price-close)/prev_price*100 calc_reward(reward) if i % 10000==0 or i==history.shape[1]-offset-1: print("offset {} win:{}, {} lose:{},{} long:{} short:{} portfolio:{}".format( i, win_long_count, win_short_count,lose_long_count,lose_short_count,long_count, short_count, portfolio)) print("统计结束")
回溯结果
offset 0 win:0, 0 lose:0,0 long:1 short:0 portfolio:3000
offset 10000 win:21, 15 lose:66,83 long:87 short:98 portfolio:152.90153695004793
offset 20000 win:40, 26 lose:133,152 long:173 short:178 portfolio:9.076793139959658
offset 30000 win:48, 35 lose:202,236 long:250 short:271 portfolio:0.314471248101765
offset 40000 win:83, 57 lose:280,325 long:363 short:382 portfolio:0.027886125010532743
offset 50000 win:112, 79 lose:341,395 long:453 short:474 portfolio:0.005074702707672284
offset 60000 win:175, 101 lose:491,490 long:666 short:592 portfolio:0.00012676749100045502
offset 70000 win:417, 162 lose:933,684 long:1351 short:846 portfolio:1.9404697306301155e-07
offset 80000 win:606, 213 lose:1235,852 long:1842 short:1065 portfolio:5.057193877376249e-09
offset 90000 win:784, 302 lose:1547,1103 long:2331 short:1406 portfolio:7.484799493919765e-12
offset 100000 win:870, 347 lose:1718,1229 long:2589 short:1576 portfolio:3.7508012437917745e-13
offset 110000 win:963, 382 lose:1890,1315 long:2854 short:1697 portfolio:5.3471279645124915e-14
offset 120000 win:1000, 421 lose:1972,1393 long:2972 short:1814 portfolio:1.1791309231419582e-14
offset 130000 win:1039, 441 lose:2068,1452 long:3108 short:1893 portfolio:2.3966056743285288e-15
offset 140000 win:1180, 493 lose:2311,1597 long:3491 short:2090 portfolio:6.822473498573493e-17
offset 150000 win:1399, 602 lose:2666,1817 long:4065 short:2419 portfolio:1.0672516995655263e-18
offset 160000 win:1485, 645 lose:2866,1939 long:4351 short:2584 portfolio:2.2175995951607598e-20
offset 170000 win:1514, 660 lose:2942,2011 long:4456 short:2671 portfolio:3.159436530936625e-21
offset 180000 win:1531, 668 lose:2986,2042 long:4517 short:2710 portfolio:1.2621767156939367e-21
offset 190000 win:1534, 673 lose:3010,2063 long:4544 short:2736 portfolio:6.152748274852602e-22
offset 200000 win:1546, 677 lose:3031,2080 long:4577 short:2758 portfolio:4.522464935136829e-22
offset 210000 win:1554, 693 lose:3051,2105 long:4605 short:2799 portfolio:3.735910505146979e-22
offset 220000 win:1584, 709 lose:3112,2153 long:4696 short:2862 portfolio:1.4179091112671753e-22
offset 230000 win:1639, 727 lose:3197,2192 long:4836 short:2919 portfolio:9.235113427458508e-23
offset 240000 win:1649, 736 lose:3238,2209 long:4887 short:2945 portfolio:4.18424055728169e-23
offset 242148 win:1651, 736 lose:3245,2212 long:4896 short:2949 portfolio:3.5146028550273e-23
原因可能是市场不同,也许在商品期货和股票市场上,CTA趋势是有效的,当然我还没有做回溯.