class Exchange(object):
def __init__(self, name, queue, currency="$", tradeThreshold=100,
volumeThreshold=250, wallThreshold=500, priceSens=15):
self.name = name
self.currency = currency
self.q = queue
self.quiet = False
self.quietQ = self.q
self.priceSens = priceSens
self.thresholdTrade = float(tradeThreshold)
self.thresholdVolume = float(volumeThreshold)
self.iterVolume = 0
self.volume = {x: 0 for x in (1440, 720, 360, 180, 120, 60, 30, 15, 10,
5, 1)}
self.volumeTimer = RepeatEvent(60, self.updateVolume)
r = requests.get( "https://www.bitstamp.net/api/ticker/" )
try:
data = r.json()
self.lastTrade = float( data['last'] )
except ValueError:
self.lastTrade = 0.0
self.quietWalls = { (0,) }
self.thresholdWall = float(wallThreshold)
self.orderPrices = { 0.0: [0.0,0.0] }
# use hash b/c order amounts aren't constant
self.orderBook = {(0, 0, 0)} # { (id, type, price) }
self.oldBook = {(0, 0, 0)} # for set comparisons
self.wallTimer = RepeatEvent(15, self.findWalls)
# orderBook is a set of 3-tuples
# use set membership tests to parse wall data
# TODO : if order deleted w/0 volume then probably filled, else pulled
def setTrade(self, amount):
self.thresholdTrade = amount
def setVolume(self, amount):
self.thresholdVolume = amount
def setWall(self, amount):
self.thresholdWall = float(amount)
def toggleQuiet(self):
if not self.quiet:
self.quiet = True
self.q = Queue.Queue()
else:
self.quiet = False
self.q = self.quietQ
def updateVolume(self):
for n in [1440, 720, 360, 180, 120, 60, 30, 15, 10, 5]:
if self.iterVolume % n == 0:
self.volume[n] = self.volume[1]
else:
self.volume[n] += self.volume[1]
if self.volume[1] >= self.thresholdVolume:
self.volumeAlert( self.volume[1] )
self.volume[1] = 0
if self.iterVolume == 1441:
self.iterVolume = 0
else:
self.iterVolume += 1
def gotVolume(self, amount):
self.volume[1] += amount
def gotTrade(self, price, amount, tradeType=None):
# TODO handle None
if tradeType == None:
if price > self.lastTrade:
tType = "Buy"
elif price < self.lastTrade:
tType = "Sell"
else:
tType = ""
else:
if tradeType == "1":
tType = "Buy"
elif tradeType == "0":
tType = "Sell"
if amount >= self.thresholdTrade:
self.tradeAlert(amount, price, tType)
self.lastTrade = price
def orderAdd(self, price, amount, orderType, orderId=None):
# TODO handle None
if amount >= self.thresholdWall and (price < self.lastTrade +
self.priceSens and price > self.lastTrade - self.priceSens):
self.orderBook.add((orderId, orderType, price))
self.orderPrices[price] = [amount, amount]
def orderDel(self, price, amount, orderType, orderId=None):
# TODO handle None
self.orderBook.discard( (orderId, orderType, price) )
if price in self.orderPrices:
oldAmount = self.orderPrices[price][0]
self.orderPrices[price] = [oldAmount,amount]
# TODO : when to prune dict?
def findWalls(self):
for order in self.orderBook - self.oldBook:
# in orderBook, but not in oldBook (new wall)
amount = self.orderPrices[order[2]]
self.wallAlert( amount[0], amount[1], order[2], order[0] )
for order in self.oldBook - self.orderBook:
# in oldBook, but not in orderBook (wall pulled)
amount = self.orderPrices[order[2]]
self.wallAlert( -1 * amount[0], -1 * amount[1], order[2], order[0] )
self.oldBook = self.orderBook.copy() # TODO : worry about concurrency?
def tradeAlert(self, amount, price, direction):
self.q.put(u"{} Trade Alert | {} {:.3f} BTC @ {}{:.3f}".format(self.name, direction, amount, self.currency, price))
def priceQuery(self):
self.q.put(u"{} Price | {}{:.3f}".format(self.name, self.currency, self.lastTrade))
def volumeQuery(self, interval):
if interval in self.volume:
self.q.put(u"{} {}m Volume | {:.3f} BTC".format(self.name, interval, self.volume[interval]))
def wallAlert(self, oldAmount, amount, price, wallId):
if (price,) in self.quietWalls:
return
direction = "Added"
if amount < 0:
direction = "Pulled"
elif amount == 0:
direction = "Eaten"
self.q.put(u"{} Wall Alert | {} {:.3f} BTC @ {}{:.3f}".format(self.name, direction, oldAmount, self.currency, price))
def volumeAlert(self, amount):
# TODO add direction
self.q.put(u"{} Volume Alert | Last 60s {:.3f} BTC ({}{:.3f})".format(self.name, amount, self.currency, self.lastTrade))
返回此
# python bot.py
Traceback (most recent call last):
File "bot.py", line 12, in <module>
import exch
File "/root/nickbot-py/exch.py", line 33
self.volume = {x: 0 for x in (1440, 720, 360, 180, 120, 60, 30, 15, 10,
^
SyntaxError: invalid syntax
知道造成这种情况的原因是什么?任何帮助表示赞赏。我在CentOS6上运行Python 2。我觉得这很简单,但我似乎无法弄明白。这与依赖关系有关吗?