/** * Get data for candle chart. * * @param Request $request */ public function getChart(Request $request) { $zoom = (int) $request->input('zoom'); $interval = $request->input('pointInterval') / 1000; list($market, $base) = explode('/', $request->input('pair')); $base_wallet = Wallet::where('short', $base)->first(); $market_wallet = Wallet::where('short', $market)->first(); $data = []; $old_data = Cache::tags('charts')->remember($base . $market . $zoom . $interval, 1, function () use($base_wallet, $market_wallet, $zoom, $interval) { $old_data = []; OrdersChart::selectRaw(' date*1000 as date, volume, open, high, low, close ')->where(function (\Illuminate\Database\Eloquent\Builder $query) use($base_wallet, $market_wallet) { $query->where('wallet_id', $base_wallet->id)->where('want_wallet_id', $market_wallet->id); })->where(DB::raw('from_unixtime(date)'), '>=', DB::raw('DATE_SUB(NOW(), INTERVAL ' . $zoom . ' HOUR)'))->where('interval', $interval)->orderBy('date', 'asc')->chunk(1000, function ($orders) use(&$old_data) { foreach ($orders as $key => $order) { $old_data[] = [(int) $order->date, (double) $order->open, (double) $order->high, (double) $order->low, (double) $order->close, (int) $order->volume]; } }); return $old_data; }); $data = array_merge($data, $old_data); usort($data, function ($a, $b) { return $a[0] - $b[0]; }); return response()->json($old_data)->setCallback($request->input('callback')); }
/** * Execute the console command. * * @return mixed */ public function handle() { $intervals = [300, 900, 1800, 7200, 14400, 86400]; $base = Wallet::where('base', true)->get(); $all = Wallet::all(); foreach ($base as $base_wallet) { foreach ($all as $market_wallet) { if ($market_wallet->id == $base_wallet->id) { continue; } foreach ($intervals as $interval) { OrdersTransaction::selectRaw(' FLOOR(MIN(unix_timestamp(`created_at`))/' . $interval . ')*' . $interval . ' AS date, SUM(amount) AS volume, SUBSTRING_INDEX(MIN(CONCAT(unix_timestamp(`created_at`), \'_\', price)), \'_\', -1) AS `open`, MAX(price) AS high, MIN(price) AS low, SUBSTRING_INDEX(MAX(CONCAT(unix_timestamp(`created_at`), \'_\', price)), \'_\', -1) AS `close` ')->where(function (\Illuminate\Database\Eloquent\Builder $query) use($base_wallet, $market_wallet) { $query->where('wallet_id', $base_wallet->id)->where('want_wallet_id', $market_wallet->id); })->where('created_at', '>=', DB::raw('DATE_SUB(NOW(), INTERVAL 24 HOUR)'))->groupBy(DB::raw('FLOOR(unix_timestamp(`created_at`)/' . $interval . ')'))->orderBy('created_at', 'asc')->chunk(100000, function ($orders) use($base_wallet, $market_wallet, $interval) { foreach ($orders as $key => $order) { try { $cache = OrdersChart::firstOrNew(['wallet_id' => $base_wallet->id, 'want_wallet_id' => $market_wallet->id, 'date' => $order->date, 'interval' => $interval]); $cache->date = $order->date; $cache->open = $order->open; $cache->high = $order->high; $cache->low = $order->low; $cache->close = $order->close; $cache->volume = $order->volume; $cache->wallet_id = $base_wallet->id; $cache->want_wallet_id = $market_wallet->id; $cache->interval = $interval; $cache->save(); } catch (\Exception $e) { } } }); } } } }
/** * Get latest ticker data. */ public static function getTickers() { $pairs = self::generatePairs(); $tickers = []; foreach ($pairs as $name => $pair) { $base = $pair['base_id']; $market = $pair['id']; $tickers[$name] = ['high' => 0, 'low' => 0, 'avg' => 0, 'vol' => 0, 'vol_curr' => 0, 'buy' => 0, 'sell' => 0, 'updated' => time()]; $last_sell = Order::select('price')->where(function (\Illuminate\Database\Eloquent\Builder $query) use($base, $market) { $query->where('wallet_id', $base)->where('want_wallet_id', $market); })->where('type', 'sell')->orderBy('created_at', 'desc')->first(); if ($last_sell) { $tickers[$name]['sell'] = (double) $last_sell->price; } $last_buy = Order::select('price')->where(function (\Illuminate\Database\Eloquent\Builder $query) use($base, $market) { $query->where('wallet_id', $base)->where('want_wallet_id', $market); })->where('type', 'buy')->orderBy('created_at', 'desc')->first(); if ($last_buy) { $tickers[$name]['buy'] = (double) $last_buy->price; } $last_chart_prices = OrdersChart::where('wallet_id', $base)->where('want_wallet_id', $market)->where('interval', 86400)->orderBy('date', 'desc')->first(); if ($last_chart_prices) { $tickers[$name]['high'] = (double) $last_chart_prices->high; $tickers[$name]['low'] = (double) $last_chart_prices->low; } $last_chart_volume = OrdersChart::where('wallet_id', $base)->where('want_wallet_id', $market)->where('interval', 86400)->orderBy('date', 'desc')->first(); if ($last_chart_volume) { $tickers[$name]['vol_curr'] = (double) $last_chart_volume->volume; } $base_volume = OrdersTransaction::selectRaw(' SUM(amount*price) AS volume ')->where(function (\Illuminate\Database\Eloquent\Builder $query) use($base, $market) { $query->where('wallet_id', $base)->where('want_wallet_id', $market); })->where('type', 'sell')->where('created_at', '>=', DB::raw('DATE_SUB(NOW(), INTERVAL 24 HOUR)'))->first(); if ($base_volume) { $tickers[$name]['vol'] = (double) $base_volume->volume; } } return $tickers; }