-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbitstamp-cli
executable file
·242 lines (232 loc) · 8.93 KB
/
bitstamp-cli
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
#!/usr/bin/php
<?php
require __DIR__ . '/vendor/autoload.php';
require __DIR__.'/lib/class.APIException.php';
require __DIR__.'/lib/class.BitstampNet.php';
require __DIR__.'/lib/bitstamp-cli-utilities.php';
use Garden\Cli\Cli;
// Load local config file
$localConfig = array();
if (file_exists(__DIR__.'/config-local.php')) {
require __DIR__.'/config-local.php';
}
// Init BitstampNet class with local config
$bitstamp = new Mx17\BitstampPHP\BitstampNet($localConfig);
$currencyOptionDescription = 'Set currency pair. Supported values: '.
implode(', ', $bitstamp->getAllowedCurrencyPairs());
$noPromptOptionsDecription = 'no prompt';
$cli = Cli::create()
->command('ticker')
->opt('currency', $currencyOptionDescription, false)
->command('tickerHour')
->opt('currency', $currencyOptionDescription, false)
// ->command('orderBook')
// ->opt('currency', $currencyOptionDescription, false)
->command('transactions')
->opt('currency', $currencyOptionDescription, false)
->command('conversionRate')
->description('EUR/USD conversion rate.')
->command('balance')
->description('Show your balance. If you specify --currency, shows only currency details.')
->opt('fiat', 'Fiat money. Possible values are "eur" or "usd"', false, 'string')
->opt('currency', $currencyOptionDescription, false)
->command('openOrders')
->command('userTransactions')
->opt('currency', $currencyOptionDescription, false)
->opt('offset:o', 'Offset', false, 'string')
->opt('limit:l', 'Limit', false, 'string')
->opt('sort:s', 'Sort. Possible values are "asc" or "desc"', false, 'string')
->command('openOrders')
->opt('currency', $currencyOptionDescription, false)
->command('cancelAllOrders')
->opt('noprompt:n', $noPromptOptionsDecription, false, 'boolean')
->command('buy')
->description('Place a buy limit order.')
->opt('currency', $currencyOptionDescription, false)
->opt('noprompt:n', $noPromptOptionsDecription, false, 'boolean')
->opt('amount:a', 'Amount. You can specify "all" to invest all your available budget', true, 'string')
->opt('price:p', 'Price', true, 'string')
->opt(
'limit-price:l',
'If the order gets executed, a new sell order will be placed, with "limit-price" as its price.',
false,
'string'
)
->opt(
'daily-order:d',
'Opens buy limit order which will be canceled at 00:00 UTC unless it already has been executed.',
false,
'boolean'
)
->command('sell')
->description('Place a sell limit order.')
->opt('currency', $currencyOptionDescription, false)
->opt('noprompt:n', $noPromptOptionsDecription, false, 'boolean')
->opt('amount:a', 'Amount. You can specify "all" to sell all your available bitcoins', true, 'string')
->opt('price:p', 'Price', true, 'string')
->opt(
'limit-price:l',
'If the order gets executed, a new buy order will be placed, with "limit-price" as its price.',
false,
'string'
)
->opt(
'daily-order:d',
'Opens sell limit order which will be canceled at 00:00 UTC unless it already has been executed.',
false,
'boolean'
)
->command('orderStatus')
->arg('orderId', 'The order id.', true)
->command('cancelOrder')
->opt('noprompt:n', $noPromptOptionsDecription, false, 'boolean')
->arg('orderId', 'The order id.', true)
->command('help-currencies')
->command('help-fiat')
->command('help-formats')
->command('*')
->opt(
'format:f',
'Change output format. See "help-formats". Default is "table".',
false,
'string'
);
$args = $cli->parse($argv, true);
// Currency option (if available)
$currencyPassed = true;
if ($args->getOpt('currency', null) === null) {
$currencyPassed = false;
}
$currency = $args->getOpt('currency', $bitstamp->getCurrency());
$bitstamp->setCurrency($currency);
$bitstamp->setFiat($args->getOpt('fiat', $bitstamp->getFiat()));
// No prompt option
$noPrompt = $args->getOpt('noprompt', false);
// Format option
$format = $args->getOpt('format', 'table');
try {
switch ($command = $args->getCommand()) {
case 'ticker':
$data = $bitstamp->ticker();
printOutput($data, $format);
break;
case 'tickerHour':
$data = $bitstamp->tickerHour();
printOutput($data, $format);
break;
// case 'orderBook':
// $data = $bitstamp->orderBook();
// printOutput($data, $format);
// break;
case 'transactions':
$data = $bitstamp->transactions();
printOutput($data, $format);
break;
case 'conversionRate':
$data = $bitstamp->conversionRate();
printOutput($data, $format);
break;
case 'balance':
if (!!$currencyPassed) {
$data = $bitstamp->balance($currency);
} else {
$data = $bitstamp->balance();
}
printOutput($data, $format);
break;
case 'openOrders':
$data = $bitstamp->openOrders();
printOutput($data, $format);
break;
case 'userTransactions':
$offset = $args->getOpt('offset', 0);
$limit = $args->getOpt('limit', 10);
$sort = $args->getOpt('sort', 'desc');
$data = $bitstamp->userTransactions($offset, $limit, $sort);
printOutput($data, $format);
break;
case 'openOrders':
$data = $bitstamp->openOrders();
printOutput($data, $format);
break;
case 'cancelAllOrders':
if ($noPrompt || askConfirmation()) {
$data = $bitstamp->cancelAllOrders();
printOutput($data, $format);
}
break;
case 'buy':
$amount = $args->getOpt('amount');
$price = $args->getOpt('price');
$limitPrice = $args->getOpt('limit-price');
$dailyOrder = $args->getOpt('daily-order');
if ($amount === 'all') {
$buyableBTC = howManyBTCCanIBuyWithAllMyBalance($bitstamp, $price);
$amount = $buyableBTC;
}
$prompt = "Buy $amount BTC for $price {$bitstamp->getCurrency()}";
if ($noPrompt || askConfirmation($prompt)) {
$data = $bitstamp->buy($amount, $price, $limitPrice, $dailyOrder);
printOutput($data, $format);
}
break;
case 'sell':
$amount = $args->getOpt('amount');
$price = $args->getOpt('price');
$limitPrice = $args->getOpt('limit-price');
$dailyOrder = $args->getOpt('daily-order');
if ($amount === 'all') {
$sellableBTC = $bitstamp->balance()['btc_available'];
$amount = $sellableBTC;
}
$income = howMuchIsIncome($bitstamp, $price, $amount);
$prompt = "Sell $amount BTC for $price {$bitstamp->getCurrency()}, income should be $income";
if ($noPrompt || askConfirmation($prompt)) {
$data = $bitstamp->sell($amount, $price, $limitPrice, $dailyOrder);
printOutput($data, $format);
}
break;
case 'orderStatus':
$orderId = $args->getArg('orderId');
$data = $bitstamp->orderStatus($orderId);
printOutput($data, $format);
break;
case 'cancelOrder':
if ($noPrompt || askConfirmation()) {
$orderId = $args->getArg('orderId');
$data = $bitstamp->cancelOrder($orderId);
printOutput($data, $format);
}
break;
case 'help-currencies':
$data = array();
foreach ($bitstamp->getAllowedCurrencyPairs() as $c) {
$data[] = array('name' => $c);
}
printOutput($data, $format);
break;
case 'help-fiat':
$data = array();
foreach ($bitstamp->getAllowedFiat() as $c) {
$data[] = array('name' => $c);
}
printOutput($data, $format);
break;
case 'help-formats':
printOutput([
[ 'format' => 'table' ],
[ 'format' => 'json' ],
[ 'format' => 'csv' ],
[ 'format' => 'php' ]
], $format);
break;
default:
echone(basename(__FILE__).": unrecognized argument \"$command\"\n");
exit(1);
break;
}
} catch (Mx17\BitstampPHP\APIErrorException $e) {
echone(basename(__FILE__).": ".$e->getMessage()."\n");
exit(33);
}
// vim: syntax=php