-
-
Save devbanana/4f1ba6c1fea8e42a930a6d2c3aa71d79 to your computer and use it in GitHub Desktop.
Get HTML list of 20-day moving averages from IEX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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