-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_indicator.php
210 lines (189 loc) · 5.69 KB
/
generate_indicator.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* Created by cryptek.
* Date: 30/09/2020
* Time: 10:07
*/
$countArgv = count($argv);
if($countArgv < 2){
echo "usage:\nphp generate_indicator.php NNFX_News_offline.mq4\n\t--nnfx-events=1\n\t--high-impact=0\n";
exit(0);
}
$options = [];
$i = 2;
while($i < $countArgv){
if(substr($argv[$i], 0, 2) == '--' && ($pos = strpos($argv[$i], '=')) !== false){
$options[substr($argv[$i], 2, $pos - 2)] = substr($argv[$i], $pos + 1);
}
$i++;
}
$newsPath = "news";
if(!is_dir($newsPath)){
echo "error: folder {$newsPath} not found\n";
return;
}
$filterEvents = [
'USD' => [
'Non-Farm Employment',
'FOMC Statement',
'Fed Chairman',
'CPI m/m'
],
'EUR' => [
'ECB Press Conference',
'ECB President',
],
'GBP' => [
'MPC Official Bank',
'Prelim GDP'
],
'CAD' => [
'BOC Rate Statement',
'Unemployment Rate',
'Non-Farm Employment',
'Core Retail Sales',
'CPI m/m',
],
'AUD' => [
'Cash Rate',
'Unemployment Rate',
'Employment Change',
],
'CHF' => [
'SNB Monetary Policy Assessment',
'Libor Rate',
],
'JPY' => [
'Monetary Policy Statement',
],
'NZD' => [
'Unemployment Rate',
'Employment Change',
'GDP',
'GDT Price Index',
'Official Cash Rate',
],
];
if (!($handle = opendir($newsPath))) {
echo "error: opendir {$newsPath}\n";
return;
}
$files = [];
while (false !== ($file = readdir($handle))) {
if ($file === "." || $file === "..") {
continue;
}
if(preg_match('/(\d{4})_(\d{2})\.html/', $file, $m)){
$files[$file] = [$m[1], $m[2]];
}
}
closedir($handle);
if(empty($files)){
echo "error: {$newsPath} is empty\n";
return;
}
ksort($files);
$events = [];
foreach($files as $file => $fDate) {
list($year, $month) = $fDate;
$html = file_get_contents($newsPath . '/' . $file);
preg_match_all('/<tr class="calendar__row calendar_row[^>]*>(.*?)<\/tr>/s', $html, $m, PREG_SET_ORDER);
$len = count($m);
$dateDay = 1;
$eventDay = '';
for ($i = 0; $i < $len; $i++) {
$trHtml = $m[$i][0];
$dom = new \DOMDocument();
$dom->loadHTML($trHtml, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED);
$tr = $dom->firstChild;
$currency = '';
$event = '';
$impact = '';
$actual = '';
$forecast = '';
$previous = '';
foreach ($tr->childNodes as $node) {
if ($node->nodeName != 'td') {
continue;
}
$className = $node->getAttribute('class');
if (strpos($className, 'calendar__cell') === false) {
continue;
}
$text = trim($node->textContent);
if (strpos($className, 'calendar__date') !== false) {
if ($text && preg_match('/.*[^0-9]([0-9]+)/', $text, $m2)) {
$dateDay = $m2[1];
}
} elseif (strpos($className, 'calendar__time') !== false) {
if ($text && preg_match('/([0-9]{1,2}):([0-9]{1,2})\s*(am|pm)/', $text, $m2)) {
$eventHour = date("H:i", strtotime($text));
$eventDay = sprintf("%d.%02d.%02d %s", $year, $month, $dateDay, $eventHour);
}
} elseif (strpos($className, 'calendar__currency') !== false) {
$currency = $text;
} elseif (strpos($className, 'calendar__event') !== false) {
$event = $text;
} elseif (strpos($className, 'calendar__impact') !== false) {
if (strpos($className, 'calendar__impact--low') !== false) {
$impact = 'low';
} elseif (strpos($className, 'calendar__impact--medium') !== false) {
$impact = 'medium';
} elseif (strpos($className, 'calendar__impact--high') !== false) {
$impact = 'high';
}
} elseif (strpos($className, 'calendar__actual') !== false) {
$actual = $text;
} elseif (strpos($className, 'calendar__forecast') !== false) {
$forecast = $text;
} elseif (strpos($className, 'calendar__previous') !== false) {
$previous = $text;
}
}
if(!array_key_exists($currency, $filterEvents)){
continue;
}
if(!isset($options['nnfx-events']) || $options['nnfx-events']) {
$found = false;
foreach ($filterEvents[$currency] as $fevent) {
if (strpos($event, $fevent) !== false) {
$found = true;
break;
}
}
if (!$found) {
echo "salta evento nao nnfx\n";
continue;
}
}
if(!empty($options['high-impact'])) {
if ($impact != "high") {
echo "salta nao é high impact\n";
continue;
}
}
$events[$currency][] = [
$eventDay,
$currency,
$event,
$impact,
$actual,
$forecast,
$previous,
];
}
}
$mq4 = file_get_contents('ind_template.mq4');
foreach($events as $currency => $ce){
$mq4 .= "void load{$currency}(int limit, int buff){\n";
$mq4 .= " static __news_event source[] = {\n";
foreach($ce as $ev){
$mq4 .= " {D'$ev[0]', \"$ev[2]\", 1},\n";
}
$mq4 .= " };\n";
$mq4 .= " setNews(source, limit, buff);\n";
$mq4 .= "};\n\n";
}
if(file_put_contents($argv[1], $mq4)){
echo "generated file $argv[1]\n";
}