| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299 |
- <?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')]);
- }
- }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')]);
- }
- }
- 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')]);
- }
- }
- }
- 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_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); //控制小数后几位
- }
- }
- ?>
|