Skip to content

Commit

Permalink
remove previous_values and now use ema 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
romulodl committed May 28, 2020
1 parent 341f185 commit 042a7c1
Show file tree
Hide file tree
Showing 6 changed files with 299 additions and 139 deletions.
14 changes: 2 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,16 @@ or add `romulodl/macd` to your `composer.json`. Please check the latest version

```php
$macd = new Romulodl\Macd();
$macd->calculate(array $values, array $previous_values = [], int $short_period = 12, int $long_period = 26);
$macd->calculate(array $values, int $short_period = 12, int $long_period = 26);
//returns a float value
```

For example:
```php
$macd = new Romulodl\Macd();
$macd->calculate([10, 12, 14, 20, 14, 10, 11]);
$macd->calculate([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]);
```

#### What is `$previous_values` for?

The EMA calculation (used by the MACD) is based on the previous round of calculation. So the n round depends on n - 1 results.
Then what is n - 1 for the first calculation? If `$previous_values` is not available, it uses a simple moving average
for it. With `$previous_values` set, it will start the calculation of the EMA before and the result will be more
accurate (at least closest to what Trading view shows.)

It is recommended that you use the same ammount of `$values` and `$previous_values` for more accurate results. FOr example, send the previous 26 values when calculating a default MACD(12, 26).


## Why did you do this?

The PECL Trading extension is crap and not everyone wants to install it.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
],
"require": {
"romulodl/ema": "^1.3"
"romulodl/ema": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
22 changes: 11 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 5 additions & 25 deletions src/Macd.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,13 @@ public function __construct($ema = null) {
* Calculate the MACD based on this formula
* macd: (EMA($short_period) - EMA($long_period))
*/
public function calculate(
array $values,
array $previous_values = [],
int $short_period = 12,
int $long_period = 26
) : float
public function calculate(array $values, int $short_period = 12, int $long_period = 26) : float
{
if (empty($values)) {
throw new \Exception('[' . __METHOD__ . '] $values parameters is empty');
if (empty($values) || count($values) < $long_period) {
throw new \Exception('[' . __METHOD__ . '] $values parameters is invalid');
}

if (count($values) !== $long_period) {
throw new \Exception(
'[' . __METHOD__ . '] $values parameter has to contain ' . $long_period . ' prices'
);
}

$total_values = array_merge($previous_values, $values);
$short_prev_values = [];
if (!empty($previous_values) && $total_values >= $short_period * 2) {
$short_prev_values = array_slice($total_values, (-1 * $short_period) * 2, $short_period);
}

return round(
$this->ema->calculate(array_slice($values, (-1 * $short_period)), $short_prev_values) -
$this->ema->calculate($values, $previous_values),
2
);
return $this->ema->calculate($values, $short_period) -
$this->ema->calculate($values, $long_period);
}
}
103 changes: 13 additions & 90 deletions tests/testMacd.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public function testCalculateMacdWithInvalidValues(): void
$values = [
9310.73
];
//15/05/2020

$this->expectException(Exception::class);

Expand All @@ -37,102 +36,26 @@ public function testCalculateMacdWithInvalidValues(): void

public function testCalculateMacdWithDefaultValues(): void
{
$values = [
6825.29,
6838,
7120.06,
7483.35,
7504.11,
7534.01,
7694.17,
7774,
7735.75,
8784.20,
8624.76,
8830.52,
8975.18,
8900,
8873.98,
9022.78,
9148.27,
9995,
9807.49,
9550.67,
8719.53,
8561.09,
8808.71,
9305.91,
9786.80,
9310.73
];
//15/05/2020
$val = require(__DIR__ . '/values.php');
$values = [];
foreach ($val as $v) {
$values[] = $v[2];
}
$values = array_slice($values, -26);

$macd = new Macd();
$this->assertSame(393.77, $macd->calculate($values));
$this->assertSame(-83.84, round($macd->calculate($values), 2));
}

public function testCalculateMacdWithAllValues(): void
{
$previous_values = [
6673.41,
6733.76,
6353.64,
6230.95,
5876.27,
6391.08,
6407.10,
6641.51,
6791.63,
6729.80,
6851.10,
6768.90,
7325.73,
7192.90,
7357.66,
7278.65,
6857.82,
6874.18,
6900.52,
6834,
6864.11,
6616.89,
7098.15,
7022.44,
7242.42,
7118.03
];

$values = [
6825.29,
6838,
7120.06,
7483.35,
7504.11,
7534.01,
7694.17,
7774,
7735.75,
8784.20,
8624.76,
8830.52,
8975.18,
8900,
8873.98,
9022.78,
9148.27,
9995,
9807.49,
9550.67,
8719.53,
8561.09,
8808.71,
9305.91,
9786.80,
9310.73
];
//15/05/2020
$val = require(__DIR__ . '/values.php');
$values = [];
foreach ($val as $v) {
$values[] = $v[2];
}

$macd = new Macd();
$this->assertSame(494.57, $macd->calculate($values, $previous_values, 12, 26));
$this->assertSame(152.24, round($macd->calculate($values), 2));
}
}
Loading

0 comments on commit 042a7c1

Please sign in to comment.