|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* User: alec |
|
4
|
|
|
* Date: 19.11.18 |
|
5
|
|
|
* Time: 17:27 |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace AlecRabbit\Counters; |
|
10
|
|
|
|
|
11
|
|
|
use AlecRabbit\Structures\Trade; |
|
12
|
|
|
use AlecRabbit\Structures\Volume; |
|
13
|
|
|
|
|
14
|
|
|
class VolumeCounterDeprecated extends TimedCounterDeprecated |
|
|
|
|
|
|
15
|
|
|
{ |
|
16
|
|
|
/** @var array */ |
|
17
|
|
|
protected $volumesTotal = []; |
|
18
|
|
|
/** @var array */ |
|
19
|
|
|
protected $volumesBuy = []; |
|
20
|
|
|
/** @var array */ |
|
21
|
|
|
protected $volumesSell = []; |
|
22
|
|
|
|
|
23
|
13 |
|
public function addTrade(Trade $trade): void |
|
24
|
|
|
{ |
|
25
|
13 |
|
$time = $this->getBaseTime($trade->timestamp); |
|
26
|
13 |
|
$this->volumesTotal[$time] = $this->volumesTotal[$time] ?? 0; |
|
27
|
13 |
|
$this->volumesTotal[$time] += $trade->amount; |
|
28
|
13 |
|
if ($trade->side === T_SELL) { |
|
29
|
13 |
|
$this->volumesSell[$time] = $this->volumesSell[$time] ?? 0; |
|
30
|
13 |
|
$this->volumesSell[$time] += $trade->amount; |
|
31
|
|
|
} else { |
|
32
|
13 |
|
$this->volumesBuy[$time] = $this->volumesBuy[$time] ?? 0; |
|
33
|
13 |
|
$this->volumesBuy[$time] += $trade->amount; |
|
34
|
|
|
} |
|
35
|
13 |
|
$this->trim(); |
|
36
|
13 |
|
} |
|
37
|
|
|
|
|
38
|
13 |
|
private function trim(): void |
|
39
|
|
|
{ |
|
40
|
13 |
|
$time = $this->relativeMode ? $this->lastTimestamp : time(); |
|
41
|
13 |
|
$threshold = $time - $this->length; |
|
42
|
13 |
|
if (null !== ($key = array_key_first($this->volumesTotal)) && ($key <= $threshold)) { |
|
43
|
9 |
|
unset($this->volumesTotal[$key]); |
|
44
|
|
|
} |
|
45
|
13 |
|
if (null !== ($key = array_key_first($this->volumesSell)) && ($key <= $threshold)) { |
|
46
|
9 |
|
unset($this->volumesSell[$key]); |
|
47
|
|
|
} |
|
48
|
13 |
|
if (null !== ($key = array_key_first($this->volumesBuy)) && ($key <= $threshold)) { |
|
49
|
9 |
|
unset($this->volumesBuy[$key]); |
|
50
|
|
|
} |
|
51
|
13 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param bool|null $reset |
|
55
|
|
|
* @return Volume |
|
56
|
|
|
*/ |
|
57
|
11 |
|
public function getCalculatedVolumes(?bool $reset = null): Volume |
|
58
|
|
|
{ |
|
59
|
11 |
|
$volume = new Volume(); |
|
60
|
11 |
|
if (0 < ($sum = array_sum($this->volumesTotal))) { |
|
61
|
|
|
$volume |
|
62
|
10 |
|
->setTotal($sum) |
|
63
|
10 |
|
->setSell(array_sum($this->volumesSell)) |
|
64
|
10 |
|
->setBuy(array_sum($this->volumesBuy)); |
|
65
|
|
|
} |
|
66
|
11 |
|
if ($reset) { |
|
67
|
10 |
|
$this->volumesTotal = []; |
|
68
|
10 |
|
$this->volumesSell = []; |
|
69
|
10 |
|
$this->volumesBuy = []; |
|
70
|
|
|
} |
|
71
|
11 |
|
return $volume; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** @return array */ |
|
75
|
3 |
|
public function getVolumes(): array |
|
76
|
|
|
{ |
|
77
|
|
|
return |
|
78
|
|
|
[ |
|
79
|
3 |
|
$this->volumesTotal, |
|
80
|
3 |
|
$this->volumesSell, |
|
81
|
3 |
|
$this->volumesBuy, |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|