forked from weex/goxsh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goxsh.py
executable file
·1165 lines (1094 loc) · 42 KB
/
goxsh.py
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
from contextlib import closing
from datetime import datetime
from decimal import Decimal, InvalidOperation, ROUND_DOWN, ROUND_UP
from functools import partial
import getpass
import inspect
import json
import locale
import re
import readline
import traceback
import urllib
import urllib2
import urlparse
# Imports for parsing config file
import ConfigParser
from ConfigParser import SafeConfigParser
import string
# Imports for storing secret key
import binascii
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
# Imports for signing API requests
import base64
import hmac
import hashlib
import time
# Set config file
cfgFile = "goxsh.cfg"
class CfgParse(object):
def readCfg(self):
cfg = ConfigParser.SafeConfigParser()
cfg.read(cfgFile)
return cfg
def setCfg(self, section, name, value):
section = section.decode('string_escape')
name = name.decode('string_escape')
value = str(value).decode('string_escape')
cfg = ConfigParser.SafeConfigParser()
cfg.read(cfgFile)
cfg.set(section, name, value)
with open (cfgFile, 'wb') as configfile:
cfg.write(configfile)
class NiceText:
"""A place to keep functions which format the output."""
@staticmethod
def colorCode(colorName):
"""Returns an ANSI color code by name from the config, printing this code to a
terminal will change the text color."""
try:
to = config.get("ansi", config.get("colors", colorName)).decode('string_escape')
return to
except ConfigParser.Error:
# In case no color is defined in config/no config file given/found set color to default
to = "\033[0;0m"
return to
@staticmethod
def resetCode():
"""Returns the ANSI reset attribute code into a string, printing this to a terminal
will reset all color options."""
try:
creset = config.get("ansi", "reset").decode('string_escape')
return creset
except ConfigParser.Error:
# In case reset is not defined in config/no config given/found use default reset code
creset = "\033[0;0m"
return creset
@staticmethod
def colorText(text, colorName):
"""Returns a string containing colored text which can be output to the console."""
return '{color}{text}{reset}'.format(color=NiceText.colorCode(colorName),
text =text,
reset=NiceText.resetCode())
class MtGoxError(Exception):
pass
class NoCredentialsError(Exception):
pass
class LoginError(Exception):
pass
class MtGox(object):
def __init__(self, user_agent):
self.unset_credentials()
self.__url_parts = urlparse.urlsplit("https://mtgox.com/api/0/")
self.__headers = {
"User-Agent": user_agent
}
def get_username(self):
return self.__credentials[0] if self.have_credentials() else None
def have_credentials(self):
return self.__credentials is not None
def set_credentials(self, username, key, secret):
if not username:
raise ValueError(u"Empty username.")
if not key:
raise ValueError(u"Empty key.")
if not secret:
raise ValueError(u"Empty secret.")
self.__credentials = (username, key, secret)
def unset_credentials(self):
self.__credentials = None
def activate(self, devicename, activationkey, appkey):
return self.__get_json("activate.php", params = {
u"name": devicename,
u"key": activationkey,
u"app": appkey
}, auth = False)
def get_btcaddress(self):
return self.__get_json("btcAddress.php")
def buy(self, amount, price):
return self.__get_json("buyBTC.php", params = {
u"amount": amount,
u"price": price
})
def cancel_order(self, kind, oid):
return self.__get_json("cancelOrder.php", params = {
u"oid": oid,
u"type": kind
})[u"orders"]
def get_balance(self):
usd = self.__get_json("info.php")[u"Wallets"][u"USD"][u"Balance"][u"value"]
btc = self.__get_json("info.php")[u"Wallets"][u"BTC"][u"Balance"][u"value"]
balance = {u"usds":usd, u"btcs":btc}
return balance
def get_orders(self):
return self.__get_json("getOrders.php")[u"orders"]
def get_ticker(self):
return self.__get_json("data/ticker.php", auth = False)[u"ticker"]
def sell(self, amount, price):
return self.__get_json("sellBTC.php", params = {
u"amount": amount,
u"price": price
})
#def withdraw(self, address, amount):
def withdraw(self, currency, destination, amount, account):
if (currency == "btc") and (destination == "btc"):
return self.__get_json("withdraw.php", params = {
u"group1": u"BTC",
u"btca": account,
u"amount": amount
})
elif (currency == "usd") and (destination == "dwolla"):
return self.__get_json("withdraw.php", params = {
u"group1": u"DWUSD",
u"dwaccount": account,
u"amount": amount
})
elif (currency == "usd") and (destination == "lr"):
return self.__get_json("withdraw.php", params = {
u"group1": u"USD",
u"account": account,
u"amount": amount
})
elif (currency == "usd") and (destination == "paxum"):
return self.__get_json("withdraw.php", params = {
u"group1": u"PAXUMUSD",
u"paxumaccount": account,
u"amount": amount
})
def get_commission(self):
return self.__get_json("info.php")[u"Trade_Fee"]
def get_depth(self):
return self.__get_json("data/getDepth.php", auth = False)
def __get_json(self, rel_path, params = {}, auth = True):
if auth and not self.have_credentials():
raise NoCredentialsError()
params = params.items()
if auth:
# Access counter
global counter
# Use time.time() to get a steadily increasing nonce
# *1000+counter to make more than one request per second possible
params += [
(u"nonce", int(time.time())*1000+counter)
]
# Increase counter by 1
counter += 1
# Encode params within POST
post_data = urllib.urlencode(params) if len(params) > 0 else None
key = self.__credentials[1]
secret = self.__credentials[2]
# Sign request by base64-encoding the not base64-encoded secert and the hmac-sha512-hashed post_data
sign = None
try:
sign = base64.b64encode(str(hmac.new(base64.b64decode(secret), post_data, hashlib.sha512).digest()))
except TypeError:
print u"Could not sign request. Please log in again."
self.unset_credentials()
# Create header for auth-requiring operations
user_agent = "goxsh"
self.__headers = {
"User-Agent": user_agent,
"Rest-Key": key,
"Rest-Sign": sign
}
else:
post_data = urllib.urlencode(params) if len(params) > 0 else None
url = urlparse.urlunsplit((
self.__url_parts.scheme,
self.__url_parts.netloc,
self.__url_parts.path + rel_path,
self.__url_parts.query,
self.__url_parts.fragment
))
req = urllib2.Request(url, post_data, self.__headers)
with closing(urllib2.urlopen(req, post_data)) as res:
data = json.load(res)
if u"error" in data:
if data[u"error"] == u"Not logged in.":
raise LoginError()
else:
raise MtGoxError(data[u"error"])
else:
return data
class TokenizationError(Exception):
pass
class CommandError(Exception):
pass
class ArityError(Exception):
pass
class GoxSh(object):
def __init__(self, mtgox, encoding):
self.__mtgox = mtgox
self.__encoding = encoding
readline.parse_and_bind("tab: complete")
readline.set_completer(self.__complete)
self.__btc_precision = Decimal("0.00000001")
self.__usd_precision = Decimal("0.00001")
self.__usd_re = re.compile(r"^\$(\d*\.?\d+)$")
collapse_escapes = partial(re.compile(r"\\(.)", re.UNICODE).sub, "\\g<1>")
self.__token_types = (
( # naked (unquoted)
re.compile(r"(?:\\[\s\\\"';#]|[^\s\\\"';#])+", re.UNICODE),
collapse_escapes
),
( # double-quoted
re.compile(r"\"(?:\\[\\\"]|[^\\])*?\"", re.UNICODE),
lambda matched: collapse_escapes(matched[1:-1])
),
( # single-quoted
re.compile(r"'(?:\\[\\']|[^\\])*?'", re.UNICODE),
lambda matched: collapse_escapes(matched[1:-1])
),
( # whitespace and comments
re.compile(r"(?:\s|#.*)+", re.UNICODE),
lambda matched: None
),
( # semicolon
re.compile(r";", re.UNICODE),
lambda matched: matched
)
)
def __tokenize_command(self, line):
remaining = line
while remaining:
found = False
for (pattern, sub) in self.__token_types:
match = pattern.match(remaining)
if match:
raw_token = match.group(0)
assert len(raw_token) > 0, u"empty token"
token = sub(raw_token)
if token is not None:
yield token
remaining = remaining[len(raw_token):]
found = True
break
if not found:
message = "\n".join(
u" {0}^".format(u" " * (len(line) - len(remaining)),
u"Syntax error."))
raise TokenizationError(message)
def __parse_tokens(self, tokens):
cmd = None
args = []
for token in tokens:
if token == u";":
if cmd:
yield (cmd, args)
cmd = None
args = []
elif not cmd:
cmd = token
else:
args.append(token)
if cmd:
yield (cmd, args)
def prompt(self):
procs = []
args = []
try:
raw_line = None
try:
text = u"{userName}{prompt} ".format(userName=NiceText.colorText(self.__mtgox.get_username() or u'', "shell_user"),
prompt =NiceText.colorText(u' $', "shell_self") if self.__mtgox.get_username() else NiceText.colorText(u'$', "shell_self"))
line = raw_input(text).decode(self.__encoding)
except EOFError, e:
print u"exit"
self.__cmd_exit__()
commands = self.__parse_tokens(self.__tokenize_command(line))
for (cmd, args) in commands:
try:
proc = self.__get_cmd_proc(cmd, self.__unknown(cmd))
(min_arity, max_arity) = self.__get_proc_arity(proc)
arg_count = len(args)
if min_arity <= arg_count and (max_arity == None or arg_count <= max_arity):
proc(*args)
else:
if min_arity == max_arity:
arity_text = unicode(min_arity)
elif max_arity == None:
arity_text = u"{0}+".format(min_arity)
else:
arity_text = u"{min}-{max}".format(min=min_arity, max=max_arity)
arg_text = u"argument" + (u"" if arity_text == u"1" else u"s")
raise ArityError(u"Expected %s %s, got %s." % (arity_text, arg_text, arg_count))
except MtGoxError, e:
print u"Mt. Gox error: {0}".format(e)
except CommandError, e:
print e
except ArityError, e:
print e
except NoCredentialsError:
print u"No login credentials entered. Use the login command first."
except LoginError:
print u"Mt. Gox rejected the login credentials. Maybe you made a typo?"
except EOFError, e:
raise e
except TokenizationError, e:
print e
except KeyboardInterrupt:
print
except Exception, e:
traceback.print_exc()
def __get_cmd_proc(self, cmd, default = None):
return getattr(self, "__cmd_{0}__".format(cmd), default)
def __cmd_name(self, attr):
match = re.match(r"^__cmd_(.+)__$", attr)
return match.group(1) if match != None else None
def __get_cmds(self, prefix = ""):
return sorted(
filter(
lambda cmd: cmd != None and cmd.startswith(prefix),
(self.__cmd_name(attr) for attr in dir(self))
)
)
def __print_cmd_info(self, cmd):
proc = self.__get_cmd_proc(cmd)
if proc:
print u"{0}".format(NiceText.colorText(cmd, "shell_help_cmd")),
argspec = inspect.getargspec(proc)
args = argspec.args[1:]
if argspec.defaults:
i = -1
for default in reversed(argspec.defaults):
args[i] = (args[i], default)
i -= 1
for arg in args:
if not isinstance(arg, tuple):
print u"{0}".format(NiceText.colorText(arg, "shell_help_arg")),
elif len(arg) == 2:
printarg = "[{0}={1}]".format(*arg)
print u"{0}".format(NiceText.colorText(printarg, "shell_help_arg")),
else:
#print u"[{0}]".format(arg[0]),
print u"[{0}]".format(NiceText.colorText(arg[0], "shell_help_cmd")),
if argspec.varargs:
print u"[...]",
print
doc = proc.__doc__ or u"--"
for line in doc.splitlines():
print " " + line
else:
self.__unknown(cmd)
def __get_proc_arity(self, proc):
argspec = inspect.getargspec(proc)
maximum = len(argspec.args[1:])
minimum = maximum - (len(argspec.defaults) if argspec.defaults != None else 0)
if argspec.varargs:
maximum = None
return (minimum, maximum)
def __complete(self, text, state):
cmds = self.__get_cmds(text)
try:
return self.__get_cmds(text)[state] + (" " if len(cmds) == 1 else "")
except IndexError:
return None
def __exchange(self, proc, amount, price):
match = self.__usd_re.match(amount)
if match:
usd_amount = Decimal(match.group(1))
btc_amount = str((usd_amount / Decimal(price)).quantize(self.__btc_precision))
else:
btc_amount = amount
ex_result = proc(btc_amount, price)
statuses = filter(None, ex_result[u"status"].split(u"<br>"))
for status in statuses:
print status
if u"orders" in ex_result:
self.__print_order_header()
for order in ex_result[u"orders"]:
self.__print_order(order)
def __print_balance(self, balance):
btc = str(format(Decimal(balance[u"btcs"]), '.8f')).rjust(14, ' ')
usd = str(format(Decimal(balance[u"usds"]), '.8f')).rjust(14, ' ')
print u"{btcSymbol} {btcBalance}".format(btcSymbol =NiceText.colorText("BTC:", "balance_btcsymbol"),
btcBalance=NiceText.colorText(btc, "balance_btcamount"))
print u"{usdSymbol} {usdBalance}".format(usdSymbol =NiceText.colorText("USD:", "balance_usdsymbol"),
usdBalance=NiceText.colorText(usd, "balance_usdamount"))
def __print_order_header(self):
print u""
print u"Kind | BTC | ID"
print u" | USD | Date (status)"
print u"============================================================"
def __print_order(self, order):
kind = {1: u"sell", 2: u"buy"}[order[u"type"]]
timestamp = datetime.fromtimestamp(int(order[u"date"])).strftime("%Y-%m-%d %H:%M:%S")
properties = []
# Append status to orders
properties.append(order[u"real_status"])
if kind == u"sell":
btc = str(format(Decimal(order[u"amount"]), '.8f')).rjust(14, ' ')
usd = str(format(Decimal(order[u"price"]), '.8f')).rjust(14, ' ')
print "{kind} | {sellAmnt} | {oid}".format(
kind =NiceText.colorText(kind, "orders_sellkind"),
sellAmnt =NiceText.colorText(btc, "orders_sellamount"),
oid =NiceText.colorText(order[u"oid"], "orders_selloid"))
print " | {sellPrice} | {timeStamp}{props}".format(
sellPrice =NiceText.colorText(usd, "orders_sellprice"),
timeStamp=NiceText.colorText(timestamp, "orders_selltime"),
props =" (" + ", ".join(properties) + ")" if properties else "")
elif kind == u"buy":
btc = str(format(Decimal(order[u"amount"]), '.8f')).rjust(14, ' ')
usd = str(format(Decimal(order[u"price"]), '.8f')).rjust(14, ' ')
print "{kind} | {buyAmnt} | {oid}".format(
kind =NiceText.colorText(kind, "orders_buykind"),
buyAmnt =NiceText.colorText(btc, "orders_buyamount"),
oid =NiceText.colorText(order[u"oid"], "orders_buyoid"))
print " | {buyPrice} | {timeStamp}{props}".format(
buyPrice =NiceText.colorText(usd, "orders_buyprice"),
timeStamp=NiceText.colorText(timestamp, "orders_buytime"),
props =" (" + ", ".join(properties) + ")" if properties else "")
print u"------------------------------------------------------------"
def __unknown(self, cmd):
def __unknown_1(*args):
print u"{0}: Unknown command.".format(cmd)
return __unknown_1
def __cmd_balance__(self):
u"Display account balance."
self.__print_balance(self.__mtgox.get_balance())
def __cmd_deposit__(self):
u"Get a Bitcoin deposit address for your account."
address = self.__mtgox.get_btcaddress()[u"addr"]
print u"Deposit to: {0}".format(address)
def __cmd_buy__(self, amount, price):
u"Buy bitcoins.\n" \
"Prefix the amount with a '$' to spend that many USD\n" \
"and calculate BTC amount automatically."
self.__exchange(self.__mtgox.buy, amount, price)
def __cmd_cancel__(self, kind, order_id):
u"Cancel the order with the specified kind (buy or sell) and order ID."
try:
num_kind = {u"sell": 1, u"buy": 2}[kind]
orders = self.__mtgox.cancel_order(num_kind, order_id)
print u"Canceled {0} {1}.".format(kind, order_id)
if orders:
self.__print_order_header()
for order in orders:
self.__print_order(order)
else:
print u"No remaining orders."
except KeyError:
raise CommandError(u"{0}: Invalid order kind.".format(kind))
def __cmd_exit__(self):
u"Exit goxsh."
raise EOFError()
def __cmd_help__(self, command = None):
u"Show help for the specified command or\n" \
"list all commands if none is given."
if not command:
cmds = self.__get_cmds()
else:
cmds = [command]
for cmd in cmds:
self.__print_cmd_info(cmd)
def __cmd_login__(self):
u"Set login credentials."
try:
config = cfgp.readCfg()
username = config.get('userauth', 'username').decode('string_escape')
secret = config.get('userauth', 'secret').decode('string_escape')
length = config.get('userauth', 'length').decode('string_escape')
key = config.get('userauth', 'key').decode('string_escape')
perror = False
except ConfigParser.Error:
perror = True
print u"Some user credentials are missing. Check configuration."
if not perror:
if all((username, secret, length, key)):
password = u""
# Providing password to decrypt secret
while not password:
password = getpass.getpass("Password: ").decode('string_escape')
hash = SHA256.new()
# Hash password
hash.update(password)
# base64-encode password
password = binascii.b2a_base64(hash.digest())
# Truncating hash to get a valid length
password = password[0:32]
# Set password for decryption
aes = AES.new(password, AES.MODE_ECB)
secret = binascii.a2b_base64(secret)
secret = aes.decrypt(secret)
# Truncate leading zeros
length = int(length)
length = 128-length
length = str(length)
secret = re.sub(r"\b0{"+length+"}","",secret)
self.__mtgox.set_credentials(username, key, secret)
else:
print u"Some user credentials are missing. Check configuration."
def __cmd_logout__(self):
u"Unset login credentials."
if self.__mtgox.have_credentials():
self.__mtgox.unset_credentials()
else:
print u"Not logged in."
def __cmd_orders__(self, kind = None):
u"List open orders.\n" \
"Specifying a kind (buy or sell) will list only orders of that kind."
try:
num_kind = {None: None, u"sell": 1, u"buy": 2}[kind]
orders = self.__mtgox.get_orders()
if orders:
self.__print_order_header()
for order in orders:
if num_kind in (None, order[u"type"]):
self.__print_order(order)
else:
print u"No orders."
except KeyError:
raise CommandError(u"{0}: Invalid order kind.".format(kind))
def __cmd_profit__(self, price):
u"Calculate profitable short/long prices for a given initial price,\n" \
"taking into account Mt. Gox's commission fee."
try:
# Get Mt. Gox trading fee (in %) and devide it by 100 to get it in decimal
self.__mtgox_commission = Decimal(str(self.__mtgox.get_commission()))/100
dec_price = Decimal(price)
if dec_price < 0:
raise CommandError(u"{0}: Invalid price.".format(price))
min_profitable_ratio = (1 - self.__mtgox_commission)**(-2)
short_value = (dec_price / min_profitable_ratio).quantize(self.__usd_precision, ROUND_DOWN)
long_value = (dec_price * min_profitable_ratio).quantize(self.__usd_precision, ROUND_UP)
short_value = str(format(short_value, '.5f')).rjust(10, ' ')
long_value = str(format(long_value, '.5f')).rjust(10, ' ')
print u"{short} {shortSign} {value}".format(
short =NiceText.colorText("Short:", "profit_shorttext"),
shortSign=NiceText.colorText("<", "profit_shortsign"),
value =NiceText.colorText(short_value, "profit_shortvalue"))
print u"{long} {longSign} {value}".format(
long =NiceText.colorText("Long:", "profit_longtext"),
longSign =NiceText.colorText(">", "profit_longsign"),
value =NiceText.colorText(long_value, "profit_longvalue"))
except InvalidOperation:
raise CommandError(u"{0}: Invalid price.".format(price))
def __cmd_sell__(self, amount, price):
u"Sell bitcoins.\n" \
"Prefix the amount with a '$' to receive that many USD\n" \
"and calculate BTC amount automatically."
self.__exchange(self.__mtgox.sell, amount, price)
def __cmd_ticker__(self):
u"Display ticker."
ticker = self.__mtgox.get_ticker()
last = str(format(ticker[u"last"], '.5f')).rjust(10, ' ')
buy = str(format(ticker[u"buy"], '.5f')).rjust(10, ' ')
sell = str(format(ticker[u"sell"], '.5f')).rjust(10, ' ')
high = str(format(ticker[u"high"], '.5f')).rjust(10, ' ')
low = str(format(ticker[u"low"], '.5f')).rjust(10, ' ')
vol = str(ticker[u"vol"]).rjust(10, ' ')
print u"Last: {0}".format(NiceText.colorText(last, "ticker_last"))
print u" Buy: {0}".format(NiceText.colorText(buy, "ticker_buy"))
print u"Sell: {0}".format(NiceText.colorText(sell, "ticker_sell"))
print u"High: {0}".format(NiceText.colorText(high, "ticker_high"))
print u" Low: {0}".format(NiceText.colorText(low, "ticker_low"))
print u"Vol.: {0}".format(NiceText.colorText(vol, "ticker_vol"))
def __cmd_withdraw__(self, currency, destination, amount, account):
u"Withdraw Bitcoins or Dollars.\n" \
"currency: usd or btc\n" \
"destination: btc, dwolla, lr (Liberty Reserve) and paxum\n" \
"amount: amount to withdraw (in decimal)\n" \
"account: btc-address, dwolla-account, LR-account or Paxum account"
withdraw_info = self.__mtgox.withdraw(currency, destination, amount, account)
print withdraw_info[u"status"]
print u"Updated balance:"
# replaced self.__print_balance(withdraw_info) by:
self.__print_balance(self.__mtgox.get_balance())
def __cmd_set__(self, section, name, value):
u"Set configuration values."
cfgp.setCfg(section, name, value)
def __cmd_activate__(self, activationkey):
u"Activate goxsh."
secret = None
password = None
# Get app auth info
config = cfgp.readCfg()
try:
# Get devicename
devicename = config.get('appauth', 'devicename').decode('string_escape')
except ConfigParser.Error:
# If key "devicename" doesn't exist assign a random name+_goxsh-suffix
devicename = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(10))
devicename += "_goxsh"
# If key "devicename" is given but value is empty assign a randome name+_goxsh-suffix
if (len(devicename) <= 0):
devicename = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(10))
devicename += "_goxsh"
try:
# Get appkey
appkey = config.get('appauth', 'appkey').decode('string_escape')
except ConfigParser.Error:
# If key "appkey" doesn't exist in config assign Optonic's goxsh appkey
appkey = "d96f4e85-990a-4115-81ef-0c8baedf6895"
# If key "appkey" is given but value is empty assign Optonic's goxsh appkey
if (len(appkey) <= 0):
appkey = "d96f4e85-990a-4115-81ef-0c8baedf6895"
# Obtain API-key and secret
activate = self.__mtgox.activate(devicename, activationkey, appkey)
# Get user's key
key = activate[u"Rest-Key"].decode('string_escape')
# Get user's secret
secret = activate[u"Secret"].decode('string_escape')
# Get user's rights
rights = activate[u"Rights"]
# rights = rights.decode('string_escape')
# Get secret length
length = len(secret)
# Write user's key, secret length and rights to config
cfgp.setCfg("userauth", "key", key)
cfgp.setCfg("userauth", "length", length)
cfgp.setCfg("userauth", "rights", rights)
print u"Enter password for secret encryption."
# Providing password to encrypt secret
while not password:
pass1 = getpass.getpass("Password: ").decode('string_escape')
pass2 = getpass.getpass("Repeat: ").decode('string_escape')
if pass1 == pass2:
password = pass1
else:
print u"Passwords don't match, try again."
hash = SHA256.new()
# Hash password
hash.update(password)
# base64-encode password
password = binascii.b2a_base64(hash.digest())
# Truncating hash to get a valid length
password = password[0:32]
# Set password to encrypt secret with
aes = AES.new(password, AES.MODE_ECB)
# Fill secret with leading zeros to get a valid length
secret = str.zfill(secret, 128)
# AES-encrypt secret
secret = aes.encrypt(secret)
# base64-encode secret
secret = binascii.b2a_base64(secret).decode('string_escape')
# Writing encrypted secret to config file
cfgp.setCfg("userauth", "secret", secret)
config = cfgp.readCfg()
def __cmd_reload__(self):
u"Reload config file."
global config
config = cfgp.readCfg()
def __cmd_bids__(self, steps=0, price=0):
u"Show orderbook (market depth) - bids side.\n" \
"steps: Number of rows to be printed.\n" \
" If set to 0 all bids of orderbook are printed.\n" \
"price: Specify price to start with. If not given/set to 0\n" \
" it starts with first bid."
try:
try:
steps = int(steps)
except:
print u"Argument \"steps\" requires a positive value (integer)."
raise Exception
try:
price = Decimal(price)
except:
print u"Argument \"price\" requires a positive value (decimal)."
raise Exception
if (price < 0):
print u"Argument \"price\" requires a positive value (decimal)."
# Show depth in x steps with last trade as starting point
elif (steps == 0) and (price == 0):
# Get current bids of order book (aka market depth) as a list
bids = list(self.__mtgox.get_depth()[u"bids"])
# Reverse order of bids for cumulation
bids.reverse()
# Create empty array cumulatedBids[]
cumulatedBids = []
# Set cumulateBids to 0
cumulateBids = 0
for i in bids:
cumulateBids += i[1]
cumulatedBids.append(cumulateBids)
# Re-reverse order of bids and reverse order of cumulatedBids
bids.reverse()
cumulatedBids.reverse()
print u""
print u"Type | Price | Amount | Sum"
print u"==================================================="
# icb -> Iterator for cumulatedBids[]
icb = 0
for i in bids:
# i[0] -> Price
# i[1] -> Amount
formatPrice = str(format(i[0], '.5f')).rjust(10, ' ')
formatAmount = str(format(i[1], '.8f')).rjust(14, ' ')
formatCumulate = str(format(cumulatedBids[icb], '.8f')).rjust(14, ' ')
print u"Bid | {0} | {1} | {2}".format(NiceText.colorText(formatPrice, "depth_bid"), formatAmount, formatCumulate)
icb += 1
print u"---------------------------------------------------"
elif (steps > 0) and (price == 0):
# Get current bids of order book (aka market depth) as a list
bids = list(self.__mtgox.get_depth()[u"bids"])
# Reverse order of bids for cumulation
bids.reverse()
# Create empty array cumulatedBids[]
cumulatedBids = []
# Set cumulateBids to 0
cumulateBids = 0
# Get x steps of bids only
bids = bids[:steps]
for i in bids:
cumulateBids += i[1]
cumulatedBids.append(cumulateBids)
# Re-reverse order of bids and reverse order of cumulatedBids
bids.reverse()
cumulatedBids.reverse()
print u""
print u"Type | Price | Amount | Sum"
print u"==================================================="
# icb -> Iterator for cumulatedBids[]
icb = 0
for i in bids:
# i[0] -> Price
# i[1] -> Amount
formatPrice = str(format(i[0], '.5f')).rjust(10, ' ')
formatAmount = str(format(i[1], '.8f')).rjust(14, ' ')
formatCumulate = str(format(cumulatedBids[icb], '.8f')).rjust(14, ' ')
print u"Bid | {0} | {1} | {2}".format(NiceText.colorText(formatPrice, "depth_bid"), formatAmount, formatCumulate)
icb += 1
print u"---------------------------------------------------"
elif (steps == 0) and (price > 0):
# Get current bids of order book (aka market depth) as a list
bids = list(self.__mtgox.get_depth()[u"bids"])
# Reverse order of bids for cumulation
bids.reverse()
# Create empty array cumulatedBids[]
cumulatedBids = []
# Set cumulateBids to 0
cumulateBids = 0
for i in bids:
cumulateBids += i[1]
cumulatedBids.append(cumulateBids)
# Re-reverse order of bids and reverse order of cumulatedBids
bids.reverse()
cumulatedBids.reverse()
print u""
print u"Type | Price | Amount | Sum"
print u"==================================================="
# icb -> Iterator for cumulatedBids[]
icb = 0
for i in bids:
# i[0] -> Price
# i[1] -> Amount
formatPrice = str(format(i[0], '.5f')).rjust(10, ' ')
formatAmount = str(format(i[1], '.8f')).rjust(14, ' ')
formatCumulate = str(format(cumulatedBids[icb], '.8f')).rjust(14, ' ')
keyvalue = Decimal(str(i[0]))
if (keyvalue <= price):
if (keyvalue < price):
print u"Bid | {0} | {1} | {2}".format(NiceText.colorText(formatPrice, "depth_bid"), formatAmount, formatCumulate)
icb += 1
elif (keyvalue == price):
print u"---------------------------------------------------"
print u"Bid | {0} | {1} | {2}".format(NiceText.colorText(formatPrice, "depth_bid"), formatAmount, formatCumulate)
break
else:
formatPrice = str(format(price, '.5f')).rjust(10, ' ')
print u"---------------------------------------------------"
print u"n/a | {0} | {1}".format(NiceText.colorText(formatPrice, "depth_no"), NiceText.colorText("No amount at given price", "depth_no"))
break
print u"---------------------------------------------------"
elif (steps > 0) and (price > 0):
print u"Not yet implemented. Tell me if you really need this :-)"
except:
print u"Execution aborted."
def __cmd_asks__(self, steps=0, price=0):
u"Show orderbook (market depth) - asks side.\n" \
"steps: Number of rows to be printed.\n" \
" If set to 0 all asks of orderbook are printed.\n" \
"price: Specify price to start with. If not given/set to 0\n" \
" it starts with first ask."
try:
try:
steps = int(steps)
except:
print u"Argument \"steps\" requires a positive value (integer)."
raise Exception
try:
price = Decimal(price)
except:
print u"Argument \"price\" requires a positive value (decimal)."
raise Exception
if (price < 0):
print u"Argument \"price\" requires a positive value (decimal)."
# Show depth in x steps with last trade as starting point
elif (steps == 0) and (price == 0):
# Get current asks of order book (aka market depth) as a list
asks = list(self.__mtgox.get_depth()[u"asks"])
# Create empty array cumulatedasks[]
cumulatedAsks = []
# Set cumulateasks to 0
cumulateAsks = 0
for i in asks:
cumulateAsks += i[1]
cumulatedAsks.append(cumulateAsks)
print u""
print u"Type | Price | Amount | Sum"
print u"==================================================="
# icb -> Iterator for cumulatedasks[]
icb = 0
for i in asks:
# i[0] -> Price
# i[1] -> Amount
formatPrice = str(format(i[0], '.5f')).rjust(10, ' ')
formatAmount = str(format(i[1], '.8f')).rjust(14, ' ')
formatCumulate = str(format(cumulatedAsks[icb], '.8f')).rjust(14, ' ')
print u"Ask | {0} | {1} | {2}".format(NiceText.colorText(formatPrice, "depth_ask"), formatAmount, formatCumulate)
icb += 1
print u"---------------------------------------------------"
elif (steps > 0) and (price == 0):
# Get current asks of order book (aka market depth) as a list
asks = list(self.__mtgox.get_depth()[u"asks"])
# Create empty array cumulatedasks[]
cumulatedAsks = []
# Set cumulateasks to 0
cumulateAsks = 0
# Get x steps of asks only
asks = asks[:steps]
for i in asks:
cumulateAsks += i[1]
cumulatedAsks.append(cumulateAsks)
print u""
print u"Type | Price | Amount | Sum"
print u"==================================================="
# icb -> Iterator for cumulatedasks[]
icb = 0
for i in asks:
# i[0] -> Price
# i[1] -> Amount
formatPrice = str(format(i[0], '.5f')).rjust(10, ' ')
formatAmount = str(format(i[1], '.8f')).rjust(14, ' ')
formatCumulate = str(format(cumulatedAsks[icb], '.8f')).rjust(14, ' ')
print u"Ask | {0} | {1} | {2}".format(NiceText.colorText(formatPrice, "depth_ask"), formatAmount, formatCumulate)
icb += 1
print u"---------------------------------------------------"
elif (steps == 0) and (price > 0):
# Get current asks of order book (aka market depth) as a list
asks = list(self.__mtgox.get_depth()[u"asks"])
# Create empty array cumulatedasks[]
cumulatedAsks = []
# Set cumulateasks to 0
cumulateAsks = 0
for i in asks:
cumulateAsks += i[1]
cumulatedAsks.append(cumulateAsks)
print u""
print u"Type | Price | Amount | Sum"
print u"==================================================="
# icb -> Iterator for cumulatedasks[]
icb = 0
for i in asks:
# i[0] -> Price
# i[1] -> Amount
formatPrice = str(format(i[0], '.5f')).rjust(10, ' ')
formatAmount = str(format(i[1], '.8f')).rjust(14, ' ')
formatCumulate = str(format(cumulatedAsks[icb], '.8f')).rjust(14, ' ')
keyvalue = Decimal(str(i[0]))
if (keyvalue <= price):
if (keyvalue < price):
print u"Ask | {0} | {1} | {2}".format(NiceText.colorText(formatPrice, "depth_ask"), formatAmount, formatCumulate)
icb += 1
elif (keyvalue == price):
print u"---------------------------------------------------"
print u"Ask | {0} | {1} | {2}".format(NiceText.colorText(formatPrice, "depth_ask"), formatAmount, formatCumulate)
break
else:
formatPrice = str(format(price, '.5f')).rjust(10, ' ')
print u"---------------------------------------------------"
print u"n/a | {0} | {1}".format(NiceText.colorText(formatPrice, "depth_no"), NiceText.colorText("No amount at given price", "depth_no"))
break
print u"---------------------------------------------------"
elif (steps > 0) and (price > 0):
print u"Not yet implemented. Tell me if you really need this :-)"
except:
print u"Execution aborted."
def __cmd_depth__(self, steps, price=0, cumulate=0):
u"Show orderbook (market depth).\n" \
"steps: Number of rows to be printed before\n" \
" and after last trade/given price.\n" \
"price: Specify price (if not given last trade is assumed).\n" \
"cumulate: Set to 1 to cumulate amount.\n" \
" Works only if price is set to 0 (e.g. depth 5 0 1)."
try:
try:
steps = int(steps)
except:
print u"Argument \"steps\" requires a positive value (integer)."
raise Exception
try:
price = Decimal(price)
except:
print u"Argument \"price\" requires a positive value (decimal)."
raise Exception
try:
cumulate = int(cumulate)
except:
print u"Allowed values for argument \"cumulate\" are 0 and 1 (integer)."
raise Exception