Skip to content

Instantly share code, notes, and snippets.

@devbanana
Last active February 3, 2020 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devbanana/4f1ba6c1fea8e42a930a6d2c3aa71d79 to your computer and use it in GitHub Desktop.
Save devbanana/4f1ba6c1fea8e42a930a6d2c3aa71d79 to your computer and use it in GitHub Desktop.
Get HTML list of 20-day moving averages from IEX
<?php
require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client([
'base_uri' => 'https://cloud.iexapis.com/stable/',
]);
$response = $client->get('stock/AAPL/indicator/sma', [
'query' => [
'token' => $_ENV['IEXKEY'],
'range' => '3m',
'input1' => 20,
],
]);
$result = json_decode($response->getBody());
$fmt = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
echo "<ul>\n";
for ($i = 0; $i < count($result->chart); $i++) {
if ($result->indicator[0][$i] === null) {
continue;
}
$date = new DateTime($result->chart[$i]->date);
$close = $result->chart[$i]->close;
$sma = $result->indicator[0][$i];
echo "<li>" . $date->format('F j') .
": close=" . $fmt->formatCurrency($close, 'USD') .
"; SMA=" . $fmt->formatCurrency($sma, 'USD') . "</li>\n";
}
echo "</ul>\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment