// Check trade configuration if (!(isset($TRADE['trade_pair']) && isset($TRADE['trade_wait']) && isset($TRADE['trade_stop_loss']) && isset($TRADE['trade_sma_short']) && isset($TRADE['trade_sma_long']) && isset($TRADE['trade_threshold']))) { die("Not all configuration options are present"); } // Enter trade loop $simulation = isset($argv[2]) && $argv[2] === 'real' ? false : true; $tradeLog = './logs/' . $profile . '.log'; $bought = 0; $buy_price = 0; $trailing_stop_margin = 0; while (true) { if (!$mysqli) { $mysqli = mysqlConnect($CONFIG); } // Get ticker status for trading pair $ticker = getTicker($TRADE['trade_pair']); if ($ticker === false) { sleep($TRADE['trade_wait']); continue; } // Get balance for trading pair from db list($currency1, $currency2) = explode('_', $TRADE['trade_pair']); $balance = $mysqli->query("SELECT * FROM balance LIMIT 1")->fetch_assoc(); $accountValue = $balance[$currency1] * $ticker['sell'] + $balance[$currency2]; // Calculate average/threshold/etc $SmaShort = floatval($mysqli->query("SELECT AVG(x." . $TRADE['trade_pair'] . ") FROM (SELECT " . $TRADE['trade_pair'] . " FROM ticker ORDER BY id DESC LIMIT " . $TRADE['trade_sma_short'] . ") x")->fetch_array()[0]); $SmaLong = floatval($mysqli->query("SELECT AVG(x." . $TRADE['trade_pair'] . ") FROM (SELECT " . $TRADE['trade_pair'] . " FROM ticker ORDER BY id DESC LIMIT " . $TRADE['trade_sma_long'] . ") x")->fetch_array()[0]); $SmaDiff = 100 * ($SmaShort - $SmaLong) / (($SmaShort + $SmaLong) / 2); $last = floatval($mysqli->query("SELECT " . $TRADE['trade_pair'] . " FROM ticker ORDER BY id DESC LIMIT 1")->fetch_array()[0]); $tradeAmount = number_format($balance[$currency2] * $TRADE['trade_amount'], 6); $changePercentage = ($ticker['buy'] - $last) / $ticker['buy'] * 100;
$website = "http://finance.yahoo.com/q?s=" . $ticker; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $website); curl_setopt($ch, CURLOPT_HTTPGET, TRUE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $output = curl_exec($ch); curl_close($ch); return $output; /* returns a string of the cURL retrieved contents. */ } $tickerInput = $_GET["ticker"]; $tickers = explode(",", $tickerInput); /* set the ticker from $_POST. */ $tickers = ["AAPL"]; $priceInfo = array(); $DOM = array(); for ($x = 0; $x < count($tickers); $x++) { $html = getTicker($tickers[$x]); $DOM[$x] = new DOMDocument(); print_r($DOM[$x]->getElementsByTagName("span")); $DOM[$x]->loadHTML(getTicker($tickers[$x])); array_push($priceInfo, array("price" => $DOM[$x]->getElementById("yfs_l84_" . $tickers[$x])->nodeValue, "percentChange" => $DOM[$x]->getElementById("yfs_p43_" . $tickers[$x])->nodeValue)); } echo json_encode($priceInfo); /* This is the PHP script work in progress for getting the information directly from Yahoo! The current issue however is that the HTML appears to be invalid enough that PHP won't correctly parse it. I may have to just use regular expressions to read the HTML like the good ole days... */