Created
July 13, 2019 18:57
-
-
Save omkoding/ede42a6410e53b3466115ff829352901 to your computer and use it in GitHub Desktop.
COT Report Parser with PHP
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 | |
namespace OmKoding\Cot; | |
use GuzzleHttp\Client; | |
class Report | |
{ | |
/** | |
* @var \GuzzleHttp\Client | |
*/ | |
protected $client; | |
/** | |
* Initiate class | |
*/ | |
public function __construct() | |
{ | |
$this->client = new Client([ | |
'http_errors' => false, | |
]); | |
} | |
/** | |
* Get latest COT Report | |
* | |
* @return array | |
*/ | |
public function latest() | |
{ | |
$url = 'https://www.cftc.gov/dea/futures/deacmesf.htm'; | |
$response = $this->client->get($url); | |
return $this->parse((string) $response->getBody()); | |
} | |
/** | |
* Get COT Report by date | |
* | |
* @param string Date of report in m/d/y format | |
* @return array | |
*/ | |
public function byDate($date) | |
{ | |
// ekstrak month, day, and year | |
list($month, $day, $year) = explode('/', $date); | |
$url = sprintf( | |
'https://www.cftc.gov/sites/default/files/files/dea/cotarchives/%s/futures/deacmesf%s%s%s.htm', | |
date('Y', strtotime($date)), // konversi menjadi tahun 4 digit | |
$month, | |
$day, | |
$year | |
); | |
$response = $this->client->get($url); | |
return $this->parse((string) $response->getBody()); | |
} | |
/** | |
* Parsing HTML response and get data | |
* | |
* @param string HTML Response from GuzzleHTTP | |
* @return array | |
*/ | |
protected function parse(string $html) | |
{ | |
$lines = explode(PHP_EOL, $html); | |
$result = []; | |
foreach ($lines as $index => $line) { | |
// cari baris yang mengandung nama item | |
if (stripos($line, ' - CHICAGO MERCANTILE EXCHANGE') !== false) { | |
// ambil nama item sebelum string berikut | |
$nameLine = explode(' - CHICAGO MERCANTILE EXCHANGE', $line); | |
$name = $nameLine[0]; | |
// ambil tanggal pada baris index + 1 dan pisahkan berdasarkan spasi | |
$dateLine = preg_split('/\s+/', trim($lines[$index + 1])); | |
$date = $date[5]; | |
// ambil Open Interest pada baris index + 7 dan pisahkan berdasarkan spasi | |
$openInterestLine = preg_split('/\s+/', trim($lines[$index + 7])); | |
// ambil index column terakhir | |
$openInterest = end($openInterestLine); | |
// ambil Open Interest Total Change pada baris index + 11 dan pisahkan | |
$openInterestChangeLine = preg_split('/\s+/', trim($lines[$index + 7])); | |
// ambil index column terakhir dan hilangkan akhiran ")" | |
$openInterestChange = rtrim(end($openInterestChangeLine), ')'); | |
// ambil posisi terkini pada baris index + 9 dan pisahkan berdasarkan spasi | |
$current = preg_split('/\s+/', trim($lines[$index + 9])); | |
// ambil perubahan dari minggu lalu pada baris index + 12 dan pisahkan berdasarkan spasi | |
$changes = preg_split('/\s+/', trim($lines[$index + 12])); | |
// ambil Open Interest Percent Changes pada baris index + 15 dan pisahkan berdasarkan spasi | |
$percent = preg_split('/\s+/', trim($lines[$index + 15])); | |
$result[$name] = [ | |
'name' => $name, | |
'date' => $date, | |
'current' => [ | |
'non-commercial' => [ | |
'long' => $current[0], | |
'short' => $current[1], | |
'spreads' => $current[2], | |
], | |
'commercial' => [ | |
'long' => $current[3], | |
'short' => $current[4], | |
], | |
], | |
'changes' => [ | |
'non-commercial' => [ | |
'long' => $changes[0], | |
'short' => $changes[1], | |
'spreads' => $changes[2], | |
], | |
'commercial' => [ | |
'long' => $changes[3], | |
'short' => $changes[4], | |
], | |
], | |
'open-interest' => [ | |
'current' => $openInterest, | |
'change' => $openInterestChange, | |
'non-commercial' => [ | |
'long' => $percent[0], | |
'short' => $percent[1], | |
'spreads' => $percent[2], | |
], | |
'commercial' => [ | |
'long' => $percent[3], | |
'short' => $percent[4], | |
], | |
], | |
]; | |
} | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment