Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
romulodl committed Jun 27, 2020
1 parent 2f6215f commit eb37822
Show file tree
Hide file tree
Showing 8 changed files with 367 additions and 363 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ssl.yml → .github/workflows/mfi.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: SSL
name: MFI

on:
push:
Expand Down
46 changes: 23 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
# SSL Channel
# Money Flow Index

Calculate the SSL Channel
Calculate the Money Flow Index (MFI) in pure PHP

## Instalation

```
composer require romulodl/ssl
composer require romulodl/mfi
```

or add `romulodl/ssl` to your `composer.json`. Please check the latest version in releases.
or add `romulodl/mfi` to your `composer.json`. Please check the latest version in releases.

## Usage

```php
$ssl = new Romulodl\Ssl();
$ssl->calculate(array $hlc_values); // [high, low, close]
//returns integer (1 for positive channel or -1 for negative channel)
$mfi = new Romulodl\Mfi();
$mfi->calculate(array $hlcv_values); // [high, low, close, volume]
//returns float
```

Example of use:
```php
$ssl = new Romulodl\Ssl();
$ssl->calculate([
[9950.00,9250.66,9786.80],
[9843.50,9100.00,9310.73],
[9585.00,9210.03,9374.99],
[9880.00,9317.16,9678.57],
[9958.67,9450.00,9731.10],
[9900.00,9462.00,9773.64],
[9838.00,9281.42,9508.11],
[9573.00,8812.20,9060.00],
[9259.38,8920.00,9166.40],
[9300.00,9076.90,9176.41],
[9294.44,8674.00,8711.37],
[8977.00,8623.38,8895.65],
[9011.82,8693.18,8837.05],
[9230.00,8811.00,9197.32],
$mfi = new Romulodl\Mfi();
$mfi->calculate([
[9950.00,9250.66,9786.80,602528.438],
[9843.50,9100.00,9310.73,504107.096],
[9585.00,9210.03,9374.99,287768.201],
[9880.00,9317.16,9678.57,300859.340],
[9958.67,9450.00,9731.10,363732.370],
[9900.00,9462.00,9773.64,376313.845],
[9838.00,9281.42,9508.11,334423.590],
[9573.00,8812.20,9060.00,461439.193],
[9259.38,8920.00,9166.40,276008.073],
[9300.00,9076.90,9176.41,201589.660],
[9294.44,8674.00,8711.37,339643.378],
[8977.00,8623.38,8895.65,302980.106],
[9011.82,8693.18,8837.05,275619.158],
[9230.00,8811.00,9197.32,272253.998],
]);
```

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "romulodl/ssl",
"description": "calculate the Ssl channel without the PECL trader extension",
"name": "romulodl/mfi",
"description": "calculate the Money Flow Index in pure PHP",
"license": "Apache-2.0",
"require-dev": {
"phpunit/phpunit": "^8.5"
Expand Down
31 changes: 15 additions & 16 deletions composer.lock

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

52 changes: 52 additions & 0 deletions src/Mfi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Romulodl;

class Mfi
{
/**
* Calculate the Money Flow Index
*
*/
public function calculate(array $hlcv_values) : float
{
if (empty($hlcv_values)) {
throw new \Exception('[' . __METHOD__ . '] values parameters is invalid');
}

$pos_money_flow = 0;
$neg_money_flow = 0;
$prev_typical_price = 0;
foreach ($hlcv_values as $val) {
if (!$this->isValidValue($val)) {
throw new \Exception('[' . __METHOD__ . '] invalid HLC value');
}

$typical_price = $this->getTypicalPrice($val[0], $val[1], $val[2]);

if ($typical_price > $prev_typical_price) {
$pos_money_flow += $typical_price * $val[3];
} elseif ($typical_price < $prev_typical_price) {
$neg_money_flow += $typical_price * $val[3];
}

$prev_typical_price = $typical_price;
}

return 100 - 100/(1 + $pos_money_flow / $neg_money_flow);
}

private function getTypicalPrice($high, $low, $close) {
return ($high + $low + $close) / 3;
}

private function isValidValue($values) : bool
{
return is_array($values) &&
count($values) === 4 &&
is_numeric($values[0]) &&
is_numeric($values[1]) &&
is_numeric($values[2]) &&
is_numeric($values[3]);
}
}
48 changes: 0 additions & 48 deletions src/Ssl.php

This file was deleted.

21 changes: 11 additions & 10 deletions tests/SslTest.php → tests/MfiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,34 @@
declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use Romulodl\Ssl;
use Romulodl\Mfi;

final class SslTest extends TestCase
final class MfiTest extends TestCase
{
public function testCalculateWithWrongTypeValues(): void
{
$this->expectException(Exception::class);

$ssl = new Ssl();
$ssl->calculate(['poop']);
$mfi = new Mfi();
$mfi->calculate(['poop']);
}

public function testCalculateWithEmptyValues(): void
{
$this->expectException(Exception::class);

$ssl = new Ssl();
$ssl->calculate([]);
$mfi = new Mfi();
$mfi->calculate([]);
}

public function testCalculateWithValidValues(): void
{
$values = require(__DIR__ . '/values.php');
$val = require(__DIR__ . '/values.php');
$values = array_slice($val, -14);

$ssl = new Ssl();
$ssl = $ssl->calculate($values);
$mfi = new Mfi();
$mfi = $mfi->calculate($values);

$this->assertSame(-1, $ssl);
$this->assertSame(41.56, round($mfi, 2));
}
}
Loading

0 comments on commit eb37822

Please sign in to comment.