-1

我每分钟收集加密货币价格并将它们保存到数据库中。

有时我的系统会出现故障,这会导致我的价格数据出现缺口。

我发现了如何在两个时间戳之间获取历史价格。例如:

https://api.pro.coinbase.com/products/ALGO-GBP/candles?start=2021-05-13T02:20:06.0Z&end=2021-05-13T09:44:12.0Z&granularity=300

当系统重新启动时,我有一个 PHP 脚本来检查差距,然后构造上面的 URL 调用来获取价格。

这适用于大多数加密货币,但对于一些我没有返回任何数据,例如 ATOM-GBP。

经过一番调查,我发现这是因为对于这些我必须指定 ATOM-USD 或 ATOM-BTC。

这里的问题首先是知道使用哪个,其次,如何将返回的价格转换为相关时间戳的英镑。

这是我的代码段:

<?php

date_default_timezone_set('Europe/London'); // YOUR timezone, of the server

ini_set('memory_limit', '256M');

?>

<!DOCTYPE html>
<html>
<head>
   <link rel="stylesheet" href="style.css?v=<?=time();?>">
</head>

<body>

<H1> Missing Prices <?= date("Y-m-d H:i:s") ?> </H1>

<p>

<?php
error_reporting(E_ALL);
include 'cfg.php';

require_once ('vendor/autoload.php');

include ('db_conn.php');


use Coinbase\Wallet\Client as Client;
use Coinbase\Wallet\Configuration as Configuration;

$configuration = Configuration::apiKey($cbase_API_Key, $cbase_API_Secret);
$client = Client::create($configuration);

// List prices from local database

$price_sql = "

SELECT p1.ccode, 
       CONVERT_TZ(p1.tstamp,'Europe/London','UTC') ts1, 
       UNIX_TIMESTAMP(CONVERT_TZ(p1.tstamp,'Europe/London','UTC')) ut
FROM prices p1
WHERE p1.tstamp BETWEEN (NOW() - INTERVAL 5 DAY ) AND (NOW() - INTERVAL 3 DAY )
ORDER BY 1, 2

";

$stmt  = $dbh->prepare($price_sql);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_all(MYSQLI_ASSOC);

$num_rows = $result -> num_rows;

$prev_ut = -1;
$prev_ts = "";
$prev_curr = "";
$c = 0;

foreach ($data as $row)
{
   if ( $prev_ut == -1 )
   {
      $prev_ut = $row['ut'];
      $prev_ts = $row['ts1'];
      $prev_curr = $row['ccode'];
   }

   $ut_diff = $row['ut'] - $prev_ut;
   
   if ( $prev_curr != $row['ccode'] )
   {
      $ut_diff = 0;
   }

   // If the gap between current and previous row is over 5 minutes then we have a gap
   
   if ( $ut_diff > 300 )
   {
      echo '<H2> Missing data for ' . $row['ccode'] . "  between " . 
           $row['ts1'] . " and " . $prev_ts . " diff " . $ut_diff . "</H2>";

      // Build up URL

      $nxt_ts = DateTime::createFromFormat('Y-m-d H:i:s', $prev_ts );
      $pts = $nxt_ts->format('Y-m-d H:i:s');
            
      $strt = str_replace(' ', 'T', $prev_ts);
      $end = str_replace(' ', 'T', $row['ts1']);
      $url = "https://api.pro.coinbase.com/products/" . $row['ccode'] . "-GBP/candles?start=" . $strt . ".0Z&end=" . $end .  ".0Z&granularity=300";

      echo '<h3> ' . $url . '</h3>';
      echo '<table border="1">';
      echo '<tr>';
      echo '<th> No</th>';
      echo '<th> Currency </th>';
      echo '<th> Time </th>';
      echo '<th> Low </th>';
      echo '<th> High </th>';
      echo '<th> Open </th>';
      echo '<th> Close </th>';
      echo '<th> Volume </th>';
      echo '</tr>';

      // Use curl to get the data
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
      curl_setopt($ch, CURLOPT_TIMEOUT, 5);
      $prices_json = json_decode(curl_exec($ch), true);

      foreach($prices_json as $price )
      {
         $c++;
         $utime = gmdate( 'r', $price[0]);
         $low = $price[1];
         $high = $price[2];
         $open = $price[3];
         $close = $price[4];
         $volume = $price[5];
         echo '<tr>';
         echo '<td>' . $c . '</td>';
         echo '<td>' . $row['ccode']  . '</td>';
         echo '<td>' . $utime . '</td>';
         echo '<td>' . $low . '</td>';
         echo '<td>' . $high . '</td>';
         echo '<td>' . $open . '</td>';
         echo '<td>' . $close . '</td>';
         echo '<td>' . $volume . '</td>';
         echo '</tr>';
      }
      
   }

   echo '</table>';

   $prev_ut = $row['ut'];
   $prev_ts = $row['ts1'];
   $prev_curr = $row['ccode'];
}


?>

结果片段:

 Missing data for ATOM between 2021-05-13 09:44:12 and 2021-05-13 02:20:06 diff 26646

https://api.pro.coinbase.com/products/ATOM-GBP/candles?start=2021-05-13T02:20:06.0Z&end=2021-05-13T09:44:12.0Z&granularity=300

Warning: gmdate() expects parameter 2 to be integer, string given in fillpricegaps.php on line 172

No  Currency    Time    Low     High    Open    Close   Volume
409 ATOM        o   t   F   o   u

Missing data for BAL between 2021-05-13 09:44:15 and 2021-05-13 02:20:10 diff 26645

https://api.pro.coinbase.com/products/BAL-GBP/candles?start=2021-05-13T02:20:10.0Z&end=2021-05-13T09:44:15.0Z&granularity=300

Warning: gmdate() expects parameter 2 to be integer, string given in fillpricegaps.php on line 172

No  Currency    Time    Low     High    Open    Close   Volume
410 BAL     o   t   F   o   u

Missing data for BAND between 2021-05-13 09:44:13 and 2021-05-13 02:20:08 diff 26645

https://api.pro.coinbase.com/products/BAND-GBP/candles?start=2021-05-13T02:20:08.0Z&end=2021-05-13T09:44:13.0Z&granularity=300

No  Currency    Time    Low     High    Open    Close   Volume
411 BAND    Thu, 13 May 2021 09:40:00 +0000 10.4942 10.5993 10.5939 10.5248 1080.08
412 BAND    Thu, 13 May 2021 09:35:00 +0000 10.6262 10.7359 10.717  10.627  185.25
413 BAND    Thu, 13 May 2021 09:30:00 +0000 10.6525 10.8151 10.6525 10.7991 338.32
414 BAND    Thu, 13 May 2021 09:25:00 +0000 10.641  10.7716 10.7521 10.641  134.54
4

1 回答 1

1

你能不抓住 BTCUSD 和 BTCGBP 来找到美元和英镑之间的兑换率吗?

于 2021-05-18T11:42:39.370 回答