2016-10-16 31 views
0

我有一個包含這樣的數據(EURUSD報價)使用數據的TXT文件

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, 

所以我需要一些提示

+1

這是一個CSV文件,所以使用CSV庫。 – Biffen

+0

這將有助於從每一行刪除日期和時間,thnx @Biffen –

回答

0

你應該使用PHP函數fgetcsvfputcsv。看到下面的工作示例,您可以調整您的需求。

它假定您給出的輸入值的格式爲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; 
} 
+0

@Samih Abdelli? – Dan

+0

Awsome工作,完全解決問題 –

+0

測試和工作完全驚人,非常感謝你 –

0

我堅信你應該打開文件,逐行閱讀,用''分解,然後存儲每一行​​到某種地圖,做一些計算並最終保存到另一個文件。

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

+0

,可以解決我的一些問題,但當我試圖計算前50或20行的平均值時,我感到困惑 –

+0

@SamihAbdelli從如何開始數組中的第0個元素到第49個,在每個循環中向變量添加值,然後除以50? – th7nder

+0

會嘗試它並使用CSV庫作爲Biffen說去除日期和時間, –