Hi All,

I'm integrating PECL trader PHP https://pecl.php.net/package/trader
I'm using a simple indicator MFI.
http://php.net/manual/en/function.trader-gmfi.php
For input of test data I use the example given on;
https://www.investopedia.com/terms/m/mfi.asp
---------------------------
Money Flow Index Calculation Example While a 14-day period is typically
used in calculating the MFI, for simplicity's sake, below is a four-day
example. Assume a stock's high, low and closing prices for four days are
listed along with volume as:

Day one: high = $24.60, low = $24.20, closing = $24.28, volume = 18,000
shares
Day two: high = $24.48, low = $24.24, closing = $24.33, volume = 7,200
shares
Day three: high = $24.56, low = $23.43, closing = $24.44, volume = 12,000
shares
Day four: high = $25.16, low = $24.25, closing = $25.05, volume = 20,000
shares

Using the above formula, the typical prices are:
Day one = $24.36
Day two = $24.35
Day three = $24.14
Day four = $24.82

Raw money flow for each day is:
Day one = $24.36 x 18,000 = 438,487
Day two = $24.35 x 7,200 = 175,323
Day three = $24.56 x 12,000 = 289,736
Day four = $25.16 x 20,000 = 496,400

Money flows are:
Positive money flow = 438,487 + 496,400 = 934,887
Negative money flow = 175,323 + 289,736 = 465,059

Money flow ratio = 934,887 / 465,059 = 2.01
Money flow index = 100 - 100 / (1 + 2.01) = 100 - 33.22 = 66.78
---------------------------


The php code I use.

// Get the values from the storage
$data = $db->get_latest_HLCV(   $indicator['pair'],
                                    $indicator['exchange'],
                                    $indicator['interval'],
                                    $indicator['period']
                                );
// Parse the storage data to data which is good for trader mfi
    foreach ($data as $key => $value) {
        $datahigh[] = floatval($value[1]);      // 1 because timestam is on
pos 0.
        $datalow[]  = floatval($value[2]);
        $dataclose[]  = floatval($value[3]);
        $datavolume[]  = floatval($value[4]);
    }

// Convert it all to an CSV array for debuging purpopes only
    $tmpdata = $db->convert_array_to_scv($data);
    var_dump($tmpdata);

// Show the content of the input, output
    var_dump($datahigh);
    var_dump($datalow);
    var_dump($dataclose);
    var_dump($datavolume);
    $mfi = trader_mfi($datahigh, $datalow,$dataclose,$datavolume);
    var_dump($mfi);




Which give me the following output

All data together (for easy porpose only)

string(139)
1529735640,24.60,24.20,24.28,18000
1529735700,24.48,24.24,24.33,7200
1529735760,24.56,23.43,24.44,12000
1529735820,25.15,24.25,25.05,20000

Array Data High

array(4) {
  [0]=>
  float(24.6)
  [1]=>
  float(24.48)
  [2]=>
  float(24.56)
  [3]=>
  float(25.15)
}

Array data Low

array(4) {
  [0]=>
  float(24.2)
  [1]=>
  float(24.24)
  [2]=>
  float(23.43)
  [3]=>
  float(24.25)
}

array data close

array(4) {
  [0]=>
  float(24.28)
  [1]=>
  float(24.33)
  [2]=>
  float(24.44)
  [3]=>
  float(25.05)
}

Array data volume

array(4) {
  [0]=>
  float(18000)
  [1]=>
  float(7200)
  [2]=>
  float(12000)
  [3]=>
  float(20000)
}

The the result of the MFI according to trader_MFI is

array(2) {
  [2]=>
  float(0)
  [3]=>
  float(63.146)
}

But according to investopedia this should be 66.78.

Q1, Is there a explanation why there are differences?
Q2 If I add the period as argument to the trader_mfi function with value 4

$mfi = trader_mfi($datahigh, $datalow,$dataclose,$datavolume,4);

the mfi cannot be calculated. Does somebody knows why?

Regards,

Reply via email to