本文整理汇总了Python中bintrees.FastRBTree.min_item方法的典型用法代码示例。如果您正苦于以下问题:Python FastRBTree.min_item方法的具体用法?Python FastRBTree.min_item怎么用?Python FastRBTree.min_item使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bintrees.FastRBTree
的用法示例。
在下文中一共展示了FastRBTree.min_item方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: OrderQueue
# 需要导入模块: from bintrees import FastRBTree [as 别名]
# 或者: from bintrees.FastRBTree import min_item [as 别名]
class OrderQueue(object):
"""
"""
def __init__(self):
self.Orders = FastRBTree()
self.totalAmount = 0.0
def append(self,order):
k = order.timestamp
self.Orders.insert(k,order)
self.totalAmount += self.Orders[k].amount
def count(self):
return self.Orders.count
def min_item(self):
return self.Orders.min_item()
def eat(self,amount):
"""
use this only if amount <= self.amount
return OrderToPop,RestAmount
"""
current_amount = amount
to_pop_orders = []
while self.totalAmount>0 and current_amount>0:
if not self.Orders or self.Orders.count==0:
#print "amount:%.4f" % self.totalAmount
self.totalAmount = 0.0
return []
min_item = self.Orders.min_item()
min_i = min_item[1]
# 1.enough
# 2.not enough
if min_i.amount <= current_amount:
current_amount -= min_i.amount
self.totalAmount -= min_i.amount
to_pop_orders.append(self.Orders.pop_min()[1])
continue
# this is a hard case , need to take care of
# first we split the order , take amount from current order
# and make
elif min_i.amount > current_amount:
new_order = min_i.copy()
new_order.amount = current_amount
to_pop_orders.append(new_order)
min_i.amount -= current_amount
self.totalAmount -= current_amount
current_amount = 0
break
# return:
# 1.orders
# 2.amount to trade
# 3.amount left
return to_pop_orders
def remove(self,order):
k = order.timestamp
self.totalAmount -= self.Orders[k].amount
self.Orders.remove(k)
def get_price_depth(self):
return self.totalAmount
def is_empty(self):
return self.Orders.count == 0