millisecondWay(); $curr_id = 2; //市价买 现价买(高->低) 市价卖 现价卖(低->高) $redis = getRedis(); $running = $redis->get('cointrades_bb'); if($running) { echo' is running'; $redis->set('cointrades_bb', false); exit; } // var_dump(json_decode($redis->get("apt_1min"),true));exit; //查询10条买单 $order_buy = Db::name('app_coin_trade_apt') ->whereIn('status','0,1') ->where('type','buy') ->order('cart','asc') ->order('price','desc') ->order('id','asc') ->limit(100) ->select(); if(empty($order_buy)){ echo "暂无买单"; $redis->set('cointrades_bb', false); //虽无数据 但是实时K线得跟着变 //1分钟 $this->apt_redis($redis, "apt_1min", [], "i",1); //5分钟 $this->apt_redis($redis, "apt_5min", [], "i",5); //15分钟 $this->apt_redis($redis, "apt_15min", [], "i",15); //30分钟 $this->apt_redis($redis, "apt_30min", [], "i",30); //60分钟 $this->apt_redis($redis, "apt_60min", [], "h",1); //1天 $this->apt_redis($redis, "apt_1day", [], "d",1); //1个月 $this->apt_redis($redis, "apt_1mon", [], "m",1); exit; } $order_sell = Db::name('app_coin_trade_apt') ->whereIn('status','0,1') ->where('type','sell') ->order('cart','asc') ->order('price','asc') ->order('id','asc') ->limit(100) ->select(); if(empty($order_sell)){ echo "暂无卖单"; $redis->set('cointrades_bb', false); //虽无数据 但是实时K线得跟着变 //1分钟 $this->apt_redis($redis, "apt_1min", [], "i",1); //5分钟 $this->apt_redis($redis, "apt_5min", [], "i",5); //15分钟 $this->apt_redis($redis, "apt_15min", [], "i",15); //30分钟 $this->apt_redis($redis, "apt_30min", [], "i",30); //60分钟 $this->apt_redis($redis, "apt_60min", [], "h",1); //1天 $this->apt_redis($redis, "apt_1day", [], "d",1); //1个月 $this->apt_redis($redis, "apt_1mon", [], "m",1); exit; } $curr = Db::name("app_currency")->where("id",$curr_id)->find(); $price = $curr['exchange']; //撮合配对 $all_data = [];$zongjie = 0; foreach ($order_buy as $key => $bvv) { if($zongjie){ break;//此跳过为 没有可撮合的交易 } if($key >= 20){ break; } // $buy_user = Db::name("app_currency_user a")->field("a.id,a.user_id,a.num,b.apy_pay") // ->join("user b","a.user_id=b.id","left") // ->where("a.user_id",$bvv['user_id'])->where("a.curr_id",$curr_id) // ->find(); foreach ($order_sell as $kk => $svv) { if($kk >= 20){ break; } // $sell_user = Db::name("app_currency_user a")->field("a.id,a.user_id,a.num,b.apy_pay") // ->join("user b","a.user_id=b.id","left") // ->where("a.user_id",$svv['user_id'])->where("a.curr_id",1) // ->find(); if($bvv['cart'] == "market"){ //买单市价 if($svv['cart'] == "market"){ //卖单市价 $buy_usdt = $bvv['amount']-$bvv['have_usdt'];//待使用USDT $buy_num = sprintf("%.6f",$buy_usdt/$price);//剩余可买数量 // echo $buy_num."--"; $sell_num = $svv['num'] - $svv['have_num'];//剩余可卖数量 $sell_usdt = sprintf("%.6f",$sell_num*$price); echo $bvv['id'].":".$buy_usdt.",".$svv['id'].":".$sell_usdt."----"; if($buy_num == $sell_num){ //双方完全成交 if(isset($all_data[$bvv['id']])){ $all_data[$bvv['id']]['num'] = $all_data[$bvv['id']]['num']+$buy_num; $all_data[$bvv['id']]['usdt'] = $all_data[$bvv['id']]['usdt']+$buy_usdt; }else{ $all_data[$bvv['id']] = array( "order_id" => $bvv['id'], "type" => "buy", "status" => 2,//2=完全成交,1=部分成交 "num" => $buy_num,//成交数量 "price" => $price,//单价 "usdt" => $buy_usdt,// "user_id" => $bvv['user_id'], "have_num" => $bvv['have_num'],//初始成交量 "have_usdt" => $bvv['have_usdt'],//初始成交USTD "cart" => $bvv['cart'], ); } if(isset($all_data[$svv['id']])){ $all_data[$svv['id']]['num'] = $all_data[$svv['id']]['num']+$sell_num; $all_data[$svv['id']]['usdt'] = $all_data[$svv['id']]['usdt']+$sell_usdt; }else{ $all_data[$svv['id']] = array( "order_id" => $svv['id'], "type" => "sell", "status" => 2,//2=完全成交,1=部分成交 "num" => $sell_num,//成交数量 "price" => $price,//单价 "usdt" => $sell_usdt,// "user_id" => $svv['user_id'], "have_num" => $svv['have_num'],//初始成交量 "have_usdt" => $svv['have_usdt'],//初始成交USTD "cart" => $svv['cart'], ); } //双方成交完 清除数据跳过整个循环 unset($order_buy[$key]); unset($order_sell[$kk]); break; }elseif($buy_num > $sell_num){ //买单数量大于卖单 卖单全成交买单部分成交 if(isset($all_data[$bvv['id']])){ $all_data[$bvv['id']]['num'] = $all_data[$bvv['id']]['num']+$sell_num; $all_data[$bvv['id']]['usdt'] = $all_data[$bvv['id']]['usdt']+$sell_usdt; }else{ $all_data[$bvv['id']] = array( "order_id" => $bvv['id'], "type" => "buy", "status" => 1,//2=完全成交,1=部分成交 "num" => $sell_num,//成交数量 "price" => $price,//单价 "usdt" => $sell_usdt,//成交USDT "user_id" => $bvv['user_id'], "have_num" => $bvv['have_num'],//初始成交量 "have_usdt" => $bvv['have_usdt'],//初始成交USTD "cart" => $bvv['cart'], ); } if(isset($all_data[$svv['id']])){ $all_data[$svv['id']]['num'] = $all_data[$svv['id']]['num']+$sell_num; $all_data[$svv['id']]['usdt'] = $all_data[$svv['id']]['usdt']+$sell_usdt; $all_data[$svv['id']]['status'] = 2; }else{ $all_data[$svv['id']] = array( "order_id" => $svv['id'], "type" => "sell", "status" => 2,//2=完全成交,1=部分成交 "num" => $sell_num,//成交数量 "price" => $price,//单价 "usdt" => $sell_usdt,// "user_id" => $svv['user_id'], "have_num" => $svv['have_num'],//初始成交量 "have_usdt" => $svv['have_usdt'],//初始成交USTD "cart" => $svv['cart'], ); } $bvv['have_num'] = $bvv['have_num']+$sell_num; $bvv['have_usdt'] = $bvv['have_usdt']+$sell_usdt; $order_buy[$key] = $bvv; //买单还有剩余 跳过本轮循环继续成交卖单 清除本次卖单 unset($order_sell[$kk]); continue; }elseif($buy_num < $sell_num){ //卖单数量大于买单 买单全成交卖单部分成交 if(isset($all_data[$bvv['id']])){ $all_data[$bvv['id']]['num'] = $all_data[$bvv['id']]['num']+$buy_num; $all_data[$bvv['id']]['usdt'] = $all_data[$bvv['id']]['usdt']+$buy_usdt; $all_data[$bvv['id']]['status'] = 2; }else{ $all_data[$bvv['id']] = array( "order_id" => $bvv['id'], "type" => "buy", "status" => 2,//2=完全成交,1=部分成交 "num" => $buy_num,//成交数量 "price" => $price,//单价 "usdt" => $buy_usdt,// "user_id" => $bvv['user_id'], "have_num" => $bvv['have_num'],//初始成交量 "have_usdt" => $bvv['have_usdt'],//初始成交USTD "cart" => $bvv['cart'], ); } if(isset($all_data[$svv['id']])){ $all_data[$svv['id']]['num'] = $all_data[$svv['id']]['num']+$buy_num; $all_data[$svv['id']]['usdt'] = $all_data[$svv['id']]['usdt']+$buy_usdt; }else{ $all_data[$svv['id']] = array( "order_id" => $svv['id'], "type" => "sell", "status" => 1,//2=完全成交,1=部分成交 "num" => $buy_num,//成交数量 "price" => $price,//单价 "usdt" => $buy_usdt,// "user_id" => $svv['user_id'], "have_num" => $svv['have_num'],//初始成交量 "have_usdt" => $svv['have_usdt'],//初始成交USTD "cart" => $svv['cart'], ); } $svv['have_num'] = $svv['have_num']+$buy_num; $svv['have_usdt'] = $svv['have_usdt']+$buy_usdt; $order_sell[$kk] = $svv; //卖单还有剩余 跳过卖单循环 让下一个买单来继续成交此卖单 清除当前买单 unset($order_buy[$key]); break; } }elseif ($svv['cart'] == "limit") { //买单市价 卖单现价 价格会根据卖单变化 $price = $svv['price']; $buy_usdt = $bvv['amount']-$bvv['have_usdt'];//待使用USDT $buy_num = sprintf("%.6f",$buy_usdt/$price);//剩余可买数量 // echo $buy_num."--"; $sell_num = $svv['num'] - $svv['have_num'];//剩余可卖数量 $sell_usdt = sprintf("%.6f",$sell_num*$price); $curr_price = $price; echo $bvv['id'].":".$buy_usdt.",".$svv['id'].":".$sell_usdt."----"; if($buy_num == $sell_num){ //双方完全成交 if(isset($all_data[$bvv['id']])){ $all_data[$bvv['id']]['num'] = $all_data[$bvv['id']]['num']+$buy_num; $all_data[$bvv['id']]['usdt'] = $all_data[$bvv['id']]['usdt']+$buy_usdt; }else{ $all_data[$bvv['id']] = array( "order_id" => $bvv['id'], "type" => "buy", "status" => 2,//2=完全成交,1=部分成交 "num" => $buy_num,//成交数量 "price" => $price,//单价 "usdt" => $buy_usdt,// "user_id" => $bvv['user_id'], "have_num" => $bvv['have_num'],//初始成交量 "have_usdt" => $bvv['have_usdt'],//初始成交USTD "cart" => $bvv['cart'], ); } if(isset($all_data[$svv['id']])){ $all_data[$svv['id']]['num'] = $all_data[$svv['id']]['num']+$sell_num; $all_data[$svv['id']]['usdt'] = $all_data[$svv['id']]['usdt']+$sell_usdt; }else{ $all_data[$svv['id']] = array( "order_id" => $svv['id'], "type" => "sell", "status" => 2,//2=完全成交,1=部分成交 "num" => $sell_num,//成交数量 "price" => $price,//单价 "usdt" => $sell_usdt,// "user_id" => $svv['user_id'], "have_num" => $svv['have_num'],//初始成交量 "have_usdt" => $svv['have_usdt'],//初始成交USTD "cart" => $svv['cart'], ); } //双方成交完 清除数据跳过整个循环 unset($order_buy[$key]); unset($order_sell[$kk]); break; }elseif($buy_num > $sell_num){ //买单数量大于卖单 卖单全成交买单部分成交 if(isset($all_data[$bvv['id']])){ $all_data[$bvv['id']]['num'] = $all_data[$bvv['id']]['num']+$sell_num; $all_data[$bvv['id']]['usdt'] = $all_data[$bvv['id']]['usdt']+$sell_usdt; }else{ $all_data[$bvv['id']] = array( "order_id" => $bvv['id'], "type" => "buy", "status" => 1,//2=完全成交,1=部分成交 "num" => $sell_num,//成交数量 "price" => $price,//单价 "usdt" => $sell_usdt,//成交USDT "user_id" => $bvv['user_id'], "have_num" => $bvv['have_num'],//初始成交量 "have_usdt" => $bvv['have_usdt'],//初始成交USTD "cart" => $bvv['cart'], ); } if(isset($all_data[$svv['id']])){ $all_data[$svv['id']]['num'] = $all_data[$svv['id']]['num']+$sell_num; $all_data[$svv['id']]['usdt'] = $all_data[$svv['id']]['usdt']+$sell_usdt; $all_data[$svv['id']]['status'] = 2; }else{ $all_data[$svv['id']] = array( "order_id" => $svv['id'], "type" => "sell", "status" => 2,//2=完全成交,1=部分成交 "num" => $sell_num,//成交数量 "price" => $price,//单价 "usdt" => $sell_usdt,// "user_id" => $svv['user_id'], "have_num" => $svv['have_num'],//初始成交量 "have_usdt" => $svv['have_usdt'],//初始成交USTD "cart" => $svv['cart'], ); } $bvv['have_num'] = $bvv['have_num']+$sell_num; $bvv['have_usdt'] = $bvv['have_usdt']+$sell_usdt; $order_buy[$key] = $bvv; //买单还有剩余 跳过本轮循环继续成交卖单 清除本次卖单 unset($order_sell[$kk]); continue; }elseif($buy_num < $sell_num){ //卖单数量大于买单 买单全成交卖单部分成交 if(isset($all_data[$bvv['id']])){ $all_data[$bvv['id']]['num'] = $all_data[$bvv['id']]['num']+$buy_num; $all_data[$bvv['id']]['usdt'] = $all_data[$bvv['id']]['usdt']+$buy_usdt; $all_data[$bvv['id']]['status'] = 2; }else{ $all_data[$bvv['id']] = array( "order_id" => $bvv['id'], "type" => "buy", "status" => 2,//2=完全成交,1=部分成交 "num" => $buy_num,//成交数量 "price" => $price,//单价 "usdt" => $buy_usdt,// "user_id" => $bvv['user_id'], "have_num" => $bvv['have_num'],//初始成交量 "have_usdt" => $bvv['have_usdt'],//初始成交USTD "cart" => $bvv['cart'], ); } if(isset($all_data[$svv['id']])){ $all_data[$svv['id']]['num'] = $all_data[$svv['id']]['num']+$buy_num; $all_data[$svv['id']]['usdt'] = $all_data[$svv['id']]['usdt']+$buy_usdt; }else{ $all_data[$svv['id']] = array( "order_id" => $svv['id'], "type" => "sell", "status" => 1,//2=完全成交,1=部分成交 "num" => $buy_num,//成交数量 "price" => $price,//单价 "usdt" => $buy_usdt,// "user_id" => $svv['user_id'], "have_num" => $svv['have_num'],//初始成交量 "have_usdt" => $svv['have_usdt'],//初始成交USTD "cart" => $svv['cart'], ); } $svv['have_num'] = $svv['have_num']+$buy_num; $svv['have_usdt'] = $svv['have_usdt']+$buy_usdt; $order_sell[$kk] = $svv; //卖单还有剩余 跳过卖单循环 让下一个买单来继续成交此卖单 清除当前买单 unset($order_buy[$key]); break; } } }elseif($bvv['cart'] == "limit"){ //买单限价 if($svv['cart'] == "market"){ //卖单市价 买单现价 价格会根据买单变化 $price = $bvv['price']; $buy_usdt = $bvv['amount']-$bvv['have_usdt'];//待使用USDT $buy_num = sprintf("%.6f",$buy_usdt/$price);//剩余可买数量 // echo $buy_num."--"; $sell_num = $svv['num'] - $svv['have_num'];//剩余可卖数量 $sell_usdt = sprintf("%.6f",$sell_num*$price); $curr_price = $price; echo $bvv['id'].":".$buy_usdt.",".$svv['id'].":".$sell_usdt."----"; if($buy_num == $sell_num){ //双方完全成交 if(isset($all_data[$bvv['id']])){ $all_data[$bvv['id']]['num'] = $all_data[$bvv['id']]['num']+$buy_num; $all_data[$bvv['id']]['usdt'] = $all_data[$bvv['id']]['usdt']+$buy_usdt; }else{ $all_data[$bvv['id']] = array( "order_id" => $bvv['id'], "type" => "buy", "status" => 2,//2=完全成交,1=部分成交 "num" => $buy_num,//成交数量 "price" => $price,//单价 "usdt" => $buy_usdt,// "user_id" => $bvv['user_id'], "have_num" => $bvv['have_num'],//初始成交量 "have_usdt" => $bvv['have_usdt'],//初始成交USTD "cart" => $bvv['cart'], ); } if(isset($all_data[$svv['id']])){ $all_data[$svv['id']]['num'] = $all_data[$svv['id']]['num']+$sell_num; $all_data[$svv['id']]['usdt'] = $all_data[$svv['id']]['usdt']+$sell_usdt; }else{ $all_data[$svv['id']] = array( "order_id" => $svv['id'], "type" => "sell", "status" => 2,//2=完全成交,1=部分成交 "num" => $sell_num,//成交数量 "price" => $price,//单价 "usdt" => $sell_usdt,// "user_id" => $svv['user_id'], "have_num" => $svv['have_num'],//初始成交量 "have_usdt" => $svv['have_usdt'],//初始成交USTD "cart" => $svv['cart'], ); } //双方成交完 清除数据跳过整个循环 unset($order_buy[$key]); unset($order_sell[$kk]); break; }elseif($buy_num > $sell_num){ //买单数量大于卖单 卖单全成交买单部分成交 if(isset($all_data[$bvv['id']])){ $all_data[$bvv['id']]['num'] = $all_data[$bvv['id']]['num']+$sell_num; $all_data[$bvv['id']]['usdt'] = $all_data[$bvv['id']]['usdt']+$sell_usdt; }else{ $all_data[$bvv['id']] = array( "order_id" => $bvv['id'], "type" => "buy", "status" => 1,//2=完全成交,1=部分成交 "num" => $sell_num,//成交数量 "price" => $price,//单价 "usdt" => $sell_usdt,//成交USDT "user_id" => $bvv['user_id'], "have_num" => $bvv['have_num'],//初始成交量 "have_usdt" => $bvv['have_usdt'],//初始成交USTD "cart" => $bvv['cart'], ); } if(isset($all_data[$svv['id']])){ $all_data[$svv['id']]['num'] = $all_data[$svv['id']]['num']+$sell_num; $all_data[$svv['id']]['usdt'] = $all_data[$svv['id']]['usdt']+$sell_usdt; $all_data[$svv['id']]['status'] = 2; }else{ $all_data[$svv['id']] = array( "order_id" => $svv['id'], "type" => "sell", "status" => 2,//2=完全成交,1=部分成交 "num" => $sell_num,//成交数量 "price" => $price,//单价 "usdt" => $sell_usdt,// "user_id" => $svv['user_id'], "have_num" => $svv['have_num'],//初始成交量 "have_usdt" => $svv['have_usdt'],//初始成交USTD "cart" => $svv['cart'], ); } $bvv['have_num'] = $bvv['have_num']+$sell_num; $bvv['have_usdt'] = $bvv['have_usdt']+$sell_usdt; $order_buy[$key] = $bvv; //买单还有剩余 跳过本轮循环继续成交卖单 清除本次卖单 unset($order_sell[$kk]); continue; }elseif($buy_num < $sell_num){ //卖单数量大于买单 买单全成交卖单部分成交 if(isset($all_data[$bvv['id']])){ $all_data[$bvv['id']]['num'] = $all_data[$bvv['id']]['num']+$buy_num; $all_data[$bvv['id']]['usdt'] = $all_data[$bvv['id']]['usdt']+$buy_usdt; $all_data[$bvv['id']]['status'] = 2; }else{ $all_data[$bvv['id']] = array( "order_id" => $bvv['id'], "type" => "buy", "status" => 2,//2=完全成交,1=部分成交 "num" => $buy_num,//成交数量 "price" => $price,//单价 "usdt" => $buy_usdt,// "user_id" => $bvv['user_id'], "have_num" => $bvv['have_num'],//初始成交量 "have_usdt" => $bvv['have_usdt'],//初始成交USTD "cart" => $bvv['cart'], ); } if(isset($all_data[$svv['id']])){ $all_data[$svv['id']]['num'] = $all_data[$svv['id']]['num']+$buy_num; $all_data[$svv['id']]['usdt'] = $all_data[$svv['id']]['usdt']+$buy_usdt; }else{ $all_data[$svv['id']] = array( "order_id" => $svv['id'], "type" => "sell", "status" => 1,//2=完全成交,1=部分成交 "num" => $buy_num,//成交数量 "price" => $price,//单价 "usdt" => $buy_usdt,// "user_id" => $svv['user_id'], "have_num" => $svv['have_num'],//初始成交量 "have_usdt" => $svv['have_usdt'],//初始成交USTD "cart" => $svv['cart'], ); } $svv['have_num'] = $svv['have_num']+$buy_num; $svv['have_usdt'] = $svv['have_usdt']+$buy_usdt; $order_sell[$kk] = $svv; //卖单还有剩余 跳过卖单循环 让下一个买单来继续成交此卖单 清除当前买单 unset($order_buy[$key]); break; } }elseif ($svv['cart'] == "limit") { //买单现价 卖单现价 必须当买单价格高于卖单才可成交 价格根据卖单变 $price = $svv['price']; $buy_price = $bvv['price']; if($buy_price >= $price){ $curr_price = $price; $buy_price = $price; $buy_usdt = $bvv['amount']-$bvv['have_usdt'];//待使用USDT $buy_num = sprintf("%.6f",$buy_usdt/$buy_price);//剩余可买数量 // echo $buy_num."--"; $sell_num = $svv['num'] - $svv['have_num'];//剩余可卖数量 $sell_usdt = sprintf("%.6f",$sell_num*$price);//剩余可卖USDT // echo $sell_usdt;exit; echo $bvv['id'].":".$buy_usdt.",".$svv['id'].":".$sell_usdt."----"; if($buy_num == $sell_num){ //双方完全成交 if(isset($all_data[$bvv['id']])){ $all_data[$bvv['id']]['num'] = $all_data[$bvv['id']]['num']+$buy_num; $all_data[$bvv['id']]['usdt'] = $all_data[$bvv['id']]['usdt']+$buy_usdt; }else{ $all_data[$bvv['id']] = array( "order_id" => $bvv['id'], "type" => "buy", "status" => 2,//2=完全成交,1=部分成交 "num" => $buy_num,//成交数量 "price" => $buy_price,//单价 "usdt" => $buy_usdt,// "user_id" => $bvv['user_id'], "have_num" => $bvv['have_num'],//初始成交量 "have_usdt" => $bvv['have_usdt'],//初始成交USTD "cart" => $bvv['cart'], ); } if(isset($all_data[$svv['id']])){ $all_data[$svv['id']]['num'] = $all_data[$svv['id']]['num']+$sell_num; $all_data[$svv['id']]['usdt'] = $all_data[$svv['id']]['usdt']+$sell_usdt; }else{ $all_data[$svv['id']] = array( "order_id" => $svv['id'], "type" => "sell", "status" => 2,//2=完全成交,1=部分成交 "num" => $sell_num,//成交数量 "price" => $price,//单价 "usdt" => $sell_usdt,// "user_id" => $svv['user_id'], "have_num" => $svv['have_num'],//初始成交量 "have_usdt" => $svv['have_usdt'],//初始成交USTD "cart" => $svv['cart'], ); } //双方成交完 清除数据跳过整个循环 unset($order_buy[$key]); unset($order_sell[$kk]); break; }elseif($buy_num > $sell_num){ //买单数量大于卖单 卖单全成交买单部分成交 $sj_buy_usdt = sprintf("%.6f",$sell_num*$buy_price);//实际花费多少USDT if(isset($all_data[$bvv['id']])){ $all_data[$bvv['id']]['num'] = $all_data[$bvv['id']]['num']+$sell_num; $all_data[$bvv['id']]['usdt'] = $all_data[$bvv['id']]['usdt']+$sj_buy_usdt; }else{ $all_data[$bvv['id']] = array( "order_id" => $bvv['id'], "type" => "buy", "status" => 1,//2=完全成交,1=部分成交 "num" => $sell_num,//成交数量 "price" => $buy_price,//单价 "usdt" => $sj_buy_usdt,//成交USDT "user_id" => $bvv['user_id'], "have_num" => $bvv['have_num'],//初始成交量 "have_usdt" => $bvv['have_usdt'],//初始成交USTD "cart" => $bvv['cart'], ); } if(isset($all_data[$svv['id']])){ $all_data[$svv['id']]['num'] = $all_data[$svv['id']]['num']+$sell_num; $all_data[$svv['id']]['usdt'] = $all_data[$svv['id']]['usdt']+$sell_usdt; $all_data[$svv['id']]['status'] = 2; }else{ $all_data[$svv['id']] = array( "order_id" => $svv['id'], "type" => "sell", "status" => 2,//2=完全成交,1=部分成交 "num" => $sell_num,//成交数量 "price" => $price,//单价 "usdt" => $sell_usdt,// "user_id" => $svv['user_id'], "have_num" => $svv['have_num'],//初始成交量 "have_usdt" => $svv['have_usdt'],//初始成交USTD "cart" => $svv['cart'], ); } $bvv['have_num'] = $bvv['have_num']+$sell_num; $bvv['have_usdt'] = $bvv['have_usdt']+$sj_buy_usdt; $order_buy[$key] = $bvv; //买单还有剩余 跳过本轮循环继续成交卖单 清除本次卖单 unset($order_sell[$kk]); continue; }elseif($buy_num < $sell_num){ //卖单数量大于买单 买单全成交卖单部分成交 if(isset($all_data[$bvv['id']])){ $all_data[$bvv['id']]['num'] = $all_data[$bvv['id']]['num']+$buy_num; $all_data[$bvv['id']]['usdt'] = $all_data[$bvv['id']]['usdt']+$buy_usdt; $all_data[$bvv['id']]['status'] = 2; }else{ $all_data[$bvv['id']] = array( "order_id" => $bvv['id'], "type" => "buy", "status" => 2,//2=完全成交,1=部分成交 "num" => $buy_num,//成交数量 "price" => $buy_price,//单价 "usdt" => $buy_usdt,// "user_id" => $bvv['user_id'], "have_num" => $bvv['have_num'],//初始成交量 "have_usdt" => $bvv['have_usdt'],//初始成交USTD "cart" => $bvv['cart'], ); } $sj_sell_usdt = sprintf("%.6f",$buy_num*$price);//实际卖了多少USDT if(isset($all_data[$svv['id']])){ $all_data[$svv['id']]['num'] = $all_data[$svv['id']]['num']+$buy_num; $all_data[$svv['id']]['usdt'] = $all_data[$svv['id']]['usdt']+$sj_sell_usdt; }else{ $all_data[$svv['id']] = array( "order_id" => $svv['id'], "type" => "sell", "status" => 1,//2=完全成交,1=部分成交 "num" => $buy_num,//成交数量 "price" => $price,//单价 "usdt" => $sj_sell_usdt,// "user_id" => $svv['user_id'], "have_num" => $svv['have_num'],//初始成交量 "have_usdt" => $svv['have_usdt'],//初始成交USTD "cart" => $svv['cart'], ); } $svv['have_num'] = $svv['have_num']+$buy_num; $svv['have_usdt'] = $svv['have_usdt']+$sj_sell_usdt; $order_sell[$kk] = $svv; //卖单还有剩余 跳过卖单循环 让下一个买单来继续成交此卖单 清除当前买单 unset($order_buy[$key]); break; } }else{ $zongjie = 1; echo "已撮合完"; break; } } } } } if(empty($all_data)){ //虽无数据 但是实时K线得跟着变 //1分钟 $this->apt_redis($redis, "apt_1min", [], "i",1); //5分钟 $this->apt_redis($redis, "apt_5min", [], "i",5); //15分钟 $this->apt_redis($redis, "apt_15min", [], "i",15); //30分钟 $this->apt_redis($redis, "apt_30min", [], "i",30); //60分钟 $this->apt_redis($redis, "apt_60min", [], "h",1); //1天 $this->apt_redis($redis, "apt_1day", [], "d",1); //1个月 $this->apt_redis($redis, "apt_1mon", [], "m",1); } if(!isset($curr_price) || !$curr_price){ $curr_price = $curr['exchange']; } //手续费 $market_bb_free = Config::get("site.market_bb_free");//标准手续费 $zhiya_free_rebate = Config::get("site.zhiya_free_rebate");//质押折扣 $apt_free = Config::get("site.apt_free");//APT手续费折扣 $apt_curr = Db::name("app_currency")->where("id",2)->find(); //平台APT // $res_curr = Db::name("app_currency")->where("id",2)->find();//交易币种 此处可能是其他币 $res_curr = $apt_curr; Db::startTrans(); try { $detail = [];$trade_order = [];$apt_1s = []; foreach ($all_data as $key => $value) { //质押手续费折扣 $count_zhiya = Db::name("app_zhiya_user")->where("user_id", $value['user_id'])->where("status",1)->sum("num"); $zhekou = 1; foreach ($zhiya_free_rebate as $kk => $vv) { if($count_zhiya > $kk){ $zhekou = $vv; } } $fee_bl = $market_bb_free*$zhekou/100;//手续费收取比例 $apy_pay = 1; //买家收入币种,卖家收入USDT if($value['type'] == "buy"){ $nums = $value['num'];//实际收入数量 $curr_id = 2;//实际收入币种 APT $user_curr = Db::name("app_currency_user a")->field("a.*,b.apy_pay") ->join("user b","a.user_id=b.id","left") ->where("a.user_id",$value['user_id']) ->where("a.curr_id",$curr_id) ->find(); //手续费扣除 if($user_curr['apy_pay'] == 1){ //平台币 APT $free_num = sprintf("%.8f",$nums*$fee_bl*$apt_free);//折扣后手续费 $fee = sprintf("%.8f",$free_num*$res_curr['exchange']/$apt_curr['exchange']);//根据汇率计算最终手续费 $user_apt = Db::name('app_currency_user')->where('curr_id',2)->where('user_id',$value['user_id'])->find();//apt余额 if($user_apt['num'] >= $fee){ $apy_pay = 2; $detail[] = array( "user_id" => $value['user_id'], "curr_id" => $user_apt['curr_id'], "price" => $fee, "cart" => 2, "type" => 7, "serial_no" => 'C'.time().Random::build('numeric',4), "description" => "币币APT手续费", "createtime" => time(), "before_num" => $user_apt['num'], "after_num" => $user_apt['num']-$fee, ); Db::name("app_currency_user")->where("id",$user_apt['id'])->setDec("num",$fee); }else{ $free_num = sprintf("%.8f",$nums*$fee_bl);//折扣后手续费 $fee = $free_num; $nums = $nums - $fee;//实际收入减去手续费 } }else{ //交易币 $free_num = sprintf("%.8f",$nums*$fee_bl);//折扣后手续费 $fee = $free_num; $nums = $nums - $fee;//实际收入减去手续费 } }else{ $curr_id = 1;//实际收入币种 USDT $nums = $value['usdt'];//实际收入数量 $user_curr = Db::name("app_currency_user a")->field("a.*,b.apy_pay") ->join("user b","a.user_id=b.id","left") ->where("a.user_id",$value['user_id']) ->where("a.curr_id",$curr_id) ->find(); //手续费扣除 if($user_curr['apy_pay'] == 1){ //平台币 APT $free_num = sprintf("%.8f",$nums*$fee_bl*$apt_free);//折扣后手续费 $fee = sprintf("%.8f",$free_num/$value['price']);//根据汇率计算最终手续费 $user_apt = Db::name('app_currency_user')->where('curr_id',2)->where('user_id',$value['user_id'])->find();//apt余额 if($user_apt['num'] >= $fee){ $apy_pay = 2; $detail[] = array( "user_id" => $value['user_id'], "curr_id" => $user_apt['curr_id'], "price" => $fee, "cart" => 2, "type" => 7, "serial_no" => 'C'.time().Random::build('numeric',4), "description" => "币币APT手续费", "createtime" => time(), "before_num" => $user_apt['num'], "after_num" => $user_apt['num']-$fee, ); Db::name("app_currency_user")->where("id",$user_apt['id'])->setDec("num",$fee); }else{ $free_num = sprintf("%.8f",$nums*$fee_bl);//折扣后手续费 $fee = $free_num; $nums = $nums - $fee;//实际收入减去手续费 } }else{ //交易币 $free_num = sprintf("%.8f",$nums*$fee_bl);//折扣后手续费 $fee = $free_num; $nums = $nums - $fee;//实际收入减去手续费 } } $detail[] = array( "user_id" => $value['user_id'], "curr_id" => $curr_id, "price" => $nums, "cart" => 1, "type" => 7, "serial_no" => 'C'.time().Random::build('numeric',4), "description" => "币币交易买入", "createtime" => time(), "before_num" => $user_curr['num'], "after_num" => $user_curr['num']+$nums, ); Db::name("app_currency_user")->where("id",$user_curr['id'])->setInc("num",$nums); //状态1=部分成交,2=完全成交 if($value['status'] == 1){ $update = array( 'have_num' => $value['have_num']+$value['num'], 'have_usdt' => $value['have_usdt']+$value['usdt'], 'status' => 1, 'updatetime' => time(), ); }else{ $update = array( 'have_num' => $value['have_num']+$value['num'], 'have_usdt' => $value['have_usdt']+$value['usdt'], 'status' => 2, 'finishtime' => time(), ); //市价 同步实际成交金额 if($value['cart'] == "market"){ $update['amount'] = $update['have_usdt']; } } Db::name("app_coin_trade_apt")->where("id",$value['order_id'])->update($update); $trade_order[] = [ 'user_id' => $value['user_id'], 'coin_id' => 2, 'trade_id' => $value['order_id'], 'num' => $value['num'], 'price' => $value['price'], 'amount' => $value['usdt'], 'fee' => $fee, 'createtime' => time(), 'type' => $value['type'], 'order_sn' => 'C' . time() . Random::build('numeric', 4), 'fee_type' => $apy_pay, ]; //记录实时K线的数据 if($value['type'] == "sell"){ if($value['price']<$curr_price){ $value['price'] = $curr_price; } //1分钟 $this->apt_redis($redis, "apt_1min", $value, "i",1); //5分钟 $this->apt_redis($redis, "apt_5min", $value, "i",5); //15分钟 $this->apt_redis($redis, "apt_15min", $value, "i",15); //30分钟 $this->apt_redis($redis, "apt_30min", $value, "i",30); //60分钟 $this->apt_redis($redis, "apt_60min", $value, "h",1); //1天 $this->apt_redis($redis, "apt_1day", $value, "d",1); //1个月 $this->apt_redis($redis, "apt_1mon", $value, "m",1); } } if($detail){ Db::name("app_detailed")->insertAll($detail); Db::name("app_coin_trade_order_apt")->insertAll($trade_order); Db::name("app_currency")->where("id",2)->update(['exchange'=>$curr_price]); Db::name("app_rate")->where("id",16)->update(['close'=>$curr_price]); } Db::commit(); echo "success"; } catch (Exception $e) { Db::rollback(); echo "执行失败"; } //用剩余的数据生成深度图 卖是从小到大 买是从大到小排列 $buy_shendu = [];$sell_shendu = []; foreach ($order_buy as $key => $value) { if(isset($buy_shendu[$value['price']])){ $buy_shendu[$value['price']][1] = sprintf("%.4f",$buy_shendu[$value['price']][1]+($value['num'] - $value['have_num'])); }else{ $buy_shendu[$value['price']] = [ $value['price'], sprintf("%.4f",$value['num'] - $value['have_num']), ]; } } foreach ($order_sell as $key => $value) { if(isset($sell_shendu[$value['price']])){ $sell_shendu[$value['price']][1] = sprintf("%.4f",$sell_shendu[$value['price']][1]+($value['num'] - $value['have_num'])); }else{ $sell_shendu[$value['price']] = [ $value['price'], sprintf("%.4f",$value['num'] - $value['have_num']), ]; } } $buy_shendu = array_values($buy_shendu); $sell_shendu = array_values($sell_shendu); //深度加工 动起来 $new_buy = [];$new_sell = []; $buy_price = 0; foreach ($buy_shendu as $key => $value) { //随机调整深度 $rand = rand(1, 9); if($buy_price > 0 && $rand < 2){ if($buy_price-$value[0] > 0.001){ $data = array( 0 => sprintf("%.4f",$buy_price - 0.0005), 1 => sprintf("%.4f",rand(10000, 100000)/10), ); //随机插入数量 $new_buy[] = $data; } } if($rand<2){ $value[1] = rand($value[1]*10000, $value[1]*20000)/10000; } if($rand>8){ $value[1] = rand($value[1]*10000, $value[1]*2000)/2000; } $buy_price = $value[0]; $new_buy[] = $value; } $sell_price = 0; foreach ($sell_shendu as $key => $value) { $rand = rand(1, 9); if($sell_price > 0 && $rand<2){ if($value[0]-$sell_price > 0.001){ $data = array( 0 => sprintf("%.4f",$sell_price + 0.0005), 1 => sprintf("%.4f",rand(10000, 100000)/10), ); $new_sell[] = $data; } } //随机调整深度 if($rand<2){ $value[1] = rand($value[1]*10000, $value[1]*20000)/10000; } if($rand>8){ $value[1] = rand($value[1]*10000, $value[1]*2000)/2000; } $sell_price = $value[0]; //随机插入数量 $new_sell[] = $value; } // $redis->set("buy_shendu",json_encode($buy_shendu)); // $redis->set("sell_shendu",json_encode($sell_shendu)); $redis->set("buy_shendu",json_encode($new_buy)); $redis->set("sell_shendu",json_encode($new_sell)); var_dump($all_data); $t2 = $this->millisecondWay(); echo "执行时长:".($t2-$t1)/1000; echo "价格:".$curr_price;exit; $redis->set('cointrades_bb', false); } /* * 模拟生成K线 */ public function kline_shendu() { $rand = rand(1, 9); if($rand < 6){ echo "不成交";exit; } sleep(rand(1, 5)); //K线调整 $order_sell = Db::name('app_coin_trade_apt') ->whereIn('status','0,1') ->where('type','sell') ->order('cart','asc') ->order('price','asc') ->order('id','asc') ->find(); $order_buy = Db::name('app_coin_trade_apt') ->whereIn('status','0,1') ->where('type','buy') ->order('cart','asc') ->order('price','desc') ->order('id','asc') ->find(); if($order_buy && $order_buy && $order_buy['price']<$order_sell['price']){ $now_price = Db::name("app_currency")->where("id",2)->value("exchange"); $num = rand(10, 90)*100; $price = rand($order_buy['price']*10000, $order_sell['price']*10000)/10000; if($price > $now_price){ if($num <= 8000){ $price = sprintf("%.4f",$now_price + ($price-$now_price)/5); } }elseif($price < $now_price){ if($num <= 8000){ $price = sprintf("%.4f",$now_price - ($now_price-$price)/5); } } $data = array( "num" => $num,//成交数量 "price" => $price,//单价 ); $redis = getRedis(); //1分钟 $this->apt_redis($redis, "apt_1min", $data, "i",1); //5分钟 $this->apt_redis($redis, "apt_5min", $data, "i",5); //15分钟 $this->apt_redis($redis, "apt_15min", $data, "i",15); //30分钟 $this->apt_redis($redis, "apt_30min", $data, "i",30); //60分钟 $this->apt_redis($redis, "apt_60min", $data, "h",1); //1天 $this->apt_redis($redis, "apt_1day", $data, "d",1); //1个月 $this->apt_redis($redis, "apt_1mon", $data, "m",1); Db::name("app_rate")->where("symbol","aptusdt")->update(['close'=>$price]); Db::name("app_currency")->where("id",2)->update(['exchange'=>$price]); } } /** * APT实时K线数据 * @param type $redis 链接redis * @param type $redis_name 设置的redis名字 * @param type $value 交易的一些数据库 * @param type $times * @param type $num */ public function apt_redis($redis,$redis_name,$value,$times,$num=1) { $apt_1min = $redis->get($redis_name); $apt_1min = json_decode($apt_1min,true); if(empty($value)){ if(!$apt_1min || empty($apt_1min)){ //未定义初始化 $apt_curr = Db::name("app_currency")->where("id",2)->find(); $apt_1min = [ "amount" => 0, "close" => $apt_curr['exchange'], "count" => 0, "high" => $apt_curr['exchange'], "id" => time(), "low" => $apt_curr['exchange'], "open" => $apt_curr['exchange'], "vol" => 0, ]; }else{ $t1 = date($times,$apt_1min['id']); $t2 = date($times, time()); if($times == "i" && $t2<$t1){//1,6 $t2 = $t2+60; } if($t2-$t1 >= $num || $t2<$t1){ $apt_curr = Db::name("app_currency")->where("id",2)->find(); $redis->set("bf_".$redis_name,json_encode($apt_1min)); //达到一个新的时间节点 $apt_1min['amount'] = 0; $apt_1min['close'] = $apt_curr['exchange']; $apt_1min['count'] = 0; $apt_1min['high'] = $apt_curr['exchange']; $apt_1min['id'] = time(); $apt_1min['low'] = $apt_curr['exchange']; $apt_1min['open'] = $apt_curr['exchange']; $apt_1min['vol'] = 0; } } }else{ if(!$apt_1min || empty($apt_1min)){ //未定义初始化 $apt_1min = [ "amount" => $value['num'], "close" => $value['price'], "count" => 1, "high" => $value['price'], "id" => time(), "low" => $value['price'], "open" => $value['price'], "vol" => $value['num'], ]; }else{ $t1 = date($times,$apt_1min['id']); $t2 = date($times, time()); if($times == "i" && $t2<$t1){//1,6 $t2 = $t2+60; } if($t2-$t1 >= $num || $t2<$t1){ $redis->set("bf_".$redis_name,json_encode($apt_1min)); //达到一个新的时间节点 $apt_1min = [ "amount" => $value['num'], "close" => $value['price'], "count" => 1, "high" => $value['price'], "id" => time(), "low" => $value['price'], "open" => $value['price'], "vol" => $value['num'], ]; }else{ $apt_1min['amount'] = $apt_1min['amount']+$value['num']; $apt_1min['close'] = $value['price']; $apt_1min['count'] = $apt_1min['count']+1; if($value['price'] > $apt_1min['high']){ $apt_1min['high'] = $value['price']; } if($value['price'] < $apt_1min['low']){ $apt_1min['low'] = $value['price']; } $apt_1min['vol'] = $apt_1min['amount']; } } } $redis->set($redis_name,json_encode($apt_1min)); } /** * 更新APT-K线数据 */ public function update_apt_kline() { $redis = getRedis(); //1分钟 $apt_1min = $redis->get("bf_apt_1min"); $apt_1min = json_decode($apt_1min,true); if($apt_1min){ $apt_1min_data = []; $last_1min = Db::name("apt_k_1min")->order("ts desc")->find(); if($last_1min){ if($last_1min['ts'] != $apt_1min['id']){ $apt_1min_data = array( "symbol" => "aptusdt", "ts" => $apt_1min['id'], "open" => $apt_1min['open'], "close" => $apt_1min['close'], "low" => $apt_1min['low'], "high" => $apt_1min['high'], "amount" => $apt_1min['amount'], "vol" => $apt_1min['vol'], "count" => $apt_1min['count'], ); } }else{ $apt_1min_data = array( "symbol" => "aptusdt", "ts" => $apt_1min['id'], "open" => $apt_1min['open'], "close" => $apt_1min['close'], "low" => $apt_1min['low'], "high" => $apt_1min['high'], "amount" => $apt_1min['amount'], "vol" => $apt_1min['vol'], "count" => $apt_1min['count'], ); } if($apt_1min_data){ Db::name("apt_k_1min")->insert($apt_1min_data); echo 1; } } //5分钟 $apt_5min = $redis->get("bf_apt_5min"); $apt_5min = json_decode($apt_5min,true); if($apt_5min){ $apt_5min_data = []; $last_5min = Db::name("apt_k_5min")->order("ts desc")->find(); if($last_5min){ if($last_5min['ts'] != $apt_5min['id']){ $apt_5min_data = array( "symbol" => "aptusdt", "ts" => $apt_5min['id'], "open" => $apt_5min['open'], "close" => $apt_5min['close'], "low" => $apt_5min['low'], "high" => $apt_5min['high'], "amount" => $apt_5min['amount'], "vol" => $apt_5min['vol'], "count" => $apt_5min['count'], ); } }else{ $apt_5min_data = array( "symbol" => "aptusdt", "ts" => $apt_5min['id'], "open" => $apt_5min['open'], "close" => $apt_5min['close'], "low" => $apt_5min['low'], "high" => $apt_5min['high'], "amount" => $apt_5min['amount'], "vol" => $apt_5min['vol'], "count" => $apt_5min['count'], ); } if($apt_5min_data){ Db::name("apt_k_5min")->insert($apt_5min_data); echo 2; } } //15分钟 $apt_15min = $redis->get("bf_apt_15min"); $apt_15min = json_decode($apt_15min,true); if($apt_15min){ $apt_15min_data = []; $last_15min = Db::name("apt_k_15min")->order("ts desc")->find(); if($last_15min){ if($last_15min['ts'] != $apt_15min['id']){ $apt_15min_data = array( "symbol" => "aptusdt", "ts" => $apt_15min['id'], "open" => $apt_15min['open'], "close" => $apt_15min['close'], "low" => $apt_15min['low'], "high" => $apt_15min['high'], "amount" => $apt_15min['amount'], "vol" => $apt_15min['vol'], "count" => $apt_15min['count'], ); } }else{ $apt_15min_data = array( "symbol" => "aptusdt", "ts" => $apt_15min['id'], "open" => $apt_15min['open'], "close" => $apt_15min['close'], "low" => $apt_15min['low'], "high" => $apt_15min['high'], "amount" => $apt_15min['amount'], "vol" => $apt_15min['vol'], "count" => $apt_15min['count'], ); } if($apt_15min_data){ Db::name("apt_k_15min")->insert($apt_15min_data); echo 3; } } //30分钟 $apt_30min = $redis->get("bf_apt_30min"); $apt_30min = json_decode($apt_30min,true); if($apt_30min){ $apt_30min_data = []; $last_30min = Db::name("apt_k_30min")->order("ts desc")->find(); if($last_30min){ if($last_30min['ts'] != $apt_30min['id']){ $apt_30min_data = array( "symbol" => "aptusdt", "ts" => $apt_30min['id'], "open" => $apt_30min['open'], "close" => $apt_30min['close'], "low" => $apt_30min['low'], "high" => $apt_30min['high'], "amount" => $apt_30min['amount'], "vol" => $apt_30min['vol'], "count" => $apt_30min['count'], ); } }else{ $apt_30min_data = array( "symbol" => "aptusdt", "ts" => $apt_30min['id'], "open" => $apt_30min['open'], "close" => $apt_30min['close'], "low" => $apt_30min['low'], "high" => $apt_30min['high'], "amount" => $apt_30min['amount'], "vol" => $apt_30min['vol'], "count" => $apt_30min['count'], ); } if($apt_30min_data){ Db::name("apt_k_30min")->insert($apt_30min_data); echo 4; } } //60分钟 $apt_60min = $redis->get("bf_apt_60min"); $apt_60min = json_decode($apt_60min,true); if($apt_60min){ $apt_60min_data = []; $last_60min = Db::name("apt_k_1h")->order("ts desc")->find(); if($last_60min){ if($last_60min['ts'] != $apt_60min['id']){ $apt_60min_data = array( "symbol" => "aptusdt", "ts" => $apt_60min['id'], "open" => $apt_60min['open'], "close" => $apt_60min['close'], "low" => $apt_60min['low'], "high" => $apt_60min['high'], "amount" => $apt_60min['amount'], "vol" => $apt_60min['vol'], "count" => $apt_60min['count'], ); } }else{ $apt_60min_data = array( "symbol" => "aptusdt", "ts" => $apt_60min['id'], "open" => $apt_60min['open'], "close" => $apt_60min['close'], "low" => $apt_60min['low'], "high" => $apt_60min['high'], "amount" => $apt_60min['amount'], "vol" => $apt_60min['vol'], "count" => $apt_60min['count'], ); } if($apt_60min_data){ Db::name("apt_k_1h")->insert($apt_60min_data); echo 5; } } //1天 $apt_1day = $redis->get("bf_apt_1day"); $apt_1day = json_decode($apt_1day,true); if($apt_1day){ $apt_1day_data = []; $last_1day = Db::name("apt_k_24h")->order("ts desc")->find(); if($last_1day){ if($last_1day['ts'] != $apt_1day['id']){ $apt_1day_data = array( "symbol" => "aptusdt", "ts" => $apt_1day['id'], "open" => $apt_1day['open'], "close" => $apt_1day['close'], "low" => $apt_1day['low'], "high" => $apt_1day['high'], "amount" => $apt_1day['amount'], "vol" => $apt_1day['vol'], "count" => $apt_1day['count'], ); } }else{ $apt_1day_data = array( "symbol" => "aptusdt", "ts" => $apt_1day['id'], "open" => $apt_1day['open'], "close" => $apt_1day['close'], "low" => $apt_1day['low'], "high" => $apt_1day['high'], "amount" => $apt_1day['amount'], "vol" => $apt_1day['vol'], "count" => $apt_1day['count'], ); } if($apt_1day_data){ Db::name("apt_k_24h")->insert($apt_1day_data); echo 6; } } //跟新rate表数据 $now_apt_1day = $redis->get("apt_1day"); $now_apt_1day = json_decode($now_apt_1day,true); $update = array( "amount" => $now_apt_1day['amount'], "high" => $now_apt_1day['high'], "low" => $now_apt_1day['low'], "open" => $now_apt_1day['open'], ); $update['increase'] = sprintf("%.4f",($now_apt_1day['close']-$update['open'])/$update['open']); Db::name("app_rate")->where("symbol","aptusdt")->update($update); //1个月 $apt_1mon = $redis->get("bf_apt_1mon"); $apt_1mon = json_decode($apt_1mon,true); if($apt_1mon){ $apt_1mon_data = []; $last_1mon = Db::name("apt_k_1month")->order("ts desc")->find(); if($last_1mon){ if($last_1mon['ts'] != $apt_1mon['id']){ $apt_1mon_data = array( "symbol" => "aptusdt", "ts" => $apt_1mon['id'], "open" => $apt_1mon['open'], "close" => $apt_1mon['close'], "low" => $apt_1mon['low'], "high" => $apt_1mon['high'], "amount" => $apt_1mon['amount'], "vol" => $apt_1mon['vol'], "count" => $apt_1mon['count'], ); } }else{ $apt_1mon_data = array( "symbol" => "aptusdt", "ts" => $apt_1mon['id'], "open" => $apt_1mon['open'], "close" => $apt_1mon['close'], "low" => $apt_1mon['low'], "high" => $apt_1mon['high'], "amount" => $apt_1mon['amount'], "vol" => $apt_1mon['vol'], "count" => $apt_1mon['count'], ); } if($apt_1mon_data){ Db::name("apt_k_1month")->insert($apt_1mon_data); echo 7; } } } //4位小数的随机数 public function randomFloat($min = 0, $max = 10 , $localnum = 4) { if($localnum == 4) { if ($max - $min <= 0.0002) { return 0; } $rand = mt_rand() / mt_getrandmax() * ($max - $min-0.0001); $num = $min + $rand; $number = sprintf("%.4f", $num); if($number == $min){ $number += 0.0001; } }else if($localnum == 5){ if ($max - $min <= 0.00002) { return 0; } $rand = mt_rand() / mt_getrandmax() * ($max - $min-0.00001); $num = $min + $rand; $number = sprintf("%.5f", $num); if($number == $min){ $number += 0.00001; } }else if($localnum == 6){ if ($max - $min <= 0.000002) { return 0; } $rand = mt_rand() / mt_getrandmax() * ($max - $min-0.000001); $num = $min + $rand; $number = sprintf("%.6f", $num); if($number == $min){ $number += 0.000001; } }else if($localnum == 2){ if ($max - $min <= 0.02) { return 0; } $rand = mt_rand() / mt_getrandmax() * ($max - $min-0.01); $num = $min + $rand; $number = sprintf("%.2f", $num); if($number == $min){ $number += 0.01; } }else if($localnum == 3){ if ($max - $min <= 0.001) { return 0; } $rand = mt_rand() / mt_getrandmax() * ($max - $min-0.001); $num = $min + $rand; $number = sprintf("%.3f", $num); if($number == $min){ $number += 0.001; } } return $number; } }