-
Notifications
You must be signed in to change notification settings - Fork 1
/
activityGenerator.php
134 lines (117 loc) · 4.03 KB
/
activityGenerator.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
<?php
use Kobens\Core\Config;
use Kobens\Core\EmergencyShutdown;
use Kobens\Core\Exception\ConnectionException;
use Kobens\Core\Http\Request\Throttler;
use Kobens\Gemini\Api\Host;
use Kobens\Gemini\Api\Key;
use Kobens\Gemini\Api\Nonce;
use Kobens\Gemini\Api\Rest\PrivateEndpoints\OrderPlacement\NewOrder\ImmediateOrCancel;
use Kobens\Gemini\Api\Rest\PrivateEndpoints\OrderPlacement\NewOrder\ImmediateOrCancelInterface;
use Kobens\Gemini\Exception\Api\Reason\InsufficientFundsException;
use Kobens\Gemini\Exception\Api\Reason\SystemException;
use Kobens\Gemini\Exchange\Currency\Pair;
require __DIR__.'/bootstrap.php';
$config = Config::getInstance();
$host = new Host($config->get('gemini')->api->host);
if ($host->getHost() !== 'api.sandbox.gemini.com') {
echo "\nActivity Generator only allowed on api.sandbox.gemini.com\n";
exit(1);
}
$adapter = \Kobens\Core\Db::getAdapter();
$shutdownDir = \array_key_exists(1, $_SERVER['argv']) && \is_dir($_SERVER['argv'][1])
? $_SERVER['argv'][1]
: __DIR__.DIRECTORY_SEPARATOR.'var'
;
$shutdown = new EmergencyShutdown($shutdownDir);
$immediateOrCancel = new ImmediateOrCancel(
$host,
new Throttler($host->getHost().'::private'),
new Key(
$config->get('gemini')->api->key->public_key,
$config->get('gemini')->api->key->secret_key
),
new Nonce()
);
function getRange(\Zend\Db\Adapter\Adapter $adapter): array
{
$range = [
'sell' => null,
'buy' => null,
'time' => \time(),
];
$prices = $adapter->query('select buy_price,sell_price from trade_repeater')->execute();
foreach ($prices as $price) {
if ($range['sell'] === null || (float) $range['sell'] > (float) $price['buy_price']) {
$range['sell'] = $price['buy_price'];
}
if ($range['buy'] === null || (float) $range['buy'] < (float) $price['sell_price']) {
$range['buy'] = $price['sell_price'];
}
}
return $range;
}
function getAmount(): string
{
$overOneBtc = \rand(0, 100) > 60;
$whole = $overOneBtc ? (string) \rand(1, 5) : '0';
$satoshi = (string) (\rand(1, 10) > 1 ? \rand(1000, 999999): \rand(1000, 49999999));
$satoshi = \str_pad($satoshi, 8, '0', \STR_PAD_LEFT);
return "$whole.$satoshi";
}
function printException(\Exception $e): void
{
do {
echo
"Code: {$e->getCode()}\n",
"Class: ".\get_class($e)."\n",
"Message: {$e->getMessage()}\n",
"Trace: {$e->getTraceAsString()}\n\n"
;
$e = $e->getPrevious();
if ($e instanceof \Exception) {
echo "Previous Exception:\n";
}
} while ($e instanceof \Exception);
}
function placeOrder(ImmediateOrCancelInterface $immediateOrCancel, string $symbol, string $side, string $amount, string $price): void
{
$data = $immediateOrCancel->place(Pair::getInstance($symbol), $side, $amount, $price);
echo
\str_pad($data->side, 5, ' ', \STR_PAD_RIGHT),
\str_pad($data->executed_amount, 12, ' ', \STR_PAD_RIGHT);
if ($data->executed_amount !== $data->original_amount) {
echo " of {$data->original_amount}";
}
$avg = (string) \round((float) $data->avg_execution_price, 2);
echo " at average price of {$avg}\n";
}
$range = \getRange($adapter);
$sleep = 0;
do {
if ($sleep) {
\sleep($sleep);
}
if (\time() - $range['time'] > 600) {
$range = \getRange($adapter);
}
$side = \rand(0, 1) ? 'buy' : 'sell';
try {
\placeOrder($immediateOrCancel, 'btcusd', $side, \getAmount(), $range[$side]);
$sleep = 1;
} catch (ConnectionException $e) {
echo "\e[91m{$e->getMessage()}\e[0m\n";
$sleep = 1;
} catch (SystemException $e) {
echo "\e[91m{$e->getMessage()}\e[0m\n";
$sleep = 10;
} catch (InsufficientFundsException $e) {
echo "\e[91m{$e->getMessage()}\e[0m\n";
$sleep = 1;
} catch (\Exception $e) {
\printException($e);
exit(1);
}
} while ($shutdown->isShutdownModeEnabled() === false);
echo "\nShutdown Detected\n";
exit(0);