我正在尝试使用 PHPs交易函数(可作为 PECL 扩展)来计算各种证券的移动平均收敛/发散 (MACD)。但是,返回的值似乎与我的计算不符。
考虑以下一组股票的收盘价:
$close = array (
0 => 459.99,
1 => 448.85,
2 => 446.06,
3 => 450.81,
4 => 442.8,
5 => 448.97,
6 => 444.57,
7 => 441.4,
8 => 430.47,
9 => 420.05,
10 => 431.14,
11 => 425.66,
12 => 430.58,
13 => 431.72,
14 => 437.87,
15 => 428.43,
16 => 428.35,
17 => 432.5,
18 => 443.66,
19 => 455.72,
20 => 454.49,
21 => 452.08,
22 => 452.73,
23 => 461.91,
24 => 463.58,
25 => 461.14,
26 => 452.08,
27 => 442.66,
28 => 428.91,
29 => 429.79,
30 => 431.99,
31 => 427.72,
32 => 423.2,
33 => 426.21,
34 => 426.98,
35 => 435.69,
36 => 434.33,
37 => 429.8,
38 => 419.85,
39 => 426.24,
40 => 402.8,
41 => 392.05,
42 => 390.53,
43 => 398.67,
44 => 406.13,
45 => 405.46,
46 => 408.38,
47 => 417.2,
48 => 430.12,
49 => 442.78,
50 => 439.29,
51 => 445.52,
52 => 449.98,
53 => 460.71,
54 => 458.66,
55 => 463.84,
56 => 456.77,
57 => 452.97,
58 => 454.74,
59 => 443.86,
60 => 428.85,
61 => 434.58,
62 => 433.26,
63 => 442.93,
64 => 439.66,
65 => 441.35,
);
基于这些数字,MACD、信号线和 MACD 直方图应显示为:
// MACD
Array
(
[0] => 8.275269504
[1] => 7.703378381
[2] => 6.416074757
[3] => 4.237519783
[4] => 2.552583325
[5] => 1.37888572
[6] => 0.102981491
[7] => -1.258401953
[8] => -2.07055819
[9] => -2.621842328
[10] => -2.32906674
[11] => -2.181632115
[12] => -2.402626273
[13] => -3.342121681
[14] => -3.530363136
[15] => -5.507471249
[16] => -7.851274229
[17] => -9.719367455
[18] => -10.42286651
[19] => -10.26016216
[20] => -10.06920961
[21] => -9.571919612
[22] => -8.369633492
[23] => -6.301635724
[24] => -3.599681509
[25] => -1.720148361
[26] => 0.269003232
[27] => 2.180173247
[28] => 4.508637809
[29] => 6.118020154
[30] => 7.722430594
[31] => 8.327453809
[32] => 8.403441185
[33] => 8.508406323
[34] => 7.625761844
[35] => 5.649949083
[36] => 4.494654765
[37] => 3.432989362
[38] => 3.333473854
[39] => 2.956662856
[40] => 2.762561216
)
// Signal line
Array
(
[0] => 3.037525869
[1] => 1.905652229
[2] => 1.058708435
[3] => 0.410640325
[4] => -0.152012994
[5] => -0.790034732
[6] => -1.338100413
[7] => -2.17197458
[8] => -3.30783451
[9] => -4.590141099
[10] => -5.756686181
[11] => -6.657381376
[12] => -7.339747023
[13] => -7.786181541
[14] => -7.902871931
[15] => -7.58262469
[16] => -6.786036054
[17] => -5.772858515
[18] => -4.564486166
[19] => -3.215554283
[20] => -1.670715865
[21] => -0.112968661
[22] => 1.45411119
[23] => 2.828779714
[24] => 3.943712008
[25] => 4.856650871
[26] => 5.410473066
[27] => 5.458368269
[28] => 5.265625568
[29] => 4.899098327
[30] => 4.585973432
[31] => 4.260111317
[32] => 3.960601297
)
// MACD histogram
Array
(
[0] => -5.108084059
[1] => -4.527494558
[2] => -3.387775176
[3] => -2.59227244
[4] => -2.250613279
[5] => -2.55208695
[6] => -2.192262723
[7] => -3.335496669
[8] => -4.543439719
[9] => -5.129226357
[10] => -4.666180327
[11] => -3.602780783
[12] => -2.729462587
[13] => -1.785738071
[14] => -0.466761561
[15] => 1.280988966
[16] => 3.186354544
[17] => 4.052710154
[18] => 4.833489398
[19] => 5.39572753
[20] => 6.179353673
[21] => 6.230988815
[22] => 6.268319404
[23] => 5.498674095
[24] => 4.459729177
[25] => 3.651755452
[26] => 2.215288778
[27] => 0.191580814
[28] => -0.770970803
[29] => -1.466108965
[30] => -1.252499579
[31] => -1.303448461
[32] => -1.198040081
)
请注意,MACD 需要计算 26 天移动平均线,因此虽然收盘价有 66 个不同的数据点,但计算得出的 MACD 值只有 41 个。此外,信号线(以及因此需要信号线的 MACD 直方图)需要 MACD 的 9 天移动平均线,因此信号线/MACD 直方图只有 31 次计算。
以上数据是在excel上计算出来的,是正确的MACD。我必须在 PHP 中计算 MACD 的简短脚本是:
<?php
// The array $close is the same as it is in the above example, omitted here for brevity.
$macd = trader_macd($close, 12, 26);
var_dump($macd);
?>
但是,var_dump($macd)
打印:
array(3) {
[0]=>
array(40) {
[24]=>
float(0)
[25]=>
float(4.415)
[26]=>
float(4.437)
[27]=>
float(3.652)
[28]=>
float(1.899)
[29]=>
float(0.574)
[30]=>
float(-0.295)
[31]=>
float(-1.314)
[32]=>
float(-2.457)
[33]=>
float(-3.085)
[34]=>
float(-3.48)
[35]=>
float(-3.055)
[36]=>
float(-2.796)
[37]=>
float(-2.923)
[38]=>
float(-3.782)
[39]=>
float(-3.903)
[40]=>
float(-5.822)
[41]=>
float(-8.118)
[42]=>
float(-9.945)
[43]=>
float(-10.614)
[44]=>
float(-10.422)
[45]=>
float(-10.206)
[46]=>
float(-9.688)
[47]=>
float(-8.467)
[48]=>
float(-6.384)
[49]=>
float(-3.67)
[50]=>
float(-1.779)
[51]=>
float(0.219)
[52]=>
float(2.138)
[53]=>
float(4.473)
[54]=>
float(6.088)
[55]=>
float(7.697)
[56]=>
float(8.306)
[57]=>
float(8.385)
[58]=>
float(8.493)
[59]=>
float(7.613)
[60]=>
float(5.639)
[61]=>
float(4.485)
[62]=>
float(3.425)
[63]=>
float(3.327)
}
[1]=>
array(40) {
[24]=>
float(4.437)
[25]=>
float(3.652)
[26]=>
float(1.899)
[27]=>
float(0.574)
[28]=>
float(-0.295)
[29]=>
float(-1.314)
[30]=>
float(-2.457)
[31]=>
float(-3.085)
[32]=>
float(-3.48)
[33]=>
float(-3.055)
[34]=>
float(-2.796)
[35]=>
float(-2.923)
[36]=>
float(-3.782)
[37]=>
float(-3.903)
[38]=>
float(-5.822)
[39]=>
float(-8.118)
[40]=>
float(-9.945)
[41]=>
float(-10.614)
[42]=>
float(-10.422)
[43]=>
float(-10.206)
[44]=>
float(-9.688)
[45]=>
float(-8.467)
[46]=>
float(-6.384)
[47]=>
float(-3.67)
[48]=>
float(-1.779)
[49]=>
float(0.219)
[50]=>
float(2.138)
[51]=>
float(4.473)
[52]=>
float(6.088)
[53]=>
float(7.697)
[54]=>
float(8.306)
[55]=>
float(8.385)
[56]=>
float(8.493)
[57]=>
float(7.613)
[58]=>
float(5.639)
[59]=>
float(4.485)
[60]=>
float(3.425)
[61]=>
float(3.327)
[62]=>
float(2.951)
[63]=>
float(2.758)
}
[2]=>
array(40) {
[24]=>
float(-4.437)
[25]=>
float(0.763)
[26]=>
float(2.538)
[27]=>
float(3.079)
[28]=>
float(2.194)
[29]=>
float(1.888)
[30]=>
float(2.162)
[31]=>
float(1.771)
[32]=>
float(1.023)
[33]=>
float(-0.03)
[34]=>
float(-0.684)
[35]=>
float(-0.133)
[36]=>
float(0.986)
[37]=>
float(0.98)
[38]=>
float(2.04)
[39]=>
float(4.215)
[40]=>
float(4.122)
[41]=>
float(2.496)
[42]=>
float(0.477)
[43]=>
float(-0.408)
[44]=>
float(-0.734)
[45]=>
float(-1.738)
[46]=>
float(-3.303)
[47]=>
float(-4.798)
[48]=>
float(-4.605)
[49]=>
float(-3.889)
[50]=>
float(-3.917)
[51]=>
float(-4.254)
[52]=>
float(-3.95)
[53]=>
float(-3.224)
[54]=>
float(-2.218)
[55]=>
float(-0.688)
[56]=>
float(-0.187)
[57]=>
float(0.772)
[58]=>
float(2.854)
[59]=>
float(3.127)
[60]=>
float(2.214)
[61]=>
float(1.159)
[62]=>
float(0.474)
[63]=>
float(0.569)
}
}
如上所述,PHP 返回一个包含 3 个数组的数组。该文档几乎没有提示每个数组是什么,仅说明 MACD 函数
返回一个包含计算数据的数组,如果失败则返回 false。
用户贡献的注释(尽管得分为 -1)添加第一个数组(索引 0)是 MACD 值,第二个数组(索引 1)是信号值,最后一个数组(索引 2)是分歧值。
即使用户注释为真,返回的数组也与我的计算不匹配(我知道这是正确的)。为什么trader_macd()
返回不正确的值/我做错了什么?
交易者函数的 PHP 文档仅包含每个函数的参数列表。作为旁注,我是否可以在任何地方获得有关交易者扩展的更详细的文档?这不是我第一次在扩展程序中遇到问题。