Skip to content

Instantly share code, notes, and snippets.

@riterrani
Created September 4, 2014 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riterrani/e01a018432e709e721db to your computer and use it in GitHub Desktop.
Save riterrani/e01a018432e709e721db to your computer and use it in GitHub Desktop.
<?php
function endc( $array ) { return end( $array ); }
$file = fopen('bitstampUSD.csv', 'r');
$firstTime = 0;
$periods = array(
// "min5" => 300, // 5 minutes
// "min30" => 1800, // 30 minutes
// "hour1" => 3600, // 1 hour
// "hour6" => 21600, // 6 hour
// "hour12" => 43200, // 12 hour
"day1" => 86400, // 24 hour
);
$data = array();
$first = fgetcsv($file);
foreach($periods as $periodName => $period){
$data[$periodName] = array(
array(
"time" => $first[0] - ($first[0] % $period),
"date" => date("Y/m/d H:i:s",$first[0] - ($first[0] % $period)),
"open" => $first[1],
"close" => $first[1],
"min" => $first[1],
"max" => $first[1],
"volume" => $first[2] * $first[1],
"volume_btc" => $first[2],
)
);
}
while (($line = fgetcsv($file)) !== FALSE) {
foreach($periods as $periodName => $period){
$last = &$data[$periodName][count($data[$periodName])-1];
if($line[0] > ($last["time"] + $period)){
$data[$periodName][] = array(
"time" => $line[0] - ($line[0] % $period),
"date" => date("Y/m/d H:i:s",$line[0] - ($line[0] % $period)),
"open" => $line[1],
"close" => $line[1],
"min" => $line[1],
"max" => $line[1],
"volume" => $line[2] * $line[1],
"volume_btc" => $line[2],
);
}else{
$last["close"] = $line[1];
$last["volume"] += $line[2] * $line[1];
$last["volume_btc"] += $line[2];
if($line[1] < $last["min"]){
$last["min"] = $line[1];
}elseif($line[1] > $last["max"]){
$last["max"] = $line[1];
}
}
}
}
fclose($file);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment