How can I effectively parse this CSV file using JavaScript?
1363085391,42.890000000000,5.432200000000
1363088879,47.570000000000,4.981800000000
1363120475,56.560000000000,1.768000000000
1363132522,53.000000000000,1.000000000000
1363214378,48.630000000000,4.000000000000
[...]
This displayed data provides a detailed history of bitcoin prices and trade volumes for the Canadian dollar. However, due to the extensive list containing every single trade, I am attempting to condense it from hundreds of daily data points to one each week. Essentially, I aim to simplify the data by aggregating volume and averaging price over specific time intervals. This approach is expected to enhance the appearance of the line in the provided line chart.
Unfortunately, the current script is ineffective; it operates under the assumption that the CSV file is a two-dimensional array when in reality, it seems to be one-dimensional. How can I modify the script to ensure accurate parsing of the CSV data?
function simplifyData(data_set) {
interval_length = 3600; // hourly intervals
last_price = 0;
idx = 0;
while (idx < data_set.length) {
// reset value for this interval
volume = 0;
price_sum = 0;
count = 0;
timestamp = data_set[idx]['timestamp'] + interval_length;
// get sums for this interval
while (data_set[idx]['timetamp'] < timestamp) {
volume += data_set[idx]['volume'];
price_sum += data_set[idx]['price'];
count++;
idx++;
if (idx >= data_set.length)
break;
}
// get average price
price = count > 0 ? price_sum / count : last_price;
last_price = price;
// add new row to monotized data array
monotized_data.append([
timestamp: timestamp,
volume: volume,
price: price
]);
}
}
// Format: time (UNIX timestamp), price, amount traded
// http://api.bitcoincharts.com/v1/csv/localbtcCAD.csv.gz
var complexCadCsv = "1363085391,42.890000000000,5.432200000000
1363088879,47.570000000000,4.981800000000
1363120475,56.560000000000,1.768000000000
1363132522,53.000000000000,1.000000000000
1363214378,48.630000000000,4.000000000000
...
";
var finalOutput = simplifyData(complexCadCsv);
$(".new_csv").append(finalOutput);
<!--<script src="https://rawgit.com/evanplaice/jquery-csv/master/src/jquery.csv.min.js"></script>-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>New simplified CSV</h1>
<div class="new_csv"></div>