-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathimport_polygon.php
71 lines (54 loc) · 2.06 KB
/
import_polygon.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/**
* Usage: php import_polygon.php [SYMBOL] [DATE]
* e.g. php import_polygon.php tvix 2018-09-25
*/
require('./vendor/autoload.php');
use Carbon\Carbon;
use Dotenv\Dotenv;
$dotenv = new Dotenv(__DIR__);
$dotenv->load();
if ($argc < 2) {
print "Usage: php import_polygon.php [SYMBOL] [DATE=YYYY-MM-DD]" . PHP_EOL;
exit();
}
$symbol = strtolower($argv[1]);
if ($argc > 2) {
$date = new Carbon($argv[2]);
} else {
$date = Carbon::now();
}
$db = new mysqli($_ENV['MYSQL_HOST'], $_ENV['MYSQL_USER'], $_ENV['MYSQL_PASS'], $_ENV['MYSQL_DB']);
$client = new GuzzleHttp\Client();
$request = $client->request(
'GET',
'https://api.polygon.io/v1/historic/agg/minute/' . strtoupper($symbol) . '?from=' . $date->format('Y-m-d') . '&to=' . $date->copy()->addDays(1)->format('Y-m-d') . '&apiKey=' . $_ENV['ALPACA_KEY'],
[
'headers' => [
'Accept' => 'application/json'
]
]
);
$data = $request->getBody();
file_put_contents("./data/alpaca/" . strtoupper($symbol) . "_{$date->format('Y-m-d')}.json", $data);
$file = json_decode($data, true);
$firstBar = $date->copy()->timezone('US/Eastern')->addDay(1)->hour(9)->minute(30)->second(0);
$lastBar = $date->copy()->timezone('US/Eastern')->addDay(1)->hour(15)->minute(59)->second(0);
foreach ($file['ticks'] as $bar) {
$start = Carbon::createFromTimestampMs($bar['d'], 'US/Eastern'); //->format('Y-m-d H:i:s');
if ($start->lessThan($firstBar) || $start->greaterThan($lastBar)) {
continue;
}
$start = $start->format('Y-m-d H:i:s');
$open = (double)$bar['o'];
$close = (double)$bar['c'];
$high = (double)$bar['h'];
$low = (double)$bar['l'];
$volume = (double)$bar['v'];
$db->query("INSERT IGNORE INTO alpaca_{$symbol} VALUES('{$start}', '{$open}', '{$high}', '{$low}', '{$close}', '{$volume}')");
if ($db->error) {
fwrite(STDOUT, "[!] MYSQL ERROR: {$db->error}" . PHP_EOL);
}
}
fwrite(STDOUT, "[+] Processed: ./data/alpaca/{$symbol}_{$date->format('Y-m-d')}.json" . PHP_EOL);
$db->close();