0

I have a text file that contain data like this (EURUSD quotes)

19710104,000000,0.53690,0.53690,0.53690,0.53690,1
19710105,000000,0.53660,0.53660,0.53660,0.53660,1
19710106,000000,0.53650,0.53650,0.53650,0.53650,1
19710107,000000,0.53680,0.53680,0.53680,0.53680,1
19710108,000000,0.53710,0.53710,0.53710,0.53710,1
19710111,000000,0.53710,0.53710,0.53710,0.53710,1
19710112,000000,0.53710,0.53710,0.53710,0.53710,1

I want to move some data to another file like

0.53690,0.53690,0.53690,0.53690

and add some difrent calculated number to each line like (Moving average and RSI, Stoch ...) so the file can be trained by Neural Network, final file must be like this

OPEN, HIGH, LOW, CLOSE, VOL, MA50, MA20, RSI14, StochMain, StochSignal,

so I need some hints

Biffen
  • 6,249
  • 6
  • 28
  • 36

2 Answers2

0

I strongly believe that you should open file, read line by line, explode it with ',' and then store every line to some kind of map, do some calculations and finally save it to another file.

http://php.net/manual/en/function.explode.php How to read a file line by line in php

Community
  • 1
  • 1
th7nder
  • 174
  • 1
  • 4
0

You should use the PHP functions fgetcsv and fputcsv. See working example below that you can tweak to your needs.

It assumes that your input values given are in the format OPEN, CLOSE, HIGH, LOW, VOL. Introduce RSI and Stochastic etc in the same way that the Moving Average works.

<?php

// Prepare variables
$row = 1;
$output = array();

// Attempt to open the file quotes.txt
if (($handle = fopen("quotes.txt", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

        $row++;

        // This part of the while loop will be hit for each line in the quotes.txt
        // $data is an array that contains the values of this line from the csv
        // $output is a new array that you are generating

        // Create a new sub-array in the output array (add a new line to the main output)
        $output[$row] = array();    

        // Add the third value of the input array to the start of the new array (the opening price)
        $output[$row][] = $data[2];

        // Add the fourth value of the input array to the new array (the closing price)
        $output[$row][] = $data[3];

        // Add the fifth value of the input array to the new array (the high price)
        $output[$row][] = $data[4];

        // Add the sixth value of the input array to the new array (the low price)
        $output[$row][] = $data[5];

        // Add the seventh value of the input array to the new array (the volume)
        $output[$row][] = $data[6];

        // Add moving average to the new array
        $output[$row][] = calculate_ma($output, $row);
    }
    fclose($handle);
}

// Create new file or open existing to save the output to
$handle = fopen('output.csv', 'w');

// Flatten the arrays and save the csv data
foreach ($output as $file) {
    $result = [];
    array_walk_recursive($file, function($item) use (&$result) {
        $result[] = $item;
    });
    fputcsv($handle, $result);
}

/**
* Calculate the value for the MA using the values in $output.
*/
function calculate_ma($output, $row) {

    // For this example we will just say that the MA is equal to the closing price of this period
    // and the previous four periods, divided by 5.
    $ma = $output[$row][1] + $output[$row-1][1] + $output[$row-2][1] + $output[$row-3][1] + $output[$row-4][1];
    $ma = $ma / 5;

    return $ma;
}

?>

The output of the above code, using the same input as you have pasted in your question, will be:

0.53690,0.53690,0.53690,0.53690,1,0.10738
0.53660,0.53660,0.53660,0.53660,1,0.2147
0.53650,0.53650,0.53650,0.53650,1,0.322
0.53680,0.53680,0.53680,0.53680,1,0.42936
0.53710,0.53710,0.53710,0.53710,1,0.53678
0.53710,0.53710,0.53710,0.53710,1,0.53682
0.53710,0.53710,0.53710,0.53710,1,0.53692

Bear in mind that the first four moving averages will be incorrect, as they do not have 5 periods of data from which to calculate the 5-period MA.

To calculate a larger MA (50-period) without a huge bunch of code, replace the MA function with:

function calculate_ma($output, $row) {
    $period = 50;

    for ($x = 0 ; $x < $period ; $x++){
        $ma = $ma + $output[$row-$x][1];
    }

    $ma = $ma / $period;

    return $ma;
}
Dan
  • 58
  • 5