def drain_asset(positions, asset, quantity):
"""
Generic method of reducing the quantity of assets across an entire set of positions
This is hard to do manually because positions can duplicates and arbitrary quantities
Traverse the entire position set reducing quantities to zero until it hits the target reduction
"""
remaining_quantity = quantity
# get a list of positions that are opposite to the quantity we are draining
positions = [_ for _ in positions if _.asset == asset and copysign(1,_.quantity) == copysign(1, quantity * -1)]
for position in positions:
if abs(remaining_quantity) <= abs(position.quantity):
# there are enough quantity in this position to complete it
position.quantity += remaining_quantity
remaining_quantity = 0
return remaining_quantity
if abs(remaining_quantity) > abs(position.quantity):
# we are going to have some left over
remaining_quantity += position.quantity
position.quantity = 0
return remaining_quantity
close_expired_options.py 文件源码
python
阅读 30
收藏 0
点赞 0
评论 0
评论列表
文章目录