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.
426 lines
18 KiB
426 lines
18 KiB
<?php
|
|
/*
|
|
* @Descripttion:
|
|
* @version:
|
|
* @Author: GuaPi
|
|
* @Date: 2021-07-29 10:40:49
|
|
* @LastEditors: GuaPi
|
|
* @LastEditTime: 2021-08-09 17:43:48
|
|
*/
|
|
|
|
namespace App\Admin\Forms;
|
|
|
|
use App\Models\ContractPair;
|
|
use Dcat\Admin\Widgets\Form;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Redis;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
|
class ContractRisk extends Form
|
|
{
|
|
/**
|
|
* Handle the form request.
|
|
*
|
|
* @param array $input
|
|
*
|
|
* @return Response
|
|
*/
|
|
public function handle(array $input)
|
|
{
|
|
$dotime = time();
|
|
$now_min_s = date('s', $dotime);
|
|
|
|
|
|
// if ($now_min_s >= 45) {
|
|
// return $this->error('最后5秒不给更新');
|
|
// }
|
|
// if ($now_min_s <= 5) {
|
|
// return $this->error('开始5秒不给更新');
|
|
// }
|
|
|
|
$symbols = ContractPair::query()->pluck('symbol');
|
|
// $PlatformCoin = config('coin.exchange_symbols');
|
|
// foreach ($PlatformCoin as $val => $key) {
|
|
// $PlatformCoinNew[] = $val;
|
|
// }
|
|
foreach ($symbols as $symbol) {
|
|
if (!empty($input[$symbol])) {
|
|
//获取传过来的数据
|
|
$data = $input[$symbol];
|
|
//获取历史数据
|
|
$risk_key = 'fkJson:' . $symbol . '/USDT';
|
|
$old_data = json_decode(Redis::get($risk_key), true);
|
|
//第一次开启
|
|
if($data['enabled'] == 1 && $old_data['enabled'] == 0){
|
|
$airCoins = \App\Models\Klike::query()->where('air_coin',$symbol)->get()->toArray();
|
|
$timmm = ['1min','5min','15min','30min','60min','1day','1week','1mon'];
|
|
foreach ($timmm as $key => $value) {
|
|
|
|
// if(!empty($airCoins)){
|
|
// $risk_key_d = 'swap:' . $airCoins[0]['like_coin'] . '_kline_'.$value;
|
|
// $zheng = $airCoins[0]['percent'];
|
|
// }else{
|
|
$risk_key_d = 'swap:' . $symbol . '_kline_'.$value;
|
|
$zheng = 1;
|
|
// }
|
|
|
|
$old_data_d = Cache::store('redis')->get($risk_key_d);
|
|
$data['hold'][$value]['price']=$old_data_d['open']*$zheng;
|
|
$data['hold'][$value]['time']=$old_data_d['id'];
|
|
|
|
}
|
|
$data['xxx']=1;
|
|
unset($airCoins);
|
|
}else if($data['enabled'] == 1 && $old_data['enabled'] == 1){
|
|
//第二次修改
|
|
$airCoins = \App\Models\Klike::query()->where('air_coin',$symbol)->get()->toArray();
|
|
$timmm = ['1min','5min','15min','30min','60min','1day','1week','1mon'];
|
|
foreach ($timmm as $key => $value) {
|
|
|
|
// if(!empty($airCoins)){
|
|
// $risk_key_d = 'swap:' . $airCoins[0]['like_coin'] . '_kline_'.$value;
|
|
// $zheng = $airCoins[0]['percent'];
|
|
// }else{
|
|
$risk_key_d = 'swap:' . $symbol . '_kline_'.$value;
|
|
$zheng = 1;
|
|
// }
|
|
|
|
$old_data_d = Cache::store('redis')->get($risk_key_d);
|
|
$data['hold'][$value]['price']=$old_data_d['open']*$zheng;
|
|
$data['hold'][$value]['time']=$old_data_d['id'];
|
|
|
|
}
|
|
$data['xxx']=$zheng;
|
|
unset($airCoins);
|
|
}elseif($data['enabled'] == 0 && $old_data['enabled'] == 1){
|
|
//关闭
|
|
$airCoins = \App\Models\Klike::query()->where('air_coin',$symbol)->get()->toArray();
|
|
$timmm = ['1min','5min','15min','30min','60min','1day','1week','1mon'];
|
|
foreach ($timmm as $key => $value) {
|
|
|
|
$risk_key_d = 'swap:' . $symbol . '_kline_'.$value;
|
|
$zheng = 1;
|
|
$old_data_d = Cache::store('redis')->get($risk_key_d);
|
|
$data['hold'][$value]['price']=$old_data_d['open']*$zheng;
|
|
$data['hold'][$value]['time']=$old_data_d['id'];
|
|
|
|
}
|
|
$data['xxx']=3;
|
|
unset($airCoins);
|
|
}else{
|
|
$data=$old_data;
|
|
$data['enabled'] = 0;
|
|
}
|
|
|
|
$data['coin'] = $symbol;
|
|
// var_dump($data);
|
|
Redis::set($risk_key, json_encode($data));
|
|
}
|
|
}
|
|
|
|
// return $this->error('Your error message.');
|
|
|
|
return $this->success('Processed successfully.');
|
|
}
|
|
|
|
/**
|
|
* Notes: 平台币风控处理不走队列
|
|
* @param $coin 币名称
|
|
* @param $data 风控数据
|
|
* User: torsenli
|
|
* Date: 2022/3/20
|
|
* Time: 22:12
|
|
*/
|
|
public function PlatformCurrencyRiskControlTotalProcessing($coin, $data,$dotime)
|
|
{
|
|
$now_time = strtotime(date('Y-m-d H:i', $dotime) . ":00");
|
|
$now_min = date('i', $dotime);
|
|
$now_min_s = date('s', $dotime);
|
|
$decimal = 100000;
|
|
|
|
if ($now_min_s >= 55) {
|
|
return "最后5秒不给更新";
|
|
}
|
|
|
|
$symbols = config('coin.exchange_symbols');
|
|
|
|
$coins = [];
|
|
$kk = 1;
|
|
foreach ($symbols as $symbol => $model) {
|
|
$coins[$symbol] = $model;
|
|
$kk++;
|
|
}
|
|
|
|
// todo 测试固定model
|
|
$model = $coins[$coin];
|
|
|
|
// $model = "App\Models\DataAaaaa";
|
|
|
|
// 更新当前一分钟数据
|
|
// 获取当前一分钟数据
|
|
$kline_book_key = 'swap:' . $coin . '_kline_book_1min';
|
|
|
|
$kline_book = Cache::store('redis')->get($kline_book_key);
|
|
$one_min_date = end($kline_book);
|
|
$one_min_date['Open'] = $one_min_date['open'];
|
|
$one_min_date['High'] = $one_min_date['high'];
|
|
$one_min_date['Low'] = $one_min_date['low'];
|
|
|
|
// \Log::info(json_encode($one_min_date));exit;
|
|
// $one_min_date = $model::query()->where('Date', $now_time)->where('is_1min', 1)->first()->toArray();
|
|
|
|
// 判断是涨还是跌
|
|
if ($one_min_date['Open'] > $data['count']) {
|
|
$UpsAndDowns = 1;
|
|
} else {
|
|
$UpsAndDowns = 0;
|
|
}
|
|
// 更新当前一分钟收盘数据
|
|
$this->UpdateTheCurrentMinute($UpsAndDowns, $data['count'], $now_time, $model, $one_min_date,$coin);
|
|
|
|
|
|
// 随机回调15-25条一分钟
|
|
$NumberOfRecovery = mt_rand(15, 25);
|
|
// $end_open = $model::query()->where('Date', $now_time + $NumberOfRecovery * 60)->where('is_1min', 1)->first()->toArray();
|
|
$end_open = $one_min_date;
|
|
|
|
// 判断修复,是涨还是跌
|
|
if ($end_open['Open'] > $data['count']) {
|
|
$high_price = $end_open['Open'];
|
|
$low_price = $data['count'];
|
|
} else {
|
|
$high_price = $data['count'];
|
|
$low_price = $end_open['Open'];
|
|
}
|
|
$open_price = $data['count'];
|
|
$close_price = $end_open['Open'];
|
|
|
|
// 生成随机修复分钟数
|
|
$period2_seconds = 60;
|
|
$periodCount = $NumberOfRecovery;
|
|
$periodsTrend = [];
|
|
$unit = custom_number_format(bcMath(($high_price - $low_price), $NumberOfRecovery - 1, '/', 8), 8);
|
|
$ups_downs_high = abs(floor($unit * $decimal * 10)); //高
|
|
$ups_downs_value = abs(floor($unit * 2 * $decimal)); //值
|
|
$ups_downs_low = floor($close_price); //低
|
|
|
|
$points = linspace($open_price, $close_price, $periodCount);
|
|
|
|
|
|
for ($i = 0; $i < $NumberOfRecovery; $i++) {
|
|
if ($end_open['Open'] > $data['count']) {
|
|
$thresholdValue = 60;
|
|
} else {
|
|
$thresholdValue = 40;
|
|
}
|
|
|
|
$periodsTrend[$now_time + ($i * $period2_seconds)] = mt_rand(1, 100) <= $thresholdValue ? 1 : 2;
|
|
$prev = $model::query()->where('is_1min', 1)->where('Date', $now_time + ($i * $period2_seconds))->first(); // 上一条K线
|
|
$close_point = $points[$i] * $decimal;
|
|
$current = $now_time + 60;
|
|
$open = $prev['Close'] * $decimal;
|
|
$up_or_down = mt_rand(1, 100);
|
|
$first = array_first($periodsTrend, function ($v, $k) use ($current, $period2_seconds) {
|
|
return $current >= $k && $current < ($k + $period2_seconds);
|
|
});
|
|
|
|
// dd($current);
|
|
|
|
if ($first == 1) {
|
|
$value = 60;
|
|
} else {
|
|
$value = 40;
|
|
}
|
|
|
|
// dd($ups_downs_low,$ups_downs_value,$ups_downs_high);
|
|
if ($up_or_down <= $value) {
|
|
// 涨
|
|
$close = mt_rand($close_point, $close_point + mt_rand($ups_downs_low, $ups_downs_high));
|
|
$high = mt_rand($close, $close + mt_rand($ups_downs_low, $ups_downs_value));
|
|
$low = mt_rand($close - mt_rand($ups_downs_low, $ups_downs_value), $close);
|
|
} else {
|
|
// 跌
|
|
$close = mt_rand($close_point - mt_rand($ups_downs_low, $ups_downs_high), $close_point);
|
|
$high = mt_rand($close, $close + mt_rand($ups_downs_value, $ups_downs_high));
|
|
$low = mt_rand($close - mt_rand($ups_downs_low, $ups_downs_value), $close);
|
|
}
|
|
$high = max($open, $close, $high, $low);
|
|
$low = min($open, $close, $high, $low);
|
|
|
|
// dd($open,$close,$high,$low,$close_point);
|
|
|
|
$open = $open / $decimal;
|
|
$close = $close / $decimal;
|
|
$high = $high / $decimal;
|
|
$low = $low / $decimal;
|
|
if ($i + 1 == $NumberOfRecovery) {
|
|
$close = $end_open['Open'];
|
|
}
|
|
$data_new = ['open' => $open, 'close' => $close, 'high' => $high, 'low' => $low];
|
|
// $data_new_after = ['Open' => $open, 'Close' => $close, 'High' => $high, 'Low' => $low,'time'=>date("Y-m-d H:i:s", $now_time + ($i * $period2_seconds)+60)];
|
|
$data_new_after = ['Open' => $open, 'Close' => $close, 'High' => $high, 'Low' => $low, 'LastClose' => $close];
|
|
// $cc['time'] = date("Y-m-d H:i:s", $now_time);
|
|
$model::query()->where('Date', $now_time + ($i * $period2_seconds) + 60)->where('is_1min', 1)->update($data_new_after);
|
|
// dump($data_new_after);
|
|
$this->OtherPeriod($now_time + ($i * $period2_seconds), $data_new, $model,$coin);
|
|
// dd($open, $close, $high, $low);
|
|
}
|
|
// dd($cc);
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
* Notes: 更新当前1分钟k线
|
|
* @param $UpsAndDowns 涨跌0为涨1为跌
|
|
* @param $UpdateValue 目标值
|
|
* @param $Timing 定时时间
|
|
* User: torsenli
|
|
* Date: 2022/3/20
|
|
* Time: 18:00
|
|
*/
|
|
public function UpdateTheCurrentMinute($UpsAndDowns, $UpdateValue, $Timing = null, $model, $one_min_date = null,$coin)
|
|
{
|
|
if ($UpsAndDowns > 0) {
|
|
$model::query()->where('Date', $Timing)->where('is_1min', 1)->update(['Low' => $UpdateValue, 'Close' => $UpdateValue, 'LastClose' => $UpdateValue]);
|
|
// $model::query()->where('Date', $Timing + 60)->where('is_1min', 1)->update(['Low' => $UpdateValue, 'Open' => $UpdateValue]);
|
|
|
|
$data_new_one = ['open' => $one_min_date['Open'], 'close' => $UpdateValue, 'high' => $one_min_date['High'], 'low' => $UpdateValue];
|
|
$this->OtherPeriod($Timing, $data_new_one, $model,$coin);
|
|
} else {
|
|
$model::query()->where('Date', $Timing)->where('is_1min', 1)->update(['High' => $UpdateValue, 'Close' => $UpdateValue, 'LastClose' => $UpdateValue]);
|
|
// $model::query()->where('Date', $Timing + 60)->where('is_1min', 1)->update(['High' => $UpdateValue, 'Open' => $UpdateValue]);
|
|
|
|
$data_new_one = ['open' => $one_min_date['Open'], 'close' => $UpdateValue, 'high' => $UpdateValue, 'low' => $one_min_date['Low']];
|
|
$this->OtherPeriod($Timing, $data_new_one, $model,$coin);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Notes: 更新其他时段值
|
|
* @param $now_time
|
|
* @param $data
|
|
* @param $model
|
|
* User: torsenli
|
|
* Date: 2022/3/22
|
|
* Time: 17:39
|
|
*/
|
|
public function OtherPeriod($now_time, $data, $model,$coin)
|
|
{
|
|
|
|
// $testtime = '1647959640';
|
|
// $testtime = strtotime('2022-03-22 22:29:00');
|
|
$testtime = $now_time;
|
|
$now_time = $testtime;
|
|
$now_min = date('i', $now_time);
|
|
|
|
|
|
$other_min['is_5min']['start'] = strtotime(date('Y-m-d H:', $testtime) . intval($now_min / 5) * 5 . ":00");
|
|
$other_min['is_5min']['end'] = strtotime(date('Y-m-d H:', $testtime) . intval($now_min / 5) * 5 . ":00") + 60 * 4;
|
|
$other_min['is_15min']['start'] = strtotime(date('Y-m-d H:', $testtime) . intval($now_min / 15) * 15 . ":00");
|
|
$other_min['is_15min']['end'] = strtotime(date('Y-m-d H:', $testtime) . intval($now_min / 15) * 15 . ":00") + 60 * 15 - 60;
|
|
$other_min['is_30min']['start'] = strtotime(date('Y-m-d H:', $testtime) . intval($now_min / 30) * 30 . ":00");
|
|
$other_min['is_30min']['end'] = strtotime(date('Y-m-d H:', $testtime) . intval($now_min / 30) * 30 . ":00") + 60 * 30 - 60;
|
|
$other_min['is_1h']['start'] = strtotime(date('Y-m-d H:', $testtime) . "00:00");
|
|
$other_min['is_1h']['end'] = strtotime(date('Y-m-d H:', $testtime) . "00:00") + 60 * 60 - 60;;
|
|
|
|
$kline_book_key = 'swap:' . $coin . '_kline_book_1min';
|
|
$kline_book = Cache::store('redis')->get($kline_book_key);
|
|
|
|
$other_time_date = end($kline_book);
|
|
// \Log::info();exit;
|
|
foreach ($other_min as $val => $key) {
|
|
$other_time_date['Open'] = $other_time_date['open'];
|
|
$other_time_date['High'] = $other_time_date['high'];
|
|
$other_time_date['Low'] = $other_time_date['low'];
|
|
$other_time_date['Close'] = $other_time_date['close'];
|
|
// 更新当前时间段最高最低
|
|
// $other_time_date = $model::query()->where('Date', $now_time)->where('is_1min', 1)->first()->toArray();
|
|
$up_date['High'] = $high = max($other_time_date['Open'], $other_time_date['High'], $other_time_date['Low'], $other_time_date['Close'], $data['open'], $data['close'], $data['high'], $data['low']);
|
|
$up_date['Low'] = $low = min($other_time_date['Open'], $other_time_date['High'], $other_time_date['Low'], $other_time_date['Close'], $data['open'], $data['close'], $data['high'], $data['low']);
|
|
// $test_date[] = ['high' => $high, 'low' => $low, 'oldhigh' => $other_time_date['High'], 'oldlow' => $other_time_date['Low']];
|
|
// 如果是时间段开始时间更新开盘
|
|
if ($now_time === $key['start']) {
|
|
$up_date['Open'] = $open = $data['open'];
|
|
$other_time_date['open'] = $up_date['Open'];
|
|
}
|
|
// 如果是时间段收盘时间更新收盘
|
|
if ($now_time === $key['end']) {
|
|
$up_date['Close'] = $close = $data['close'];
|
|
$up_date['LastClose'] = $close = $data['close'];
|
|
$other_time_date['close'] = $up_date['Close'];
|
|
}
|
|
|
|
// $up_date['time'] = date("Y-m-d H:i:s", $now_time);
|
|
// $up_date['type'] = $val;
|
|
// $up_date['kkyk'] = date("Y-m-d H:i:s", $now_time - 60);
|
|
// $up_date['k1kyk'] = date("Y-m-d H:i:s", $key['start']);
|
|
// $up_date['k2kyk'] = date("Y-m-d H:i:s", $key['end']);
|
|
$other_time_date['high'] = $up_date['High'];
|
|
$other_time_date['low'] = $up_date['Low'];
|
|
unset($kline_book[array_key_last($kline_book)]);
|
|
$kline_book = array_values($kline_book);
|
|
array_push($kline_book, $other_time_date);
|
|
Cache::store('redis')->put($kline_book_key,$kline_book);
|
|
// $model::query()->where('Date', $key['start'])->where($val, 1)->update($up_date);
|
|
// dump($up_date);
|
|
unset($up_date);
|
|
|
|
}
|
|
// dd($up_date);
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
* Build a form here.
|
|
*/
|
|
public function form()
|
|
{
|
|
$symbols = ContractPair::query()->pluck('symbol');
|
|
$index = 1;
|
|
foreach ($symbols as $symbol) {
|
|
$func = function () use ($symbol) {
|
|
// 获取风控任务
|
|
$risk_key = 'fkJson:' . $symbol . '/USDT';
|
|
$risk = json_decode(Redis::get($risk_key), true);
|
|
$this->row(function ($row) use ($symbol, $risk) {
|
|
$minUnit = $risk['minUnit'] ?? 0;
|
|
$count = $risk['count'] ?? 0;
|
|
$enabled = $risk['enabled'] ?? 0;
|
|
$strat_time = $risk['strat_time'] ?? 0;
|
|
//$end_time = $risk['end_time'] ?? 0;
|
|
$pin = $risk['pin'] ?? 0;
|
|
|
|
$row->width(3)->text($symbol . '.' . 'minUnit', '单位')->default($minUnit);
|
|
$row->width(3)->text($symbol . '.' . 'count', '计数')->default($count);
|
|
$row->time($symbol . '.' . 'strat_time', '开始时间')->default($strat_time);
|
|
//$row->time($symbol . '.' . 'end_time', '结束时间')->default($end_time);
|
|
$row->width(3)->switch($symbol . '.' . 'enabled', '开关')->default($enabled);
|
|
$row->width(3)->switch($symbol . '.' . 'pin', '是否指定时间')->default($pin);
|
|
|
|
});
|
|
};
|
|
// 第一个参数是选项卡标题,第二个参数是内容,第三个参数是是否选中
|
|
$title = $symbol . '合约';
|
|
if ($index == 1) {
|
|
$this->tab($title, $func, true);
|
|
} else {
|
|
$this->tab($title, $func);
|
|
}
|
|
$index++;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The data of the form.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function default()
|
|
{
|
|
return [];
|
|
}
|
|
}
|
|
|