From 042a7c10fb71f69c382564bf61212cc2428f438e Mon Sep 17 00:00:00 2001 From: Romulo De Lazzari Date: Fri, 29 May 2020 00:33:26 +0100 Subject: [PATCH] remove previous_values and now use ema 2.0 --- README.md | 14 +-- composer.json | 2 +- composer.lock | 22 ++-- src/Macd.php | 30 +---- tests/testMacd.php | 103 +++-------------- tests/values.php | 267 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 299 insertions(+), 139 deletions(-) create mode 100644 tests/values.php diff --git a/README.md b/README.md index 4ff7cf0..b562cdb 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/composer.json b/composer.json index 2a75666..f7ad657 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ } ], "require": { - "romulodl/ema": "^1.3" + "romulodl/ema": "^2.0" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index efc208d..31590c0 100644 --- a/composer.lock +++ b/composer.lock @@ -4,20 +4,20 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f1de209378e6602e3d56189143351ae1", + "content-hash": "5c9635ff356b7c9297536da4592a4a82", "packages": [ { "name": "romulodl/ema", - "version": "v1.3.0", + "version": "v2.0.0", "source": { "type": "git", "url": "https://github.com/romulodl/ema.git", - "reference": "1636dd151184c663407b2ae9c3c57aadbcdb9399" + "reference": "97bd9efdf32d866b23d78e2596b2a4b1aabc7af9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/romulodl/ema/zipball/1636dd151184c663407b2ae9c3c57aadbcdb9399", - "reference": "1636dd151184c663407b2ae9c3c57aadbcdb9399", + "url": "https://api.github.com/repos/romulodl/ema/zipball/97bd9efdf32d866b23d78e2596b2a4b1aabc7af9", + "reference": "97bd9efdf32d866b23d78e2596b2a4b1aabc7af9", "shasum": "" }, "require-dev": { @@ -40,7 +40,7 @@ } ], "description": "EMA without the PECL trader extension", - "time": "2020-05-19T23:23:45+00:00" + "time": "2020-05-28T23:20:25+00:00" } ], "packages-dev": [ @@ -715,16 +715,16 @@ }, { "name": "phpunit/phpunit", - "version": "8.5.4", + "version": "8.5.5", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "8474e22d7d642f665084ba5ec780626cbd1efd23" + "reference": "63dda3b212a0025d380a745f91bdb4d8c985adb7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/8474e22d7d642f665084ba5ec780626cbd1efd23", - "reference": "8474e22d7d642f665084ba5ec780626cbd1efd23", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/63dda3b212a0025d380a745f91bdb4d8c985adb7", + "reference": "63dda3b212a0025d380a745f91bdb4d8c985adb7", "shasum": "" }, "require": { @@ -794,7 +794,7 @@ "testing", "xunit" ], - "time": "2020-04-23T04:39:42+00:00" + "time": "2020-05-22T13:51:52+00:00" }, { "name": "sebastian/code-unit-reverse-lookup", diff --git a/src/Macd.php b/src/Macd.php index 3d02322..640a4f3 100644 --- a/src/Macd.php +++ b/src/Macd.php @@ -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); } } diff --git a/tests/testMacd.php b/tests/testMacd.php index db88274..efdfa4b 100644 --- a/tests/testMacd.php +++ b/tests/testMacd.php @@ -27,7 +27,6 @@ public function testCalculateMacdWithInvalidValues(): void $values = [ 9310.73 ]; - //15/05/2020 $this->expectException(Exception::class); @@ -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)); } } diff --git a/tests/values.php b/tests/values.php new file mode 100644 index 0000000..62d060d --- /dev/null +++ b/tests/values.php @@ -0,0 +1,267 @@ +