Hi,

On Sat, 2018-06-23 at 17:15 +0200, Marcel Kelders wrote:
>  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?
> 
> 
Thanks for the question. The MFI index is one of those that have
"memories". Its result depends on previous values. As per the formula,
it's about negative and positive money flow in this case. It's not
quite clear from the investopedia.com, how that part is calculated. I
guess, the first values are imputed as positive money flow (as none was
known before) there. Thus, if you prepend every high, low, etc. arrays
with values of zero and set time period to 4, it should be identical to
the results on that page.

See more about the unstable periods here http://php.net/manual/en/funct
ion.trader-set-unstable-period.php  and on the linked TA-Lib doc page.
In that case also, the unstable period has to be set to zero,
obviously. The unstable period is also why it doesn't calculate
anything when you pass a period of 4, the data contains exactly 4
entries. If you ommit it, the period will be set to 2 by default, as
per trader_mfi() doc, that's what your current calculation did.
Depending on the exact index, those having memories will in most case
require "time_period < num_entries - unstable_period" to be valid.

I think this should solve your issue.

Regards

Anatol


Reply via email to