You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
219 lines
7.7 KiB
219 lines
7.7 KiB
<?php
|
|
require "../index.php";
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Redis;
|
|
use Workerman\Connection\AsyncTcpConnection;
|
|
use Workerman\Lib\Timer;
|
|
use Workerman\Worker;
|
|
use GatewayWorker\Lib\Gateway;
|
|
|
|
|
|
$symbols = \App\Models\ContractPair::query()->where('status', 1)->pluck('symbol');
|
|
// $interval = '1m'; //某时间线线 3m-5m-15m-30m-1h-2h-4h-6h-8h-12h-1d-3d-1w-1M
|
|
foreach ($symbols as $symbol) {
|
|
|
|
$period = '5min'; //1
|
|
if ($symbol == "BGV" || $symbol == "NEC" || $symbol == "RVM" || $symbol == "MPGB" || $symbol == "STE" ||
|
|
$symbol == "GLPE" || $symbol == "UNQ" || $symbol == "TZW" || $symbol == "GLPE" || $symbol == "YERT" ||
|
|
$symbol == "EODC" || $symbol == "BGK"
|
|
) {
|
|
continue;
|
|
}
|
|
// if($symbol != "BTC"){
|
|
// continue;
|
|
// }
|
|
|
|
$periodMap = [
|
|
'1min' => ['period' => '1min', 'seconds' => 60, 'interval' => '1m','lines'=>'1000'],
|
|
'5min' => ['period' => '5min', 'seconds' => 300, 'interval' => '5m','lines'=>'1000'],
|
|
'15min' => ['period' => '15min', 'seconds' => 900, 'interval' => '15m','lines'=>'1000'],
|
|
'30min' => ['period' => '30min', 'seconds' => 1800, 'interval' => '30m','lines'=>'1000'],
|
|
'60min' => ['period' => '60min', 'seconds' => 3600, 'interval' => '1h','lines'=>'1000'],
|
|
'1day' => ['period' => '1day', 'seconds' => 86400, 'interval' => '1d','lines'=>'1000'],
|
|
'1week' => ['period' => '1week', 'seconds' => 604800, 'interval' => '1w','lines'=>'280'],
|
|
'1mon' => ['period' => '1mon', 'seconds' => 2592000, 'interval' => '1M','lines'=>'60'],
|
|
];
|
|
$llimint = $periodMap[$period]['lines'];
|
|
$seconds = $periodMap[$period]['seconds'];
|
|
$interval = $periodMap[$period]['interval'];
|
|
$bass = new Binance();
|
|
|
|
|
|
// $aaaa = $bass->getPrice('BTCUSDT');
|
|
// var_dump($aaaa);
|
|
// exit();
|
|
$doittime = time();
|
|
$symbol = $symbol;
|
|
$symbol_get = $symbol . 'USDT';
|
|
$aa = $bass->bbgetKlines($symbol_get, $interval, $doittime - $periodMap[$period]['seconds'] * $llimint . '000', $doittime . '000', $llimint);
|
|
$kline_book_key = 'swap:' . $symbol . '_kline_book_' . $period;
|
|
$kline_book = Cache::store('redis')->get($kline_book_key);
|
|
if (!blank($kline_book)) {
|
|
for ($i = 0; $i < $llimint; $i++) {
|
|
array_pop($kline_book);
|
|
Cache::store('redis')->put($kline_book_key, $kline_book);
|
|
}
|
|
}
|
|
foreach ($aa as $k => $v) {
|
|
// var_dump($v);
|
|
// 1分钟基线
|
|
$cache_data = [
|
|
"id" => substr($v['0'], 0, 10),
|
|
// "id" => $v['0'],
|
|
"open" => $v['1'] * 1,
|
|
"close" => $v['4'] * 1,
|
|
"low" => $v['3'] * 1,
|
|
"high" => $v['2'] * 1,
|
|
"amount" => $v['7'] * 1,
|
|
'vol' => $v['7'] * 1,
|
|
'trade_turnover' => $v['2'] * 1,
|
|
'count' => $v['2'] * 1,
|
|
];
|
|
$last_close = $v['4'] * 1;
|
|
$kline_book_key = 'swap:' . $symbol . '_kline_book_' . $period;
|
|
$kline_book = Cache::store('redis')->get($kline_book_key);
|
|
if (!blank($kline_book)) {
|
|
$prev_id = $cache_data['id'] - $seconds;
|
|
$prev_item = array_last($kline_book, function ($value, $key) use ($prev_id) {
|
|
return $value['id'] == $prev_id;
|
|
});
|
|
// $cache_data['open'] = $prev_item['close'];
|
|
}
|
|
// var_dump($cache_data);
|
|
// echo time();
|
|
if ($cache_data['id'] == strtotime(date("Y-m-d H:i:00", $doittime))) {
|
|
// var_dump($cache_data);
|
|
$cache_data['open'] = $last_close;
|
|
$cache_on = $cache_data;
|
|
$cache_on['last_now'] = $cache_data['id'];
|
|
Cache::store('redis')->put('swap:' . $symbol . '_kline_' . $period, $cache_on);
|
|
}
|
|
|
|
if (blank($kline_book)) {
|
|
Cache::store('redis')->put($kline_book_key, [$cache_data]);
|
|
} else {
|
|
$last_item1 = array_pop($kline_book);
|
|
if ($last_item1['id'] == $cache_data['id']) {
|
|
echo 111;
|
|
array_push($kline_book, $cache_data);
|
|
} else {
|
|
echo 2222;
|
|
array_push($kline_book, $last_item1, $cache_data);
|
|
}
|
|
|
|
if (count($kline_book) > 3000) {
|
|
array_shift($kline_book);
|
|
}
|
|
Cache::store('redis')->put($kline_book_key, $kline_book);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
class Binance {
|
|
// private $base = 'https://fstream.binance.com';
|
|
// private $base = 'https://testnet.binancefuture.com'; //合约
|
|
private $base = 'https://api.binance.com'; //币币
|
|
private $api = '';
|
|
private $api_method = '';
|
|
|
|
|
|
/********** 基础信息 **********/
|
|
|
|
/**
|
|
* @title 获取所有交易对
|
|
* @description 获取币安的所有交易对
|
|
* @author 开发者
|
|
* @url /api/binance/exchangeInfo
|
|
* @method GET
|
|
*/
|
|
public function exchangeInfo() {
|
|
$this->api_method = "v3/exchangeInfo";
|
|
$this->api = $this->base . $this->api_method;
|
|
|
|
$res = $this->request($this->api);
|
|
return json_decode($res, true);
|
|
}
|
|
|
|
/**
|
|
* 请求
|
|
*/
|
|
private function request($url, $params = [], $method = "GET") {
|
|
$opt = [
|
|
"http" => [
|
|
"method" => $method,
|
|
"header" => "User-Agent: Mozilla/4.0 (compatible; PHP Binance API)\r\n",
|
|
],
|
|
];
|
|
|
|
$query = http_build_query($params, '', '&');
|
|
//$opt['http']['usurl'] = $url.'?'.$query;
|
|
$rqurl = 'http://8.217.104.10:5678/hkm.php?usurl='.base64_encode($url.'?'.$query);
|
|
//echo($rqurl);
|
|
$res = file_get_contents($rqurl);
|
|
//$context = stream_context_create($opt);
|
|
// $query = http_build_query($params, '', '&');
|
|
|
|
//$res = file_get_contents($rqurl, false, $context);
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* @title 获取某币最新币价
|
|
* @description 获取币安的某币最新价格
|
|
* @param $symbol string 例:BTCUSDT
|
|
* @author 开发者
|
|
* @url /api/binance/get_common_symbols
|
|
* @method GET
|
|
*
|
|
*/
|
|
public function getPrice($symbol) {
|
|
$param = ["symbol" => $symbol];
|
|
|
|
$this->api_method = "/fapi/v1/ticker/price";
|
|
$this->api = $this->base . $this->api_method;
|
|
|
|
$res = $this->request($this->api, $param);
|
|
return json_decode($res, true);
|
|
}
|
|
|
|
/**
|
|
* Notes:
|
|
* @param $symbol btcusdt
|
|
* @param $interval 时间段k线1m 3m-5m-15m-30m-1h-2h-4h-6h-8h-12h-1d-3d-1w-1M
|
|
* @param $start_time
|
|
* @param $end_time
|
|
* @param $limint
|
|
* @return mixed
|
|
* User: torsenli
|
|
* Date: 2022/10/26
|
|
* Time: 13:10
|
|
*/
|
|
public function bbgetKlines($symbol, $interval, $start_time, $end_time, $limint = 10) {
|
|
$param = ["symbol" => $symbol, "interval" => $interval, "startTime" => $start_time, "endTime" => $end_time, "limit" => $limint];
|
|
|
|
$this->api_method = "/api/v3/klines";
|
|
$this->api = $this->base . $this->api_method;
|
|
|
|
$res = $this->request($this->api, $param);
|
|
return json_decode($res, true);
|
|
}
|
|
|
|
|
|
public function ctgetklines($symbol, $interval, $start_time, $end_time, $limint = 10) {
|
|
|
|
$param = ["symbol" => $symbol, "interval" => $interval, "startTime" => $start_time, "endTime" => $end_time, "limit" => $limint];
|
|
|
|
$this->api_method = "/api/v3/klines";
|
|
$this->api = $this->base . $this->api_method;
|
|
|
|
$res = $this->request($this->api, $param);
|
|
return json_decode($res, true);
|
|
}
|
|
|
|
|
|
}
|
|
|