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.
 
 
 

453 lines
19 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;
use Illuminate\Support\Facades\Log;
use App\Models\DataTobr;
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])) {
$risk_key = 'fkJson:' . $symbol . '/USDT';
$data = $input[$symbol];
if (in_array($symbol, $PlatformCoinNew)) {
if ($input[$symbol]['enabled'] == 1) {
$dotime = strtotime(date('Y-m-d ', time()) . $input[$symbol]['strat_time']);
// $doit = $this->PlatformCurrencyRiskControlTotalProcessing('AAAAA', $input[$symbol]);
if ($input[$symbol]['pin'] == 1) {
$doit = $this->PlatformCurrencyRiskControlTotalProcessing($symbol, $input[$symbol], $dotime);
} else {
$doit = $this->PlatformCurrencyRiskControlTotalProcessing($symbol, $input[$symbol], time());
}
if ($doit) {
return $this->error($doit);
}
}
}
// !blank($risk)
$old_data = json_decode(Redis::get($risk_key), true);
if($old_data['now_time'] ==strtotime(date("Y-m-d H:i:00"))){
return $this->error('此根还在路上,请下一分钟再操作');
}
if(empty($old_data['now_time']) || $old_data['enabled'] == 1 || $data['enabled'] == 1){
$data['now_time'] = strtotime(date("Y-m-d H:i:00"));
if($data['enabled'] == 1 && $old_data['enabled'] == 1){
// // 二次改价
$data['back_count'] = $old_data['count'];
}elseif ($data['enabled'] == 0 && $old_data['enabled'] == 1){
// // 关闭风控
$data['back_count'] = $data['count'];
}else{
// // 开启风控
$data['back_count'] = $data['count'];
}
}else{
if(empty($old_data['now_time'])){
$old_data['now_time'] = strtotime(date("Y-m-d H:i:00"))-180;
}
$data['now_time'] = $old_data['now_time'];
}
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";
// 更新当前一分钟数据
// 获取当前一分钟数据
$one_min_date = $model::query()->where('Date', $now_time)->where('is_1min', 1)->first()->toArray();
Log::info($one_min_date);
// 判断是涨还是跌
if ($one_min_date['Open'] > $data['count']) {
$UpsAndDowns = 1;
} else {
$UpsAndDowns = 0;
}
// 更新当前一分钟收盘数据
$this->UpdateTheCurrentMinute($UpsAndDowns, $data['count'], $now_time, $model, $one_min_date);
// 随机回调15-25条一分钟
// $NumberOfRecovery = mt_rand(15, 25);
$NumberOfRecovery = 35;
$end_open = $model::query()->where('Date', $now_time + $NumberOfRecovery * 60)->where('is_1min', 1)->first()->toArray();
// 判断修复,是涨还是跌
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'];
// 更新缓存最高最低
// $periods = ['5min', '15min', '30min', '60min'];
// foreach ($periods as $period){
// Log::info($coin);
// $old_per = Cache::store('redis')->get('swap:' . $coin . '_kline_' . $period);
// Log::info($old_per);
// //
// $old_per['high'] = $old_per['open'];
// $old_per['low'] = $old_per['open'];
// Cache::store('redis')->put('swap:' . $coin . '_kline_' . $period,$old_per);
// }
// 生成随机修复分钟数
$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);
// Log::info(11111);
$this->OtherPeriod($now_time + ($i * $period2_seconds), $data_new, $model);
// 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)
{
// Log::info(2222);
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);
} 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);
}
}
/**
* Notes: 更新其他时段值
* @param $now_time
* @param $data
* @param $model
* User: torsenli
* Date: 2022/3/22
* Time: 17:39
*/
public function OtherPeriod($now_time, $data, $model)
{
// $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']['uup'] = strtotime(date('Y-m-d H:', $testtime) . intval($now_min / 5) * 5 . ":00")- 60 * 5;
$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_5min']['next'] = strtotime(date('Y-m-d H:', $testtime) . intval($now_min / 5) * 5 . ":00") + 60 * 5;
$other_min['is_15min']['uup'] = strtotime(date('Y-m-d H:', $testtime) . intval($now_min / 15) * 15 . ":00") - 60 * 15;
$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_15min']['next'] = strtotime(date('Y-m-d H:', $testtime) . intval($now_min / 15) * 15 . ":00") + 60 * 15;
$other_min['is_30min']['uup'] = strtotime(date('Y-m-d H:', $testtime) . intval($now_min / 30) * 30 . ":00")-60 * 30;
$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_30min']['next'] = strtotime(date('Y-m-d H:', $testtime) . intval($now_min / 30) * 30 . ":00") + 60 * 30;
$other_min['is_1h']['uup'] = strtotime(date('Y-m-d H:', $testtime) . "00:00")-60 * 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;
$other_min['is_1h']['next'] = strtotime(date('Y-m-d H:', $testtime) . "00:00") + 60 * 60;
foreach ($other_min as $val => $key) {
// 更新当前时间段最高最低
$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']) {
$uup_data = $model::query()->where('Date', $key['uup'])->where($val, 1)->first()->toArray();
$up_date['Open'] = $open = $uup_data['Close'];
// Log::info($data['open']);
// Log::info($key['start']);
// Log::info($uup_data['Close']);
// Log::info($val);
// Log::info($now_time);
if($val == 'is_15min'){
Log::info($val);
// Log::info($uup_data);
Log::info($key['start']);
Log::info($key['uup']);
Log::info($key['next']);
}
}
// 如果是时间段收盘时间更新收盘
if ($now_time === $key['end']) {
$up_date['Close'] = $close = $data['close'];
$up_date['LastClose'] = $close = $data['close'];
$next_line['Open'] = $data['close'];
$model::query()->where('Date', $key['next'])->where($val, 1)->update($next_line);
}
// $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']);
$model::query()->where('Date', $key['start'])->where($val, 1)->update($up_date);
//更新缓存
// Log::info($key['start']);
// Log::info($now_time);
// $period = 'is_'. $val;
// 可能有用
// $now_high = $model::query()->where('is_1min', 1)->whereBetween('Date', [$key['start'],$now_time])->max('High');
// $now_low = $model::query()->where('is_1min', 1)->whereBetween('Date', [$key['start'],$now_time])->min('Low');
// $low = DataRao::query()->where('is_1min', 1)->whereBetween('Date', [$where_start, $where_end])->min('Low');
// $now_high = $model::query()->whereBetween('Date', $key['start'],$now_time)->where($period, 1)->max('High');
// $now_low = $model::query()->whereBetween('Date', $key['start'],$now_time)->where($period, 1)->max('Low');
// $periods = ['5min', '15min', '30min', '60min'];
// foreach ($periods as $period){
// Log::info($coin);
// 可能有用
// $old_per = Cache::store('redis')->get('swap:' . $other_time_date['Symbol'] . '_kline_' . $val);
// $old_per['high'] = $now_high;
// $old_per['low'] = $now_low;
// Log::info($now_high);
// Log::info($old_per);
// //
// $old_per['high'] = $old_per['open'];
// $old_per['low'] = $old_per['open'];
// 可能有用
// Cache::store('redis')->put('swap:' . $other_time_date['Symbol'] . '_kline_' . $val,$old_per);
// }
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 [];
}
}