/** * 采集所有基金历史数据(一次性) */ public function actionHistory($page = 1) { @set_time_limit(0); @ini_set('memory_limit', '1280M'); $size = 100; $fundNum = FundNum::find()->offset(($page - 1) * $size)->limit($size)->all(); $client = new Client(); foreach ($fundNum as $k => $fund) { $num = $fund->fund_num; $url = 'http://fund.eastmoney.com/f10/F10DataApi.aspx?type=lsjz&code=' . $num . '&page=1&per=20'; $crawler = $client->request('GET', $url); if ($crawler) { $content = $crawler->text(); $content = substr($content, strpos($content, 'pages:') + 6); $pagesize = substr($content, 0, strpos($content, ',')); $pagesize = min($pagesize, 10); for ($i = 1; $i <= $pagesize; $i++) { $url = 'http://fund.eastmoney.com/f10/F10DataApi.aspx?type=lsjz&code=' . $num . '&page=' . $i . '&per=20'; $model = FundUrlLog::find()->where(['md5_url' => md5($url)])->one(); if ($model) { continue; } $crawler = $client->request('GET', $url); $crawler->filter('tr')->each(function ($node) use($num) { $td = $node->filter('td'); try { if ($td) { $date = $td->eq(0); $rate = $td->eq(3); if ($date && $rate) { $date = $date->text(); $rate = $rate->text(); if (!FundHistory::find()->where(['fund_num' => $num, 'date' => $date])->one()) { echo $num . '------' . $date, '------' . time() . PHP_EOL; $history = new FundHistory(['fund_num' => $num, 'date' => $date, 'rate' => $rate]); $history->save(); } } } } catch (\Exception $e) { echo $e->getMessage() . time() . PHP_EOL; } }); $url_log = new FundUrlLog(['md5_url' => md5($url), 'url' => $url]); $url_log->save(); } } } }
/** * 计算自选基金加自选后累计涨幅 * @return number */ public function getTotalRate() { $history = FundHistory::find()->where(['fund_num' => $this->fund_num])->andWhere(['>=', 'date', date('Y-m-d', $this->created_at)])->all(); $rates = ArrayHelper::getColumn($history, 'rate'); return array_sum($rates); }
/** * 执行类型5:涨幅超过5%的天数是跌幅超过5%的天数的2倍 */ public static function saveType5() { $fund_nums = FundNum::find()->all(); foreach ($fund_nums as $v) { $biggerCount = FundHistory::biggerCount($v->fund_num, 5); $smallerCount = FundHistory::smallerCount($v->fund_num, -5); if ($biggerCount >= $smallerCount * 2) { $model = new self(['type' => self::TYPE_5, 'date' => date('Y-m-d'), 'fund_num' => $v->fund_num]); $model->save(); } } }