/
ticker.py
68 lines (60 loc) · 1.97 KB
/
ticker.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# import logging
# logging.basicConfig(
# format = '%(name)-12s: %(message)s',
# )
# logging.getLogger('MtGoxCore').setLevel(logging.DEBUG)
# logging.getLogger('MtGox').setLevel(logging.DEBUG)
import time, convert, pynotify, logging
from mtgoxcore import MtGoxCore
from config import KEY, SEC
from decimal import Decimal
FEE = Decimal('0.0043')
gox = MtGoxCore(KEY, SEC)
bal = convert.balance(gox.balance())
#bal = {'btcs': Decimal(40), 'usds': Decimal(0)}
while True:
try:
tic = convert.ticker(gox.ticker())
newbal = convert.balance(gox.balance())
last = tic['last']
buy = tic['sell']
sell = tic['buy']
usds = bal['usds']
btcs = bal['btcs']
newusds = newbal['usds']
newbtcs = newbal['btcs']
valusds = (1 - FEE) * newbtcs * last + newusds
valbtcs = (1 - FEE) * newusds / last + newbtcs
print '%.3f BTC + %.3f USD' % (newbtcs, newusds)
print '%.3f BTC / %.3f USD' % (valbtcs, valusds)
print 'Buy %.3f - Sell %.3f - Last %.3f' % (buy, sell, last)
print ''
if btcs != newbtcs:
amount = newbtcs - btcs
if usds != newusds:
# Trade
price = (usds - newusds) / amount
if amount > 0:
#Buy
n = pynotify.Notification \
('Bought', '%.2f ฿ at %.2f $' % (amount, price))
n.show()
else:
#Sell
n = pynotify.Notification \
('Sold', '%.2f BTC at %.2f $' % (-amount, price))
n.show()
else:
#Transfer
n = pynotify.Notification('Transer', '%.2f BTC' % amount)
n.show()
bal = newbal
except KeyboardInterrupt:
gox.stop()
exit(0)
except Exception, e:
print e
pass
time.sleep(60)