空最弱,多最强?

我们经常能看到某些操盘手说,操作期货时要多最强,空最弱.但是从回溯来看,并不是这样的.
回溯采用的BTC,ETH,XRP,ETC,LTC,DASH的5分钟数据线(20160815-20181204).每次都是多最强,空最弱的(1小时内),盈利3%止盈,亏1.5%止损.

运行结果是亏个精光

  1. import h5py
  2. import numpy as np
  3. import pandas as pd
  4. import matplotlib.pyplot as plt
  5.  
  6. #空最弱,多最强策略
  7.  
  8. history=None
  9. with h5py.File('coin_history_5min_6_20170815_20181204.h5', 'r') as f:
  10. history = f['history'][:]
  11. history= np.moveaxis(history, 0, -1)
  12. #btc,eth, xrp, etc, ltc,dash
  13. #coinxlenght*close high low open
  14.  
  15. offset=5*12
  16.  
  17. EMPTY=0
  18. LONG=1
  19. SHORT=2
  20.  
  21.  
  22. def reset_action():
  23. global prev_coin, prev_price, prev_action
  24. prev_coin = -1
  25. prev_price = -1
  26. prev_action = EMPTY
  27. prev_index = -1
  28.  
  29.  
  30. reset_action()
  31.  
  32. portfolio=3000
  33.  
  34.  
  35. def calc_delta(max, current, max_idx, current_idx):
  36. delta_y = (max - current) / max * 100
  37.  
  38. return delta_y
  39.  
  40. win_long_count=0
  41. win_short_count=0
  42. lose_long_count=0
  43. lose_short_count=0
  44. long_count=0
  45. short_count=0
  46. bias_threshold=[50,50,50,50,50,50]
  47.  
  48. def calc_reward(reward):
  49. global win_long_count, win_short_count, lose_long_count,lose_short_count, portfolio
  50. if (reward > 3 ) :
  51. # 止盈
  52. #print(" 止盈 {}".format(reward))
  53. portfolio = portfolio * (1 + reward / 100) * 0.997
  54. if (prev_action==LONG):
  55. win_long_count += 1
  56. else:
  57. win_short_count+=1
  58. reset_action()
  59. elif reward < -1.5:
  60. # 止损
  61. portfolio = portfolio * (1 + reward / 100) * 0.997
  62. if (prev_action==LONG):
  63. lose_long_count += 1
  64. else:
  65. lose_short_count+=1
  66. reset_action()
  67.  
  68. for i in range(0, history.shape[1]-offset):
  69.  
  70. uplist=[]
  71. downlist=[]
  72. #for i in range(0,200):
  73. max_up=0
  74. max_down=0
  75. up_coin=0
  76. down_coin=0
  77. total_up=0
  78. total_down=0
  79.  
  80. for coin in range(0, history.shape[0]):
  81. data = history[coin, i:offset+i, :]
  82. low=np.min(data[:, 2])
  83. low_idx=np.argmin(data[:,2])
  84. high=np.max(data[:,1])
  85. high_idx=np.argmax(data[:,1])
  86. close=data[-1,0]
  87. #计算上升斜率
  88. rate=calc_delta(close, low, low_idx, offset-1)
  89. assert rate >=0
  90. total_up+=rate
  91. if rate>max_up:
  92. max_up=rate
  93. up_coin=coin
  94. #计算下降斜率
  95. rate= calc_delta(close, high, high_idx,offset-1)
  96. assert rate<=0
  97. if rate<max_down:
  98. max_down=rate
  99. down_coin=coin
  100. total_down+=rate
  101. avg_up=total_up/history.shape[0]
  102. avg_down=total_down/history.shape[0]
  103.  
  104. for coin in range(0, history.shape[0]):
  105. data = history[coin, i:offset+i, :]
  106. low=np.min(data[:, 2])
  107. low_idx=np.argmin(data[:,2])
  108. high=np.max(data[:,1])
  109. high_idx=np.argmax(data[:,1])
  110. close=data[-1,0]
  111. #计算上升斜率
  112. rate=calc_delta(close, low, low_idx, offset-1)
  113. assert rate >=0
  114.  
  115. #上升趋势检查
  116. if coin==up_coin and max_up>avg_up+3 and avg_up>0 and prev_action==EMPTY:
  117. #上升趋势,多
  118. prev_action=LONG
  119. prev_coin=coin
  120. prev_price=close
  121. prev_index=i+offset-1
  122. long_count+=1
  123.  
  124. #计算下降斜率
  125. rate= calc_delta(close, high, high_idx,offset-1)
  126.  
  127. assert rate<=0
  128.  
  129. # 下降趋势检查
  130. if coin==down_coin and rate < avg_down-3 and avg_down <0 and prev_action == EMPTY:
  131. # 下降趋势,空
  132. prev_action = SHORT
  133. prev_coin = coin
  134. prev_price = close
  135. prev_index = i + offset - 1
  136. short_count+=1
  137.  
  138. #止盈,止损检查
  139. if prev_coin==coin:
  140. if prev_action==LONG:
  141. reward=(close-prev_price)/prev_price*100
  142. calc_reward(reward)
  143. elif prev_action==SHORT:
  144. reward=(prev_price-close)/prev_price*100
  145. calc_reward(reward)
  146.  
  147. if i % 10000==0 or i==history.shape[1]-offset-1:
  148. print("offset {} win:{}, {} lose:{},{} long:{} short:{} portfolio:{}".format(
  149. i, win_long_count, win_short_count,lose_long_count,lose_short_count,long_count, short_count, portfolio))
  150.  
  151. 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趋势是有效的,当然我还没有做回溯.