我有一个包含这样的数据的文本文件(EURUSD引用)
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
我想将一些数据移动到另一个文件,如
0.53690,0.53690,0.53690,0.53690
并为每一行添加一些不同的计算数字,如(移动平均线和RSI,Stoch ......),因此文件可以通过神经网络训练,最终文件必须是这样的
OPEN, HIGH, LOW, CLOSE, VOL, MA50, MA20, RSI14, StochMain, StochSignal,
所以我需要一些提示
答案 0 :(得分:0)
我坚信你应该打开文件,逐行阅读,用','将其展开,然后将每一行存储到某种地图,进行一些计算,最后将其保存到另一个文件。
http://php.net/manual/en/function.explode.php How to read a file line by line in php
答案 1 :(得分:0)
您应该使用PHP函数fgetcsv和fputcsv。请参阅下面的工作示例,您可以调整以满足您的需求。
它假设您给出的输入值的格式为OPEN,CLOSE,HIGH,LOW,VOL。以与移动平均线相同的方式引入RSI和随机指标等。
<?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;
}
?>
使用您在问题中粘贴的相同输入,上述代码的输出将为:
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
请记住,前四个移动平均线是不正确的,因为它们没有5个周期的数据来计算5周期MA。
要在没有大量代码的情况下计算更大的MA(50个周期),请将MA函数替换为:
function calculate_ma($output, $row) {
$period = 50;
for ($x = 0 ; $x < $period ; $x++){
$ma = $ma + $output[$row-$x][1];
}
$ma = $ma / $period;
return $ma;
}