AutoexeController.class.php 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291
  1. <?php
  2. namespace Mobile\Controller;
  3. class AutoexeController extends \Think\Controller
  4. {
  5. protected function _initialize()
  6. {
  7. $allow_action = array("hycarryout", "ups_and_downs", "getnewprice", "setwl", "setwl_ty", "autokjsy", "releasedjprofit", "autoxjtade", "authsharesjsy", "releaseissue", "hycarryout_ty", "AutoCreateOrder", "AutoCloseOrder");
  8. if (!in_array(ACTION_NAME, $allow_action)) {
  9. $this->error("非法操作!");
  10. }
  11. }
  12. //永续合约自动结算
  13. public function AutoCloseOrder()
  14. {
  15. $list = M("leverorder")->where(array('status' => 2))->select();
  16. if (!empty($list)) {
  17. $sysinfo = M("leversetting")->where(array('id' => 1))->find();
  18. $lever_fee = $sysinfo['lever_fee'];//费率
  19. $new_close = [];
  20. foreach ($list as $key => $vo) {
  21. $id = $vo['id'];
  22. $uid = $vo['uid'];
  23. $uinfo = M("user")->where(array('id' => $uid))->field("id,username")->find();
  24. // $minfo = M("user_coin")->where(array('userid' => $uid))->find();
  25. // $bond_money_info = M("levermoney")->where(array('uid' => $uid))->find();
  26. $lowercoin = $vo['coinname'];
  27. $user = M('levermoney')->where(['uid' => $uid])->find();
  28. if (!isset($new_close[$vo['coinname']])) {
  29. if ($lowercoin == 'ukbusdt') {
  30. $o_symbol = $vo['symbol'];
  31. $symbol_arr = explode('/', $o_symbol);
  32. $symbol = strtolower($symbol_arr[0]) . "_" . strtolower($symbol_arr[1]);
  33. $mlist = M("market")->where(array('name' => $symbol))->field("new_price,min_price")->find();
  34. $close = $mlist['new_price'];
  35. } else {
  36. $oy_list = do_request(oy_url($vo['coinname'], 'api/v5/market/ticker?instId='), []);
  37. $oy_list = $oy_list->data[0];
  38. // $url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$lowercoin;
  39. // $result = $this->getprice($url);
  40. // $pdata = $result['data'][0];
  41. $close = $oy_list->last;//现价
  42. $new_close[$vo['coinname']] = $close;
  43. }
  44. } else {
  45. $close = $new_close[$vo['coinname']];
  46. }
  47. if ($close) {
  48. $price = $vo['price']; //开仓价格
  49. $direction = $vo['direction']; //交易方向
  50. $zsprice = $vo['zsprice']; //止损价格
  51. $zyprice = $vo['zyprice']; //止盈价格
  52. if ($zyprice > 0) {
  53. if ($direction == 1) {
  54. // 买多
  55. if ($close >= $zyprice) {
  56. //收益计算
  57. //例:BTC/USDT永续合约开多仓100张,成交价格为10000 USDT,以11000 USDT价格平仓,
  58. //则:平仓盈亏 = 收益(平仓) =( 11000 – 10000 )* 100 * 0.001 = 100 USDT。
  59. $profit = sprintf("%.2f", ($close - $price) * $vo['num']);
  60. $tprofit = abs($profit);
  61. $data['pc_price'] = $close;
  62. $data['status'] = 3;
  63. $data['yk_status'] = 1;
  64. $data['ylmoney'] = $tprofit;
  65. $data['endtime'] = date("Y-m-d H:i:s", time());
  66. $result = M("leverorder")->where(array('id' => $id))->save($data);
  67. $ubill['uid'] = $uid;
  68. $ubill['username'] = $uinfo['username'];
  69. $ubill['num'] = $tprofit;
  70. $ubill['coinname'] = "lever";
  71. $ubill['afternum'] = $user['money'] + $tprofit;
  72. $ubill['type'] = 20;
  73. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  74. $ubill['st'] = 1;
  75. $ubill['remark'] = L('合约交易止盈平仓盈利收益');
  76. $ubillre = M("bill")->add($ubill);
  77. $bond_ubill = [
  78. 'uid' => $uid,
  79. 'username' => $uinfo['username'],
  80. 'num' => $vo['bond'],
  81. 'coinname' => 'usdt',
  82. 'afternum' => $user['money'] + $vo['bond'],
  83. 'type' => 25,
  84. 'addtime' => date("Y-m-d H:i:s", time()),
  85. 'st' => 1,
  86. 'remark' => '合約交易止盈平倉返還佔用合約'
  87. ];// 扣除合约保证金
  88. M("levermoney")->where(array('uid' => $uid))->setInc('money', $vo['bond']);
  89. M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
  90. M("bill")->add($bond_ubill);
  91. if ($result && $ubillre) {
  92. M("levermoney")->where(array('uid' => $uid))->setInc('money', $tprofit);
  93. echo "=订单ID:" . $id . ",止盈平仓=";
  94. }
  95. }
  96. } elseif ($direction == 2) {
  97. //买空
  98. if ($close <= $zyprice) {
  99. $profit = sprintf("%.2f", ($price - $close) * $vo['num']);
  100. $tprofit = abs($profit);
  101. $data['pc_price'] = $close;
  102. $data['status'] = 3;
  103. $data['yk_status'] = 1;
  104. $data['ylmoney'] = $tprofit;
  105. $data['endtime'] = date("Y-m-d H:i:s", time());
  106. $result = M("leverorder")->where(array('id' => $id))->save($data);
  107. $ubill['uid'] = $uid;
  108. $ubill['username'] = $uinfo['username'];
  109. $ubill['num'] = $tprofit;
  110. $ubill['coinname'] = "lever";
  111. $ubill['afternum'] = $user['money'] + $tprofit;
  112. $ubill['type'] = 20;
  113. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  114. $ubill['st'] = 1;
  115. $ubill['remark'] = L('合约交易止盈平仓盈利收益');
  116. $ubillre = M("bill")->add($ubill);
  117. $bond_ubill = [
  118. 'uid' => $uid,
  119. 'username' => $uinfo['username'],
  120. 'num' => $vo['bond'],
  121. 'coinname' => 'usdt',
  122. 'afternum' => $user['money'] + $vo['bond'],
  123. 'type' => 25,
  124. 'addtime' => date("Y-m-d H:i:s", time()),
  125. 'st' => 1,
  126. 'remark' => '合約交易止盈平倉返還佔用合約'
  127. ];// 扣除合约保证金
  128. M("levermoney")->where(array('uid' => $uid))->setInc('money', $vo['bond']);
  129. M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
  130. M("bill")->add($bond_ubill);
  131. if ($result && $ubillre) {
  132. M("levermoney")->where(array('uid' => $uid))->setInc('money', $tprofit);
  133. echo "=订单ID:" . $id . ",止盈平仓=";
  134. }
  135. }
  136. }
  137. }
  138. if ($zsprice > 0) {
  139. if ($direction == 1) {
  140. // 买多
  141. if ($close <= $zsprice) {
  142. //收益计算
  143. //例:BTC/USDT永续合约开多仓100张,成交价格为10000 USDT,以11000 USDT价格平仓,
  144. //则:平仓盈亏 = 收益(平仓) =( 11000 – 10000 )* 100 * 0.001 = 100 USDT。
  145. $profit = sprintf("%.2f", ($close - $price) * $vo['num']);
  146. $tprofit = abs($profit);
  147. if ($tprofit < $user['money']) {
  148. //亏损小于保证金
  149. $data['pc_price'] = $close;
  150. $data['status'] = 3;
  151. $data['yk_status'] = 2;
  152. $data['ylmoney'] = $tprofit;
  153. $data['endtime'] = date("Y-m-d H:i:s", time());
  154. $bond = ($user['money'] + $vo['bond']) - $tprofit;
  155. $ubill['uid'] = $uid;
  156. $ubill['username'] = $uinfo['username'];
  157. $ubill['num'] = $vo['bond'];
  158. $ubill['afternum'] = $user['money'] + $vo['bond'];
  159. $ubill['coinname'] = "lever";
  160. $ubill['type'] = 20;
  161. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  162. $ubill['st'] = 1;
  163. $ubill['remark'] = '合約交易止損平倉返回保證金';
  164. $ubillre = M("bill")->add($ubill);
  165. $ubill['num'] = $tprofit;
  166. $ubill['afternum'] = $bond;
  167. $ubill['coinname'] = "lever";
  168. $ubill['st'] = 2;
  169. $ubill['remark'] = '合約交易止損平倉虧損';
  170. $ubillre = M("bill")->add($ubill);
  171. M("leverorder")->where(array('id' => $id))->save($data);
  172. M("levermoney")->where(['uid' => $uid])->setInc('money', $vo['bond']);
  173. M("levermoney")->where(['uid' => $uid])->setDec('money', $tprofit);
  174. M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
  175. echo "=订单ID:" . $id . ",止损平仓=";
  176. } else {
  177. if ($user['money'] + $vo['bond'] >= $tprofit) {
  178. $user_bond = $vo['bond'] - ($tprofit - $user['money']);
  179. $data['pc_price'] = $close;
  180. $data['status'] = 3;
  181. $data['yk_status'] = 2;
  182. $data['ylmoney'] = $tprofit;
  183. $data['endtime'] = date("Y-m-d H:i:s", time());
  184. $bond = $tprofit - $vo['bond'];
  185. $ubill['uid'] = $uid;
  186. $ubill['username'] = $uinfo['username'];
  187. $ubill['num'] = $user_bond;
  188. $ubill['afternum'] = $user_bond;
  189. $ubill['coinname'] = "lever";
  190. $ubill['type'] = 20;
  191. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  192. $ubill['st'] = 1;
  193. $ubill['remark'] = '合約交易止損平倉退回剩餘保證金';
  194. $ubillre = M("bill")->add($ubill);
  195. $ubill['num'] = $user['money'];
  196. $ubill['afternum'] = 0;
  197. $ubill['type'] = 20;
  198. $ubill['st'] = 2;
  199. $ubill['remark'] = '合約交易手動平倉虧損';
  200. M("leverorder")->where(array('id' => $id))->save($data);
  201. M("levermoney")->where(['uid' => $uid])->setInc('money', $user_bond);
  202. M("levermoney")->where(array('uid' => $uid))->setDec('money', $user['money']);
  203. M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
  204. echo "=订单ID:" . $id . ",止损平仓=";
  205. } else {
  206. $data['pc_price'] = $close;
  207. $data['status'] = 3;
  208. $data['yk_status'] = 2;
  209. $data['ylmoney'] = $tprofit;
  210. $data['endtime'] = date("Y-m-d H:i:s", time());
  211. $bond = $tprofit - $vo['bond'];
  212. $ubill['uid'] = $uid;
  213. $ubill['username'] = $uinfo['username'];
  214. $ubill['num'] = $user['money'];
  215. $ubill['afternum'] = 0;
  216. $ubill['coinname'] = "lever";
  217. $ubill['type'] = 20;
  218. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  219. $ubill['st'] = 1;
  220. $ubill['remark'] = '合約交易ID' . $vo['id'] . '止損平倉扣除完保證金,並扣除合約帳戶';
  221. $ubillre = M("bill")->add($ubill);
  222. M("leverorder")->where(array('id' => $id))->save($data);
  223. M("levermoney")->where(['uid' => $uid])->setDec('money', $user['money']);
  224. M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
  225. echo "=订单ID:" . $id . ",止损平仓=";
  226. }
  227. }
  228. }
  229. } elseif ($direction == 2) {
  230. //买空
  231. if ($close >= $zsprice) {
  232. $profit = sprintf("%.2f", ($price - $close) * $vo['num']);
  233. $tprofit = abs($profit);
  234. if ($tprofit < $user['money']) {
  235. //亏损小于保证金
  236. $data['pc_price'] = $close;
  237. $data['status'] = 3;
  238. $data['yk_status'] = 2;
  239. $data['ylmoney'] = $tprofit;
  240. $data['endtime'] = date("Y-m-d H:i:s", time());
  241. $bond = ($user['money'] + $vo['bond']) - $tprofit;
  242. $ubill['uid'] = $uid;
  243. $ubill['username'] = $uinfo['username'];
  244. $ubill['num'] = $vo['bond'];
  245. $ubill['afternum'] = $user['money'] + $vo['bond'];
  246. $ubill['coinname'] = "lever";
  247. $ubill['type'] = 20;
  248. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  249. $ubill['st'] = 1;
  250. $ubill['remark'] = '合約交易止損平倉返回保證金';
  251. $ubillre = M("bill")->add($ubill);
  252. $ubill['num'] = $tprofit;
  253. $ubill['afternum'] = $bond;
  254. $ubill['coinname'] = "lever";
  255. $ubill['st'] = 2;
  256. $ubill['remark'] = '合約交易止損平倉虧損';
  257. $ubillre = M("bill")->add($ubill);
  258. M("leverorder")->where(array('id' => $id))->save($data);
  259. M("levermoney")->where(['uid' => $uid])->setInc('money', $vo['bond']);
  260. M("levermoney")->where(['uid' => $uid])->setDec('money', $tprofit);
  261. M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
  262. echo "=订单ID:" . $id . ",止损平仓=";
  263. } else {
  264. if ($user['money'] + $vo['bond'] >= $tprofit) {
  265. $user_bond = $vo['bond'] - ($tprofit - $user['money']);
  266. $data['pc_price'] = $close;
  267. $data['status'] = 3;
  268. $data['yk_status'] = 2;
  269. $data['ylmoney'] = $tprofit;
  270. $data['endtime'] = date("Y-m-d H:i:s", time());
  271. $bond = $tprofit - $vo['bond'];
  272. $ubill['uid'] = $uid;
  273. $ubill['username'] = $uinfo['username'];
  274. $ubill['num'] = $user_bond;
  275. $ubill['afternum'] = $user_bond;
  276. $ubill['coinname'] = "lever";
  277. $ubill['type'] = 20;
  278. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  279. $ubill['st'] = 1;
  280. $ubill['remark'] = '合約交易止損平倉退回剩餘保證金';
  281. $ubillre = M("bill")->add($ubill);
  282. $ubill['num'] = $user['money'];
  283. $ubill['afternum'] = 0;
  284. $ubill['type'] = 20;
  285. $ubill['st'] = 2;
  286. $ubill['remark'] = '合約交易手動平倉虧損';
  287. M("leverorder")->where(array('id' => $id))->save($data);
  288. M("levermoney")->where(['uid' => $uid])->setInc('money', $user_bond);
  289. M("levermoney")->where(array('uid' => $uid))->setDec('money', $user['money']);
  290. M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
  291. echo "=订单ID:" . $id . ",止损平仓=";
  292. } else {
  293. $data['pc_price'] = $close;
  294. $data['status'] = 3;
  295. $data['yk_status'] = 2;
  296. $data['ylmoney'] = $tprofit;
  297. $data['endtime'] = date("Y-m-d H:i:s", time());
  298. $bond = $tprofit - $vo['bond'];
  299. $ubill['uid'] = $uid;
  300. $ubill['username'] = $uinfo['username'];
  301. $ubill['num'] = $user['money'];
  302. $ubill['afternum'] = 0;
  303. $ubill['coinname'] = "lever";
  304. $ubill['type'] = 20;
  305. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  306. $ubill['st'] = 1;
  307. $ubill['remark'] = '合約交易ID' . $vo['id'] . '止損平倉扣除完保證金,並扣除合約帳戶';
  308. $ubillre = M("bill")->add($ubill);
  309. M("leverorder")->where(array('id' => $id))->save($data);
  310. M("levermoney")->where(['uid' => $uid])->setDec('money', $user['money']);
  311. M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $vo['bond']);
  312. echo "=订单ID:" . $id . ",止损平仓=";
  313. }
  314. }
  315. }
  316. }
  317. }
  318. if ($zsprice == 0) {
  319. if ($direction == 1) {
  320. $profit_fd = sprintf("%.2f", ($close - $price) * $vo['num']);
  321. if ($profit_fd < 0) {
  322. if (abs($profit_fd) > ($user['money'] + $user['moneyd'])) {
  323. $data['pc_price'] = $close;
  324. $data['status'] = 4;
  325. $data['yk_status'] = 2;
  326. $data['ylmoney'] = $profit_fd;
  327. $data['endtime'] = date("Y-m-d H:i:s", time());
  328. $ubill['uid'] = $uid;
  329. $ubill['username'] = $uinfo['username'];
  330. $ubill['num'] = $user['money'];
  331. $ubill['afternum'] = 0;
  332. $ubill['coinname'] = "lever";
  333. $ubill['type'] = 20;
  334. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  335. $ubill['st'] = 1;
  336. $ubill['remark'] = '合約交易ID' . $vo['id'] . '爆倉';
  337. $ubillre = M("bill")->add($ubill);
  338. M("leverorder")->where(array('id' => $id))->save($data);
  339. M("levermoney")->where(['uid' => $uid])->save(['money' => 0, 'moneyd' => 0]);
  340. $pc_list = M('leverorder')->where(['uid' => $uid])->where(['status' => 2])->select();
  341. foreach ($pc_list as $item) {
  342. $direction = $item['direction'];
  343. if ($direction == 1) { //做多
  344. $profit = sprintf("%.2f", ($close - $item['price']) * $item['num']);
  345. } elseif ($direction == 2) { //做空
  346. $profit = sprintf("%.2f", ($item['price'] - $close) * $item['num']);
  347. }
  348. if ($profit >= 0) {
  349. $yk_status = 1;
  350. } else {
  351. $yk_status = 2;
  352. }
  353. $tprofit = abs($profit);
  354. $datas['pc_price'] = $close;
  355. $datas['status'] = 3;
  356. $datas['yk_status'] = $yk_status;
  357. $datas['ylmoney'] = $tprofit;
  358. $datas['endtime'] = date("Y-m-d H:i:s", time());
  359. $result = M("leverorder")->where(array('id' => $item['id'], 'status' => 2))->save($datas);
  360. $user = M('levermoney')->where(['uid' => $uid])->find();
  361. if ($result) {
  362. if ($profit >= 0) {
  363. M("levermoney")->where(array('uid' => $uid))->setInc('money', $tprofit);
  364. $ubill['uid'] = $uid;
  365. $ubill['username'] = $uinfo['username'];
  366. $ubill['num'] = $tprofit;
  367. $ubill['coinname'] = "lever";
  368. $ubill['afternum'] = $user['money'] + $tprofit;
  369. $ubill['type'] = 20;
  370. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  371. $ubill['st'] = 1;
  372. $ubill['remark'] = L('合约交易自動平仓盈利收益');
  373. $ubillre = M("bill")->add($ubill);
  374. }
  375. }
  376. }
  377. echo "=订单ID:" . $id . ",爆仓=";
  378. }
  379. } elseif ($direction == 2) {
  380. $profit_fd = sprintf("%.2f", ($price - $close) * $vo['num']);
  381. if ($profit_fd < 0) {
  382. if (abs($profit_fd) > ($vo['bond'] + $user['moneyd'])) {
  383. $data['pc_price'] = $close;
  384. $data['status'] = 4;
  385. $data['yk_status'] = 2;
  386. $data['ylmoney'] = $profit_fd;
  387. $data['endtime'] = date("Y-m-d H:i:s", time());
  388. $ubill['uid'] = $uid;
  389. $ubill['username'] = $uinfo['username'];
  390. $ubill['num'] = $user['money'];
  391. $ubill['afternum'] = 0;
  392. $ubill['coinname'] = "lever";
  393. $ubill['type'] = 20;
  394. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  395. $ubill['st'] = 1;
  396. $ubill['remark'] = '合約交易ID' . $vo['id'] . '爆倉';
  397. $ubillre = M("bill")->add($ubill);
  398. M("leverorder")->where(array('id' => $id))->save($data);
  399. M("levermoney")->where(['uid' => $uid])->save(['money' => 0, 'moneyd' => 0]);
  400. $pc_list = M('leverorder')->where(['uid' => $uid])->where(['status' => 2])->select();
  401. foreach ($pc_list as $item) {
  402. $direction = $item['direction'];
  403. if ($direction == 1) { //做多
  404. $profit = sprintf("%.2f", ($close - $item['price']) * $item['num']);
  405. } elseif ($direction == 2) { //做空
  406. $profit = sprintf("%.2f", ($item['price'] - $close) * $item['num']);
  407. }
  408. if ($profit >= 0) {
  409. $yk_status = 1;
  410. } else {
  411. $yk_status = 2;
  412. }
  413. $tprofit = abs($profit);
  414. $datas['pc_price'] = $close;
  415. $datas['status'] = 3;
  416. $datas['yk_status'] = $yk_status;
  417. $datas['ylmoney'] = $tprofit;
  418. $datas['endtime'] = date("Y-m-d H:i:s", time());
  419. $result = M("leverorder")->where(array('id' => $item['id'], 'status' => 2))->save($datas);
  420. $user = M('levermoney')->where(['uid' => $uid])->find();
  421. if ($result) {
  422. if ($profit >= 0) {
  423. M("levermoney")->where(array('uid' => $uid))->setInc('money', $tprofit);
  424. $ubill['uid'] = $uid;
  425. $ubill['username'] = $uinfo['username'];
  426. $ubill['num'] = $tprofit;
  427. $ubill['coinname'] = "lever";
  428. $ubill['afternum'] = $user['money'] + $tprofit;
  429. $ubill['type'] = 20;
  430. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  431. $ubill['st'] = 1;
  432. $ubill['remark'] = L('合约交易自動平仓盈利收益');
  433. $ubillre = M("bill")->add($ubill);
  434. }
  435. }
  436. }
  437. echo "=订单ID:" . $id . ",爆仓=";
  438. }
  439. }
  440. }
  441. }
  442. }
  443. // echo "正常请求".$close;
  444. } else {
  445. echo "未获取到参数" . $id;
  446. }
  447. }
  448. }
  449. }
  450. //永续合约委托定单自动建仓
  451. public function AutoCreateOrder()
  452. {
  453. $list = M("leverorder")->where(array('status' => 1))->select();
  454. if (!empty($list)) {
  455. $sysinfo = M("leversetting")->where(array('id' => 1))->find();
  456. $lever_fee = $sysinfo['lever_fee'];//费率
  457. foreach ($list as $key => $vo) {
  458. $id = $vo['id'];
  459. $uid = $vo['uid'];
  460. $uinfo = M("user")->where(array('id' => $uid))->field("id,username")->find();
  461. $minfo = M("user_coin")->where(array('userid' => $uid))->find();
  462. $usermoney = M('levermoney')->where(['uid' => $uid])->find();
  463. $lowercoin = $vo['coinname'];
  464. if ($lowercoin == 'ukbusdt') {
  465. $o_symbol = $vo['symbol'];
  466. $symbol_arr = explode('/', $o_symbol);
  467. $symbol = strtolower($symbol_arr[0]) . "_" . strtolower($symbol_arr[1]);
  468. $mlist = M("market")->where(array('name' => $symbol))->field("new_price,min_price")->find();
  469. $close = $mlist['new_price'];
  470. } else {
  471. $oy_list = do_request(oy_url($vo['coinname'], 'api/v5/market/ticker?instId='), []);
  472. $oy_list = $oy_list->data[0];
  473. $close = $oy_list->last;//现价
  474. }
  475. //手续费计算(开仓价格 * 开仓数量 * 手续费率) ,开仓时扣除手续费
  476. $wt_price = $vo['wt_price']; //委托价格
  477. $direction = $vo['direction']; //交易方向
  478. if ($direction == 1) { //做多(上涨) 当前价小于等于委托价格就开仓 要跌了才开仓
  479. if ($close <= $wt_price) {
  480. $data['fee'] = $fee = $vo['fee'];
  481. if ($usermoney['money'] >= $fee) {
  482. $data['price'] = $close;
  483. $data['status'] = 2;
  484. $data['addtime'] = date("Y-m-d H:i:s", time());
  485. $re_a = M("leverorder")->where(array('id' => $id))->save($data);
  486. $re_b = M("levermoney")->where(array('uid' => $uid))->setDec('money', $fee);
  487. $ubill['uid'] = $uid;
  488. $ubill['username'] = $uinfo['username'];
  489. $ubill['num'] = $fee;
  490. $ubill['coinname'] = "lever";
  491. $ubill['afternum'] = $usermoney['money'] - $fee;
  492. $ubill['type'] = 25;
  493. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  494. $ubill['st'] = 2;
  495. $ubill['remark'] = L('合约交易开仓手续费');
  496. $re_c = M("bill")->add($ubill);
  497. if ($re_a && $re_b && $re_c) {
  498. echo "=订单ID:" . $id . ",开仓成功=";
  499. }
  500. } else {
  501. echo "=订单ID:" . $id . ",手续费不足=";
  502. }
  503. } else {
  504. echo "=订单ID:" . $id . ",做多,价格不合适=";
  505. }
  506. } else if ($direction == 2) { //做空(下跌) 当前价大于等于委托价格就开仓 要涨了才开仓
  507. if ($close >= $wt_price) {
  508. $data['fee'] = $fee = $vo['fee'];
  509. if ($usermoney['money'] >= $fee) {
  510. $data['price'] = $close;
  511. $data['status'] = 2;
  512. $data['addtime'] = date("Y-m-d H:i:s", time());
  513. $re_a = M("leverorder")->where(array('id' => $id))->save($data);
  514. $re_b = M("levermoney")->where(array('uid' => $uid))->setDec('money', $fee);
  515. $ubill['uid'] = $uid;
  516. $ubill['username'] = $uinfo['username'];
  517. $ubill['num'] = $fee;
  518. $ubill['coinname'] = "usdt";
  519. $ubill['afternum'] = $usermoney['money'] - $fee;
  520. $ubill['type'] = 25;
  521. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  522. $ubill['st'] = 2;
  523. $ubill['remark'] = L('合约交易开仓手续费');
  524. $re_c = M("bill")->add($ubill);
  525. if ($re_a && $re_b && $re_c) {
  526. echo "=订单ID:" . $id . ",开仓成功=";
  527. }
  528. } else {
  529. echo "=订单ID:" . $id . ",手续费不足=";
  530. }
  531. } else {
  532. echo "=订单ID:" . $id . ",做空,价格不合适=";
  533. }
  534. }
  535. }
  536. }
  537. }
  538. //自动释放冻结的认购币,设置计划任务,每天执行一次
  539. public function releaseissue()
  540. {
  541. $nowday = date("Y-m-d", time());
  542. $map['status'] = 1;
  543. $map['endday'] = array('elt', $nowday);
  544. $list = M("issue_log")->where($map)->select();
  545. if (!empty($list)) {
  546. foreach ($list as $key => $vo) {
  547. $id = $vo['id'];
  548. $uid = $vo['uid'];
  549. $num = $vo['num'];
  550. $cname = trim($vo['coinname']);
  551. $cnamed = trim($vo['coinname']) . "d";
  552. //修改记录状态
  553. $result = M("issue_log")->where(array('id' => $id))->save(array('status' => 2));
  554. if ($result) {
  555. $minfo = M("user_coin")->where(array('userid' => $uid))->find();
  556. //扣除冻结的资产
  557. M("user_coin")->where(array('userid' => $uid))->setDec($cnamed, $num);
  558. //增加可用资产的数量
  559. M("user_coin")->where(array('userid' => $uid))->setInc($cname, $num);
  560. //写入日志
  561. $data['uid'] = $uid;
  562. $data['username'] = $vo['account'];
  563. $data['num'] = $num;
  564. $data['coinname'] = $cname;
  565. $data['afternum'] = $minfo[$cname] + $num;
  566. $data['type'] = 18;
  567. $data['addtime'] = date("Y-m-d H:i:s", time());
  568. $data['st'] = 1;
  569. $data['remark'] = L("认购资产释放");
  570. M("bill")->add($data);
  571. echo "==认购记录ID:" . $id . "释放成功";
  572. } else {
  573. echo "==认购记录ID:" . $id . "释放失败";
  574. }
  575. }
  576. } else {
  577. echo "==没有可释放认购记录==";
  578. }
  579. }
  580. //委托订单自动交易
  581. //设置成5-10秒执行一次的计划任务
  582. public function autoxjtade()
  583. {
  584. $list = M("bborder")->where(array('ordertype' => 1, 'status' => 1))->select();
  585. if (!empty($list)) {
  586. foreach ($list as $k => $v) {
  587. $type = $v['type'];
  588. $uid = $v['uid'];
  589. $id = $v['id'];
  590. $symbol = strtolower($v['coin']) . 'usdt';
  591. $lowercoin = strtolower($v['coin']);
  592. //限价单价
  593. $xjprice = $v['xjprice'];
  594. $sxfbl = $v['sxfbl'];
  595. if ($lowercoin == "ukb") {
  596. $priceinfo = M("market")->where(array('name' => "ukb_usdt"))->field("new_price")->find();
  597. $newprice = $priceinfo['new_price'];
  598. }else if ($lowercoin == "gcb") {
  599. $priceinfo = M("market")->where(array('name' => "gcb_usdt"))->field("new_price")->find();
  600. $newprice = $priceinfo['new_price'];
  601. } else {
  602. //获取当前行情价
  603. $oy_list = do_request(oy_url(strtolower($v['coin']), 'api/v5/market/ticker?instId='), []);
  604. $oy_list = $oy_list->data[0];
  605. $newprice = $oy_list->last;//现价
  606. // $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $symbol;
  607. // $newprice = $this->getnewprice($coinapi);
  608. }
  609. //买入,当行情价小于等于限价时则交易
  610. $minfo = M("user_coin")->where(array('userid' => $uid))->find();
  611. if ($type == 1) {
  612. $usdtnum = $v['usdtnum'];
  613. if ($newprice <= $xjprice) {
  614. //计算能够买到的量
  615. $buy_coinnum = sprintf("%.8f", ($usdtnum / $newprice));
  616. //手续费
  617. $fee = $buy_coinnum * $sxfbl / 100;
  618. //实际到账号的金额
  619. $tcoinnum = $buy_coinnum - $fee;
  620. //更新订单
  621. $data['coinnum'] = $tcoinnum;
  622. $data['price'] = $newprice;
  623. $data['tradetime'] = date("Y-m-d H:i:s", time());
  624. $data['fee'] = $fee;
  625. $data['status'] = 2;
  626. $savere = M("bborder")->where(array('id' => $id))->save($data);
  627. //增加购买数量并写入日志
  628. $incre = M("user_coin")->where(array('userid' => $uid))->setInc($lowercoin, $tcoinnum);
  629. $cincbill['uid'] = $uid;
  630. $cincbill['username'] = $v['account'];
  631. $cincbill['num'] = $tcoinnum;
  632. $cincbill['coinname'] = $lowercoin;
  633. $cincbill['afternum'] = $minfo[$lowercoin] + $tcoinnum;
  634. $cincbill['type'] = 10;
  635. $cincbill['addtime'] = date("Y-m-d H:i:s", time());
  636. $cincbill['st'] = 1;
  637. $cincbill['remark'] = L('币币交易限价购买委托成交');
  638. $cincre = M("bill")->add($cincbill);
  639. //扣除冻结的USDT并写入日志
  640. $decre = M("user_coin")->where(array('userid' => $uid))->setDec("usdtd", $usdtnum);
  641. $uincbill['uid'] = $uid;
  642. $uincbill['username'] = $v['account'];
  643. $uincbill['num'] = $usdtnum;
  644. $uincbill['coinname'] = "usdt";
  645. $uincbill['afternum'] = $minfo['usdt'] - $usdtnum;
  646. $uincbill['type'] = 9;
  647. $uincbill['addtime'] = date("Y-m-d H:i:s", time());
  648. $uincbill['st'] = 2;
  649. $uincbill['remark'] = L('币币交易限价购买委托成交');
  650. $uincre = M("bill")->add($uincbill);
  651. if ($savere && $cincre && $uincre) {
  652. $notice['uid'] = $uid;
  653. $notice['account'] = $v['account'];
  654. $notice['title'] = L('币币交易限价委托交易');
  655. $notice['content'] = L('币币交易限价购买委托订单购买成功');
  656. $notice['addtime'] = date("Y-m-d H:i:s", time());
  657. $notice['status'] = 1;
  658. M("notice")->add($notice);
  659. echo "==委托订单ID:" . $id . ",购买成功==";
  660. }
  661. } else {
  662. echo "==委托订单ID:" . $id . ",没有达到限价购买价格==";
  663. }
  664. //卖出,当行情价大于等于限价时则交易
  665. } elseif ($type == 2) {
  666. $coinnum = $v['coinnum'];
  667. if ($newprice >= $xjprice) {
  668. //求出卖出所得的USDT量
  669. $allusdt = sprintf("%.8f", ($coinnum * $newprice));
  670. //求出手续费
  671. $fee = $allusdt * $sxfbl / 100;
  672. //求出实际到账USDT量
  673. $tusdtnum = $allusdt - $fee;
  674. //更新订单
  675. $data['usdtnum'] = $tusdtnum;
  676. $data['price'] = $newprice;
  677. $data['tradetime'] = date("Y-m-d H:i:s", time());
  678. $data['fee'] = $fee;
  679. $data['status'] = 2;
  680. $savere = M("bborder")->where(array('id' => $id))->save($data);
  681. //增加卖出所得的USDT量并写入日志
  682. $incre = M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $tusdtnum);
  683. $uincbill['uid'] = $uid;
  684. $uincbill['username'] = $v['account'];
  685. $uincbill['num'] = $tusdtnum;
  686. $uincbill['coinname'] = 'usdt';
  687. $uincbill['afternum'] = $minfo['usdt'] + $tusdtnum;
  688. $uincbill['type'] = 9;
  689. $uincbill['addtime'] = date("Y-m-d H:i:s", time());
  690. $uincbill['st'] = 1;
  691. $uincbill['remark'] = L('币币交易限价出售委托成交');
  692. $uincre = M("bill")->add($uincbill);
  693. //扣除冻结的卖出币量并写入日志
  694. $decre = M("user_coin")->where(array('userid' => $uid))->setDec($lowercoin . "d", $coinnum);
  695. $cincbill['uid'] = $uid;
  696. $cincbill['username'] = $v['account'];
  697. $cincbill['num'] = $coinnum;
  698. $cincbill['coinname'] = $lowercoin;
  699. $cincbill['afternum'] = $minfo[$lowercoin] - $coinnum;
  700. $cincbill['type'] = 10;
  701. $cincbill['addtime'] = date("Y-m-d H:i:s", time());
  702. $cincbill['st'] = 2;
  703. $cincbill['remark'] = L('币币交易限价出售委托成交');
  704. $cincre = M("bill")->add($cincbill);
  705. if ($savere && $cincre && $uincre) {
  706. $notice['uid'] = $uid;
  707. $notice['account'] = $v['account'];
  708. $notice['title'] = L('币币交易限价委托交易');
  709. $notice['content'] = L('币币交易限价购买委托订单出售成功');
  710. $notice['addtime'] = date("Y-m-d H:i:s", time());
  711. $notice['status'] = 1;
  712. M("notice")->add($notice);
  713. echo "==委托订单ID:" . $id . ",出售成功==";
  714. }
  715. } else {
  716. echo "==委托订单ID:" . $id . ",没有达到限价出售价格==";
  717. }
  718. }
  719. }
  720. } else {
  721. echo "没有限价委托可交易!";
  722. }
  723. }
  724. //释放冻结的矿机收益币
  725. //设置一天执行一次的计划任务
  726. public function releasedjprofit()
  727. {
  728. $nowday = date("Y-m-d", time());
  729. $where['thawday'] = array('elt', $nowday);
  730. $where['status'] = array('eq', 1);
  731. $list = M("djprofit")->where($where)->select();
  732. if (!empty($list)) {
  733. foreach ($list as $key => $vo) {
  734. $id = $vo['id'];
  735. $uid = $vo['uid'];
  736. $username = $vo['username'];
  737. $num = $vo['num'];
  738. $coinname = trim($vo['coin']);
  739. $minfo = M("user_coin")->where(array('userid' => $uid))->find();
  740. //修改冻结状态
  741. M("djprofit")->where(array('id' => $id))->save(array('status' => 2));
  742. //添加财务日志
  743. $billdata['uid'] = $uid;
  744. $billdata['username'] = $username;
  745. $billdata['num'] = $num;
  746. $billdata['coinname'] = $coinname;
  747. $billdata['afternum'] = $minfo[$coinname] + $num;
  748. $billdata['type'] = 8;
  749. $billdata['addtime'] = date("Y-m-d H:i:s", time());
  750. $billdata['st'] = 1;
  751. $billdata['remark'] = L('释放冻结收益');
  752. M("bill")->add($billdata);
  753. //增加会员资产,减少冻结额度
  754. $coinname_d = $coinname . "d";
  755. M("user_coin")->where(array('userid' => $uid))->setDec($coinname_d, $num);
  756. M("user_coin")->where(array('userid' => $uid))->setInc($coinname, $num);
  757. $notice['uid'] = $uid;
  758. $notice['account'] = $username;
  759. $notice['title'] = L('释放冻结收益');
  760. $notice['content'] = L('您冻结的矿机收益释放成功,可以交易');
  761. $notice['addtime'] = date("Y-m-d H:i:s", time());
  762. $notice['status'] = 1;
  763. M("notice")->add($notice);
  764. echo "==ID:" . $id . "释放" . $num . $coinname . "成功==";
  765. echo "<br />";
  766. }
  767. } else {
  768. echo "====没有可释放的冻结记录====";
  769. }
  770. }
  771. //共享矿机自动结算收益,设置一天执行一次的计划任务
  772. public function authsharesjsy()
  773. {
  774. $kjlist = M("kjorder")->where(array('status' => 1, 'type' => 2))->select();
  775. if (!empty($kjlist)) {
  776. foreach ($kjlist as $key => $vo) {
  777. $id = $vo['id'];
  778. $uid = $vo['uid'];
  779. $username = $vo['username'];
  780. $minfo = M("user_coin")->where(array('userid' => $uid))->find();
  781. $kid = $vo['kid'];
  782. $nowdate = date("Y-m-d", time());
  783. $profitinfo = M("kjprofit")->where(array('uid' => $uid, 'kid' => $id, 'day' => $nowdate))->find();
  784. if (empty($profitinfo)) {
  785. $sharbltxt = $vo['sharbltxt'];
  786. if ($sharbltxt <= 0) {
  787. echo "===共享矿机ID" . $id . "共享码有误===";
  788. } else {
  789. $sharekj = M("kjorder")->where(array('sharbltxt' => $sharbltxt))->count();
  790. if ($sharekj >= 2) {
  791. //查找矿机收益的类型以及查找收益是否需要冻结及冻结天数
  792. $outtype = $vo['outtype'];
  793. if ($outtype == 1) {//按产值需要查找产出币种的最新行情
  794. $coinname = strtolower(trim($vo['outcoin']));
  795. $outnum = $vo['outusdt'];
  796. $symbol = $coinname . 'usdt';
  797. $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $symbol;
  798. $newprice = $this->getnewprice($coinapi);
  799. $tcoinnum = sprintf("%.6f", ($outnum / $newprice)); //实际产生的币量,保留6位小数
  800. } elseif ($outtype == 2) {
  801. $coinname = strtolower(trim($vo['outcoin']));
  802. $tcoinnum = $vo['outnum'];
  803. }
  804. $djout = $vo['djout'];//1冻结2不冻结
  805. $djday = $vo['djnum'];//冻结天数
  806. //写入矿机收益日志
  807. $kjprofit_d['uid'] = $uid;
  808. $kjprofit_d['username'] = $username;
  809. $kjprofit_d['kid'] = $id;
  810. $kjprofit_d['ktitle'] = $vo['kjtitle'];
  811. $kjprofit_d['num'] = $tcoinnum;
  812. $kjprofit_d['coin'] = $coinname;
  813. $kjprofit_d['addtime'] = date("Y-m-d H:i:s", time());
  814. $kjprofit_d['day'] = date("Y-m-d", time());
  815. M("kjprofit")->add($kjprofit_d);
  816. if ($djout == 2) {
  817. $coin_d = $coinname . "d";
  818. M("user_coin")->where(array('userid' => $uid))->setInc($coin_d, $tcoinnum);
  819. $djprofit_d['uid'] = $uid;
  820. $djprofit_d['username'] = $username;
  821. $djprofit_d['num'] = $tcoinnum;
  822. $djprofit_d['coin'] = $coinname;
  823. $djprofit_d['status'] = 1;
  824. $djprofit_d['addtime'] = date("Y-m-d H:i:s", time());
  825. $djprofit_d['addday'] = date("Y-m-d", time());
  826. $djprofit_d['thawtime'] = date("Y-m-d H:i:s", (time() + 86400 * $djday));
  827. $djprofit_d['thawday'] = date("Y-m-d", (time() + 86400 * $djday));
  828. $djprofit_d['remark'] = L('冻结矿机释放收益');
  829. M("djprofit")->add($djprofit_d);
  830. //写资金日志
  831. $billdata['uid'] = $uid;
  832. $billdata['username'] = $username;
  833. $billdata['num'] = $tcoinnum;
  834. $billdata['coinname'] = $coinname;
  835. $billdata['afternum'] = $minfo[$coin_d] + $tcoinnum;
  836. $billdata['type'] = 7;
  837. $billdata['addtime'] = date("Y-m-d H:i:s", time());
  838. $billdata['st'] = 1;
  839. $billdata['remark'] = L('矿机收益释放冻结');
  840. M("bill")->add($billdata);
  841. $notice['uid'] = $uid;
  842. $notice['account'] = $username;
  843. $notice['title'] = L('矿机收益');
  844. $notice['content'] = L('今日矿机收益已成功到账,请注册查收');
  845. $notice['addtime'] = date("Y-m-d H:i:s", time());
  846. $notice['status'] = 1;
  847. M("notice")->add($notice);
  848. } elseif ($djout == 1) {
  849. M("user_coin")->where(array('userid' => $uid))->setInc($coinname, $tcoinnum);
  850. //写资金日志
  851. $billdata['uid'] = $uid;
  852. $billdata['username'] = $username;
  853. $billdata['num'] = $tcoinnum;
  854. $billdata['coinname'] = $coinname;
  855. $billdata['afternum'] = $minfo[$coinname] + $tcoinnum;
  856. $billdata['type'] = 8;
  857. $billdata['addtime'] = date("Y-m-d H:i:s", time());
  858. $billdata['st'] = 1;
  859. $billdata['remark'] = L('矿机收益释放');
  860. M("bill")->add($billdata);
  861. }
  862. //修改矿机收益次数
  863. M("kjorder")->where(array('id' => $id))->setDec("synum", 1);
  864. $reinfo = M("kjorder")->where(array('id' => $id))->find();
  865. if ($reinfo['synum'] < 1) {
  866. M("kjorder")->where(array('id' => $id))->save(array('status' => 3));
  867. }
  868. echo "==共享矿机ID:" . $kid . "收益成功==";
  869. echo "<br />";
  870. } else {
  871. echo "===共享矿机ID" . $id . "另一部分没有购买,不能收益===";
  872. }
  873. }
  874. } else {
  875. echo "==矿机ID:" . $kid . "不能重复收益==";
  876. echo "<br />";
  877. }
  878. }
  879. }
  880. }
  881. //独资矿机自动收益,每天执行一次
  882. //设置一天执行一次的计划任务
  883. public function autokjsy()
  884. {
  885. $kjlist = M("kjorder")->where(array('status' => 1, 'type' => 1))->select();
  886. if (!empty($kjlist)) {
  887. foreach ($kjlist as $key => $vo) {
  888. $id = $vo['id'];
  889. $uid = $vo['uid'];
  890. $username = $vo['username'];
  891. $minfo = M("user_coin")->where(array('userid' => $uid))->find();
  892. $kid = $vo['kid'];
  893. $nowdate = date("Y-m-d", time());
  894. $profitinfo = M("kjprofit")->where(array('uid' => $uid, 'kid' => $id, 'day' => $nowdate))->find();
  895. if (empty($profitinfo)) {
  896. //查找矿机收益的类型以及查找收益是否需要冻结及冻结天数
  897. $outtype = $vo['outtype'];
  898. if ($outtype == 1) {//按产值需要查找产出币种的最新行情
  899. $coinname = strtolower(trim($vo['outcoin']));
  900. $outnum = $vo['outusdt'];
  901. $symbol = $coinname . 'usdt';
  902. $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $symbol;
  903. $newprice = $this->getnewprice($coinapi);
  904. $tcoinnum = sprintf("%.6f", ($outnum / $newprice)); //实际产生的币量,保留6位小数
  905. } elseif ($outtype == 2) {
  906. $coinname = strtolower(trim($vo['outcoin']));
  907. $tcoinnum = $vo['outnum'];
  908. }
  909. $djout = $vo['djout'];//1冻结2不冻结
  910. $djday = $vo['djnum'];//冻结天数
  911. //写入矿机收益日志
  912. $kjprofit_d['uid'] = $uid;
  913. $kjprofit_d['username'] = $username;
  914. $kjprofit_d['kid'] = $id;
  915. $kjprofit_d['ktitle'] = $vo['kjtitle'];
  916. $kjprofit_d['num'] = $tcoinnum;
  917. $kjprofit_d['coin'] = $coinname;
  918. $kjprofit_d['addtime'] = date("Y-m-d H:i:s", time());
  919. $kjprofit_d['day'] = date("Y-m-d", time());
  920. M("kjprofit")->add($kjprofit_d);
  921. if ($djout == 2) {
  922. $coin_d = $coinname . "d";
  923. M("user_coin")->where(array('userid' => $uid))->setInc($coin_d, $tcoinnum);
  924. $djprofit_d['uid'] = $uid;
  925. $djprofit_d['username'] = $username;
  926. $djprofit_d['num'] = $tcoinnum;
  927. $djprofit_d['coin'] = $coinname;
  928. $djprofit_d['status'] = 1;
  929. $djprofit_d['addtime'] = date("Y-m-d H:i:s", time());
  930. $djprofit_d['addday'] = date("Y-m-d", time());
  931. $djprofit_d['thawtime'] = date("Y-m-d H:i:s", (time() + 86400 * $djday));
  932. $djprofit_d['thawday'] = date("Y-m-d", (time() + 86400 * $djday));
  933. $djprofit_d['remark'] = L('冻结矿机释放收益');
  934. M("djprofit")->add($djprofit_d);
  935. //写资金日志
  936. $billdata['uid'] = $uid;
  937. $billdata['username'] = $username;
  938. $billdata['num'] = $tcoinnum;
  939. $billdata['coinname'] = $coinname;
  940. $billdata['afternum'] = $minfo[$coin_d] + $tcoinnum;
  941. $billdata['type'] = 7;
  942. $billdata['addtime'] = date("Y-m-d H:i:s", time());
  943. $billdata['st'] = 1;
  944. $billdata['remark'] = L('矿机收益释放冻结');
  945. M("bill")->add($billdata);
  946. $notice['uid'] = $uid;
  947. $notice['account'] = $username;
  948. $notice['title'] = L('矿机收益');
  949. $notice['content'] = L('今日矿机收益已成功到账,请注册查收');
  950. $notice['addtime'] = date("Y-m-d H:i:s", time());
  951. $notice['status'] = 1;
  952. M("notice")->add($notice);
  953. } elseif ($djout == 1) {
  954. M("user_coin")->where(array('userid' => $uid))->setInc($coinname, $tcoinnum);
  955. //写资金日志
  956. $billdata['uid'] = $uid;
  957. $billdata['username'] = $username;
  958. $billdata['num'] = $tcoinnum;
  959. $billdata['coinname'] = $coinname;
  960. $billdata['afternum'] = $minfo[$coinname] + $tcoinnum;
  961. $billdata['type'] = 8;
  962. $billdata['addtime'] = date("Y-m-d H:i:s", time());
  963. $billdata['st'] = 1;
  964. $billdata['remark'] = L('矿机收益释放');
  965. M("bill")->add($billdata);
  966. }
  967. //修改矿机收益次数
  968. M("kjorder")->where(array('id' => $id))->setDec("synum", 1);
  969. $reinfo = M("kjorder")->where(array('id' => $id))->find();
  970. if ($reinfo['synum'] < 1) {
  971. M("kjorder")->where(array('id' => $id))->save(array('status' => 3));
  972. }
  973. echo "==矿机ID:" . $kid . "收益成功==";
  974. echo "<br />";
  975. } else {
  976. echo "==矿机ID:" . $kid . "不能重复收益==";
  977. echo "<br />";
  978. }
  979. }
  980. } else {
  981. echo "++||没有正常运行的矿机||++";
  982. }
  983. }
  984. //休验订单自动按风控比例设置订单的盈亏比例
  985. //设置成5-10秒执行一次的计划任务
  986. public function setwl_ty()
  987. {
  988. $map['status'] = 1;
  989. $map['kongyk'] = 0;
  990. $orderobj = M("tyhyorder");
  991. $count = $orderobj->where($map)->count();
  992. $setting = M("hysetting")->where(array('id' => 1))->field("hy_fkgl")->find();
  993. if ($setting['hy_fkgl'] > 0) {
  994. $ylcount = intval($count * $setting['hy_fkgl'] / 100);
  995. $kscount = $count - $ylcount;
  996. if ($ylcount > 0) {
  997. $yllist = $orderobj->where($map)->order("num asc")->limit($ylcount)->select();
  998. if (!empty($yllist)) {
  999. foreach ($yllist as $k => $v) {
  1000. $yid = $v['id'];
  1001. $orderobj->where(array('id' => $yid))->save(array('kongyk' => 1));
  1002. echo "订单ID:" . $yid . "设为盈利==";
  1003. }
  1004. }
  1005. }
  1006. if ($kscount > 0) {
  1007. $kslist = $orderobj->where($map)->order("num asc")->limit($kscount)->select();
  1008. if (!empty($kslist)) {
  1009. foreach ($kslist as $k => $v) {
  1010. $kid = $v['id'];
  1011. $orderobj->where(array('id' => $kid))->save(array('kongyk' => 2));
  1012. echo "订单ID:" . $kid . "设为亏损==";
  1013. }
  1014. }
  1015. }
  1016. }
  1017. echo "操作成功";
  1018. }
  1019. //自动按风控比例设置订单的盈亏比例
  1020. //设置成5-10秒执行一次的计划任务
  1021. public function setwl()
  1022. {
  1023. $map['status'] = 1;
  1024. $map['kongyk'] = 0;
  1025. $orderobj = M("hyorder");
  1026. $count = $orderobj->where($map)->count();
  1027. $setting = M("hysetting")->where(array('id' => 1))->field("hy_fkgl")->find();
  1028. if ($setting['hy_fkgl'] > 0) {
  1029. $ylcount = intval($count * $setting['hy_fkgl'] / 100);
  1030. $kscount = $count - $ylcount;
  1031. if ($ylcount > 0) {
  1032. $yllist = $orderobj->where($map)->order("num asc")->limit($ylcount)->select();
  1033. if (!empty($yllist)) {
  1034. foreach ($yllist as $k => $v) {
  1035. $yid = $v['id'];
  1036. $orderobj->where(array('id' => $yid))->save(array('kongyk' => 1));
  1037. echo "订单ID:" . $yid . "设为盈利==";
  1038. }
  1039. }
  1040. }
  1041. if ($kscount > 0) {
  1042. $kslist = $orderobj->where($map)->order("num asc")->limit($kscount)->select();
  1043. if (!empty($kslist)) {
  1044. foreach ($kslist as $k => $v) {
  1045. $kid = $v['id'];
  1046. $orderobj->where(array('id' => $kid))->save(array('kongyk' => 2));
  1047. echo "订单ID:" . $kid . "设为亏损==";
  1048. }
  1049. }
  1050. }
  1051. }
  1052. echo "操作成功";
  1053. }
  1054. //自动结算合约订单
  1055. public function hycarryout_ty()
  1056. {
  1057. $nowtime = time();
  1058. $map['status'] = 1;
  1059. $map['intselltime'] = array('elt', $nowtime);
  1060. $orderobj = M("tyhyorder");
  1061. $list = $orderobj->where($map)->select();
  1062. if (!empty($list)) {
  1063. foreach ($list as $key => $vo) {
  1064. $coinname = $vo['coinname'];
  1065. $coinarr = explode("/", $coinname);
  1066. $symbol = strtolower($coinarr[0]) . strtolower($coinarr[1]);
  1067. $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $symbol;
  1068. $newprice = $this->getnewprice($coinapi);
  1069. $randnum = "0." . rand(1000, 9999);
  1070. $buyprice = $vo['buyprice'];
  1071. $otype = $vo['hyzd']; //合约方向
  1072. $dkong = $vo['kongyk']; //单控设置
  1073. $uid = $vo['uid'];//会员ID
  1074. $id = $vo['id'];//记录ID
  1075. $num = $vo['num'];
  1076. $hybl = $vo['hybl']; //收益比例
  1077. $ylnum = $num * ($hybl / 100); //盈利金额
  1078. $money = $num + $ylnum;//本金+盈利金额
  1079. //$dkong分三种情况 1、0表示随行情,1表示盈利 2 表示亏损
  1080. //盈利时增加$money,,亏损时亏本金
  1081. if ($dkong == 0) {
  1082. //买涨
  1083. if ($otype == 1) { //买跌
  1084. if ($newprice > $buyprice) { //盈利
  1085. //增加资产
  1086. //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
  1087. M("user")->where(array('id' => $uid))->setInc("money", $money);
  1088. //修改订单状态
  1089. $sd['status'] = 2;
  1090. $sd['is_win'] = 1;
  1091. $sd['sellprice'] = $newprice;
  1092. $sd['ploss'] = $ylnum;
  1093. $orderobj->where(array('id' => $id))->save($sd);
  1094. //写财务日志
  1095. //$this->addlog($uid,$vo['username'],$money);
  1096. } else {//亏损
  1097. //修改订单状态
  1098. $sd['status'] = 2;
  1099. $sd['is_win'] = 2;
  1100. $sd['sellprice'] = $newprice;
  1101. $sd['ploss'] = $num;
  1102. $orderobj->where(array('id' => $id))->save($sd);
  1103. }
  1104. } elseif ($otype == 2) { //买跌
  1105. if ($newprice < $buyprice) { //盈利
  1106. //增加资产
  1107. //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
  1108. M("user")->where(array('id' => $uid))->setInc("money", $money);
  1109. //修改订单状态
  1110. $sd['status'] = 2;
  1111. $sd['is_win'] = 1;
  1112. $sd['sellprice'] = $newprice;
  1113. $sd['ploss'] = $ylnum;
  1114. $orderobj->where(array('id' => $id))->save($sd);
  1115. //写财务日志
  1116. //$this->addlog($uid,$vo['username'],$money);
  1117. } else {//亏损
  1118. //修改订单状态
  1119. $sd['status'] = 2;
  1120. $sd['is_win'] = 2;
  1121. $sd['sellprice'] = $newprice;
  1122. $sd['ploss'] = $num;
  1123. $orderobj->where(array('id' => $id))->save($sd);
  1124. }
  1125. }
  1126. } elseif ($dkong == 1) {//单控盈利
  1127. if ($otype == 1) {//买涨
  1128. if ($newprice > $buyprice) {
  1129. $sellprice = $newprice;
  1130. } elseif ($newprice == $buyprice) {
  1131. $sellprice = $newprice + $randnum;
  1132. } elseif ($newprice < $buyprice) {
  1133. $sellprice = $buyprice + $randnum;
  1134. }
  1135. //增加资产
  1136. //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
  1137. M("user")->where(array('id' => $uid))->setInc("money", $money);
  1138. //修改订单状态
  1139. $sd['status'] = 2;
  1140. $sd['is_win'] = 1;
  1141. $sd['sellprice'] = $sellprice;
  1142. $sd['ploss'] = $ylnum;
  1143. $orderobj->where(array('id' => $id))->save($sd);
  1144. //写财务日志
  1145. //$this->addlog($uid,$vo['username'],$money);
  1146. } elseif ($otype == 2) {//买跌
  1147. if ($newprice > $buyprice) {
  1148. $sellprice = $buyprice - $randnum;
  1149. } elseif ($newprice == $buyprice) {
  1150. $sellprice = $buyprice - $randnum;
  1151. } elseif ($newprice < $buyprice) {
  1152. $sellprice = $newprice;
  1153. }
  1154. //增加资产
  1155. //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
  1156. M("user")->where(array('id' => $uid))->setInc("money", $money);
  1157. //修改订单状态
  1158. $sd['status'] = 2;
  1159. $sd['is_win'] = 1;
  1160. $sd['sellprice'] = $sellprice;
  1161. $sd['ploss'] = $ylnum;
  1162. $orderobj->where(array('id' => $id))->save($sd);
  1163. //写财务日志
  1164. //$this->addlog($uid,$vo['username'],$money);
  1165. }
  1166. } elseif ($dkong == 2) {
  1167. if ($otype == 1) {//买涨
  1168. //买涨,指定亏损,结算价格要低于买入价格
  1169. if ($newprice > $buyprice) {
  1170. $sellprice = $buyprice - $randnum;
  1171. } elseif ($newprice == $buyprice) {
  1172. $sellprice = $buyprice - $randnum;
  1173. } elseif ($newprice < $buyprice) {
  1174. $sellprice = $newprice;
  1175. }
  1176. //修改订单状态
  1177. $sd['status'] = 2;
  1178. $sd['is_win'] = 2;
  1179. $sd['sellprice'] = $sellprice;
  1180. $sd['ploss'] = $num;
  1181. $orderobj->where(array('id' => $id))->save($sd);
  1182. } elseif ($otype == 2) {//买跌
  1183. if ($newprice > $buyprice) {
  1184. $sellprice = $newprice;
  1185. } elseif ($newprice == $buyprice) {
  1186. $sellprice = $buyprice + $randnum;
  1187. } elseif ($newprice < $buyprice) {
  1188. $sellprice = $buyprice + $randnum;
  1189. }
  1190. //修改订单状态
  1191. $sd['status'] = 2;
  1192. $sd['is_win'] = 2;
  1193. $sd['sellprice'] = $sellprice;
  1194. $sd['ploss'] = $num;
  1195. $orderobj->where(array('id' => $id))->save($sd);
  1196. }
  1197. }
  1198. echo "==订单ID:" . $id . "出售成功==";
  1199. }
  1200. } else {
  1201. echo "没有订单可以结算!";
  1202. }
  1203. }
  1204. //自动结算体验合约订单
  1205. public function hycarryout_ty_old()
  1206. {
  1207. $nowtime = time();
  1208. $map['status'] = 1;
  1209. $map['intselltime'] = array('elt', $nowtime);
  1210. $orderobj = M("tyhyorder");
  1211. $list = $orderobj->where($map)->select();
  1212. //获取合约参数
  1213. $setting = M("hysetting")->where(array('id' => 1))->field("hy_ksid,hy_ylid,hy_fkgl")->find();
  1214. //指定盈利ID组
  1215. $winarr = explode(',', $setting['hy_ylid']);
  1216. //指定亏损ID组
  1217. $lossarr = explode(',', $setting['hy_ksid']);
  1218. //风控比例组
  1219. $fkarr = explode(',', $setting['hy_fkgl']);
  1220. if (!empty($list)) {
  1221. foreach ($list as $key => $vo) {
  1222. $coinname = $vo['coinname'];
  1223. $coinarr = explode("/", $coinname);
  1224. $symbol = strtolower($coinarr[0]) . strtolower($coinarr[1]);
  1225. $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $symbol;
  1226. $newprice = $this->getnewprice($coinapi);
  1227. $randnum = "0." . rand(1000, 9999);
  1228. $buyprice = $vo['buyprice'];
  1229. $otype = $vo['hyzd']; //合约方向
  1230. $dkong = $vo['kongyk']; //单控设置
  1231. $uid = $vo['uid'];//会员ID
  1232. $id = $vo['id'];//记录ID
  1233. $num = $vo['num'];
  1234. $hybl = $vo['hybl'];
  1235. $ylnum = $num * ($hybl / 100);
  1236. $money = $num + $ylnum;//盈利金额
  1237. //买涨
  1238. if ($otype == 1) {
  1239. if (in_array($uid, $winarr)) {//如果有指定盈利ID,则按盈利结算
  1240. if ($newprice > $buyprice) {
  1241. $sellprice = $newprice;
  1242. } elseif ($newprice == $buyprice) {
  1243. $sellprice = $newprice + $randnum;
  1244. } elseif ($newprice < $buyprice) {
  1245. $sellprice = $buyprice + $randnum;
  1246. }
  1247. //增加资产
  1248. //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
  1249. M("user")->where(array('id' => $uid))->setInc("money", $money);
  1250. //修改订单状态
  1251. $sd['status'] = 2;
  1252. $sd['is_win'] = 1;
  1253. $sd['sellprice'] = $sellprice;
  1254. $sd['ploss'] = $ylnum;
  1255. $orderobj->where(array('id' => $id))->save($sd);
  1256. //写财务日志
  1257. //$this->addlog($uid,$vo['username'],$money);
  1258. } elseif (in_array($uid, $lossarr)) {//如果有指定亏损ID,则按亏损结算
  1259. //买涨,指定亏损,结算价格要低于买入价格
  1260. if ($newprice > $buyprice) {
  1261. $sellprice = $buyprice - $randnum;
  1262. } elseif ($newprice == $buyprice) {
  1263. $sellprice = $buyprice - $randnum;
  1264. } elseif ($newprice < $buyprice) {
  1265. $sellprice = $newprice;
  1266. }
  1267. //修改订单状态
  1268. $sd['status'] = 2;
  1269. $sd['is_win'] = 2;
  1270. $sd['sellprice'] = $sellprice;
  1271. $sd['ploss'] = $num;
  1272. $orderobj->where(array('id' => $id))->save($sd);
  1273. } else {//如果未指定盈利和亏损,则按单控的计算
  1274. if ($dkong == 1) {//盈利
  1275. if ($newprice > $buyprice) {
  1276. $sellprice = $newprice;
  1277. } elseif ($newprice == $buyprice) {
  1278. $sellprice = $newprice + $randnum;
  1279. } elseif ($newprice < $buyprice) {
  1280. $sellprice = $buyprice + $randnum;
  1281. }
  1282. // echo '买入价格:'.$buyprice;
  1283. // echo "<br />";
  1284. // echo '结算价格:'.$sellprice;die;
  1285. //增加资产
  1286. //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
  1287. M("user")->where(array('id' => $uid))->setInc("money", $money);
  1288. //修改订单状态
  1289. $sd['status'] = 2;
  1290. $sd['is_win'] = 1;
  1291. $sd['sellprice'] = $sellprice;
  1292. $sd['ploss'] = $ylnum;
  1293. $orderobj->where(array('id' => $id))->save($sd);
  1294. //写财务日志
  1295. //$this->addlog($uid,$vo['username'],$money);
  1296. } elseif ($dkong == 2) {//亏损
  1297. if ($newprice > $buyprice) {
  1298. $sellprice = $buyprice - $randnum;
  1299. } elseif ($newprice == $buyprice) {
  1300. $sellprice = $buyprice - $randnum;
  1301. } elseif ($newprice < $buyprice) {
  1302. $sellprice = $newprice;
  1303. }
  1304. // echo '买入价格:'.$buyprice;
  1305. // echo "<br />";
  1306. // echo '结算价格:'.$sellprice;die;
  1307. //修改订单状态
  1308. $sd['status'] = 2;
  1309. $sd['is_win'] = 2;
  1310. $sd['sellprice'] = $sellprice;
  1311. $sd['ploss'] = $num;
  1312. $orderobj->where(array('id' => $id))->save($sd);
  1313. }
  1314. }
  1315. //买跌
  1316. } elseif ($otype == 2) {
  1317. if (in_array($uid, $winarr)) {//如果有指定盈利ID,则按盈利结算
  1318. if ($newprice > $buyprice) {
  1319. $sellprice = $buyprice - $randnum;
  1320. } elseif ($newprice == $buyprice) {
  1321. $sellprice = $buyprice - $randnum;
  1322. } elseif ($newprice < $buyprice) {
  1323. $sellprice = $newprice;
  1324. }
  1325. //增加资产
  1326. //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
  1327. M("user")->where(array('id' => $uid))->setInc("money", $money);
  1328. //修改订单状态
  1329. $sd['status'] = 2;
  1330. $sd['is_win'] = 1;
  1331. $sd['sellprice'] = $sellprice;
  1332. $sd['ploss'] = $ylnum;
  1333. $orderobj->where(array('id' => $id))->save($sd);
  1334. //写财务日志
  1335. //$this->addlog($uid,$vo['username'],$money);
  1336. } elseif (in_array($uid, $lossarr)) {//如果有指定亏损ID,则按亏损结算
  1337. if ($newprice > $buyprice) {
  1338. $sellprice = $newprice;
  1339. } elseif ($newprice == $buyprice) {
  1340. $sellprice = $buyprice + $randnum;
  1341. } elseif ($newprice < $buyprice) {
  1342. $sellprice = $buyprice + $randnum;
  1343. }
  1344. //修改订单状态
  1345. $sd['status'] = 2;
  1346. $sd['is_win'] = 2;
  1347. $sd['sellprice'] = $sellprice;
  1348. $sd['ploss'] = $num;
  1349. $orderobj->where(array('id' => $id))->save($sd);
  1350. } else {//如果未指定盈利和亏损,则按单控的计算
  1351. if ($dkong == 1) {//盈利
  1352. if ($newprice > $buyprice) {
  1353. $sellprice = $buyprice - $randnum;
  1354. } elseif ($newprice == $buyprice) {
  1355. $sellprice = $buyprice - $randnum;
  1356. } elseif ($newprice < $buyprice) {
  1357. $sellprice = $newprice;
  1358. }
  1359. //增加资产
  1360. //M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$money);
  1361. M("user")->where(array('id' => $uid))->setInc("money", $money);
  1362. //修改订单状态
  1363. $sd['status'] = 2;
  1364. $sd['is_win'] = 1;
  1365. $sd['sellprice'] = $sellprice;
  1366. $sd['ploss'] = $ylnum;
  1367. $orderobj->where(array('id' => $id))->save($sd);
  1368. //写财务日志
  1369. //$this->addlog($uid,$vo['username'],$money);
  1370. } elseif ($dkong == 2) {//亏损
  1371. if ($newprice > $buyprice) {
  1372. $sellprice = $newprice;
  1373. } elseif ($newprice == $buyprice) {
  1374. $sellprice = $buyprice + $randnum;
  1375. } elseif ($newprice < $buyprice) {
  1376. $sellprice = $buyprice + $randnum;
  1377. }
  1378. //修改订单状态
  1379. $sd['status'] = 2;
  1380. $sd['is_win'] = 2;
  1381. $sd['sellprice'] = $sellprice;
  1382. $sd['ploss'] = $num;
  1383. $orderobj->where(array('id' => $id))->save($sd);
  1384. }
  1385. }
  1386. }
  1387. echo "==订单ID:" . $id . "出售成功==";
  1388. }
  1389. } else {
  1390. echo "没有订单可以结算!";
  1391. }
  1392. }
  1393. //自动结算合约订单
  1394. public function hycarryout()
  1395. {
  1396. $nowtime = time();
  1397. $map['status'] = 1;
  1398. $map['intselltime'] = array('elt', $nowtime);
  1399. $orderobj = M("hyorder");
  1400. $list = $orderobj->where($map)->select();
  1401. if (!empty($list)) {
  1402. foreach ($list as $key => $vo) {
  1403. $coinname = $vo['coinname'];
  1404. $coinarr = explode("/", $coinname);
  1405. $symbol = strtolower($coinarr[0]) . strtolower($coinarr[1]);
  1406. $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $symbol;
  1407. $newprice = $this->getnewprice($coinapi);
  1408. $randnum = "0." . rand(1000, 9999);
  1409. $buyprice = $vo['buyprice'];
  1410. $otype = $vo['hyzd']; //合约方向
  1411. $dkong = $vo['kongyk']; //单控设置
  1412. $uid = $vo['uid'];//会员ID
  1413. $id = $vo['id'];//记录ID
  1414. $num = $vo['num'];
  1415. $hybl = $vo['hybl']; //收益比例
  1416. $ylnum = $num * ($hybl / 100); //盈利金额
  1417. $money = $num + $ylnum;//本金+盈利金额
  1418. //$dkong分三种情况 1、0表示随行情,1表示盈利 2 表示亏损
  1419. //盈利时增加$money,,亏损时亏本金
  1420. if ($dkong == 0) {
  1421. //买涨
  1422. if ($otype == 1) { //买跌
  1423. if ($newprice > $buyprice) { //盈利
  1424. //增加资产
  1425. M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
  1426. //修改订单状态
  1427. $sd['status'] = 2;
  1428. $sd['is_win'] = 1;
  1429. $sd['sellprice'] = $newprice;
  1430. $sd['ploss'] = $ylnum;
  1431. $orderobj->where(array('id' => $id))->save($sd);
  1432. //写财务日志
  1433. $this->addlog($uid, $vo['username'], $money);
  1434. } else {//亏损
  1435. //修改订单状态
  1436. $sd['status'] = 2;
  1437. $sd['is_win'] = 2;
  1438. $sd['sellprice'] = $newprice;
  1439. $sd['ploss'] = $num;
  1440. $orderobj->where(array('id' => $id))->save($sd);
  1441. }
  1442. } elseif ($otype == 2) { //买跌
  1443. if ($newprice < $buyprice) { //盈利
  1444. //增加资产
  1445. M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
  1446. //修改订单状态
  1447. $sd['status'] = 2;
  1448. $sd['is_win'] = 1;
  1449. $sd['sellprice'] = $newprice;
  1450. $sd['ploss'] = $ylnum;
  1451. $orderobj->where(array('id' => $id))->save($sd);
  1452. //写财务日志
  1453. $this->addlog($uid, $vo['username'], $money);
  1454. } else {//亏损
  1455. //修改订单状态
  1456. $sd['status'] = 2;
  1457. $sd['is_win'] = 2;
  1458. $sd['sellprice'] = $newprice;
  1459. $sd['ploss'] = $num;
  1460. $orderobj->where(array('id' => $id))->save($sd);
  1461. }
  1462. }
  1463. } elseif ($dkong == 1) {//单控盈利
  1464. if ($otype == 1) {//买涨
  1465. if ($newprice > $buyprice) {
  1466. $sellprice = $newprice;
  1467. } elseif ($newprice == $buyprice) {
  1468. $sellprice = $newprice + $randnum;
  1469. } elseif ($newprice < $buyprice) {
  1470. $sellprice = $buyprice + $randnum;
  1471. }
  1472. //增加资产
  1473. M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
  1474. //修改订单状态
  1475. $sd['status'] = 2;
  1476. $sd['is_win'] = 1;
  1477. $sd['sellprice'] = $sellprice;
  1478. $sd['ploss'] = $ylnum;
  1479. $orderobj->where(array('id' => $id))->save($sd);
  1480. //写财务日志
  1481. $this->addlog($uid, $vo['username'], $money);
  1482. } elseif ($otype == 2) {//买跌
  1483. if ($newprice > $buyprice) {
  1484. $sellprice = $buyprice - $randnum;
  1485. } elseif ($newprice == $buyprice) {
  1486. $sellprice = $buyprice - $randnum;
  1487. } elseif ($newprice < $buyprice) {
  1488. $sellprice = $newprice;
  1489. }
  1490. //增加资产
  1491. M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
  1492. //修改订单状态
  1493. $sd['status'] = 2;
  1494. $sd['is_win'] = 1;
  1495. $sd['sellprice'] = $sellprice;
  1496. $sd['ploss'] = $ylnum;
  1497. $orderobj->where(array('id' => $id))->save($sd);
  1498. //写财务日志
  1499. $this->addlog($uid, $vo['username'], $money);
  1500. }
  1501. } elseif ($dkong == 2) {
  1502. if ($otype == 1) {//买涨
  1503. //买涨,指定亏损,结算价格要低于买入价格
  1504. if ($newprice > $buyprice) {
  1505. $sellprice = $buyprice - $randnum;
  1506. } elseif ($newprice == $buyprice) {
  1507. $sellprice = $buyprice - $randnum;
  1508. } elseif ($newprice < $buyprice) {
  1509. $sellprice = $newprice;
  1510. }
  1511. //修改订单状态
  1512. $sd['status'] = 2;
  1513. $sd['is_win'] = 2;
  1514. $sd['sellprice'] = $sellprice;
  1515. $sd['ploss'] = $num;
  1516. $orderobj->where(array('id' => $id))->save($sd);
  1517. } elseif ($otype == 2) {//买跌
  1518. if ($newprice > $buyprice) {
  1519. $sellprice = $newprice;
  1520. } elseif ($newprice == $buyprice) {
  1521. $sellprice = $buyprice + $randnum;
  1522. } elseif ($newprice < $buyprice) {
  1523. $sellprice = $buyprice + $randnum;
  1524. }
  1525. //修改订单状态
  1526. $sd['status'] = 2;
  1527. $sd['is_win'] = 2;
  1528. $sd['sellprice'] = $sellprice;
  1529. $sd['ploss'] = $num;
  1530. $orderobj->where(array('id' => $id))->save($sd);
  1531. }
  1532. }
  1533. echo "==订单ID:" . $id . "出售成功==";
  1534. }
  1535. } else {
  1536. echo "没有订单可以结算!";
  1537. }
  1538. }
  1539. //自动结算合约订单
  1540. public function hycarryout____old()
  1541. {
  1542. $nowtime = time();
  1543. $map['status'] = 1;
  1544. $map['intselltime'] = array('elt', $nowtime);
  1545. $orderobj = M("hyorder");
  1546. $list = $orderobj->where($map)->select();
  1547. //获取合约参数
  1548. $setting = M("hysetting")->where(array('id' => 1))->field("hy_ksid,hy_ylid,hy_fkgl")->find();
  1549. //指定盈利ID组
  1550. $winarr = explode(',', $setting['hy_ylid']);
  1551. //指定亏损ID组
  1552. $lossarr = explode(',', $setting['hy_ksid']);
  1553. //风控比例组
  1554. $fkarr = explode(',', $setting['hy_fkgl']);
  1555. if (!empty($list)) {
  1556. foreach ($list as $key => $vo) {
  1557. $coinname = $vo['coinname'];
  1558. $coinarr = explode("/", $coinname);
  1559. $symbol = strtolower($coinarr[0]) . strtolower($coinarr[1]);
  1560. $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $symbol;
  1561. $newprice = $this->getnewprice($coinapi);
  1562. $randnum = "0." . rand(1000, 9999);
  1563. $buyprice = $vo['buyprice'];
  1564. $otype = $vo['hyzd']; //合约方向
  1565. $dkong = $vo['kongyk']; //单控设置
  1566. $uid = $vo['uid'];//会员ID
  1567. $id = $vo['id'];//记录ID
  1568. $num = $vo['num'];
  1569. $hybl = $vo['hybl'];
  1570. $ylnum = $num * ($hybl / 100);
  1571. $money = $num + $ylnum;//盈利金额
  1572. //买涨
  1573. if ($otype == 1) {
  1574. if (in_array($uid, $winarr)) {//如果有指定盈利ID,则按盈利结算
  1575. if ($newprice > $buyprice) {
  1576. $sellprice = $newprice;
  1577. } elseif ($newprice == $buyprice) {
  1578. $sellprice = $newprice + $randnum;
  1579. } elseif ($newprice < $buyprice) {
  1580. $sellprice = $buyprice + $randnum;
  1581. }
  1582. //增加资产
  1583. M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
  1584. //修改订单状态
  1585. $sd['status'] = 2;
  1586. $sd['is_win'] = 1;
  1587. $sd['sellprice'] = $sellprice;
  1588. $sd['ploss'] = $ylnum;
  1589. $orderobj->where(array('id' => $id))->save($sd);
  1590. //写财务日志
  1591. $this->addlog($uid, $vo['username'], $money);
  1592. } elseif (in_array($uid, $lossarr)) {//如果有指定亏损ID,则按亏损结算
  1593. //买涨,指定亏损,结算价格要低于买入价格
  1594. if ($newprice > $buyprice) {
  1595. $sellprice = $buyprice - $randnum;
  1596. } elseif ($newprice == $buyprice) {
  1597. $sellprice = $buyprice - $randnum;
  1598. } elseif ($newprice < $buyprice) {
  1599. $sellprice = $newprice;
  1600. }
  1601. //修改订单状态
  1602. $sd['status'] = 2;
  1603. $sd['is_win'] = 2;
  1604. $sd['sellprice'] = $sellprice;
  1605. $sd['ploss'] = $num;
  1606. $orderobj->where(array('id' => $id))->save($sd);
  1607. } else {//如果未指定盈利和亏损,则按单控的计算
  1608. if ($dkong == 1) {//盈利
  1609. if ($newprice > $buyprice) {
  1610. $sellprice = $newprice;
  1611. } elseif ($newprice == $buyprice) {
  1612. $sellprice = $newprice + $randnum;
  1613. } elseif ($newprice < $buyprice) {
  1614. $sellprice = $buyprice + $randnum;
  1615. }
  1616. // echo '买入价格:'.$buyprice;
  1617. // echo "<br />";
  1618. // echo '结算价格:'.$sellprice;die;
  1619. //增加资产
  1620. M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
  1621. //修改订单状态
  1622. $sd['status'] = 2;
  1623. $sd['is_win'] = 1;
  1624. $sd['sellprice'] = $sellprice;
  1625. $sd['ploss'] = $ylnum;
  1626. $orderobj->where(array('id' => $id))->save($sd);
  1627. //写财务日志
  1628. $this->addlog($uid, $vo['username'], $money);
  1629. } elseif ($dkong == 2) {//亏损
  1630. if ($newprice > $buyprice) {
  1631. $sellprice = $buyprice - $randnum;
  1632. } elseif ($newprice == $buyprice) {
  1633. $sellprice = $buyprice - $randnum;
  1634. } elseif ($newprice < $buyprice) {
  1635. $sellprice = $newprice;
  1636. }
  1637. // echo '买入价格:'.$buyprice;
  1638. // echo "<br />";
  1639. // echo '结算价格:'.$sellprice;die;
  1640. //修改订单状态
  1641. $sd['status'] = 2;
  1642. $sd['is_win'] = 2;
  1643. $sd['sellprice'] = $sellprice;
  1644. $sd['ploss'] = $num;
  1645. $orderobj->where(array('id' => $id))->save($sd);
  1646. }
  1647. }
  1648. //买跌
  1649. } elseif ($otype == 2) {
  1650. if (in_array($uid, $winarr)) {//如果有指定盈利ID,则按盈利结算
  1651. if ($newprice > $buyprice) {
  1652. $sellprice = $buyprice - $randnum;
  1653. } elseif ($newprice == $buyprice) {
  1654. $sellprice = $buyprice - $randnum;
  1655. } elseif ($newprice < $buyprice) {
  1656. $sellprice = $newprice;
  1657. }
  1658. //增加资产
  1659. M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
  1660. //修改订单状态
  1661. $sd['status'] = 2;
  1662. $sd['is_win'] = 1;
  1663. $sd['sellprice'] = $sellprice;
  1664. $sd['ploss'] = $ylnum;
  1665. $orderobj->where(array('id' => $id))->save($sd);
  1666. //写财务日志
  1667. $this->addlog($uid, $vo['username'], $money);
  1668. } elseif (in_array($uid, $lossarr)) {//如果有指定亏损ID,则按亏损结算
  1669. if ($newprice > $buyprice) {
  1670. $sellprice = $newprice;
  1671. } elseif ($newprice == $buyprice) {
  1672. $sellprice = $buyprice + $randnum;
  1673. } elseif ($newprice < $buyprice) {
  1674. $sellprice = $buyprice + $randnum;
  1675. }
  1676. //修改订单状态
  1677. $sd['status'] = 2;
  1678. $sd['is_win'] = 2;
  1679. $sd['sellprice'] = $sellprice;
  1680. $sd['ploss'] = $num;
  1681. $orderobj->where(array('id' => $id))->save($sd);
  1682. } else {//如果未指定盈利和亏损,则按单控的计算
  1683. if ($dkong == 1) {//盈利
  1684. if ($newprice > $buyprice) {
  1685. $sellprice = $buyprice - $randnum;
  1686. } elseif ($newprice == $buyprice) {
  1687. $sellprice = $buyprice - $randnum;
  1688. } elseif ($newprice < $buyprice) {
  1689. $sellprice = $newprice;
  1690. }
  1691. //增加资产
  1692. M("user_coin")->where(array('userid' => $uid))->setInc("usdt", $money);
  1693. //修改订单状态
  1694. $sd['status'] = 2;
  1695. $sd['is_win'] = 1;
  1696. $sd['sellprice'] = $sellprice;
  1697. $sd['ploss'] = $ylnum;
  1698. $orderobj->where(array('id' => $id))->save($sd);
  1699. //写财务日志
  1700. $this->addlog($uid, $vo['username'], $money);
  1701. } elseif ($dkong == 2) {//亏损
  1702. if ($newprice > $buyprice) {
  1703. $sellprice = $newprice;
  1704. } elseif ($newprice == $buyprice) {
  1705. $sellprice = $buyprice + $randnum;
  1706. } elseif ($newprice < $buyprice) {
  1707. $sellprice = $buyprice + $randnum;
  1708. }
  1709. //修改订单状态
  1710. $sd['status'] = 2;
  1711. $sd['is_win'] = 2;
  1712. $sd['sellprice'] = $sellprice;
  1713. $sd['ploss'] = $num;
  1714. $orderobj->where(array('id' => $id))->save($sd);
  1715. }
  1716. }
  1717. }
  1718. echo "==订单ID:" . $id . "出售成功==";
  1719. }
  1720. } else {
  1721. echo "没有订单可以结算!";
  1722. }
  1723. }
  1724. //写财务日志
  1725. public function addlog($uid, $username, $money)
  1726. {
  1727. $minfo = M("user_coin")->where(array('userid' => $uid))->find();
  1728. $data['uid'] = $uid;
  1729. $data['username'] = $username;
  1730. $data['num'] = $money;
  1731. $data['coinname'] = "usdt";
  1732. $data['afternum'] = $minfo['usdt'] + $money;
  1733. $data['type'] = 4;
  1734. $data['addtime'] = date("Y-m-d H:i:s", time());
  1735. $data['st'] = 1;
  1736. $data['remark'] = L('合约出售');
  1737. M("bill")->add($data);
  1738. $notice['uid'] = $uid;
  1739. $notice['account'] = $username;
  1740. $notice['title'] = L('快速合约交易');
  1741. $notice['content'] = L('快速合约已平仓,请及时加仓');
  1742. $notice['addtime'] = date("Y-m-d H:i:s", time());
  1743. $notice['status'] = 1;
  1744. M("notice")->add($notice);
  1745. }
  1746. //获取行情数据
  1747. public function getnewprice($api)
  1748. {
  1749. $ch = curl_init();
  1750. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  1751. curl_setopt($ch, CURLOPT_URL, $api);
  1752. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  1753. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  1754. $result = json_decode(curl_exec($ch), true);
  1755. $price_arr = $result['data'][0];
  1756. $close = $price_arr['close'];//现价
  1757. return $close;
  1758. }
  1759. public function ups_and_downs()
  1760. {
  1761. $coin = M('coin')->where(['type' => 2, 'status' => 1])->select();
  1762. $delete = M('trade_log')->where('addtime < '.(time()-600))->delete();
  1763. foreach ($coin as $item) {
  1764. $name = $item['name'] . '_' . 'usdt';
  1765. $market = M('market')->where(['name' => $name])->find();
  1766. $trade_list = M('trade_json')->where(['type' => 1])->order('id DESC')->find();// k线15分钟
  1767. $trade_list1 = M('trade_json')->where(['type' => 15])->order('id DESC')->find();// k线15分钟
  1768. $trade_list2 = M('trade_json')->where(['type' => 30])->order('id DESC')->find();// k线30分钟
  1769. $trade_list3 = M('trade_json')->where(['type' => 60])->order('id DESC')->find();// k线1小时
  1770. $trade_list4 = M('trade_json')->where(['type' => 240])->order('id DESC')->find();// k线4小时
  1771. $trade_list5 = M('trade_json')->where(['type' => 1440])->order('id DESC')->find();// k线1天
  1772. if ($market['zhang'] and $market['die']) {
  1773. //同时设置涨跌幅
  1774. $zd = rand(1, 2); //1涨幅2下跌
  1775. if ($zd == 1) {
  1776. $zf = $this->randomFloat(0, $market['zhang']); // 涨幅价格
  1777. $price = $market['new_price'] + ($market['new_price'] * ($zf / 100));//最新涨幅价格
  1778. if ($market['max_price'] < $price) {
  1779. $market['max_price'] = $price;
  1780. M('market')->where(['id' => $market['id']])->save(['max_price' => $price]);
  1781. }
  1782. } else {
  1783. $xd = $this->randomFloat(0, $market['die']); // 下跌价格
  1784. $price = $market['new_price'] - ($market['new_price'] * ($xd / 100));//最新下跌价格
  1785. if ($market['min_price'] > $price) {
  1786. $market['min_price'] = $price;
  1787. M('market')->where(['id' => $market['id']])->save(['min_price' => $price]);
  1788. }
  1789. }
  1790. M('market')->where(['id' => $market['id']])->save(['new_price' => $price]);
  1791. if ($market['hou_time'] < strtotime('today')) {
  1792. M('market')->where(['id' => $market['id']])->save(['hou_price' => $price, 'hou_time' => strtotime('today')]);
  1793. }
  1794. }else{
  1795. if ($market['zhang'] > 0 and $market['die'] == 0){
  1796. $zf = $this->randomFloat(0, $market['zhang']); // 涨幅价格
  1797. $price = $market['new_price'] + ($market['new_price'] * ($zf / 100));//最新涨幅价格
  1798. if ($market['max_price'] < $price) {
  1799. $market['max_price'] = $price;
  1800. M('market')->where(['id' => $market['id']])->save(['max_price' => $price]);
  1801. }
  1802. M('market')->where(['id' => $market['id']])->save(['new_price' => $price]);
  1803. if ($market['hou_time'] < strtotime('today')) {
  1804. M('market')->where(['id' => $market['id']])->save(['hou_price' => $price, 'hou_time' => strtotime('today')]);
  1805. }
  1806. }
  1807. if ($market['zhang'] == 0 and $market['die'] > 0){
  1808. $xd = $this->randomFloat(0, $market['die']); // 下跌价格
  1809. $price = $market['new_price'] - ($market['new_price'] * ($xd / 100));//最新下跌价格
  1810. if ($market['min_price'] > $price) {
  1811. $market['min_price'] = $price;
  1812. M('market')->where(['id' => $market['id']])->save(['min_price' => $price]);
  1813. }
  1814. M('market')->where(['id' => $market['id']])->save(['new_price' => $price]);
  1815. if ($market['hou_time'] < strtotime('today')) {
  1816. M('market')->where(['id' => $market['id']])->save(['hou_price' => $price, 'hou_time' => strtotime('today')]);
  1817. }
  1818. }
  1819. }
  1820. if ($price){
  1821. $price = round($price, 6);
  1822. $sd = $this->randomFloat($market['sdlow_num'], $market['sdhigh_num']);
  1823. for ($i = 0; $i <= $sd; $i ++){
  1824. M('trade_log')->add([
  1825. 'market' => $name,
  1826. 'price' => $price,
  1827. 'num' => $this->randomFloat(0, 1),
  1828. 'mum' => $this->randomFloat(0, 1),
  1829. 'addtime' => time() - rand(1, 10),
  1830. 'type' => rand(1, 2)
  1831. ]);
  1832. }
  1833. if ($price > $market['max_price']){
  1834. M('market')->where(['id' => $market['id']])->save(['max_price' => $price]);
  1835. $market['max_price'] = $price;
  1836. }
  1837. if ($price < $market['min_price']){
  1838. M('market')->where(['id' => $market['id']])->save(['min_price' => $price]);
  1839. $market['min_price'] = $price;
  1840. }
  1841. if ($trade_list) {
  1842. $data = json_decode($trade_list['data']);
  1843. $time = $trade_list['addtime'] + ($trade_list['type'] * 60);
  1844. if ($time <= time()) {
  1845. M('market')->where(['id' => $market['id']])->save(['max_price' => $price]);
  1846. M('market')->where(['id' => $market['id']])->save(['min_price' => $price]);
  1847. $json = [
  1848. 'market' => $name,
  1849. 'data' => json_encode([
  1850. time() * 1000,
  1851. $data[4],
  1852. $market['max_price'],
  1853. $market['min_price'],
  1854. $price,
  1855. $this->randomFloat(0, $market['sdhigh_num'])
  1856. ]),
  1857. 'type' => 1,
  1858. 'addtime' => time()
  1859. ];
  1860. M('trade_json')->add($json);
  1861. }
  1862. } else {
  1863. $json = [
  1864. 'market' => $name,
  1865. 'data' => json_encode([
  1866. time() * 1000,
  1867. $market['new_price'],
  1868. $market['max_price'],
  1869. $market['min_price'],
  1870. $price,
  1871. $this->randomFloat(0, $market['sdhigh_num'])
  1872. ]),
  1873. 'type' => 1,
  1874. 'addtime' => time()
  1875. ];
  1876. M('trade_json')->add($json);
  1877. }
  1878. if ($trade_list1) {
  1879. $data = json_decode($trade_list1['data']);
  1880. $time = $trade_list1['addtime'] + ($trade_list1['type'] * 60);
  1881. if ($time <= time()) {
  1882. $json = [
  1883. 'market' => $name,
  1884. 'data' => json_encode([
  1885. time() * 1000,
  1886. $data[4],
  1887. $market['max_price'],
  1888. $market['min_price'],
  1889. $price,
  1890. $this->randomFloat(0, $market['sdhigh_num'])
  1891. ]),
  1892. 'type' => 15,
  1893. 'addtime' => time()
  1894. ];
  1895. M('trade_json')->add($json);
  1896. }
  1897. } else {
  1898. $json = [
  1899. 'market' => $name,
  1900. 'data' => json_encode([
  1901. time() * 1000,
  1902. $market['new_price'],
  1903. $market['max_price'],
  1904. $market['min_price'],
  1905. $price,
  1906. $this->randomFloat(0, $market['sdhigh_num'])
  1907. ]),
  1908. 'type' => 15,
  1909. 'addtime' => time()
  1910. ];
  1911. M('trade_json')->add($json);
  1912. }
  1913. if ($trade_list2) {
  1914. $data = json_decode($trade_list2['data']);
  1915. $time = $trade_list2['addtime'] + ($trade_list2['type'] * 60);
  1916. if ($time <= time()) {
  1917. $json = [
  1918. 'market' => $name,
  1919. 'data' => json_encode([
  1920. time() * 1000,
  1921. $data[4],
  1922. $market['max_price'],
  1923. $market['min_price'],
  1924. $price,
  1925. $this->randomFloat(0, $market['sdhigh_num'])
  1926. ]),
  1927. 'type' => 30,
  1928. 'addtime' => time()
  1929. ];
  1930. M('trade_json')->add($json);
  1931. }
  1932. } else {
  1933. $json = [
  1934. 'market' => $name,
  1935. 'data' => json_encode([
  1936. time() * 1000,
  1937. $market['new_price'],
  1938. $market['max_price'],
  1939. $market['min_price'],
  1940. $price,
  1941. $this->randomFloat(0, $market['sdhigh_num'])
  1942. ]),
  1943. 'type' => 30,
  1944. 'addtime' => time()
  1945. ];
  1946. M('trade_json')->add($json);
  1947. }
  1948. if ($trade_list3) {
  1949. $data = json_decode($trade_list3['data']);
  1950. $time = $trade_list3['addtime'] + ($trade_list3['type'] * 60);
  1951. if ($time <= time()) {
  1952. $json = [
  1953. 'market' => $name,
  1954. 'data' => json_encode([
  1955. time() * 1000,
  1956. $data[4],
  1957. $market['max_price'],
  1958. $market['min_price'],
  1959. $price,
  1960. $this->randomFloat(0, $market['sdhigh_num'])
  1961. ]),
  1962. 'type' => 60,
  1963. 'addtime' => time()
  1964. ];
  1965. M('trade_json')->add($json);
  1966. }
  1967. } else {
  1968. $json = [
  1969. 'market' => $name,
  1970. 'data' => json_encode([
  1971. time() * 1000,
  1972. $market['new_price'],
  1973. $market['max_price'],
  1974. $market['min_price'],
  1975. $price,
  1976. $this->randomFloat(0, $market['sdhigh_num'])
  1977. ]),
  1978. 'type' => 60,
  1979. 'addtime' => time()
  1980. ];
  1981. M('trade_json')->add($json);
  1982. }
  1983. if ($trade_list4) {
  1984. $data = json_decode($trade_list4['data']);
  1985. $time = $trade_list4['addtime'] + ($trade_list4['type'] * 60);
  1986. if ($time <= time()) {
  1987. $json = [
  1988. 'market' => $name,
  1989. 'data' => json_encode([
  1990. time() * 1000,
  1991. $data[4],
  1992. $market['max_price'],
  1993. $market['min_price'],
  1994. $price,
  1995. $this->randomFloat(0, $market['sdhigh_num'])
  1996. ]),
  1997. 'type' => 240,
  1998. 'addtime' => time()
  1999. ];
  2000. M('trade_json')->add($json);
  2001. }
  2002. } else {
  2003. $json = [
  2004. 'market' => $name,
  2005. 'data' => json_encode([
  2006. time() * 1000,
  2007. $market['new_price'],
  2008. $market['max_price'],
  2009. $market['min_price'],
  2010. $price,
  2011. $this->randomFloat(0, $market['sdhigh_num'])
  2012. ]),
  2013. 'type' => 240,
  2014. 'addtime' => time()
  2015. ];
  2016. M('trade_json')->add($json);
  2017. }
  2018. if ($trade_list5) {
  2019. $data = json_decode($trade_list5['data']);
  2020. $time = $trade_list5['addtime'] + ($trade_list5['type'] * 60);
  2021. if ($time <= time()) {
  2022. $json = [
  2023. 'market' => $name,
  2024. 'data' => json_encode([
  2025. time() * 1000,
  2026. $data[4],
  2027. $market['max_price'],
  2028. $market['min_price'],
  2029. $price,
  2030. $this->randomFloat(0, $market['sdhigh_num'])
  2031. ]),
  2032. 'type' => 1440,
  2033. 'addtime' => time()
  2034. ];
  2035. M('trade_json')->add($json);
  2036. }
  2037. } else {
  2038. $json = [
  2039. 'market' => $name,
  2040. 'data' => json_encode([
  2041. time() * 1000,
  2042. $market['new_price'],
  2043. $market['max_price'],
  2044. $market['min_price'],
  2045. $price,
  2046. $this->randomFloat(0, $market['sdhigh_num'])
  2047. ]),
  2048. 'type' => 1440,
  2049. 'addtime' => time()
  2050. ];
  2051. M('trade_json')->add($json);
  2052. }
  2053. }
  2054. }
  2055. echo '自动增长';
  2056. }
  2057. function randomFloat($min = 0, $max = 1)
  2058. {
  2059. $num = $min + mt_rand() / mt_getrandmax() * ($max - $min);
  2060. return sprintf("%.4f", $num); //控制小数后几位
  2061. }
  2062. }
  2063. ?>