-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.php
314 lines (242 loc) · 8.83 KB
/
index.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
/*
GAB
index / run strategy
*/
require_once 'system/conf.php';
require_once $conf->dirs->system . 'functions.php';
require_once $conf->dirs->system . 'class.gab.php';
$page = 'run';
$page_title = 'Run strategy';
require $conf->dirs->views . 'header.php';
# new instance
$gab = new \GAB\core($conf);
# get exchanges and pairs
$datasets = $gab->get_datasets();
# force UTC timezone (else curl errors)
date_default_timezone_set('UTC');
?>
<form method='post' action='<?php echo $conf->urls->system . 'post.php'; ?>' id='gab_selectForm'>
<!--
DATASET SELECTION
-->
<?php
# create temp arr
$new = [];
/* pre-process: fix multi-period exchange/pair */
foreach( $datasets as $key => $set )
{
# so this stupid exchange-pair has multiple ranges, need to add to new array
$ranges = $set['ranges'];
if( count($ranges) > 1 )
{
foreach( $ranges as $rk => $r )
{
$setCopy = $datasets[$key]; // copy original
unset($setCopy['ranges']); // rm original ranges
$setCopy['ranges'][] = $r;
$new[] = $setCopy;
}
} // if
// just copy
else {
$new[] = $datasets[$key];
}
}
/* pre-process: sort datasets */
foreach( $new as $key => $set )
{
# get dates 0 (can be multiple)
$from = $set['ranges'][0]['from'];
$to = $set['ranges'][0]['to'];
# get no.of days
$numDays = date_between( date('Y-m-d', $from), date('Y-m-d', $to));
$numDays = $numDays->days;
$new[$key] = $set; // copy-paste
$new[$key]['numDays'] = $numDays;
} // foreach
# set new as datasets and rm $new (restore)
$datasets = $new;
$new = null;
# sort desc with usort
usort($datasets, function($a, $b) { return $b['numDays'] - $a['numDays']; });
/* loop - output table */
$str = '<table class="colored striped"><thead>';
$numDatasets = count($datasets);
foreach( $datasets as $key => $set )
{
# keys to vars [ exchange, currency, asset, (array) ranges ]
foreach( $set as $k => $v ){ $$k = $v; }
# create table headers
if( $key === 0 )
{
$str .= "<tr>";
foreach( $set as $k => $v ){
if( $k === 'numDays' ) continue;
$str .= "<th>" . $k . "</th>";
}
$str .= "<th>duration</th>";
$str .= "</tr></thead><tbody>";
}
# TEMP / MUSTFIX: brutefoce using first range
$ranges = $ranges[0];
$from = $ranges['from'];
$to = $ranges['to'];
$dateFullFrom = date('Y-m-d h:m', $from);
$dateFullTo = date('Y-m-d h:m', $to);
$dateFrom = substr($dateFullFrom, 0, -6);
$dateTo = substr($dateFullTo, 0, -6);
$diff = date_between( $dateFrom, $dateTo);
$niceRange = '';
# get diff (ugly)
$y = $diff->y;
$m = $diff->m;
$w = floor($diff->d/7);
if( $y ) $niceRange .= $y == 1 ? $y . ' year, ' : $y . ' years, ';
if( $m ) $niceRange .= $m == 1 ? $m . ' month, ' : $m . ' months, ';
if( !$y && $w ){
$niceRange .= $w == 1 ? $w . ' week, ' : $w . ' weeks, ';
}
$niceRange = rtrim($niceRange, ', ');
$exchange = ucfirst($exchange); // please..
// last always checked (since it has least data = failure to select won't take forever)
$key === $numDatasets-1 ? $selected = 'checked' : $selected = '';
// set dates (min-max)
$set['from'] = $dateFrom;
$set['to'] = $dateTo;
# get flay array
unset($set['numDays']);
$set_flat = json_encode(array_flatten($set));
# output rows
$str .= "<tr class='$selected'>";
$str .= '<td>';
$str .= "<input type='radio' name='dataset' data-from='$dateFullFrom' data-to='$dateFullTo' value='$set_flat' $selected> $exchange";
$str .= '</td>';
$str .= "<td>$asset</td>";
$str .= "<td>$currency</td>";
$str .= "<td>$dateFrom – $dateTo</td>";
$str .= "<td>$niceRange</td>";
$str .= "</tr>";
}
$str .= "</tbody></table>";
?>
<section>
<a href="#" class="button button-outline small right tip" data-tip="New data? Clear the cache and reload the page." onclick="ajax.get('./system/clear_cache.php?clearcache=yes','Cache was cleared.<br>Reload the page to get new datasets etc');return false;">Clear cache</a>
</section>
<?php
# date fields
$date_fields = [
'from' => $dateFrom,
'to' => $dateTo,
];
$dates = '';
foreach( $date_fields as $key => $val )
{
$min = $date_fields['from'];
$max = $date_fields['to'];
$niceKey = ucfirst($key);
$pattern = "(?:19|20)[0-9]{2}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9])|(?:(?!02)(?:0[1-9]|1[0-2])-(?:30))|(?:(?:0[13578]|1[02])-31))";
$dates .= "
<column>
<label for='$key'>$niceKey <small>Y-m-d eg: 2017-12-30</small></label>
<div class='tip error' data-tip='Error! Match the format e.g. 2017-12-30 (year-month-date)'>
<input name='$key' class='tip $key' data-tip='Some stuff here' type='text' value='$val' placeholder='ex: 2017-12-30' maxlength='10' minlength='10' pattern='$pattern'>
</div>
</column>
";
// add arrow
if( $key == 'from' ) $dates .= '<column class="arrow">→</column>';
}
?>
<section id="datasets">
<h3>Datasets</h3>
<input type="text" id="filter" placeholder="Type to filter datasets" autofocus>
<?php echo $str; ?>
<a href="#" id="recent_daterange" class='button small button-outline right tip' data-tip="Load recently used custom date range">Load recently used</a>
<h4>Date range</h4>
<row id="dates">
<?php echo $dates; ?>
</row>
</section>
<!--
STRATEGY SELECTION
-->
<?php
# get available
$strategies = $gab->get_strategies( $parse = false );
unset($strategies['notoml']); // no toml = cannot use it
# get all strategy names
$strategy_names = array_keys($strategies);
?>
<section>
<hr>
<a href="#" class="button button-outline small right tip" data-tip="New strategies? Clear the cache and reload the page." onclick="ajax.get('./system/clear_cache.php?clearcache=yes','Cache was cleared.<br>Reload the page to get new datasets etc');return false;">Clear cache</a>
</section>
<section id="strategies">
<h3>Strategies</h3>
<select name="strat" id='strat'>
<?php
foreach( $strategy_names as $k => $v ) {
echo "<option>" . $v . '</option>';
}
?>
</select>
<textarea name="toml" id="toml" class="monospace"><?= array_values($strategies)[0]; ?></textarea>
<?php
/* generate list of ids with strats (for fast selections later via JS) */
/* change this to include prev params */
foreach( $strategies as $name => $params ) {
if( contains('.', $name ) ) $name = str_replace('.','_', $name);
echo "<textarea name='_{$name}' class='$name hidden'>$params</textarea>";
echo "<pre class='{$name}_default hidden'>$params</pre>";
}
?>
<a href="#" id="strat_restore" class="button button-outline small mt tip" data-tip="Restore above strategy to its original settings. Helpful if the TOML-file has changed or if you just want to clear.">Restore</a>
</section>
<section>
<hr>
<h3>Other settings</h3>
<row>
<!-- CANDLE SIZE -->
<column>
<label for="candle_size">Candle size <small>in minutes, supports dynamic values e.g. 5:15,5</small></label>
<input name="candle_size" id="candle_size" value="30" placeholder="Minutes or Range eg 5:15,5">
</column>
<!-- HISTORY SIZE -->
<column>
<label for="history_size">History size <small>Size in no. of candles</small></label>
<input name="history_size" id="history_size" value="10" placeholder="History size in number of candles">
</column>
</row>
<row>
<column>
<!-- TIMEOUT -->
<label for="ajax_timeout">AJAX timeout <small>In minutes</small></label>
<input type="number" name="ajax_timeout" id="ajax_timeout" value="15" placeholder="Timeout a run after X minutes, this due to the fact that some threads can hang.">
</column>
<!-- THREADS -->
<column>
<label for="threads">Threads <small>Number of threads</small></label>
<input type="number" min="1" max="30" maxlength="2" name="threads" id="threads" value="3" placeholder="Note: Too many threads will not always speed things up.">
</column>
</row>
</section>
<section>
<!-- SUBMIT -->
<input type="submit" value="RUN IT!" id="submit" class="button-large">
<!-- DEBUG -->
<button id="debug" class="button-large button-outline tip" data-debug="false" data-tip="Do a fake run where you see all the dynamic params change 500 times">DEBUG</button>
</section>
</form>
<!-- echo entire config as json object (for later use by js) -->
<div id="conf" class="hidden"><?= json_encode($conf) ?></div>
<!-- logger -->
<section id="logger">
<hr>
<h3>Log</h3> <button class="button-outline small right" id="log_clear">CLEAR</button>
<pre id="log_stat"><u>Status</u> <span id="log_status">Idle</span> <u>Completed</u> <span id="log_runs">0</span> <u>Duration</u> <span id="log_duration">0h 0m 0s</span></pre>
<pre id="logs" class="hidden"></pre>
</section>
<?php
require $conf->dirs->views . 'footer.php';
?>