KdataController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. <?php
  2. namespace backend\controllers;
  3. use backend\forms\KdataForm;
  4. use components\Exception;
  5. use Yii;
  6. use yii\web\Controller;
  7. use yii\filters\VerbFilter;
  8. use yii\filters\AccessControl;
  9. use common\models\LoginForm;
  10. /**
  11. * Site controller
  12. */
  13. class KdataController extends Controller
  14. {
  15. // /**
  16. // * @inheritdoc
  17. // */
  18. // public function behaviors()
  19. // {
  20. // return [
  21. // 'access' => [
  22. // 'class' => AccessControl::className(),
  23. // 'rules' => [
  24. // [
  25. // 'actions' => ['login', 'error', 'index','testcreatequeue'],
  26. // 'allow' => true,
  27. // ],
  28. // [
  29. // 'actions' => ['logout'],
  30. // 'allow' => true,
  31. // 'roles' => ['@'],
  32. // ],
  33. // ],
  34. // ],
  35. // 'verbs' => [
  36. // 'class' => VerbFilter::className(),
  37. // 'actions' => [
  38. // 'logout' => ['post'],
  39. // ],
  40. // ],
  41. // ];
  42. // }
  43. /**
  44. * @inheritdoc
  45. */
  46. public function actions()
  47. {
  48. return [
  49. 'error' => [
  50. 'class' => 'yii\web\ErrorAction',
  51. ],
  52. ];
  53. }
  54. /**
  55. * Displays homepage.
  56. *
  57. * @return string
  58. */
  59. public function actionIndex()
  60. {
  61. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  62. header("Access-Control-Allow-Origin: *");
  63. header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
  64. header('Access-Control-Allow-Headers:x-requested-with,content-type');
  65. $data= "{\"id\":[0,1,2,3,4,5],\"time\":[1524787200,1524441600,1524182400,1524182400,1523491200,1522195200],\"color\":[\"red\",\"blue\",\"green\",\"red\",\"blue\",\"green\"],\"text\":[\"Today\",\"4 days back\",\"7 days back + Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\",\"7 days back once again\",\"15 days back\",\"30 days back\"],\"label\":[\"A\",\"B\",\"CORE\",\"D\",\"EURO\",\"F\"],\"labelFontColor\":[\"white\",\"white\",\"red\",\"#FFFFFF\",\"white\",\"#000\"],\"minSize\":[14,28,7,40,7,14]}";
  66. echo ($data);
  67. }
  68. /**
  69. * Displays homepage.
  70. *
  71. * @return string
  72. */
  73. public function actionConfig()
  74. {
  75. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  76. header("Access-Control-Allow-Origin: *");
  77. header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
  78. header('Access-Control-Allow-Headers:x-requested-with,content-type');
  79. $data= "{\"supports_search\":false,\"supports_group_request\":true,
  80. \"supports_marks\":false,\"supports_timescale_marks\":false,\"supports_time\":false,
  81. \"exchanges\":[{\"value\":\"\",\"name\":\"All Exchanges\",\"desc\":\"\"},{\"value\":\"NasdaqNM\",\"name\":\"NasdaqNM\",\"desc\":\"NasdaqNM\"},{\"value\":\"NYSE\",\"name\":\"NYSE\",\"desc\":\"NYSE\"},{\"value\":\"NCM\",\"name\":\"NCM\",\"desc\":\"NCM\"},{\"value\":\"NGM\",\"name\":\"NGM\",\"desc\":\"NGM\"}],\"symbols_types\":[{\"name\":\"All types\",\"value\":\"\"},{\"name\":\"Stock\",\"value\":\"stock\"},{\"name\":\"Index\",\"value\":\"index\"}],\"supported_resolutions\":[\"D\",\"2D\",\"3D\",\"W\",\"3W\",\"M\",\"6M\"]}";
  82. // $data="
  83. //{
  84. // \"supports_search\": false,
  85. // \"supports_group_request\": true,
  86. // \"supports_marks\": false,
  87. // \"supports_timescale_marks\": false,
  88. // \"supports_time\": false,
  89. // \"exchanges\": [
  90. // {
  91. // \"value\": \"中国第一股票\",
  92. // \"name\": \"All Exchanges\",
  93. // \"desc\": \"中国第一股票\"
  94. // }
  95. // ],
  96. // \"symbols_types\": [
  97. // {
  98. // \"name\": \"All types\",
  99. // \"value\": \"\"
  100. // }
  101. // ],
  102. // \"supported_resolutions\": [
  103. // \"D\",
  104. // \"2D\",
  105. // \"6M\"
  106. // ]
  107. //}";
  108. $data="
  109. {
  110. \"supports_search\": true,
  111. \"supports_group_request\": true,
  112. \"supports_marks\": false,
  113. \"supports_timescale_marks\": false,
  114. \"supports_time\": true,
  115. \"exchanges\": [
  116. {
  117. \"value\": \"BTCUSDT\",
  118. \"name\": \"BTCUSDT\",
  119. \"desc\": \"BTCUSDT\"
  120. } ,
  121. {
  122. \"value\": \"BTCUSDT1\",
  123. \"name\": \"BTCUSDT1\",
  124. \"desc\": \"BTCUSDT1\"
  125. }
  126. ],
  127. \"symbols_types\": [
  128. {
  129. \"name\": \"stock\",
  130. \"value\": \"BTCUSDT\"
  131. } ,
  132. {
  133. \"name\": \"All types\",
  134. \"value\": \"\"
  135. }
  136. ],
  137. \"has_no_volume\": true,
  138. \"supported_resolutions\":[
  139. \"1\",
  140. \"3\",
  141. \"5\",
  142. \"15\",
  143. \"30\",
  144. \"60\",
  145. \"120\",
  146. \"240\",
  147. \"360\",
  148. \"720\",
  149. \"D\",
  150. \"3D\",
  151. \"5D\",
  152. \"7D\",
  153. \"15D\",
  154. \"M\"
  155. ]
  156. }";
  157. echo ($data);
  158. }
  159. /**
  160. * Displays homepage.
  161. *
  162. * @return string
  163. */
  164. public function actionMarks()
  165. {
  166. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  167. header("Access-Control-Allow-Origin: *");
  168. header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
  169. header('Access-Control-Allow-Headers:x-requested-with,content-type');
  170. $data= "{\"id\":[0,1,2,3,4,5],\"time\":[1524787200,1524441600,1524182400,1524182400,1523491200,1522195200],\"color\":[\"red\",\"blue\",\"green\",\"red\",\"blue\",\"green\"],\"text\":[\"Today\",\"4 days back\",\"7 days back + Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\",\"7 days back once again\",\"15 days back\",\"30 days back\"],\"label\":[\"A\",\"B\",\"CORE\",\"D\",\"EURO\",\"F\"],\"labelFontColor\":[\"white\",\"white\",\"red\",\"#FFFFFF\",\"white\",\"#000\"],\"minSize\":[14,28,7,40,7,14]}";
  171. echo ($data);
  172. }
  173. /**
  174. * Displays homepage.
  175. *
  176. * @return string
  177. */
  178. public function actionSearch()
  179. {
  180. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  181. header("Access-Control-Allow-Origin: *");
  182. header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
  183. header('Access-Control-Allow-Headers:x-requested-with,content-type');
  184. $data= "[
  185. {
  186. \"symbol\": \"BTCUSDT\",
  187. \"full_name\": \"BTCUSDT\",
  188. \"description\": \"BTCUSDT Inc.\",
  189. \"exchange\": \"BTCUSDT\",
  190. \"type\": \"stock\"
  191. },
  192. {
  193. \"symbol\": \"BTCUSDT1\",
  194. \"full_name\": \"BTCUSDT1\",
  195. \"description\": \"BTCUSDT1 Inc.\",
  196. \"exchange\": \"BTCUSDT1\",
  197. \"type\": \"stock\"
  198. }
  199. ]";
  200. echo ($data);
  201. }
  202. /**
  203. * Displays homepage.
  204. *
  205. * @return string
  206. */
  207. public function actionSymbol_info()
  208. {
  209. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  210. header("Access-Control-Allow-Origin: *");
  211. header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
  212. header('Access-Control-Allow-Headers:x-requested-with,content-type');
  213. $data= "{
  214. \"symbol\": [
  215. \"BTCUSDT\",
  216. \"BTCUSDT1\"
  217. ],
  218. \"description\": [
  219. \"BTCUSDT\" ,
  220. \"BTCUSDT1\"
  221. ],
  222. \"exchange-listed\": \"中国第一股\",
  223. \"exchange-traded\": \"快速交易\",
  224. \"minmov\": 1,
  225. \"minmov2\": 0,
  226. \"pricescale\": [
  227. 1
  228. ],
  229. \"has-intraday\": true,
  230. \"has_no_volume\": true,
  231. \"has_weekly_and_monthly\": true,
  232. \"has_seconds\": true,
  233. \"has_daily\": true,
  234. \"intraday_multipliers\": [
  235. 1
  236. ],
  237. \"volume_precision\": [
  238. 3
  239. ],
  240. \"supported_resolutions\": [
  241. \"1\",
  242. \"3\",
  243. \"5\",
  244. \"15\",
  245. \"30\",
  246. \"60\",
  247. \"120\",
  248. \"240\",
  249. \"360\",
  250. \"720\",
  251. \"D\",
  252. \"3D\",
  253. \"5D\",
  254. \"7D\",
  255. \"15D\",
  256. \"M\"
  257. ],
  258. \"type\": [
  259. \"stock\"
  260. ],
  261. \"ticker\": [
  262. \"BTCUSDT\",
  263. \"BTCUSDT1\"
  264. ],
  265. \"timezone\": \"America/New_York\",
  266. \"session-regular\": \"0900-1600\"
  267. }";
  268. echo ($data);
  269. }
  270. /**
  271. * Displays homepage.
  272. *
  273. * @return string
  274. */
  275. public function actionSymbols()
  276. {
  277. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  278. header("Access-Control-Allow-Origin: *");
  279. header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
  280. header('Access-Control-Allow-Headers:x-requested-with,content-type');
  281. $data= "{\"name\":\"AAPL\",\"exchange-traded\":\"NasdaqNM\",\"exchange-listed\":\"NasdaqNM\",\"timezone\":\"America/New_York\",\"minmov\":1,\"minmov2\":0,\"pointvalue\":1,\"session\":\"0930-1630\",\"has_intraday\":false,\"has_no_volume\":false,\"description\":\"Apple Inc.\",\"type\":\"stock\",\"supported_resolutions\":[\"D\",\"2D\",\"3D\",\"W\",\"3W\",\"M\",\"6M\"],\"pricescale\":100,\"ticker\":\"AAPL\"}";
  282. // $data= "{
  283. // \"symbol\": [
  284. // \"BTCUSDT\"
  285. // ],
  286. // \"description\": [
  287. // \"BTCUSDT\"
  288. // ],
  289. // \"exchange-listed\": \"中国第一股\",
  290. // \"exchange-traded\": \"快速交易\",
  291. // \"minmov\": 1,
  292. // \"minmov2\": 0,
  293. // \"pricescale\": [
  294. // 1
  295. // ],
  296. // \"has-intraday\": true,
  297. // \"has_no_volume\": [
  298. // true
  299. // ],
  300. // \"has_weekly_and_monthly\": true,
  301. //
  302. // \"has_seconds\": true,
  303. // \"has_daily\": true,
  304. //
  305. //
  306. // \"intraday_multipliers\": [
  307. // 1
  308. // ],
  309. // \"supported_resolutions\": [
  310. // \"60\",
  311. // \"2D\",
  312. // \"3D\",
  313. // \"W\",
  314. // \"3W\",
  315. // \"M\",
  316. // \"6M\"
  317. // ],
  318. //
  319. //
  320. //
  321. //
  322. //
  323. //
  324. //
  325. // \"type\": [
  326. // \"stock\"
  327. // ],
  328. // \"ticker\": [
  329. // \"BTCUSDT\"
  330. // ],
  331. // \"timezone\": \"UTC\",
  332. // \"session-regular\": \"0900-1600\"
  333. //}";
  334. $data="{
  335. \"name\": \"BTCUSDT\",
  336. \"exchange-traded\": \"BTCUSDT\",
  337. \"exchange-listed\": \"BTCUSDT\",
  338. \"timezone\": \"America/New_York\",
  339. \"minmov\": 1,
  340. \"minmov2\": 0,
  341. \"pointvalue\": 1,
  342. \"session\": \"0930-1630\",
  343. \"has_intraday\": true,
  344. \"has_no_volume\": true,
  345. \"has_seconds\": true,
  346. \"description\": \"BTCUSDT\",
  347. \"type\": \"stock\",
  348. \"supported_resolutions\": [
  349. \"1\",
  350. \"3\",
  351. \"5\",
  352. \"15\",
  353. \"30\",
  354. \"60\",
  355. \"120\",
  356. \"240\",
  357. \"360\",
  358. \"720\",
  359. \"D\",
  360. \"3D\",
  361. \"5D\",
  362. \"7D\",
  363. \"15D\",
  364. \"M\"
  365. ],
  366. \"pricescale\": 100,
  367. \"ticker\": \"BTCUSDT\"
  368. }";
  369. echo ($data);
  370. }
  371. /**
  372. * Displays homepage.
  373. *
  374. * @return string
  375. */
  376. public function actionTime()
  377. {
  378. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  379. header("Access-Control-Allow-Origin: *");
  380. header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
  381. header('Access-Control-Allow-Headers:x-requested-with,content-type');
  382. $data= "1524713662";
  383. echo ($data);
  384. }
  385. /**
  386. * Displays homepage.
  387. *
  388. * @return string
  389. */
  390. public function actionTimescale_marks()
  391. {
  392. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  393. header("Access-Control-Allow-Origin: *");
  394. header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
  395. header('Access-Control-Allow-Headers:x-requested-with,content-type');
  396. $data= "{\"name\":\"AAPL\",\"exchange-traded\":\"NasdaqNM\",\"exchange-listed\":\"NasdaqNM\",\"timezone\":\"America/New_York\",\"minmov\":1,\"minmov2\":0,\"pointvalue\":1,\"session\":\"0930-1630\",\"has_intraday\":false,\"has_no_volume\":false,\"description\":\"Apple Inc.\",\"type\":\"stock\",\"supported_resolutions\":[\"D\",\"2D\",\"3D\",\"W\",\"3W\",\"M\",\"6M\"],\"pricescale\":100,\"ticker\":\"AAPL\"}";
  397. $data= "{
  398. \"symbol\": [
  399. \"BTCUSDT\"
  400. ],
  401. \"description\": [
  402. \"BTCUSDT\"
  403. ],
  404. \"exchange-listed\": \"中国第一股\",
  405. \"exchange-traded\": \"快速交易\",
  406. \"minmov\": 1,
  407. \"minmov2\": 0,
  408. \"pricescale\": [
  409. 1
  410. ],
  411. \"has_intraday\": true,
  412. \"has_weekly_and_monthly\": true,
  413. \"type\": [
  414. \"stock\"
  415. ],
  416. \"ticker\": [
  417. \"BTCUSDT\"
  418. ],
  419. \"timezone\": \"UTC\",
  420. \"session-regular\": \"0900-1600\"
  421. }";
  422. echo ($data);
  423. }
  424. /**
  425. * Displays homepage.
  426. *
  427. * @return string
  428. */
  429. public function actionHistory()
  430. {
  431. header("Access-Control-Allow-Origin: *");
  432. header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
  433. header('Access-Control-Allow-Headers:x-requested-with,content-type');
  434. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  435. // $params = Yii::$app->getRequest()->params;
  436. $params=Yii::$app->request->get();//获取参数
  437. $rmap = array(
  438. '1'=>'m1',
  439. '3'=>'m3',
  440. '5'=>'m5',
  441. '15'=>'m15',
  442. '30'=>'m30',
  443. '60'=>'h1',
  444. '120'=>'h2',
  445. '240'=>'h4',
  446. '360'=>'h6',
  447. '720'=>'h12',
  448. 'D'=>'d1',
  449. '3D'=>'d3',
  450. '5D'=>'d5',
  451. '7D'=>'d7',
  452. '15D'=>'d15',
  453. 'M'=>'mo'
  454. );
  455. $level = $rmap[$params['resolution']];
  456. // $url='http://59.110.169.112:89/kdata/';
  457. //新增k线级别
  458. $params['period']=$level;
  459. // $data = file_get_contents($url."history?symbol={$params['symbol']}&period={$level}&from={$params['from']}&to={$params['to']}");
  460. // $data = file_get_contents($url."history?symbol=BTCUSDT&period=m1&from=1493796539&to=1493796540");
  461. // $a=rand(1,9);
  462. // if($a > 6){
  463. ////
  464. //// header("Access-Control-Allow-Origin: *");
  465. //// header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
  466. //// header('Access-Control-Allow-Headers:x-requested-with,content-type');
  467. //// $data= "{\"t\":[1492041600,1492387200,1492473600,1492560000,1492646400,1492732800,1492992000,1493078400],\"o\":[141.91,141.48,141.41,141.88,141.22,142.44,143.5,143.91],\"h\":[142.38,141.88,142.04,142,142.92,142.68,143.95,144.9],\"l\":[141.05,140.87,141.11,140.45,141.16,141.85,143.18,143.87],\"c\":[141.05,141.83,141.2,140.68,142.44,142.27,143.64,144.54],\"v\":[17822880,16582094,14697544,17328375,23319562,17320928,17116599,18216472],\"s\":\"ok\"}";
  468. ////
  469. // $url='http://59.110.169.112:89/kdata/';
  470. // $data = file_put_contents($url."history?symbol={$params['symbol']}&period={$params['resolution']}&from={$params['from']}&to={$params['to']}");
  471. //
  472. //
  473. //
  474. //
  475. //
  476. //
  477. //// }else{
  478. //
  479. //// $data="{\"s\":\"no_data\",\"nextTime\":1522108800}";
  480. //
  481. //// }
  482. ///
  483. ///
  484. ///
  485. ///
  486. ///
  487. ///
  488. $params = Yii::$app->request->queryParams;
  489. $symbolArray = array('BTCUSDT','BCCUSDT','BTYUSDT','ETHUSDT','ZECUSDT','LTCUSDT','DCRUSDT','BTSUSDT','BCCBTC','ETHBTC','ETCBTC','ZECBTC','LTCBTC');
  490. $periodArray = array('m1','m3','m5','m15','m30','h1','h2','h4','h6','h12','d1','d3','d5','d7','d15','mo');
  491. //参数为空的异常处理:
  492. if (!isset($params['symbol']))
  493. throw new Exception(1001,'交易对不能为空!');
  494. if (!isset($level))
  495. throw new Exception(1002,'k线级别不能为空!');
  496. if ((!isset($params['from']))&&(!isset($params['to'])))
  497. throw new Exception(1003,'起点终点时间戳 至少有一个要有值!');
  498. if((!isset($params['from'])||!isset($params['to']))&&!isset($params['count']))
  499. throw new Exception(1004,'起点终点时间戳只有一个有值时 count不能为空!');
  500. //参数类型异常处理:
  501. if (!is_string($params['symbol']) || !is_string($level))
  502. throw new Exception(2001,'交易对和k线级别参数类型只能是字符串!');
  503. if(isset($params['count'])&&!is_int($params['count']+0))
  504. throw new Exception(2002,'count必须是整数!');
  505. //参数值异常处理:
  506. // if(isset($params['symbol'])&&!in_array($params['symbol'],$symbolArray))
  507. // throw new Exception(3001,'无此交易对!');
  508. if(isset($level)&&!in_array($level,$periodArray))
  509. throw new Exception(3002,'不支持此级别k线!');
  510. if((isset($params['from'])&&isset($params['to']))&&$params['to']<$params['from'])
  511. throw new Exception(3003,'终点时间戳必须大于起点时间戳!');
  512. $symbol = $params['symbol'];
  513. $period = $level;
  514. $from = isset($params['from'])?$params['from']:-1;
  515. $to = isset($params['to'])?$params['to']:-1;
  516. $count = isset($params['count'])?$params['count']:-1;
  517. $model = new KdataForm();
  518. $data = $model->getData($symbol,$period,$from,$to,$count);
  519. $data = json_encode($data);
  520. echo ($data);
  521. }
  522. }