$symbol = strtoupper($currency . 'usd'); //currency symbol used for making orders //database price field $price_field = 'bitfinex_' . $currency; //get moving averages $queryMA = $db->query('SELECT (AVG(' . $price_field . ')) AS ma_7 FROM ' . $context['pricesTable'] . ' WHERE count <= 7'); foreach ($queryMA as $row) { $ma_7 = $row['ma_7']; } $queryMA = $db->query('SELECT (AVG(' . $price_field . ')) AS ma_30 FROM ' . $context['pricesTable'] . ' WHERE count <= 30'); foreach ($queryMA as $row) { $ma_30 = $row['ma_30']; } $btcPrice = getBitfinexPrice('btc'); //current market BTC price $ltcPrice = getBitfinexPrice('ltc'); //current market LTC price if ($currency == 'btc') { $latestPrice = $btcPrice; } else { $latestPrice = $ltcPrice; } $acctFunds = $bitfinexAPI->get_balances(); $acctMargin = $bitfinexAPI->margin_infos(); //array_debug($acctFunds); //array_debug($acc); //funds in margin balance $acctBTC = $acctFunds[4]['amount']; $acctLTC = $acctFunds[5]['amount']; $acctUSD = $acctFunds[7]['amount']; $marginUSD = $acctMargin[0]['margin_limits'][0]['tradable_balance'];
function getBitfinexPrice($currency) { $urlTickerPrice = 'https://api.bitfinex.com/v1/pubticker/' . $currency . 'usd'; $urlJSON = file_get_contents($urlTickerPrice); $currentPrice = json_decode($urlJSON); return $currentPrice->last_price; } function getCEXPrice($currency) { $urlTickerPrice = 'https://cex.io/api/ticker/' . $currency . '/USD'; $urlJSON = retrieveJSON($urlTickerPrice); return $urlJSON['last']; } //Bitfinex prices $bitfinexPriceBTC = getBitfinexPrice('btc'); $bitfinexPriceLTC = getBitfinexPrice('ltc'); //CEX.IO prices $cexPriceBTC = getCEXPrice('BTC'); $cexPriceLTC = getCEXPrice('LTC'); //echo $cexPriceBTC.' '.$cexPriceLTC; // exit; //shift all prices down 1 count $upd = 'UPDATE ' . $context['pricesTable2h'] . ' as new JOIN (SELECT * FROM ' . $context['pricesTable2h'] . ' AS old) AS old ON old.count = new.count-1 SET new.btce_btc = old.btce_btc, new.btce_ltc = old.btce_ltc, new.bitfinex_btc = old.bitfinex_btc, new.bitfinex_ltc = old.bitfinex_ltc, new.cex_btc = old.cex_btc, new.cex_ltc = old.cex_ltc, new.time = old.time'; $db->exec($upd); echo 'time: ' . $time . " \n";