12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306 |
- <?php
- namespace Mobile\Controller;
- class AutoexeController extends \Think\Controller
- {
- protected function _initialize()
- {
- $allow_action = array("hycarryout", "ups_and_downs", "getnewprice", "setwl", "setwl_ty", "autokjsy", "releasedjprofit", "autoxjtade", "authsharesjsy", "releaseissue", "hycarryout_ty", "AutoCreateOrder", "AutoCloseOrder");
- if (!in_array(ACTION_NAME, $allow_action)) {
- $this->error("非法操作!");
- }
- }
- //永续合约自动结算
- public function AutoCloseOrder()
- {
- $list = M("leverorder")->where(array('status' => 2))->select();
- if (!empty($list)) {
- $sysinfo = M("leversetting")->where(array('id' => 1))->find();
- $lever_fee = $sysinfo['lever_fee'];//费率
- $new_close = [];
- foreach ($list as $key => $vo) {
- $id = $vo['id'];
- $uid = $vo['uid'];
- $uinfo = M("user")->where(array('id' => $uid))->field("id,username")->find();
- // $minfo = M("user_coin")->where(array('userid' => $uid))->find();
- // $bond_money_info = M("levermoney")->where(array('uid' => $uid))->find();
- $lowercoin = $vo['coinname'];
- $user = M('levermoney')->where(['uid' => $uid])->find();
- if (!isset($new_close[$vo['coinname']])) {
- if ($lowercoin == 'ukbusdt') {
- $o_symbol = $vo['symbol'];
- $symbol_arr = explode('/', $o_symbol);
- $symbol = strtolower($symbol_arr[0]) . "_" . strtolower($symbol_arr[1]);
- $mlist = M("market")->where(array('name' => $symbol))->field("new_price,min_price")->find();
- $close = $mlist['new_price'];
- } else {
- $oy_list = do_request(oy_url($vo['coinname'], 'api/v5/market/ticker?instId='), []);
- $oy_list = $oy_list->data[0];
- // $url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$lowercoin;
- // $result = $this->getprice($url);
- // $pdata = $result['data'][0];
- $close = $oy_list->last;//现价
- $new_close[$vo['coinname']] = $close;
- }
- } else {
- $close = $new_close[$vo['coinname']];
- }
- if ($close) {
- $price = $vo['price']; //开仓价格
- $direction = $vo['direction']; //交易方向
- $zsprice = $vo['zsprice']; //止损价格
- $zyprice = $vo['zyprice']; //止盈价格
- if ($zyprice > 0) {
- if ($direction == 1) {
- // 买多
- if ($close >= $zyprice) {
- //收益计算
- //例:BTC/USDT永续合约开多仓100张,成交价格为10000 USDT,以11000 USDT价格平仓,
- //则:平仓盈亏 = 收益(平仓) =( 11000 – 10000 )* 100 * 0.001 = 100 USDT。
- $profit = sprintf("%.2f", ($close - $price) * $vo['num']);
- $tprofit = abs($profit);
- $data['pc_price'] = $close;
- $data['status'] = 3;
- $data['yk_status'] = 1;
- $data['ylmoney'] = $tprofit;
- $data['endtime'] = date("Y-m-d H:i:s", time());
- $result = M("leverorder")->where(array('id' => $id))->save($data);
- $ubill['uid'] = $uid;
- $ubill['username'] = $uinfo['username'];
- $ubill['num'] = $tprofit;
- $ubill['coinname'] = "lever";
- $ubill['afternum'] = $user['money'] + $tprofit;
- $ubill['type'] = 20;
- $ubill['addtime'] = date("Y-m-d H:i:s", time());
- $ubill['st'] = 1;
- $ubill['remark'] = L('合约交易止盈平仓盈利收益');
- $ubillre = M("bill")->add($ubill);
- $bond_ubill = [
- 'uid' => $uid,
- 'username' => $uinfo['username'],
- 'num' => $vo['bond'],
- 'coinname' => 'usdt',
- 'afternum' => $user['money'] + $vo['bond'],
- 'type' => 25,
- 'addtime' => date("Y-m-d H:i:s", time()),
- 'st' => 1,
- 'remark' => '合約交易止盈平倉返還佔用合約'
- ];// 扣除合约保证金
- M("levermoney")->where(array('uid' => $uid))->setInc('money', $vo['bond']);
- M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
- M("bill")->add($bond_ubill);
- if ($result && $ubillre) {
- M("levermoney")->where(array('uid' => $uid))->setInc('money', $tprofit);
- echo "=订单ID:" . $id . ",止盈平仓=";
- }
- }
- } elseif ($direction == 2) {
- //买空
- if ($close <= $zyprice) {
- $profit = sprintf("%.2f", ($price - $close) * $vo['num']);
- $tprofit = abs($profit);
- $data['pc_price'] = $close;
- $data['status'] = 3;
- $data['yk_status'] = 1;
- $data['ylmoney'] = $tprofit;
- $data['endtime'] = date("Y-m-d H:i:s", time());
- $result = M("leverorder")->where(array('id' => $id))->save($data);
- $ubill['uid'] = $uid;
- $ubill['username'] = $uinfo['username'];
- $ubill['num'] = $tprofit;
- $ubill['coinname'] = "lever";
- $ubill['afternum'] = $user['money'] + $tprofit;
- $ubill['type'] = 20;
- $ubill['addtime'] = date("Y-m-d H:i:s", time());
- $ubill['st'] = 1;
- $ubill['remark'] = L('合约交易止盈平仓盈利收益');
- $ubillre = M("bill")->add($ubill);
- $bond_ubill = [
- 'uid' => $uid,
- 'username' => $uinfo['username'],
- 'num' => $vo['bond'],
- 'coinname' => 'usdt',
- 'afternum' => $user['money'] + $vo['bond'],
- 'type' => 25,
- 'addtime' => date("Y-m-d H:i:s", time()),
- 'st' => 1,
- 'remark' => '合約交易止盈平倉返還佔用合約'
- ];// 扣除合约保证金
- M("levermoney")->where(array('uid' => $uid))->setInc('money', $vo['bond']);
- M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
- M("bill")->add($bond_ubill);
- if ($result && $ubillre) {
- M("levermoney")->where(array('uid' => $uid))->setInc('money', $tprofit);
- echo "=订单ID:" . $id . ",止盈平仓=";
- }
- }
- }
- }
- if ($zsprice > 0) {
- if ($direction == 1) {
- // 买多
- if ($close <= $zsprice) {
- //收益计算
- //例:BTC/USDT永续合约开多仓100张,成交价格为10000 USDT,以11000 USDT价格平仓,
- //则:平仓盈亏 = 收益(平仓) =( 11000 – 10000 )* 100 * 0.001 = 100 USDT。
- $profit = sprintf("%.2f", ($close - $price) * $vo['num']);
- $tprofit = abs($profit);
- if ($tprofit < $user['money']) {
- //亏损小于保证金
- $data['pc_price'] = $close;
- $data['status'] = 3;
- $data['yk_status'] = 2;
- $data['ylmoney'] = $tprofit;
- $data['endtime'] = date("Y-m-d H:i:s", time());
- $bond = ($user['money'] + $vo['bond']) - $tprofit;
- $ubill['uid'] = $uid;
- $ubill['username'] = $uinfo['username'];
- $ubill['num'] = $vo['bond'];
- $ubill['afternum'] = $user['money'] + $vo['bond'];
- $ubill['coinname'] = "lever";
- $ubill['type'] = 20;
- $ubill['addtime'] = date("Y-m-d H:i:s", time());
- $ubill['st'] = 1;
- $ubill['remark'] = '合約交易止損平倉返回保證金';
- $ubillre = M("bill")->add($ubill);
- $ubill['num'] = $tprofit;
- $ubill['afternum'] = $bond;
- $ubill['coinname'] = "lever";
- $ubill['st'] = 2;
- $ubill['remark'] = '合約交易止損平倉虧損';
- $ubillre = M("bill")->add($ubill);
- M("leverorder")->where(array('id' => $id))->save($data);
- M("levermoney")->where(['uid' => $uid])->setInc('money', $vo['bond']);
- M("levermoney")->where(['uid' => $uid])->setDec('money', $tprofit);
- M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
- echo "=订单ID:" . $id . ",止损平仓=";
- } else {
- if ($user['money'] + $vo['bond'] >= $tprofit) {
- $user_bond = $vo['bond'] - ($tprofit - $user['money']);
- $data['pc_price'] = $close;
- $data['status'] = 3;
- $data['yk_status'] = 2;
- $data['ylmoney'] = $tprofit;
- $data['endtime'] = date("Y-m-d H:i:s", time());
- $bond = $tprofit - $vo['bond'];
- $ubill['uid'] = $uid;
- $ubill['username'] = $uinfo['username'];
- $ubill['num'] = $user_bond;
- $ubill['afternum'] = $user_bond;
- $ubill['coinname'] = "lever";
- $ubill['type'] = 20;
- $ubill['addtime'] = date("Y-m-d H:i:s", time());
- $ubill['st'] = 1;
- $ubill['remark'] = '合約交易止損平倉退回剩餘保證金';
- $ubillre = M("bill")->add($ubill);
- $ubill['num'] = $user['money'];
- $ubill['afternum'] = 0;
- $ubill['type'] = 20;
- $ubill['st'] = 2;
- $ubill['remark'] = '合約交易手動平倉虧損';
- M("leverorder")->where(array('id' => $id))->save($data);
- M("levermoney")->where(['uid' => $uid])->setInc('money', $user_bond);
- M("levermoney")->where(array('uid' => $uid))->setDec('money', $user['money']);
- M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
- echo "=订单ID:" . $id . ",止损平仓=";
- } else {
- $data['pc_price'] = $close;
- $data['status'] = 3;
- $data['yk_status'] = 2;
- $data['ylmoney'] = $tprofit;
- $data['endtime'] = date("Y-m-d H:i:s", time());
- $bond = $tprofit - $vo['bond'];
- $ubill['uid'] = $uid;
- $ubill['username'] = $uinfo['username'];
- $ubill['num'] = $user['money'];
- $ubill['afternum'] = 0;
- $ubill['coinname'] = "lever";
- $ubill['type'] = 20;
- $ubill['addtime'] = date("Y-m-d H:i:s", time());
- $ubill['st'] = 1;
- $ubill['remark'] = '合約交易ID' . $vo['id'] . '止損平倉扣除完保證金,並扣除合約帳戶';
- $ubillre = M("bill")->add($ubill);
- M("leverorder")->where(array('id' => $id))->save($data);
- M("levermoney")->where(['uid' => $uid])->setDec('money', $user['money']);
- M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
- echo "=订单ID:" . $id . ",止损平仓=";
- }
- }
- }
- } elseif ($direction == 2) {
- //买空
- if ($close >= $zsprice) {
- $profit = sprintf("%.2f", ($price - $close) * $vo['num']);
- $tprofit = abs($profit);
- if ($tprofit < $user['money']) {
- //亏损小于保证金
- $data['pc_price'] = $close;
- $data['status'] = 3;
- $data['yk_status'] = 2;
- $data['ylmoney'] = $tprofit;
- $data['endtime'] = date("Y-m-d H:i:s", time());
- $bond = ($user['money'] + $vo['bond']) - $tprofit;
- $ubill['uid'] = $uid;
- $ubill['username'] = $uinfo['username'];
- $ubill['num'] = $vo['bond'];
- $ubill['afternum'] = $user['money'] + $vo['bond'];
- $ubill['coinname'] = "lever";
- $ubill['type'] = 20;
- $ubill['addtime'] = date("Y-m-d H:i:s", time());
- $ubill['st'] = 1;
- $ubill['remark'] = '合約交易止損平倉返回保證金';
- $ubillre = M("bill")->add($ubill);
- $ubill['num'] = $tprofit;
- $ubill['afternum'] = $bond;
- $ubill['coinname'] = "lever";
- $ubill['st'] = 2;
- $ubill['remark'] = '合約交易止損平倉虧損';
- $ubillre = M("bill")->add($ubill);
- M("leverorder")->where(array('id' => $id))->save($data);
- M("levermoney")->where(['uid' => $uid])->setInc('money', $vo['bond']);
- M("levermoney")->where(['uid' => $uid])->setDec('money', $tprofit);
- M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
- echo "=订单ID:" . $id . ",止损平仓=";
- } else {
- if ($user['money'] + $vo['bond'] >= $tprofit) {
- $user_bond = $vo['bond'] - ($tprofit - $user['money']);
- $data['pc_price'] = $close;
- $data['status'] = 3;
- $data['yk_status'] = 2;
- $data['ylmoney'] = $tprofit;
- $data['endtime'] = date("Y-m-d H:i:s", time());
- $bond = $tprofit - $vo['bond'];
- $ubill['uid'] = $uid;
- $ubill['username'] = $uinfo['username'];
- $ubill['num'] = $user_bond;
- $ubill['afternum'] = $user_bond;
- $ubill['coinname'] = "lever";
- $ubill['type'] = 20;
- $ubill['addtime'] = date("Y-m-d H:i:s", time());
- $ubill['st'] = 1;
- $ubill['remark'] = '合約交易止損平倉退回剩餘保證金';
- $ubillre = M("bill")->add($ubill);
- $ubill['num'] = $user['money'];
- $ubill['afternum'] = 0;
- $ubill['type'] = 20;
- $ubill['st'] = 2;
- $ubill['remark'] = '合約交易手動平倉虧損';
- M("leverorder")->where(array('id' => $id))->save($data);
- M("levermoney")->where(['uid' => $uid])->setInc('money', $user_bond);
- M("levermoney")->where(array('uid' => $uid))->setDec('money', $user['money']);
- M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
- echo "=订单ID:" . $id . ",止损平仓=";
- } else {
- $data['pc_price'] = $close;
- $data['status'] = 3;
- $data['yk_status'] = 2;
- $data['ylmoney'] = $tprofit;
- $data['endtime'] = date("Y-m-d H:i:s", time());
- $bond = $tprofit - $vo['bond'];
- $ubill['uid'] = $uid;
- $ubill['username'] = $uinfo['username'];
- $ubill['num'] = $user['money'];
- $ubill['afternum'] = 0;
- $ubill['coinname'] = "lever";
- $ubill['type'] = 20;
- $ubill['addtime'] = date("Y-m-d H:i:s", time());
- $ubill['st'] = 1;
- $ubill['remark'] = '合約交易ID' . $vo['id'] . '止損平倉扣除完保證金,並扣除合約帳戶';
- $ubillre = M("bill")->add($ubill);
- M("leverorder")->where(array('id' => $id))->save($data);
- M("levermoney")->where(['uid' => $uid])->setDec('money', $user['money']);
- M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
- echo "=订单ID:" . $id . ",止损平仓=";
- }
- }
- }
- }
- }
- if ($zsprice == 0) {
- if ($direction == 1) {
- $profit_fd = sprintf("%.2f", ($close - $price) * $vo['num']);
- if ($profit_fd < 0) {
- if (abs($profit_fd) > ($user['money'] + $user['moneyd'])) {
- $data['pc_price'] = $close;
- $data['status'] = 4;
- $data['yk_status'] = 2;
- $data['ylmoney'] = $profit_fd;
- $data['endtime'] = date("Y-m-d H:i:s", time());
- $ubill['uid'] = $uid;
- $ubill['username'] = $uinfo['username'];
- $ubill['num'] = $user['money'];
- $ubill['afternum'] = 0;
- $ubill['coinname'] = "lever";
- $ubill['type'] = 20;
- $ubill['addtime'] = date("Y-m-d H:i:s", time());
- $ubill['st'] = 1;
- $ubill['remark'] = '合約交易ID' . $vo['id'] . '爆倉';
- $ubillre = M("bill")->add($ubill);
- M("leverorder")->where(array('id' => $id))->save($data);
- M("levermoney")->where(['uid' => $uid])->save(['money' => 0, 'moneyd' => 0]);
- $pc_list = M('leverorder')->where(['uid' => $uid])->where(['status' => 2])->select();
- foreach ($pc_list as $item) {
- $direction = $item['direction'];
- if ($direction == 1) { //做多
- $profit = sprintf("%.2f", ($close - $item['price']) * $item['num']);
- } elseif ($direction == 2) { //做空
- $profit = sprintf("%.2f", ($item['price'] - $close) * $item['num']);
- }
- if ($profit >= 0) {
- $yk_status = 1;
- } else {
- $yk_status = 2;
- }
- $tprofit = abs($profit);
- $datas['pc_price'] = $close;
- $datas['status'] = 3;
- $datas['yk_status'] = $yk_status;
- $datas['ylmoney'] = $tprofit;
- $datas['endtime'] = date("Y-m-d H:i:s", time());
- $result = M("leverorder")->where(array('id' => $item['id'], 'status' => 2))->save($datas);
- $user = M('levermoney')->where(['uid' => $uid])->find();
- if ($result) {
- if ($profit >= 0) {
- M("levermoney")->where(array('uid' => $uid))->setInc('money', $tprofit);
- $ubill['uid'] = $uid;
- $ubill['username'] = $uinfo['username'];
- $ubill['num'] = $tprofit;
- $ubill['coinname'] = "lever";
- $ubill['afternum'] = $user['money'] + $tprofit;
- $ubill['type'] = 20;
- $ubill['addtime'] = date("Y-m-d H:i:s", time());
- $ubill['st'] = 1;
- $ubill['remark'] = L('合约交易自動平仓盈利收益');
- $ubillre = M("bill")->add($ubill);
- }
- }
- }
- echo "=订单ID:" . $id . ",爆仓=";
- }
- } elseif ($direction == 2) {
- $profit_fd = sprintf("%.2f", ($price - $close) * $vo['num']);
- if ($profit_fd < 0) {
- if (abs($profit_fd) > ($vo['bond'] + $user['moneyd'])) {
- $data['pc_price'] = $close;
- $data['status'] = 4;
- $data['yk_status'] = 2;
- $data['ylmoney'] = $profit_fd;
- $data['endtime'] = date("Y-m-d H:i:s", time());
- $ubill['uid'] = $uid;
- $ubill['username'] = $uinfo['username'];
- $ubill['num'] = $user['money'];
- $ubill['afternum'] = 0;
- $ubill['coinname'] = "lever";
- $ubill['type'] = 20;
- $ubill['addtime'] = date("Y-m-d H:i:s", time());
- $ubill['st'] = 1;
- $ubill['remark'] = '合約交易ID' . $vo['id'] . '爆倉';
- $ubillre = M("bill")->add($ubill);
- M("leverorder")->where(array('id' => $id))->save($data);
- M("levermoney")->where(['uid' => $uid])->save(['money' => 0, 'moneyd' => 0]);
- $pc_list = M('leverorder')->where(['uid' => $uid])->where(['status' => 2])->select();
- foreach ($pc_list as $item) {
- $direction = $item['direction'];
- if ($direction == 1) { //做多
- $profit = sprintf("%.2f", ($close - $item['price']) * $item['num']);
- } elseif ($direction == 2) { //做空
- $profit = sprintf("%.2f", ($item['price'] - $close) * $item['num']);
- }
- if ($profit >= 0) {
- $yk_status = 1;
- } else {
- $yk_status = 2;
- }
- $tprofit = abs($profit);
- $datas['pc_price'] = $close;
- $datas['status'] = 3;
- $datas['yk_status'] = $yk_status;
- $datas['ylmoney'] = $tprofit;
- $datas['endtime'] = date("Y-m-d H:i:s", time());
- $result = M("leverorder")->where(array('id' => $item['id'], 'status' => 2))->save($datas);
- $user = M('levermoney')->where(['uid' => $uid])->find();
- if ($result) {
- if ($profit >= 0) {
- M("levermoney")->where(array('uid' => $uid))->setInc('money', $tprofit);
- $ubill['uid'] = $uid;
- $ubill['username'] = $uinfo['username'];
- $ubill['num'] = $tprofit;
- $ubill['coinname'] = "lever";
- $ubill['afternum'] = $user['money'] + $tprofit;
- $ubill['type'] = 20;
- $ubill['addtime'] = date("Y-m-d H:i:s", time());
- $ubill['st'] = 1;
- $ubill['remark'] = L('合约交易自動平仓盈利收益');
- $ubillre = M("bill")->add($ubill);
- }
- }
- }
- echo "=订单ID:" . $id . ",爆仓=";
- }
- }
- }
- }
- }
- // echo "正常请求".$close;
- } else {
- echo "未获取到参数" . $id;
- }
- }
- }
- }
- //永续合约委托定单自动建仓
- public function AutoCreateOrder()
- {
- $list = M("leverorder")->where(array('status' => 1))->select();
- if (!empty($list)) {
- $sysinfo = M("leversetting")->where(array('id' => 1))->find();
- $lever_fee = $sysinfo['lever_fee'];//费率
- foreach ($list as $key => $vo) {
- $id = $vo['id'];
- $uid = $vo['uid'];
- $uinfo = M("user")->where(array('id' => $uid))->field("id,username")->find();
- $minfo = M("user_coin")->where(array('userid' => $uid))->find();
- $usermoney = M('levermoney')->where(['uid' => $uid])->find();
- $lowercoin = $vo['coinname'];
- if ($lowercoin == 'ukbusdt') {
- $o_symbol = $vo['symbol'];
- $symbol_arr = explode('/', $o_symbol);
- $symbol = strtolower($symbol_arr[0]) . "_" . strtolower($symbol_arr[1]);
- $mlist = M("market")->where(array('name' => $symbol))->field("new_price,min_price")->find();
- $close = $mlist['new_price'];
- } else {
- $oy_list = do_request(oy_url($vo['coinname'], 'api/v5/market/ticker?instId='), []);
- $oy_list = $oy_list->data[0];
- $close = $oy_list->last;//现价
- }
- if ($close) {
- //手续费计算(开仓价格 * 开仓数量 * 手续费率) ,开仓时扣除手续费
- $wt_price = $vo['wt_price']; //委托价格
- $direction = $vo['direction']; //交易方向
- if ($direction == 1) { //做多(上涨) 当前价小于等于委托价格就开仓 要跌了才开仓
- if ($close <= $wt_price) {
- $data['fee'] = $fee = $vo['fee'];
- if ($usermoney['money'] >= $fee) {
- $data['price'] = $close;
- $data['status'] = 2;
- $data['addtime'] = date("Y-m-d H:i:s", time());
- $re_a = M("leverorder")->where(array('id' => $id))->save($data);
- $re_b = M("levermoney")->where(array('uid' => $uid))->setDec('money', $fee);
- $ubill['uid'] = $uid;
- $ubill['username'] = $uinfo['username'];
- $ubill['num'] = $fee;
- $ubill['coinname'] = "lever";
- $ubill['afternum'] = $usermoney['money'] - $fee;
- $ubill['type'] = 25;
- $ubill['addtime'] = date("Y-m-d H:i:s", time());
- $ubill['st'] = 2;
- $ubill['remark'] = L('合约交易开仓手续费');
- $re_c = M("bill")->add($ubill);
- if ($re_a && $re_b && $re_c) {
- echo "=订单ID:" . $id . ",开仓成功=";
- }
- } else {
- echo "=订单ID:" . $id . ",手续费不足=";
- }
- } else {
- echo "=订单ID:" . $id . ",做多,价格不合适=";
- }
- } else if ($direction == 2) { //做空(下跌) 当前价大于等于委托价格就开仓 要涨了才开仓
- if ($close >= $wt_price) {
- $data['fee'] = $fee = $vo['fee'];
- if ($usermoney['money'] >= $fee) {
- $data['price'] = $close;
- $data['status'] = 2;
- $data['addtime'] = date("Y-m-d H:i:s", time());
- $re_a = M("leverorder")->where(array('id' => $id))->save($data);
- $re_b = M("levermoney")->where(array('uid' => $uid))->setDec('money', $fee);
- $ubill['uid'] = $uid;
- $ubill['username'] = $uinfo['username'];
- $ubill['num'] = $fee;
- $ubill['coinname'] = "usdt";
- $ubill['afternum'] = $usermoney['money'] - $fee;
- $ubill['type'] = 25;
- $ubill['addtime'] = date("Y-m-d H:i:s", time());
- $ubill['st'] = 2;
- $ubill['remark'] = L('合约交易开仓手续费');
- $re_c = M("bill")->add($ubill);
- if ($re_a && $re_b && $re_c) {
- echo "=订单ID:" . $id . ",开仓成功=";
- }
- } else {
- echo "=订单ID:" . $id . ",手续费不足=";
- }
- } else {
- echo "=订单ID:" . $id . ",做空,价格不合适=";
- }
- }
- }else{
- echo "未获取到参数";
- }
- }
- }
- }
- //自动释放冻结的认购币,设置计划任务,每天执行一次
- public function releaseissue()
- {
- $nowday = date("Y-m-d", time());
- $map['status'] = 1;
- $map['endday'] = array('elt', $nowday);
- $list = M("issue_log")->where($map)->select();
- if (!empty($list)) {
- foreach ($list as $key => $vo) {
- $id = $vo['id'];
- $uid = $vo['uid'];
- $num = $vo['num'];
- $cname = trim($vo['coinname']);
- $cnamed = trim($vo['coinname']) . "d";
- //修改记录状态
- $result = M("issue_log")->where(array('id' => $id))->save(array('status' => 2));
- if ($result) {
- $minfo = M("user_coin")->where(array('userid' => $uid))->find();
- //扣除冻结的资产
- M("user_coin")->where(array('userid' => $uid))->setDec($cnamed, $num);
- //增加可用资产的数量
- M("user_coin")->where(array('userid' => $uid))->setInc($cname, $num);
- //写入日志
- $data['uid'] = $uid;
- $data['username'] = $vo['account'];
- $data['num'] = $num;
- $data['coinname'] = $cname;
- $data['afternum'] = $minfo[$cname] + $num;
- $data['type'] = 18;
- $data['addtime'] = date("Y-m-d H:i:s", time());
- $data['st'] = 1;
- $data['remark'] = L("认购资产释放");
- M("bill")->add($data);
- echo "==认购记录ID:" . $id . "释放成功";
- } else {
- echo "==认购记录ID:" . $id . "释放失败";
- }
- }
- } else {
- echo "==没有可释放认购记录==";
- }
- }
- //委托订单自动交易
- //设置成5-10秒执行一次的计划任务
- public function autoxjtade()
- {
- $list = M("bborder")->where(array('ordertype' => 1, 'status' => 1))->select();
- if (!empty($list)) {
- foreach ($list as $k => $v) {
- $type = $v['type'];
- $uid = $v['uid'];
- $id = $v['id'];
- $symbol = strtolower($v['coin']) . 'usdt';
- $lowercoin = strtolower($v['coin']);
- //限价单价
- $xjprice = $v['xjprice'];
- $sxfbl = $v['sxfbl'];
- if ($lowercoin == "ukb") {
- $priceinfo = M("market")->where(array('name' => "ukb_usdt"))->field("new_price")->find();
- $newprice = $priceinfo['new_price'];
- }else if ($lowercoin == "gcb") {
- $priceinfo = M("market")->where(array('name' => "gcb_usdt"))->field("new_price")->find();
- $newprice = $priceinfo['new_price'];
- } else {
- //获取当前行情价
- $oy_list = do_request(oy_url(strtolower($v['coin']), 'api/v5/market/ticker?instId='), []);
- $oy_list = $oy_list->data[0];
- $newprice = $oy_list->last;//现价
- // $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $symbol;
- // $newprice = $this->getnewprice($coinapi);
- }
- if ($newprice){
- //买入,当行情价小于等于限价时则交易
- $minfo = M("user_coin")->where(array('userid' => $uid))->find();
- if ($type == 1) {
- $usdtnum = $v['usdtnum'];
- if ($newprice <= $xjprice) {
- //计算能够买到的量
- $buy_coinnum = sprintf("%.8f", ($usdtnum / $newprice));
- //手续费
- $fee = $buy_coinnum * $sxfbl / 100;
- //实际到账号的金额
- $tcoinnum = $buy_coinnum - $fee;
- //更新订单
- $data['coinnum'] = $tcoinnum;
- $data['price'] = $newprice;
- $data['tradetime'] = date("Y-m-d H:i:s", time());
- $data['fee'] = $fee;
- $data['status'] = 2;
- $savere = M("bborder")->where(array('id' => $id))->save($data);
- //增加购买数量并写入日志
- $incre = M("user_coin")->where(array('userid' => $uid))->setInc($lowercoin, $tcoinnum);
- $cincbill['uid'] = $uid;
- $cincbill['username'] = $v['account'];
- $cincbill['num'] = $tcoinnum;
- $cincbill['coinname'] = $lowercoin;
- $cincbill['afternum'] = $minfo[$lowercoin] + $tcoinnum;
- $cincbill['type'] = 10;
- $cincbill['addtime'] = date("Y-m-d H:i:s", time());
- $cincbill['st'] = 1;
- $cincbill['remark'] = L('币币交易限价购买委托成交');
- $cincre = M("bill")->add($cincbill);
- //扣除冻结的USDT并写入日志
- $decre = M("user_coin")->where(array('userid' => $uid))->setDec("usdtd", $usdtnum);
- $uincbill['uid'] = $uid;
- $uincbill['username'] = $v['account'];
- $uincbill['num'] = $usdtnum;
- $uincbill['coinname'] = "usdt";
- $uincbill['afternum'] = $minfo['usdt'];
- $uincbill['type'] = 9;
- $uincbill['addtime'] = date("Y-m-d H:i:s", time());
- $uincbill['st'] = 2;
- $uincbill['remark'] = L('币币交易限价购买委托成交');
- $uincre = M("bill")->add($uincbill);
- if ($savere && $cincre && $uincre) {
- $notice['uid'] = $uid;
- $notice['account'] = $v['account'];
- $notice['title'] = L('币币交易限价委托交易');
- $notice['content'] = L('币币交易限价购买委托订单购买成功');
- $notice['addtime'] = date("Y-m-d H:i:s", time());
- $notice['status'] = 1;
- M("notice")->add($notice);
- echo "==委托订单ID:" . $id . ",购买成功==";
- }
- } else {
- echo "==委托订单ID:" . $id . ",没有达到限价购买价格==";
- }
- //卖出,当行情价大于等于限价时则交易
- } elseif ($type == 2) {
- $coinnum = $v['coinnum'];
- if ($newprice >= $xjprice) {
- //求出卖出所得的USDT量
- $allusdt = sprintf("%.8f", ($coinnum * $newprice));
- //求出手续费
- $fee = $allusdt * $sxfbl / 100;
- //求出实际到账USDT量
- $tusdtnum = $allusdt - $fee;
- //更新订单
- $data['usdtnum'] = $tusdtnum;
- $data['price'] = $newprice;
- $data['tradetime'] = date("Y-m-d H:i:s", time());
- $data['fee'] = $fee;
- $data['status'] = 2;
- $savere = M("bborder")->where(array('id' => $id))->save($data);
- //增加卖出所得的USDT量并写入日志
- $incre = M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $tusdtnum);
- $uincbill['uid'] = $uid;
- $uincbill['username'] = $v['account'];
- $uincbill['num'] = $tusdtnum;
- $uincbill['coinname'] = 'usdt';
- $uincbill['afternum'] = $minfo['usdt'] + $tusdtnum;
- $uincbill['type'] = 9;
- $uincbill['addtime'] = date("Y-m-d H:i:s", time());
- $uincbill['st'] = 1;
- $uincbill['remark'] = L('币币交易限价出售委托成交');
- $uincre = M("bill")->add($uincbill);
- //扣除冻结的卖出币量并写入日志
- $decre = M("user_coin")->where(array('userid' => $uid))->setDec($lowercoin . "d", $coinnum);
- $cincbill['uid'] = $uid;
- $cincbill['username'] = $v['account'];
- $cincbill['num'] = $coinnum;
- $cincbill['coinname'] = $lowercoin;
- $cincbill['afternum'] = $minfo[$lowercoin] - $coinnum;
- $cincbill['type'] = 10;
- $cincbill['addtime'] = date("Y-m-d H:i:s", time());
- $cincbill['st'] = 2;
- $cincbill['remark'] = L('币币交易限价出售委托成交');
- $cincre = M("bill")->add($cincbill);
- if ($savere && $cincre && $uincre) {
- $notice['uid'] = $uid;
- $notice['account'] = $v['account'];
- $notice['title'] = L('币币交易限价委托交易');
- $notice['content'] = L('币币交易限价购买委托订单出售成功');
- $notice['addtime'] = date("Y-m-d H:i:s", time());
- $notice['status'] = 1;
- M("notice")->add($notice);
- echo "==委托订单ID:" . $id . ",出售成功==";
- }
- } else {
- echo "==委托订单ID:" . $id . ",没有达到限价出售价格==";
- }
- }
- }else{
- echo "未获取到参数";
- }
- }
- } else {
- echo "没有限价委托可交易!";
- }
- }
- //释放冻结的矿机收益币
- //设置一天执行一次的计划任务
- public function releasedjprofit()
- {
- $nowday = date("Y-m-d", time());
- $where['thawday'] = array('elt', $nowday);
- $where['status'] = array('eq', 1);
- $list = M("djprofit")->where($where)->select();
- if (!empty($list)) {
- foreach ($list as $key => $vo) {
- $id = $vo['id'];
- $uid = $vo['uid'];
- $username = $vo['username'];
- $num = $vo['num'];
- $coinname = trim($vo['coin']);
- $minfo = M("user_coin")->where(array('userid' => $uid))->find();
- //修改冻结状态
- M("djprofit")->where(array('id' => $id))->save(array('status' => 2));
- //添加财务日志
- $billdata['uid'] = $uid;
- $billdata['username'] = $username;
- $billdata['num'] = $num;
- $billdata['coinname'] = $coinname;
- $billdata['afternum'] = $minfo[$coinname] + $num;
- $billdata['type'] = 8;
- $billdata['addtime'] = date("Y-m-d H:i:s", time());
- $billdata['st'] = 1;
- $billdata['remark'] = L('释放冻结收益');
- M("bill")->add($billdata);
- //增加会员资产,减少冻结额度
- $coinname_d = $coinname . "d";
- M("user_coin")->where(array('userid' => $uid))->setDec($coinname_d, $num);
- M("user_coin")->where(array('userid' => $uid))->setInc($coinname, $num);
- $notice['uid'] = $uid;
- $notice['account'] = $username;
- $notice['title'] = L('释放冻结收益');
- $notice['content'] = L('您冻结的矿机收益释放成功,可以交易');
- $notice['addtime'] = date("Y-m-d H:i:s", time());
- $notice['status'] = 1;
- M("notice")->add($notice);
- echo "==ID:" . $id . "释放" . $num . $coinname . "成功==";
- echo "<br />";
- }
- } else {
- echo "====没有可释放的冻结记录====";
- }
- }
- //共享矿机自动结算收益,设置一天执行一次的计划任务
- public function authsharesjsy()
- {
- $kjlist = M("kjorder")->where(array('status' => 1, 'type' => 2))->select();
- if (!empty($kjlist)) {
- foreach ($kjlist as $key => $vo) {
- $id = $vo['id'];
- $uid = $vo['uid'];
- $username = $vo['username'];
- $minfo = M("user_coin")->where(array('userid' => $uid))->find();
- $kid = $vo['kid'];
- $nowdate = date("Y-m-d", time());
- $profitinfo = M("kjprofit")->where(array('uid' => $uid, 'kid' => $id, 'day' => $nowdate))->find();
- if (empty($profitinfo)) {
- $sharbltxt = $vo['sharbltxt'];
- if ($sharbltxt <= 0) {
- echo "===共享矿机ID" . $id . "共享码有误===";
- } else {
- $sharekj = M("kjorder")->where(array('sharbltxt' => $sharbltxt))->count();
- if ($sharekj >= 2) {
- //查找矿机收益的类型以及查找收益是否需要冻结及冻结天数
- $outtype = $vo['outtype'];
- if ($outtype == 1) {//按产值需要查找产出币种的最新行情
- $coinname = strtolower(trim($vo['outcoin']));
- $outnum = $vo['outusdt'];
- $symbol = $coinname . 'usdt';
- $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $symbol;
- $newprice = $this->getnewprice($coinapi);
- $tcoinnum = sprintf("%.6f", ($outnum / $newprice)); //实际产生的币量,保留6位小数
- } elseif ($outtype == 2) {
- $coinname = strtolower(trim($vo['outcoin']));
- $tcoinnum = $vo['outnum'];
- }
- $djout = $vo['djout'];//1冻结2不冻结
- $djday = $vo['djnum'];//冻结天数
- //写入矿机收益日志
- $kjprofit_d['uid'] = $uid;
- $kjprofit_d['username'] = $username;
- $kjprofit_d['kid'] = $id;
- $kjprofit_d['ktitle'] = $vo['kjtitle'];
- $kjprofit_d['num'] = $tcoinnum;
- $kjprofit_d['coin'] = $coinname;
- $kjprofit_d['addtime'] = date("Y-m-d H:i:s", time());
- $kjprofit_d['day'] = date("Y-m-d", time());
- M("kjprofit")->add($kjprofit_d);
- if ($djout == 2) {
- $coin_d = $coinname . "d";
- M("user_coin")->where(array('userid' => $uid))->setInc($coin_d, $tcoinnum);
- $djprofit_d['uid'] = $uid;
- $djprofit_d['username'] = $username;
- $djprofit_d['num'] = $tcoinnum;
- $djprofit_d['coin'] = $coinname;
- $djprofit_d['status'] = 1;
- $djprofit_d['addtime'] = date("Y-m-d H:i:s", time());
- $djprofit_d['addday'] = date("Y-m-d", time());
- $djprofit_d['thawtime'] = date("Y-m-d H:i:s", (time() + 86400 * $djday));
- $djprofit_d['thawday'] = date("Y-m-d", (time() + 86400 * $djday));
- $djprofit_d['remark'] = L('冻结矿机释放收益');
- M("djprofit")->add($djprofit_d);
- //写资金日志
- $billdata['uid'] = $uid;
- $billdata['username'] = $username;
- $billdata['num'] = $tcoinnum;
- $billdata['coinname'] = $coinname;
- $billdata['afternum'] = $minfo[$coin_d] + $tcoinnum;
- $billdata['type'] = 7;
- $billdata['addtime'] = date("Y-m-d H:i:s", time());
- $billdata['st'] = 1;
- $billdata['remark'] = L('矿机收益释放冻结');
- M("bill")->add($billdata);
- $notice['uid'] = $uid;
- $notice['account'] = $username;
- $notice['title'] = L('矿机收益');
- $notice['content'] = L('今日矿机收益已成功到账,请注册查收');
- $notice['addtime'] = date("Y-m-d H:i:s", time());
- $notice['status'] = 1;
- M("notice")->add($notice);
- } elseif ($djout == 1) {
- M("user_coin")->where(array('userid' => $uid))->setInc($coinname, $tcoinnum);
- //写资金日志
- $billdata['uid'] = $uid;
- $billdata['username'] = $username;
- $billdata['num'] = $tcoinnum;
- $billdata['coinname'] = $coinname;
- $billdata['afternum'] = $minfo[$coinname] + $tcoinnum;
- $billdata['type'] = 8;
- $billdata['addtime'] = date("Y-m-d H:i:s", time());
- $billdata['st'] = 1;
- $billdata['remark'] = L('矿机收益释放');
- M("bill")->add($billdata);
- }
- //修改矿机收益次数
- M("kjorder")->where(array('id' => $id))->setDec("synum", 1);
- $reinfo = M("kjorder")->where(array('id' => $id))->find();
- if ($reinfo['synum'] < 1) {
- M("kjorder")->where(array('id' => $id))->save(array('status' => 3));
- }
- echo "==共享矿机ID:" . $kid . "收益成功==";
- echo "<br />";
- } else {
- echo "===共享矿机ID" . $id . "另一部分没有购买,不能收益===";
- }
- }
- } else {
- echo "==矿机ID:" . $kid . "不能重复收益==";
- echo "<br />";
- }
- }
- }
- }
- //独资矿机自动收益,每天执行一次
- //设置一天执行一次的计划任务
- public function autokjsy()
- {
- $kjlist = M("kjorder")->where(array('status' => 1, 'type' => 1))->select();
- if (!empty($kjlist)) {
- foreach ($kjlist as $key => $vo) {
- $id = $vo['id'];
- $uid = $vo['uid'];
- $username = $vo['username'];
- $minfo = M("user_coin")->where(array('userid' => $uid))->find();
- $kid = $vo['kid'];
- $nowdate = date("Y-m-d", time());
- $profitinfo = M("kjprofit")->where(array('uid' => $uid, 'kid' => $id, 'day' => $nowdate))->find();
- if (empty($profitinfo)) {
- //查找矿机收益的类型以及查找收益是否需要冻结及冻结天数
- $outtype = $vo['outtype'];
- if ($outtype == 1) {//按产值需要查找产出币种的最新行情
- $coinname = strtolower(trim($vo['outcoin']));
- $outnum = $vo['outusdt'];
- $symbol = $coinname . 'usdt';
- $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $symbol;
- $newprice = $this->getnewprice($coinapi);
- $tcoinnum = sprintf("%.6f", ($outnum / $newprice)); //实际产生的币量,保留6位小数
- } elseif ($outtype == 2) {
- $coinname = strtolower(trim($vo['outcoin']));
- $tcoinnum = $vo['outnum'];
- }
- $djout = $vo['djout'];//1冻结2不冻结
- $djday = $vo['djnum'];//冻结天数
- //写入矿机收益日志
- $kjprofit_d['uid'] = $uid;
- $kjprofit_d['username'] = $username;
- $kjprofit_d['kid'] = $id;
- $kjprofit_d['ktitle'] = $vo['kjtitle'];
- $kjprofit_d['num'] = $tcoinnum;
- $kjprofit_d['coin'] = $coinname;
- $kjprofit_d['addtime'] = date("Y-m-d H:i:s", time());
- $kjprofit_d['day'] = date("Y-m-d", time());
- M("kjprofit")->add($kjprofit_d);
- if ($djout == 2) {
- $coin_d = $coinname . "d";
- M("user_coin")->where(array('userid' => $uid))->setInc($coin_d, $tcoinnum);
- $djprofit_d['uid'] = $uid;
- $djprofit_d['username'] = $username;
- $djprofit_d['num'] = $tcoinnum;
- $djprofit_d['coin'] = $coinname;
- $djprofit_d['status'] = 1;
- $djprofit_d['addtime'] = date("Y-m-d H:i:s", time());
- $djprofit_d['addday'] = date("Y-m-d", time());
- $djprofit_d['thawtime'] = date("Y-m-d H:i:s", (time() + 86400 * $djday));
- $djprofit_d['thawday'] = date("Y-m-d", (time() + 86400 * $djday));
- $djprofit_d['remark'] = L('冻结矿机释放收益');
- M("djprofit")->add($djprofit_d);
- //写资金日志
- $billdata['uid'] = $uid;
- $billdata['username'] = $username;
- $billdata['num'] = $tcoinnum;
- $billdata['coinname'] = $coinname;
- $billdata['afternum'] = $minfo[$coin_d] + $tcoinnum;
- $billdata['type'] = 7;
- $billdata['addtime'] = date("Y-m-d H:i:s", time());
- $billdata['st'] = 1;
- $billdata['remark'] = L('矿机收益释放冻结');
- M("bill")->add($billdata);
- $notice['uid'] = $uid;
- $notice['account'] = $username;
- $notice['title'] = L('矿机收益');
- $notice['content'] = L('今日矿机收益已成功到账,请注册查收');
- $notice['addtime'] = date("Y-m-d H:i:s", time());
- $notice['status'] = 1;
- M("notice")->add($notice);
- } elseif ($djout == 1) {
- M("user_coin")->where(array('userid' => $uid))->setInc($coinname, $tcoinnum);
- //写资金日志
- $billdata['uid'] = $uid;
- $billdata['username'] = $username;
- $billdata['num'] = $tcoinnum;
- $billdata['coinname'] = $coinname;
- $billdata['afternum'] = $minfo[$coinname] + $tcoinnum;
- $billdata['type'] = 8;
- $billdata['addtime'] = date("Y-m-d H:i:s", time());
- $billdata['st'] = 1;
- $billdata['remark'] = L('矿机收益释放');
- M("bill")->add($billdata);
- }
- //修改矿机收益次数
- M("kjorder")->where(array('id' => $id))->setDec("synum", 1);
- $reinfo = M("kjorder")->where(array('id' => $id))->find();
- if ($reinfo['synum'] < 1) {
- M("kjorder")->where(array('id' => $id))->save(array('status' => 3));
- }
- echo "==矿机ID:" . $kid . "收益成功==";
- echo "<br />";
- } else {
- echo "==矿机ID:" . $kid . "不能重复收益==";
- echo "<br />";
- }
- }
- } else {
- echo "++||没有正常运行的矿机||++";
- }
- }
- //休验订单自动按风控比例设置订单的盈亏比例
- //设置成5-10秒执行一次的计划任务
- public function setwl_ty()
- {
- $map['status'] = 1;
- $map['kongyk'] = 0;
- $orderobj = M("tyhyorder");
- $count = $orderobj->where($map)->count();
- $setting = M("hysetting")->where(array('id' => 1))->field("hy_fkgl")->find();
- if ($setting['hy_fkgl'] > 0) {
- $ylcount = intval($count * $setting['hy_fkgl'] / 100);
- $kscount = $count - $ylcount;
- if ($ylcount > 0) {
- $yllist = $orderobj->where($map)->order("num asc")->limit($ylcount)->select();
- if (!empty($yllist)) {
- foreach ($yllist as $k => $v) {
- $yid = $v['id'];
- $orderobj->where(array('id' => $yid))->save(array('kongyk' => 1));
- echo "订单ID:" . $yid . "设为盈利==";
- }
- }
- }
- if ($kscount > 0) {
- $kslist = $orderobj->where($map)->order("num asc")->limit($kscount)->select();
- if (!empty($kslist)) {
- foreach ($kslist as $k => $v) {
- $kid = $v['id'];
- $orderobj->where(array('id' => $kid))->save(array('kongyk' => 2));
- echo "订单ID:" . $kid . "设为亏损==";
- }
- }
- }
- }
- echo "操作成功";
- }
- //自动按风控比例设置订单的盈亏比例
- //设置成5-10秒执行一次的计划任务
- public function setwl()
- {
- $map['status'] = 1;
- $map['kongyk'] = 0;
- $orderobj = M("hyorder");
- $count = $orderobj->where($map)->count();
- $setting = M("hysetting")->where(array('id' => 1))->field("hy_fkgl")->find();
- if ($setting['hy_fkgl'] > 0) {
- $ylcount = intval($count * $setting['hy_fkgl'] / 100);
- $kscount = $count - $ylcount;
- if ($ylcount > 0) {
- $yllist = $orderobj->where($map)->order("num asc")->limit($ylcount)->select();
- if (!empty($yllist)) {
- foreach ($yllist as $k => $v) {
- $yid = $v['id'];
- $orderobj->where(array('id' => $yid))->save(array('kongyk' => 1));
- echo "订单ID:" . $yid . "设为盈利==";
- }
- }
- }
- if ($kscount > 0) {
- $kslist = $orderobj->where($map)->order("num asc")->limit($kscount)->select();
- if (!empty($kslist)) {
- foreach ($kslist as $k => $v) {
- $kid = $v['id'];
- $orderobj->where(array('id' => $kid))->save(array('kongyk' => 2));
- echo "订单ID:" . $kid . "设为亏损==";
- }
- }
- }
- }
- echo "操作成功";
- }
- //自动结算合约订单
- public function hycarryout_ty()
- {
- $nowtime = time();
- $map['status'] = 1;
- $map['intselltime'] = array('elt', $nowtime);
- $orderobj = M("tyhyorder");
- $list = $orderobj->where($map)->select();
- if (!empty($list)) {
- foreach ($list as $key => $vo) {
- $coinname = $vo['coinname'];
- $coinarr = explode("/", $coinname);
- $symbol = strtolower($coinarr[0]) . strtolower($coinarr[1]);
- $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $symbol;
- $newprice = $this->getnewprice($coinapi);
- $randnum = "0." . rand(1000, 9999);
- $buyprice = $vo['buyprice'];
- $otype = $vo['hyzd']; //合约方向
- $dkong = $vo['kongyk']; //单控设置
- $uid = $vo['uid'];//会员ID
- $id = $vo['id'];//记录ID
- $num = $vo['num'];
- $hybl = $vo['hybl']; //收益比例
- $ylnum = $num * ($hybl / 100); //盈利金额
- $money = $num + $ylnum;//本金+盈利金额
- //$dkong分三种情况 1、0表示随行情,1表示盈利 2 表示亏损
- //盈利时增加$money,,亏损时亏本金
- if ($dkong == 0) {
- //买涨
- if ($otype == 1) { //买跌
- if ($newprice > $buyprice) { //盈利
- //增加资产
- //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
- M("user")->where(array('id' => $uid))->setInc("money", $money);
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 1;
- $sd['sellprice'] = $newprice;
- $sd['ploss'] = $ylnum;
- $orderobj->where(array('id' => $id))->save($sd);
- //写财务日志
- //$this->addlog($uid,$vo['username'],$money);
- } else {//亏损
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 2;
- $sd['sellprice'] = $newprice;
- $sd['ploss'] = $num;
- $orderobj->where(array('id' => $id))->save($sd);
- }
- } elseif ($otype == 2) { //买跌
- if ($newprice < $buyprice) { //盈利
- //增加资产
- //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
- M("user")->where(array('id' => $uid))->setInc("money", $money);
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 1;
- $sd['sellprice'] = $newprice;
- $sd['ploss'] = $ylnum;
- $orderobj->where(array('id' => $id))->save($sd);
- //写财务日志
- //$this->addlog($uid,$vo['username'],$money);
- } else {//亏损
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 2;
- $sd['sellprice'] = $newprice;
- $sd['ploss'] = $num;
- $orderobj->where(array('id' => $id))->save($sd);
- }
- }
- } elseif ($dkong == 1) {//单控盈利
- if ($otype == 1) {//买涨
- if ($newprice > $buyprice) {
- $sellprice = $newprice;
- } elseif ($newprice == $buyprice) {
- $sellprice = $newprice + $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $buyprice + $randnum;
- }
- //增加资产
- //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
- M("user")->where(array('id' => $uid))->setInc("money", $money);
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 1;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $ylnum;
- $orderobj->where(array('id' => $id))->save($sd);
- //写财务日志
- //$this->addlog($uid,$vo['username'],$money);
- } elseif ($otype == 2) {//买跌
- if ($newprice > $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice == $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $newprice;
- }
- //增加资产
- //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
- M("user")->where(array('id' => $uid))->setInc("money", $money);
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 1;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $ylnum;
- $orderobj->where(array('id' => $id))->save($sd);
- //写财务日志
- //$this->addlog($uid,$vo['username'],$money);
- }
- } elseif ($dkong == 2) {
- if ($otype == 1) {//买涨
- //买涨,指定亏损,结算价格要低于买入价格
- if ($newprice > $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice == $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $newprice;
- }
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 2;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $num;
- $orderobj->where(array('id' => $id))->save($sd);
- } elseif ($otype == 2) {//买跌
- if ($newprice > $buyprice) {
- $sellprice = $newprice;
- } elseif ($newprice == $buyprice) {
- $sellprice = $buyprice + $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $buyprice + $randnum;
- }
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 2;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $num;
- $orderobj->where(array('id' => $id))->save($sd);
- }
- }
- echo "==订单ID:" . $id . "出售成功==";
- }
- } else {
- echo "没有订单可以结算!";
- }
- }
- //自动结算体验合约订单
- public function hycarryout_ty_old()
- {
- $nowtime = time();
- $map['status'] = 1;
- $map['intselltime'] = array('elt', $nowtime);
- $orderobj = M("tyhyorder");
- $list = $orderobj->where($map)->select();
- //获取合约参数
- $setting = M("hysetting")->where(array('id' => 1))->field("hy_ksid,hy_ylid,hy_fkgl")->find();
- //指定盈利ID组
- $winarr = explode(',', $setting['hy_ylid']);
- //指定亏损ID组
- $lossarr = explode(',', $setting['hy_ksid']);
- //风控比例组
- $fkarr = explode(',', $setting['hy_fkgl']);
- if (!empty($list)) {
- foreach ($list as $key => $vo) {
- $coinname = $vo['coinname'];
- $coinarr = explode("/", $coinname);
- $symbol = strtolower($coinarr[0]) . strtolower($coinarr[1]);
- $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $symbol;
- $newprice = $this->getnewprice($coinapi);
- $randnum = "0." . rand(1000, 9999);
- $buyprice = $vo['buyprice'];
- $otype = $vo['hyzd']; //合约方向
- $dkong = $vo['kongyk']; //单控设置
- $uid = $vo['uid'];//会员ID
- $id = $vo['id'];//记录ID
- $num = $vo['num'];
- $hybl = $vo['hybl'];
- $ylnum = $num * ($hybl / 100);
- $money = $num + $ylnum;//盈利金额
- //买涨
- if ($otype == 1) {
- if (in_array($uid, $winarr)) {//如果有指定盈利ID,则按盈利结算
- if ($newprice > $buyprice) {
- $sellprice = $newprice;
- } elseif ($newprice == $buyprice) {
- $sellprice = $newprice + $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $buyprice + $randnum;
- }
- //增加资产
- //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
- M("user")->where(array('id' => $uid))->setInc("money", $money);
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 1;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $ylnum;
- $orderobj->where(array('id' => $id))->save($sd);
- //写财务日志
- //$this->addlog($uid,$vo['username'],$money);
- } elseif (in_array($uid, $lossarr)) {//如果有指定亏损ID,则按亏损结算
- //买涨,指定亏损,结算价格要低于买入价格
- if ($newprice > $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice == $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $newprice;
- }
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 2;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $num;
- $orderobj->where(array('id' => $id))->save($sd);
- } else {//如果未指定盈利和亏损,则按单控的计算
- if ($dkong == 1) {//盈利
- if ($newprice > $buyprice) {
- $sellprice = $newprice;
- } elseif ($newprice == $buyprice) {
- $sellprice = $newprice + $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $buyprice + $randnum;
- }
- // echo '买入价格:'.$buyprice;
- // echo "<br />";
- // echo '结算价格:'.$sellprice;die;
- //增加资产
- //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
- M("user")->where(array('id' => $uid))->setInc("money", $money);
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 1;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $ylnum;
- $orderobj->where(array('id' => $id))->save($sd);
- //写财务日志
- //$this->addlog($uid,$vo['username'],$money);
- } elseif ($dkong == 2) {//亏损
- if ($newprice > $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice == $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $newprice;
- }
- // echo '买入价格:'.$buyprice;
- // echo "<br />";
- // echo '结算价格:'.$sellprice;die;
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 2;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $num;
- $orderobj->where(array('id' => $id))->save($sd);
- }
- }
- //买跌
- } elseif ($otype == 2) {
- if (in_array($uid, $winarr)) {//如果有指定盈利ID,则按盈利结算
- if ($newprice > $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice == $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $newprice;
- }
- //增加资产
- //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
- M("user")->where(array('id' => $uid))->setInc("money", $money);
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 1;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $ylnum;
- $orderobj->where(array('id' => $id))->save($sd);
- //写财务日志
- //$this->addlog($uid,$vo['username'],$money);
- } elseif (in_array($uid, $lossarr)) {//如果有指定亏损ID,则按亏损结算
- if ($newprice > $buyprice) {
- $sellprice = $newprice;
- } elseif ($newprice == $buyprice) {
- $sellprice = $buyprice + $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $buyprice + $randnum;
- }
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 2;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $num;
- $orderobj->where(array('id' => $id))->save($sd);
- } else {//如果未指定盈利和亏损,则按单控的计算
- if ($dkong == 1) {//盈利
- if ($newprice > $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice == $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $newprice;
- }
- //增加资产
- //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
- M("user")->where(array('id' => $uid))->setInc("money", $money);
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 1;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $ylnum;
- $orderobj->where(array('id' => $id))->save($sd);
- //写财务日志
- //$this->addlog($uid,$vo['username'],$money);
- } elseif ($dkong == 2) {//亏损
- if ($newprice > $buyprice) {
- $sellprice = $newprice;
- } elseif ($newprice == $buyprice) {
- $sellprice = $buyprice + $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $buyprice + $randnum;
- }
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 2;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $num;
- $orderobj->where(array('id' => $id))->save($sd);
- }
- }
- }
- echo "==订单ID:" . $id . "出售成功==";
- }
- } else {
- echo "没有订单可以结算!";
- }
- }
- //自动结算合约订单
- public function hycarryout()
- {
- $nowtime = time();
- $map['status'] = 1;
- $map['intselltime'] = array('elt', $nowtime);
- $orderobj = M("hyorder");
- $list = $orderobj->where($map)->select();
- if (!empty($list)) {
- foreach ($list as $key => $vo) {
- $coinname = $vo['coinname'];
- $coinarr = explode("/", $coinname);
- $symbol = strtolower($coinarr[0]) . strtolower($coinarr[1]);
- $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $symbol;
- $newprice = $this->getnewprice($coinapi);
- $randnum = "0." . rand(1000, 9999);
- $buyprice = $vo['buyprice'];
- $otype = $vo['hyzd']; //合约方向
- $dkong = $vo['kongyk']; //单控设置
- $uid = $vo['uid'];//会员ID
- $id = $vo['id'];//记录ID
- $num = $vo['num'];
- $hybl = $vo['hybl']; //收益比例
- $ylnum = $num * ($hybl / 100); //盈利金额
- $money = $num + $ylnum;//本金+盈利金额
- //$dkong分三种情况 1、0表示随行情,1表示盈利 2 表示亏损
- //盈利时增加$money,,亏损时亏本金
- if ($dkong == 0) {
- //买涨
- if ($otype == 1) { //买跌
- if ($newprice > $buyprice) { //盈利
- //增加资产
- M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 1;
- $sd['sellprice'] = $newprice;
- $sd['ploss'] = $ylnum;
- $orderobj->where(array('id' => $id))->save($sd);
- //写财务日志
- $this->addlog($uid, $vo['username'], $money);
- } else {//亏损
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 2;
- $sd['sellprice'] = $newprice;
- $sd['ploss'] = $num;
- $orderobj->where(array('id' => $id))->save($sd);
- }
- } elseif ($otype == 2) { //买跌
- if ($newprice < $buyprice) { //盈利
- //增加资产
- M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 1;
- $sd['sellprice'] = $newprice;
- $sd['ploss'] = $ylnum;
- $orderobj->where(array('id' => $id))->save($sd);
- //写财务日志
- $this->addlog($uid, $vo['username'], $money);
- } else {//亏损
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 2;
- $sd['sellprice'] = $newprice;
- $sd['ploss'] = $num;
- $orderobj->where(array('id' => $id))->save($sd);
- }
- }
- } elseif ($dkong == 1) {//单控盈利
- if ($otype == 1) {//买涨
- if ($newprice > $buyprice) {
- $sellprice = $newprice;
- } elseif ($newprice == $buyprice) {
- $sellprice = $newprice + $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $buyprice + $randnum;
- }
- //增加资产
- M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 1;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $ylnum;
- $orderobj->where(array('id' => $id))->save($sd);
- //写财务日志
- $this->addlog($uid, $vo['username'], $money);
- } elseif ($otype == 2) {//买跌
- if ($newprice > $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice == $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $newprice;
- }
- //增加资产
- M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 1;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $ylnum;
- $orderobj->where(array('id' => $id))->save($sd);
- //写财务日志
- $this->addlog($uid, $vo['username'], $money);
- }
- } elseif ($dkong == 2) {
- if ($otype == 1) {//买涨
- //买涨,指定亏损,结算价格要低于买入价格
- if ($newprice > $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice == $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $newprice;
- }
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 2;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $num;
- $orderobj->where(array('id' => $id))->save($sd);
- } elseif ($otype == 2) {//买跌
- if ($newprice > $buyprice) {
- $sellprice = $newprice;
- } elseif ($newprice == $buyprice) {
- $sellprice = $buyprice + $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $buyprice + $randnum;
- }
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 2;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $num;
- $orderobj->where(array('id' => $id))->save($sd);
- }
- }
- echo "==订单ID:" . $id . "出售成功==";
- }
- } else {
- echo "没有订单可以结算!";
- }
- }
- //自动结算合约订单
- public function hycarryout____old()
- {
- $nowtime = time();
- $map['status'] = 1;
- $map['intselltime'] = array('elt', $nowtime);
- $orderobj = M("hyorder");
- $list = $orderobj->where($map)->select();
- //获取合约参数
- $setting = M("hysetting")->where(array('id' => 1))->field("hy_ksid,hy_ylid,hy_fkgl")->find();
- //指定盈利ID组
- $winarr = explode(',', $setting['hy_ylid']);
- //指定亏损ID组
- $lossarr = explode(',', $setting['hy_ksid']);
- //风控比例组
- $fkarr = explode(',', $setting['hy_fkgl']);
- if (!empty($list)) {
- foreach ($list as $key => $vo) {
- $coinname = $vo['coinname'];
- $coinarr = explode("/", $coinname);
- $symbol = strtolower($coinarr[0]) . strtolower($coinarr[1]);
- $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $symbol;
- $newprice = $this->getnewprice($coinapi);
- $randnum = "0." . rand(1000, 9999);
- $buyprice = $vo['buyprice'];
- $otype = $vo['hyzd']; //合约方向
- $dkong = $vo['kongyk']; //单控设置
- $uid = $vo['uid'];//会员ID
- $id = $vo['id'];//记录ID
- $num = $vo['num'];
- $hybl = $vo['hybl'];
- $ylnum = $num * ($hybl / 100);
- $money = $num + $ylnum;//盈利金额
- //买涨
- if ($otype == 1) {
- if (in_array($uid, $winarr)) {//如果有指定盈利ID,则按盈利结算
- if ($newprice > $buyprice) {
- $sellprice = $newprice;
- } elseif ($newprice == $buyprice) {
- $sellprice = $newprice + $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $buyprice + $randnum;
- }
- //增加资产
- M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 1;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $ylnum;
- $orderobj->where(array('id' => $id))->save($sd);
- //写财务日志
- $this->addlog($uid, $vo['username'], $money);
- } elseif (in_array($uid, $lossarr)) {//如果有指定亏损ID,则按亏损结算
- //买涨,指定亏损,结算价格要低于买入价格
- if ($newprice > $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice == $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $newprice;
- }
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 2;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $num;
- $orderobj->where(array('id' => $id))->save($sd);
- } else {//如果未指定盈利和亏损,则按单控的计算
- if ($dkong == 1) {//盈利
- if ($newprice > $buyprice) {
- $sellprice = $newprice;
- } elseif ($newprice == $buyprice) {
- $sellprice = $newprice + $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $buyprice + $randnum;
- }
- // echo '买入价格:'.$buyprice;
- // echo "<br />";
- // echo '结算价格:'.$sellprice;die;
- //增加资产
- M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 1;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $ylnum;
- $orderobj->where(array('id' => $id))->save($sd);
- //写财务日志
- $this->addlog($uid, $vo['username'], $money);
- } elseif ($dkong == 2) {//亏损
- if ($newprice > $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice == $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $newprice;
- }
- // echo '买入价格:'.$buyprice;
- // echo "<br />";
- // echo '结算价格:'.$sellprice;die;
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 2;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $num;
- $orderobj->where(array('id' => $id))->save($sd);
- }
- }
- //买跌
- } elseif ($otype == 2) {
- if (in_array($uid, $winarr)) {//如果有指定盈利ID,则按盈利结算
- if ($newprice > $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice == $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $newprice;
- }
- //增加资产
- M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 1;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $ylnum;
- $orderobj->where(array('id' => $id))->save($sd);
- //写财务日志
- $this->addlog($uid, $vo['username'], $money);
- } elseif (in_array($uid, $lossarr)) {//如果有指定亏损ID,则按亏损结算
- if ($newprice > $buyprice) {
- $sellprice = $newprice;
- } elseif ($newprice == $buyprice) {
- $sellprice = $buyprice + $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $buyprice + $randnum;
- }
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 2;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $num;
- $orderobj->where(array('id' => $id))->save($sd);
- } else {//如果未指定盈利和亏损,则按单控的计算
- if ($dkong == 1) {//盈利
- if ($newprice > $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice == $buyprice) {
- $sellprice = $buyprice - $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $newprice;
- }
- //增加资产
- M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 1;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $ylnum;
- $orderobj->where(array('id' => $id))->save($sd);
- //写财务日志
- $this->addlog($uid, $vo['username'], $money);
- } elseif ($dkong == 2) {//亏损
- if ($newprice > $buyprice) {
- $sellprice = $newprice;
- } elseif ($newprice == $buyprice) {
- $sellprice = $buyprice + $randnum;
- } elseif ($newprice < $buyprice) {
- $sellprice = $buyprice + $randnum;
- }
- //修改订单状态
- $sd['status'] = 2;
- $sd['is_win'] = 2;
- $sd['sellprice'] = $sellprice;
- $sd['ploss'] = $num;
- $orderobj->where(array('id' => $id))->save($sd);
- }
- }
- }
- echo "==订单ID:" . $id . "出售成功==";
- }
- } else {
- echo "没有订单可以结算!";
- }
- }
- //写财务日志
- public function addlog($uid, $username, $money)
- {
- $minfo = M("user_coin")->where(array('userid' => $uid))->find();
- $data['uid'] = $uid;
- $data['username'] = $username;
- $data['num'] = $money;
- $data['coinname'] = "usdt";
- $data['afternum'] = $minfo['usdt'] + $money;
- $data['type'] = 4;
- $data['addtime'] = date("Y-m-d H:i:s", time());
- $data['st'] = 1;
- $data['remark'] = L('合约出售');
- M("bill")->add($data);
- $notice['uid'] = $uid;
- $notice['account'] = $username;
- $notice['title'] = L('快速合约交易');
- $notice['content'] = L('快速合约已平仓,请及时加仓');
- $notice['addtime'] = date("Y-m-d H:i:s", time());
- $notice['status'] = 1;
- M("notice")->add($notice);
- }
- //获取行情数据
- public function getnewprice($api)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
- curl_setopt($ch, CURLOPT_URL, $api);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
- $result = json_decode(curl_exec($ch), true);
- $price_arr = $result['data'][0];
- $close = $price_arr['close'];//现价
- return $close;
- }
- public function ups_and_downs()
- {
- $coin = M('coin')->where(['type' => 2, 'status' => 1])->select();
- $delete = M('trade_log')->where('addtime < '.(time()-600))->delete();
- foreach ($coin as $item) {
- $name = $item['name'] . '_' . 'usdt';
- $market = M('market')->where(['name' => $name])->find();
- $trade_list = M('trade_json')->where(['type' => 1])->order('id DESC')->find();// k线15分钟
- $trade_list1 = M('trade_json')->where(['type' => 15])->order('id DESC')->find();// k线15分钟
- $trade_list2 = M('trade_json')->where(['type' => 30])->order('id DESC')->find();// k线30分钟
- $trade_list3 = M('trade_json')->where(['type' => 60])->order('id DESC')->find();// k线1小时
- $trade_list4 = M('trade_json')->where(['type' => 240])->order('id DESC')->find();// k线4小时
- $trade_list5 = M('trade_json')->where(['type' => 1440])->order('id DESC')->find();// k线1天
- if ($market['zhang'] and $market['die']) {
- //同时设置涨跌幅
- $zd = rand(1, 2); //1涨幅2下跌
- if ($zd == 1) {
- $zf = $this->randomFloat(0, $market['zhang']); // 涨幅价格
- $price = $market['new_price'] + ($market['new_price'] * ($zf / 100));//最新涨幅价格
- if ($market['max_price'] < $price) {
- $market['max_price'] = $price;
- M('market')->where(['id' => $market['id']])->save(['max_price' => $price]);
- }
- } else {
- $xd = $this->randomFloat(0, $market['die']); // 下跌价格
- $price = $market['new_price'] - ($market['new_price'] * ($xd / 100));//最新下跌价格
- if ($market['min_price'] > $price) {
- $market['min_price'] = $price;
- M('market')->where(['id' => $market['id']])->save(['min_price' => $price]);
- }
- }
- M('market')->where(['id' => $market['id']])->save(['new_price' => $price]);
- if ($market['hou_time'] < strtotime('today')) {
- M('market')->where(['id' => $market['id']])->save(['hou_price' => $price, 'hou_time' => strtotime('today'), 'max_day' => $price, 'min_day' => $price]);
- }
- }else{
- if ($market['zhang'] > 0 and $market['die'] == 0){
- $zf = $this->randomFloat(0, $market['zhang']); // 涨幅价格
- $price = $market['new_price'] + ($market['new_price'] * ($zf / 100));//最新涨幅价格
- if ($market['max_price'] < $price) {
- $market['max_price'] = $price;
- M('market')->where(['id' => $market['id']])->save(['max_price' => $price]);
- }
- M('market')->where(['id' => $market['id']])->save(['new_price' => $price]);
- if ($market['hou_time'] < strtotime('today')) {
- M('market')->where(['id' => $market['id']])->save(['hou_price' => $price, 'hou_time' => strtotime('today'), 'max_day' => $price, 'min_day' => $price]);
- }
- }
- if ($market['zhang'] == 0 and $market['die'] > 0){
- $xd = $this->randomFloat(0, $market['die']); // 下跌价格
- $price = $market['new_price'] - ($market['new_price'] * ($xd / 100));//最新下跌价格
- if ($market['min_price'] > $price) {
- $market['min_price'] = $price;
- M('market')->where(['id' => $market['id']])->save(['min_price' => $price]);
- }
- M('market')->where(['id' => $market['id']])->save(['new_price' => $price]);
- if ($market['hou_time'] < strtotime('today')) {
- M('market')->where(['id' => $market['id']])->save(['hou_price' => $price, 'hou_time' => strtotime('today'), 'max_day' => $price, 'min_day' => $price]);
- }
- }
- }
- if ($price){
- $price = round($price, 6);
- $sd = $this->randomFloat($market['sdlow_num'], $market['sdhigh_num']);
- for ($i = 0; $i <= $sd; $i ++){
- M('trade_log')->add([
- 'market' => $name,
- 'price' => $price,
- 'num' => $this->randomFloat(0, 1),
- 'mum' => $this->randomFloat(0, 1),
- 'addtime' => time() - rand(1, 10),
- 'type' => rand(1, 2)
- ]);
- }
- if ($price > $market['max_day']){
- M('market')->where(['id' => $market['id']])->save(['max_day' => $price]);
- }
- if ($price < $market['min_day']){
- M('market')->where(['id' => $market['id']])->save(['min_day' => $price]);
- }
- if ($price > $market['max_price']){
- M('market')->where(['id' => $market['id']])->save(['max_price' => $price]);
- $market['max_price'] = $price;
- }
- if ($price < $market['min_price']){
- M('market')->where(['id' => $market['id']])->save(['min_price' => $price]);
- $market['min_price'] = $price;
- }
- if ($trade_list) {
- $data = json_decode($trade_list['data']);
- $time = $trade_list['addtime'] + ($trade_list['type'] * 60);
- if ($time <= time()) {
- M('market')->where(['id' => $market['id']])->save(['max_price' => $price]);
- M('market')->where(['id' => $market['id']])->save(['min_price' => $price]);
- $json = [
- 'market' => $name,
- 'data' => json_encode([
- time() * 1000,
- $data[4],
- $market['max_price'],
- $market['min_price'],
- $price,
- $this->randomFloat(0, $market['sdhigh_num'])
- ]),
- 'type' => 1,
- 'addtime' => time()
- ];
- M('trade_json')->add($json);
- }
- } else {
- $json = [
- 'market' => $name,
- 'data' => json_encode([
- time() * 1000,
- $market['new_price'],
- $market['max_price'],
- $market['min_price'],
- $price,
- $this->randomFloat(0, $market['sdhigh_num'])
- ]),
- 'type' => 1,
- 'addtime' => time()
- ];
- M('trade_json')->add($json);
- }
- if ($trade_list1) {
- $data = json_decode($trade_list1['data']);
- $time = $trade_list1['addtime'] + ($trade_list1['type'] * 60);
- if ($time <= time()) {
- $json = [
- 'market' => $name,
- 'data' => json_encode([
- time() * 1000,
- $data[4],
- $market['max_price'],
- $market['min_price'],
- $price,
- $this->randomFloat(0, $market['sdhigh_num'])
- ]),
- 'type' => 15,
- 'addtime' => time()
- ];
- M('trade_json')->add($json);
- }
- } else {
- $json = [
- 'market' => $name,
- 'data' => json_encode([
- time() * 1000,
- $market['new_price'],
- $market['max_price'],
- $market['min_price'],
- $price,
- $this->randomFloat(0, $market['sdhigh_num'])
- ]),
- 'type' => 15,
- 'addtime' => time()
- ];
- M('trade_json')->add($json);
- }
- if ($trade_list2) {
- $data = json_decode($trade_list2['data']);
- $time = $trade_list2['addtime'] + ($trade_list2['type'] * 60);
- if ($time <= time()) {
- $json = [
- 'market' => $name,
- 'data' => json_encode([
- time() * 1000,
- $data[4],
- $market['max_price'],
- $market['min_price'],
- $price,
- $this->randomFloat(0, $market['sdhigh_num'])
- ]),
- 'type' => 30,
- 'addtime' => time()
- ];
- M('trade_json')->add($json);
- }
- } else {
- $json = [
- 'market' => $name,
- 'data' => json_encode([
- time() * 1000,
- $market['new_price'],
- $market['max_price'],
- $market['min_price'],
- $price,
- $this->randomFloat(0, $market['sdhigh_num'])
- ]),
- 'type' => 30,
- 'addtime' => time()
- ];
- M('trade_json')->add($json);
- }
- if ($trade_list3) {
- $data = json_decode($trade_list3['data']);
- $time = $trade_list3['addtime'] + ($trade_list3['type'] * 60);
- if ($time <= time()) {
- $json = [
- 'market' => $name,
- 'data' => json_encode([
- time() * 1000,
- $data[4],
- $market['max_price'],
- $market['min_price'],
- $price,
- $this->randomFloat(0, $market['sdhigh_num'])
- ]),
- 'type' => 60,
- 'addtime' => time()
- ];
- M('trade_json')->add($json);
- }
- } else {
- $json = [
- 'market' => $name,
- 'data' => json_encode([
- time() * 1000,
- $market['new_price'],
- $market['max_price'],
- $market['min_price'],
- $price,
- $this->randomFloat(0, $market['sdhigh_num'])
- ]),
- 'type' => 60,
- 'addtime' => time()
- ];
- M('trade_json')->add($json);
- }
- if ($trade_list4) {
- $data = json_decode($trade_list4['data']);
- $time = $trade_list4['addtime'] + ($trade_list4['type'] * 60);
- if ($time <= time()) {
- $json = [
- 'market' => $name,
- 'data' => json_encode([
- time() * 1000,
- $data[4],
- $market['max_price'],
- $market['min_price'],
- $price,
- $this->randomFloat(0, $market['sdhigh_num'])
- ]),
- 'type' => 240,
- 'addtime' => time()
- ];
- M('trade_json')->add($json);
- }
- } else {
- $json = [
- 'market' => $name,
- 'data' => json_encode([
- time() * 1000,
- $market['new_price'],
- $market['max_price'],
- $market['min_price'],
- $price,
- $this->randomFloat(0, $market['sdhigh_num'])
- ]),
- 'type' => 240,
- 'addtime' => time()
- ];
- M('trade_json')->add($json);
- }
- if ($trade_list5) {
- $data = json_decode($trade_list5['data']);
- $time = $trade_list5['addtime'] + ($trade_list5['type'] * 60);
- if ($time <= time()) {
- $json = [
- 'market' => $name,
- 'data' => json_encode([
- time() * 1000,
- $data[4],
- $market['max_price'],
- $market['min_price'],
- $price,
- $this->randomFloat(0, $market['sdhigh_num'])
- ]),
- 'type' => 1440,
- 'addtime' => time()
- ];
- M('trade_json')->add($json);
- }
- } else {
- $json = [
- 'market' => $name,
- 'data' => json_encode([
- time() * 1000,
- $market['new_price'],
- $market['max_price'],
- $market['min_price'],
- $price,
- $this->randomFloat(0, $market['sdhigh_num'])
- ]),
- 'type' => 1440,
- 'addtime' => time()
- ];
- M('trade_json')->add($json);
- }
- }
- }
- echo '自动增长';
- }
- function randomFloat($min = 0, $max = 1)
- {
- $num = $min + mt_rand() / mt_getrandmax() * ($max - $min);
- return sprintf("%.4f", $num); //控制小数后几位
- }
- }
- ?>
|