策略思路
当股价的短期移动平均线上穿长期平均线,则全仓买入,保持满仓直至出现卖出信号;当股价的短期移动平均线下穿长期平均线,则全部卖出,保持空仓直至出现买入信号。
编程思路
- 获得行情数据
- 计算复权价格
- 创建交易信号
- 计算动态仓位
- 执行策略回测
程序运行结果
以天齐锂业的历史行情为例,将其上市一年后至今的数据作为样本,遍历短期[1-50]和长期[5-100]的均线数据。
参数 总资产
0 1-5 1.945691e+06
1 1-6 1.792746e+06
2 1-7 1.477179e+06
3 1-8 2.232109e+06
4 1-9 1.564631e+06
5 1-10 2.008678e+06
6 1-11 2.943406e+06
7 1-12 3.761954e+06
8 1-13 2.266948e+06
9 1-14 2.174012e+06
10 1-15 2.818679e+06
11 1-16 4.298873e+06
12 1-17 2.072009e+06
13 1-18 2.652133e+06
14 1-19 2.268937e+06
15 1-20 2.192922e+06
16 1-21 3.515629e+06
17 1-22 2.716420e+06
18 1-23 3.664102e+06
19 1-24 2.991172e+06
20 1-25 2.401649e+06
21 1-26 2.492089e+06
22 1-27 2.268127e+06
23 1-28 1.982600e+06
24 1-29 2.636149e+06
25 1-30 2.784285e+06
26 1-31 2.961171e+06
27 1-32 2.589515e+06
28 1-33 1.843293e+06
29 1-34 2.293695e+06
30 1-35 1.941964e+06
31 1-36 1.702358e+06
32 1-37 1.629671e+06
33 1-38 1.729131e+06
34 1-39 1.517199e+06
35 1-40 1.483546e+06
36 1-41 1.444332e+06
37 1-42 1.500976e+06
38 1-43 1.515400e+06
39 1-44 1.327069e+06
40 1-45 1.406203e+06
41 1-46 1.707250e+06
42 1-47 1.603647e+06
43 1-48 1.656368e+06
44 1-49 1.444016e+06
45 1-50 1.415228e+06
46 1-51 1.411444e+06
47 1-52 1.567291e+06
48 1-53 1.431678e+06
49 1-54 1.761459e+06
50 1-55 1.828223e+06
51 1-56 1.855062e+06
52 1-57 2.006486e+06
53 1-58 2.075254e+06
54 1-59 2.073672e+06
55 1-60 1.909274e+06
56 1-61 1.921723e+06
57 1-62 2.081094e+06
58 1-63 1.854276e+06
59 1-64 1.616009e+06
60 1-65 1.472742e+06
61 1-66 1.423118e+06
62 1-67 1.366027e+06
63 1-68 1.309566e+06
64 1-69 1.418786e+06
65 1-70 1.383138e+06
66 1-71 1.424437e+06
67 1-72 1.453731e+06
68 1-73 1.593327e+06
69 1-74 1.782078e+06
70 1-75 1.798621e+06
71 1-76 1.913154e+06
72 1-77 1.918001e+06
73 1-78 1.768608e+06
74 1-79 1.847948e+06
75 1-80 1.731261e+06
76 1-81 1.793749e+06
77 1-82 1.255853e+06
78 1-83 1.597775e+06
79 1-84 1.723813e+06
80 1-85 1.703327e+06
81 1-86 1.692554e+06
82 1-87 1.069071e+06
83 1-88 9.887971e+05
84 1-89 9.274659e+05
85 1-90 9.239604e+05
86 1-91 8.248628e+05
87 1-92 8.275980e+05
88 1-93 9.438226e+05
89 1-94 8.274207e+05
90 1-95 8.034367e+05
91 1-96 9.332358e+05
92 1-97 8.908192e+05
93 1-98 7.727504e+05
94 1-99 7.811221e+05
95 1-100 8.134282e+05
96 1-101 9.390567e+05
97 1-102 1.013196e+06
98 1-103 1.074453e+06
99 1-104 1.104123e+06
100 1-105 1.140698e+06
4600 49-72 2.166837e+06
4601 49-73 1.965120e+06
4602 49-74 1.930835e+06
4603 49-75 2.236667e+06
4604 49-76 2.550129e+06
4605 49-77 2.145390e+06
4606 49-78 1.837656e+06
4607 49-79 1.525379e+06
4608 49-80 1.634286e+06
4609 49-81 1.358086e+06
4610 49-82 1.205978e+06
4611 49-83 1.108610e+06
4612 49-84 1.208241e+06
4613 49-85 1.335457e+06
4614 49-86 1.324262e+06
4615 49-87 1.425597e+06
4616 49-88 1.396380e+06
4617 49-89 1.381030e+06
4618 49-90 1.499403e+06
4619 49-91 1.564278e+06
4620 49-92 1.602255e+06
4621 49-93 1.939945e+06
4622 49-94 1.953359e+06
4623 49-95 1.616149e+06
4624 49-96 1.585049e+06
4625 49-97 1.438583e+06
4626 49-98 1.482266e+06
4627 49-99 1.741671e+06
4628 49-100 1.741468e+06
4629 49-101 1.708907e+06
4630 49-102 1.573567e+06
4631 49-103 1.502618e+06
4632 49-104 1.327753e+06
4633 49-105 1.582946e+06
4634 49-106 1.300522e+06
4635 49-107 1.195365e+06
4636 49-108 1.143127e+06
4637 49-109 1.235092e+06
4638 49-110 1.165166e+06
4639 49-111 1.035079e+06
4640 49-112 1.021198e+06
4641 49-113 1.053032e+06
4642 49-114 1.071614e+06
4643 49-115 1.096191e+06
4644 49-116 1.144201e+06
4645 49-117 1.168022e+06
4646 49-118 1.106204e+06
4647 49-119 1.167273e+06
4648 49-120 1.146053e+06
4649 50-51 1.104497e+06
4650 50-52 1.235971e+06
4651 50-53 1.345079e+06
4652 50-54 2.277224e+06
4653 50-55 2.600369e+06
4654 50-56 2.371688e+06
4655 50-57 2.023186e+06
4656 50-58 1.752259e+06
4657 50-59 2.397109e+06
4658 50-60 2.390165e+06
4659 50-61 2.092186e+06
4660 50-62 1.779955e+06
4661 50-63 2.393042e+06
4662 50-64 3.484405e+06
4663 50-65 2.929429e+06
4664 50-66 2.243097e+06
4665 50-67 2.110794e+06
4666 50-68 2.680897e+06
4667 50-69 2.434380e+06
4668 50-70 2.276505e+06
4669 50-71 2.238400e+06
4670 50-72 2.590026e+06
4671 50-73 2.722325e+06
4672 50-74 2.897639e+06
4673 50-75 3.222603e+06
4674 50-76 2.598308e+06
4675 50-77 2.471996e+06
4676 50-78 1.996919e+06
4677 50-79 1.561181e+06
4678 50-80 1.545307e+06
4679 50-81 1.369229e+06
4680 50-82 1.276871e+06
4681 50-83 1.287765e+06
4682 50-84 1.453645e+06
4683 50-85 1.310949e+06
4684 50-86 1.358342e+06
4685 50-87 1.331484e+06
4686 50-88 1.341950e+06
4687 50-89 1.429486e+06
4688 50-90 1.413460e+06
4689 50-91 1.623038e+06
4690 50-92 1.792356e+06
4691 50-93 2.206920e+06
4692 50-94 2.026693e+06
4693 50-95 2.124875e+06
4694 50-96 1.689232e+06
4695 50-97 1.610484e+06
4696 50-98 1.553837e+06
4697 50-99 1.692873e+06
4698 50-100 1.717806e+06
4699 50-101 1.568938e+06
4700 50-102 1.413988e+06
4701 50-103 1.452046e+06
4702 50-104 1.270102e+06
4703 50-105 1.254049e+06
4704 50-106 1.222300e+06
4705 50-107 1.127080e+06
4706 50-108 9.528736e+05
4707 50-109 9.474388e+05
4708 50-110 1.019838e+06
4709 50-111 9.735766e+05
4710 50-112 1.035628e+06
4711 50-113 9.270701e+05
4712 50-114 9.625674e+05
4713 50-115 9.726050e+05
4714 50-116 8.766499e+05
4715 50-117 8.811681e+05
4716 50-118 8.636465e+05
4717 50-119 8.794319e+05
4718 50-120 8.511175e+05
其中,将参数分别设为ma13和ma16时,可获得最为陡峭的资金曲线,共带来827%的收益。若从上市一年后便持有资金,获得的股价收益仅为400%左右。相比之下,该策略能够显著提高投资收益。
最优参数为:
参数 总资产
1358 13-16 8.268498e+06
回测详情
最佳参数get√
总结
这一节,我们感受到了完整编写一套量化投资策略的有趣之处。若靠自己用试错法去寻找合适的参数,工作量之大如大海捞针,但借助Python和计算机的力量,寻找到合适的参数真的是小菜一碟。后面我们将继续尝试更多、更有实战意义的策略,敬请期待。
刺猬偷腥
2018年9月30日
网友评论