Created
November 25, 2021 10:15
-
-
Save jorgecasas/53224538d7d6be87a9ccb4fcc4050baf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$intervals = ["1h","30m","15m","5m","1m"]; | |
$timestamp_start = strtotime('-2 days'); | |
//get list of symbols | |
$xinfo = json_decode(file_get_contents("https://api.binance.com/api/v1/exchangeInfo"), true); | |
$symbols = []; | |
foreach($xinfo["symbols"] as $sym) { | |
$symbols[] = $sym["symbol"]; | |
} | |
asort($symbols); | |
foreach($symbols as $sym) { | |
echo PHP_EOL.PHP_EOL .$sym.' -----------------------------'.PHP_EOL; | |
foreach ($intervals as $int){ | |
echo "Interval: ". $int . PHP_EOL; | |
$filename = "binance_".$sym."_".$int.".json"; | |
if (file_exists ( $filename )){ | |
echo PHP_EOL. "File ".$filename." exists. Skipping.".PHP_EOL; | |
continue; | |
} | |
$ts_ms = round(microtime(true)*1000); //current GMT UNIX time in miliseconds | |
$qty = 10; | |
$ts_last = $timestamp_start; | |
while ($ts_last < $ts_ms && $qty > 0){ | |
echo "UTC timestamp: ". gmdate("Y-m-d\TH:i:s\Z", $ts_last/1000) .PHP_EOL; | |
$string = file_get_contents("https://api.binance.com/api/v1/klines?symbol=".$sym."&interval=".$int."&startTime=".$ts_last); | |
if ( stripos( "200 OK",$http_response_header[0] ) === false ) { | |
print_r($http_response_header); | |
echo $string . PHP_EOL. $http_response_header[0]; exit; | |
} | |
$json = json_decode($string, true); | |
//json trim [ /not first/ | |
if ($ts_last != $timestamp_start) { | |
$string = substr($string, 1); | |
} | |
$qty = count($json); | |
$ts_last = $json[ $qty - 1 ][6]; | |
$ts_last++; | |
echo 'Quantity: '.$qty . PHP_EOL; | |
//json trim ] add, /not last, should have less than 500, 0.2% chance/ | |
if ($qty >= 500) { | |
$string = substr($string, 0, -1); | |
$string .= ",".PHP_EOL; | |
} | |
file_put_contents($filename, $string, FILE_APPEND | LOCK_EX); | |
echo PHP_EOL; | |
usleep(200000); | |
set_time_limit (60); | |
} | |
echo "Done! " . $filename . PHP_EOL; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment