通过神经网络进行交易

来源:https://uqer.io/community/share/55b8acbaf9f06c91fa18c5ce

  1. start = '2014-01-01' # 回测起始时间
  2. end = '2015-05-25' # 回测结束时间
  3. benchmark = 'HS300' # 策略参考标准
  4. universe = set_universe('HS300') # 证券池,支持股票和基金
  5. capital_base = 1000000 # 起始资金
  6. freq = 'd' # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测
  7. refresh_rate = 1 # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟
  8. import pybrain as brain
  9. from pybrain.tools.shortcuts import buildNetwork
  10. from pybrain.tools.customxml import NetworkReader
  11. HISTORY = 10 # 通过前十日数据预测
  12. fnn = buildNetwork(HISTORY, 15, 7, 1) # 初始化神经网络
  13. def initialize(account): # 初始化虚拟账户状态
  14. fnn = NetworkReader.readFrom('net.csv')
  15. def handle_data(account): # 每个交易日的买入卖出指令
  16. hist = account.get_attribute_history('closePrice', 10)
  17. bucket = []
  18. for s in account.universe:
  19. sample = hist[s]
  20. possibility = fnn.activate(sample)
  21. bucket.append((possibility, s))
  22. if possibility < 0 and s in account.valid_secpos:
  23. order_to(s, 0)
  24. bucket = sorted(bucket, key=lambda x: x[0], reverse=True)
  25. print bucket[0][0]
  26. if bucket[0][0] < 0:
  27. raise Exception('Network Error')
  28. for s in bucket[:10]:
  29. if s[0] > 0.5 and s[1] not in account.valid_secpos:
  30. order(s[1], 10000 * s[0] * 80000)

通过神经网络进行交易 - 图1

  1. [ 1.44446298]
  2. [ 1.57722526]
  3. [ 1.44509945]
  4. [ 1.44829344]
  5. [ 1.48584942]
  6. [ 1.60968867]
  7. [ 1.61088618]
  8. [ 1.43639898]
  9. [ 1.43767639]
  10. [ 1.43911414]
  11. [ 1.43768517]
  12. [ 1.43585662]
  13. [ 1.43720968]
  14. [ 1.43317016]
  15. [ 1.43301566]
  16. [ 1.42953404]
  17. [ 1.42678559]
  18. [ 1.43098489]
  19. [ 1.42855878]
  20. [ 1.42709837]
  21. [ 1.42726163]
  22. [ 1.42585347]
  23. [ 1.42973957]
  24. [ 1.42980622]
  25. [ 1.43599317]
  26. [ 1.44286782]
  27. [ 1.57580564]
  28. [ 1.59120978]
  29. [ 1.6130606]
  30. [ 1.59582232]
  31. [ 1.5850841]
  32. [ 1.61084701]
  33. [ 1.59595849]
  34. [ 1.52961191]
  35. [ 1.50583099]
  36. [ 1.46038687]
  37. [ 1.44689328]
  38. [ 1.5432668]
  39. [ 1.55312445]
  40. [ 1.44337678]
  41. [ 1.44056972]
  42. [ 1.50173311]
  43. [ 1.59748366]
  44. [ 1.4267731]
  45. [ 1.47709901]
  46. [ 1.62105239]
  47. [ 1.60780394]
  48. [ 1.53541989]
  49. [ 1.60721757]
  50. [ 1.58754631]
  51. [ 1.5909996]
  52. [ 1.60486746]
  53. [ 1.48532045]
  54. [ 1.56199286]
  55. [ 1.42685994]
  56. [ 1.42218871]
  57. [ 1.42513733]
  58. [ 1.42560821]
  59. [ 1.42627889]
  60. [ 1.42422753]
  61. [ 1.42382572]
  62. [ 1.42222283]
  63. [ 1.41752142]
  64. [ 1.41257471]
  65. [ 1.41516891]
  66. [ 1.41390184]
  67. [ 1.58426403]
  68. [ 1.53824457]
  69. [ 1.45517987]
  70. [ 1.500387]
  71. [ 1.48309551]
  72. [ 1.51026016]
  73. [ 1.52573794]
  74. [ 1.53639431]
  75. [ 1.35975534]
  76. [ 1.3949126]
  77. [ 1.41854269]
  78. [ 1.5371124]
  79. [ 1.5318818]
  80. [ 1.61626035]
  81. [ 1.46463971]
  82. [ 1.35377736]
  83. [ 1.3781526]
  84. [ 1.36485304]
  85. [ 1.35738739]
  86. [ 1.35879235]
  87. [ 1.35848317]
  88. [ 1.35674074]
  89. [ 1.35842602]
  90. [ 1.35549472]
  91. [ 1.40440556]
  92. [ 1.35685947]
  93. [ 1.35700859]
  94. [ 1.44201184]
  95. [ 1.43235995]
  96. [ 1.37015535]
  97. [ 1.35396728]
  98. [ 1.35545512]
  99. [ 1.35623892]
  100. [ 1.39545221]
  101. [ 1.35725555]
  102. [ 1.52999178]
  103. [ 1.52399418]
  104. [ 1.39365249]
  105. [ 1.36779515]
  106. [ 1.35482391]
  107. [ 1.40293755]
  108. [ 1.37213596]
  109. [ 1.35738371]
  110. [ 1.35808458]
  111. [ 1.35662849]
  112. [ 1.35528448]
  113. [ 1.35510845]
  114. [ 1.35379783]
  115. [ 1.35430934]
  116. [ 1.35312843]
  117. [ 1.35581243]
  118. [ 1.36879701]
  119. [ 1.41158962]
  120. [ 1.44027263]
  121. [ 1.44380821]
  122. [ 1.48272708]
  123. [ 1.51507127]
  124. [ 1.46605994]
  125. [ 1.61084145]
  126. [ 1.58922279]
  127. [ 1.46771218]
  128. [ 1.40289457]
  129. [ 1.34716878]
  130. [ 1.35043834]
  131. [ 1.35590544]
  132. [ 1.37653415]
  133. [ 1.34764272]
  134. [ 1.34831244]
  135. [ 1.34689904]
  136. [ 1.34150245]
  137. [ 1.33927252]
  138. [ 1.33978952]
  139. [ 1.3470568]
  140. [ 1.34433552]
  141. [ 1.34484056]
  142. [ 1.34160806]
  143. [ 1.3407761]
  144. [ 1.3424078]
  145. [ 1.3433431]
  146. [ 1.34328446]
  147. [ 1.33992925]
  148. [ 1.34388204]
  149. [ 1.34802088]
  150. [ 1.3453579]
  151. [ 1.3428265]
  152. [ 1.34329775]
  153. [ 1.34191156]
  154. [ 1.34611248]
  155. [ 1.37349663]
  156. [ 1.34815805]
  157. [ 1.34014992]
  158. [ 1.34521152]
  159. [ 1.34456372]
  160. [ 1.34089661]
  161. [ 1.34023757]
  162. [ 1.3410812]
  163. [ 1.33807578]
  164. [ 1.33572014]
  165. [ 1.34433535]
  166. [ 1.33505861]
  167. [ 1.33827504]
  168. [ 1.33755043]
  169. [ 1.38559783]
  170. [ 1.35527351]
  171. [ 1.33053597]
  172. [ 1.33701674]
  173. [ 1.33273647]
  174. [ 1.33668717]
  175. [ 1.33941937]
  176. [ 1.34060378]
  177. [ 1.3372182]
  178. [ 1.61340736]
  179. [ 1.59055412]
  180. [ 1.33505241]
  181. [ 1.60308339]
  182. [ 1.51156137]
  183. [ 1.35797843]
  184. [ 1.34580909]
  185. [ 1.48117895]
  186. [ 1.44494812]
  187. [ 1.35293003]
  188. [ 1.35665647]
  189. [ 1.37410369]
  190. [ 1.35666235]
  191. [ 1.33729064]
  192. [ 1.45931719]
  193. [ 1.55375605]
  194. [ 1.48339986]
  195. [ 1.35060715]
  196. [ 1.36146995]
  197. [ 1.34245541]
  198. [ 1.35342592]
  199. [ 1.35796042]
  200. [ 1.37098111]
  201. [ 1.34045319]
  202. [ 1.42147708]
  203. [ 1.365122]
  204. [ 1.4076879]
  205. [ 1.39762825]
  206. [ 1.34262013]
  207. [ 1.38706403]
  208. [ 1.33523713]
  209. [ 1.33186205]
  210. [ 1.33077059]
  211. [ 1.3324637]
  212. [ 1.33112122]
  213. [ 1.32952302]
  214. [ 1.33383435]
  215. [ 1.32954544]
  216. [ 1.33443469]
  217. [ 1.33090967]
  218. [ 1.33522262]
  219. [ 1.33175321]
  220. [ 1.49987289]
  221. [ 1.51376666]
  222. [ 1.4208718]
  223. [ 1.49241705]
  224. [ 1.36766608]
  225. [ 1.36990194]
  226. [ 1.33322159]
  227. [ 1.34836793]
  228. [ 1.34669257]
  229. [ 1.36690579]
  230. [ 1.37890552]
  231. [ 1.59037649]
  232. [ 1.60582728]
  233. [ 1.61743431]
  234. [ 1.62123338]
  235. [ 1.61336502]
  236. [ 1.60121318]
  237. [ 1.62107838]
  238. [ 1.41357384]
  239. [ 1.61966948]
  240. [ 1.51775743]
  241. [ 1.33704794]
  242. [ 1.37279934]
  243. [ 1.34484306]
  244. [ 1.3705884]
  245. [ 1.41262748]
  246. [ 1.44408315]
  247. [ 1.52046936]
  248. [ 1.38814136]
  249. [ 1.38882472]
  250. [ 1.35596408]
  251. [ 1.52776999]
  252. [ 1.55767315]
  253. [ 1.33500518]
  254. [ 1.33840795]
  255. [ 1.34727997]
  256. [ 1.43367698]
  257. [ 1.35595655]
  258. [ 1.34698186]
  259. [ 1.59583696]
  260. [ 1.374913]
  261. [ 1.60214431]
  262. [ 1.53554784]
  263. [ 1.49221176]
  264. [ 1.59822169]
  265. [ 1.35287993]
  266. [ 1.34985064]
  267. [ 1.34512204]
  268. [ 1.33554636]
  269. [ 1.33612458]
  270. [ 1.32905663]
  271. [ 1.32990288]
  272. [ 1.36225504]
  273. [ 1.59836396]
  274. [ 1.32984726]
  275. [ 1.33153792]
  276. [ 1.39786779]
  277. [ 1.3416728]
  278. [ 1.3547156]
  279. [ 1.3417874]
  280. [ 1.33787953]
  281. [ 1.42237594]
  282. [ 1.32939148]
  283. [ 1.34560785]
  284. [ 1.33542025]
  285. [ 1.32921129]
  286. [ 1.32924703]
  287. [ 1.32956219]
  288. [ 1.32953676]
  289. [ 1.32962066]
  290. [ 1.33064464]
  291. [ 1.32916515]
  292. [ 1.32946366]
  293. [ 1.33199463]
  294. [ 1.32940815]
  295. [ 1.33035788]
  296. [ 1.33158764]
  297. [ 1.33103393]
  298. [ 1.3312874]
  299. [ 1.32907548]
  300. [ 1.33131474]
  301. [ 1.33113065]
  302. [ 1.33056411]
  303. [ 1.54542979]
  304. [ 1.43053565]
  305. [ 1.44441014]
  306. [ 1.55239121]
  307. [ 1.37602661]
  308. [ 1.62125583]
  309. [ 1.36640902]
  310. [ 1.56636469]
  311. [ 1.33713086]
  312. [ 1.33348418]
  313. [ 1.33584004]
  314. [ 1.35366715]
  315. [ 1.39788942]
  316. [ 1.41189411]
  317. [ 1.57317611]
  318. [ 1.40385926]
  319. [ 1.61962342]
  320. [ 1.55777659]
  321. [ 1.5813632]
  322. [ 1.52487439]
  323. [ 1.44917861]
  324. [ 1.35809968]
  325. [ 1.35031112]
  326. [ 1.34328138]
  327. [ 1.3453355]
  328. [ 1.36096032]
  329. [ 1.34087397]